Skip to main content
GameDev.net gamedev.net
🔒 Locked

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

Started by NotTakenSN Mar 7, 2013 at 9:09 AM 2 replies 2.3k views
Original Post
NotTakenSN
NotTakenSN

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?

turch
turch

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.
Endurion
Endurion

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>

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.