Sign in to follow this  
Followers 0
NotTakenSN

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

3 posts in this topic

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?

0

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0