Public Group

Win32 Drag/Drop for argv[1]?

This topic is 4833 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I have written a small program to convert .tga files to .bmp files for my Warcraft friends who can't afford Photoshop or use Google for the word "viewer". Anyways... Problem is that I'm converting argv[1] (the .tga image) to a .bmp file. Everything works if I open a command prompt and type "wowview whatever.tga". However, I want this to work via drag and drop. Should dragging an image onto an exe open that image as argv[1]? If not, what do I need to do for this functionality?

Share on other sites
i beleive setting .tga to open defaultly with your converter will work, did for me.

Share on other sites
Quote:
 Original post by DerAngedi beleive setting .tga to open defaultly with your converter will work, did for me.

I'm sorry, I don't quite follow.

#pragma comment (lib, "SDL.lib")#pragma comment (lib, "SDLmain.lib")#pragma comment (lib, "SDL_Image.lib")#include <SDL/SDL.h>#include <SDL/SDL_Image.h>#include <string>#include <iostream>int main(int argc, char **argv){  // Image Surface.  SDL_Surface* Image;  // File Name.  std::string  FileName;  // Resulting File Name  std::string  Result;  // Output the welcoming message.  std::cout << "Welcome to WoWView by REMOVED." << std::endl;  // Set FileName to the first (and only) argument.  FileName = argv[1];  std::cout << "Converting... ";  // Create an SDL_Surface from the TGA image.  Image = IMG_Load(FileName.c_str());  // Set up the Result file name.  Result = (("wv_" + FileName));  int i = Result.find(".tga");  Result.replace((i--), 4, ".bmp");  std::cout << "DONE!" << std::endl;  // Save the BMP.  SDL_SaveBMP(Image, Result.c_str());  return 0;}

Share on other sites
With further testing I've attempted to print argv[1] to a file and it never happens. It appears argv[1] is not getting assigned the file name that's dragged on the exe. I've looked around online and it appears that argv[1] SHOULD be the file name that was dragged. I'm baffled.

Share on other sites
That style of drag and drop behaviour only seems to work if the program is built as a console application. SDL programs are normally built as GUI applications, and you'll probably find that your program isn't even making it to your main() when you attempt to drag a file on to the executable.

It looks like SDL's pre-main() setup code should handle running as a console application, but my quick experiment with this resulted in the console application exiting before reaching my main().

Share on other sites
Nice code! Needs a few improvments though [wink] Take a look at this, works for me:
#pragma comment (lib, "SDL.lib")#pragma comment (lib, "SDL_Image.lib")#include <SDL/SDL.h>#include <SDL/SDL_Image.h>#include <string>#include <iostream>#undef mainint main(int argc, char **argv){  // Image Surface.  SDL_Surface* Image;  // File Name.  std::string  FileName;  // Resulting File Name  std::string  Result;  // Output the welcoming message.  std::cout << "Welcome to WoWView by REMOVED." << std::endl;  if( argc < 2 )  {	  std::cout << "Need's 1 argument!";	  // Tell correct usage!	  return -1;  }  // Set FileName to the first (and only) argument.  FileName = argv[1];  std::cout << "Converting... ";  // Create an SDL_Surface from the TGA image.  Image = IMG_Load( FileName.c_str() );  if( !Image )  {	  std::cout << "File cannot be opened!";	  // Some problem!	  return -1;  }  // Set up the Result file name.  Result = argv[1];  Result = Result.substr( 0, Result.find_last_of("."));  Result += ".bmp";  std::cout << "DONE!" << std::endl;  // Save the BMP.  SDL_SaveBMP(Image, Result.c_str());  // At least let them see success ;)  system("pause");  return 0;}

I just removed all use of SDL_Main as well as fixed a few other things. By all means use whatever you want, all you asked for was the drag-n-drop, but I noticed if you don't handle a few things, you get crashes. [smile]

- Drew

Share on other sites
Drew saves the day again :) I'm a little confused though. How do you eliminate SDL_Main? It must be linker settings because I don't see anything in the code that does it. Thanks!

Edit: Nevermind. I see it [rolleyes]

• 18
• 11
• 16
• 9
• 49
• Forum Statistics

• Total Topics
631395
• Total Posts
2999781
×

Important Information

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!