Passing file name to main with Windows "open with" option

Started by
2 comments, last by Endurion 11 years, 1 month ago

I would like to be able to right click on a file, such as a jpg, and open it with my application under the "open with" menu in Windows. How do I get the filename and directory to be passed along to the arguments in main (or winmain, since my app has a graphics interface)?

RIght now, my application has a predefined directory that it reads from. It executes fine when I double click the executable, but when I try opening my application under the "open with" drop down menu from a random file, I get an invalid allocation size error. My current code doesn't actually use any of the command line arguments to winmain, so why am I getting this error? Shouldn't it just launch the same way as when I double click my executable?

Advertisement

Shouldn't it just launch the same way as when I double click my executable?

When your application is launched by an "open with", the current working directory is %WINDIR%\system32, so if you use any relative file paths they will be broken.

How do I get the filename and directory to be passed along to the arguments in main (or winmain, since my app has a graphics interface)?

It will be the first argument passed in to the application.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms633559(v=vs.85).aspx

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

Play safe:

Use GetModuleFileName to fetch your executable file name.

Remove the file name.

Use the resulting path to create absolute paths for every file access you have.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

This topic is closed to new replies.

Advertisement