Jump to content
  • Advertisement
Sign in to follow this  
KroKoGer

Editing and saving a file opened with ShellExecute

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi!

I want to open a simple .txt file for editing in the default editor. I'm using c++ and windows, so I use ShellExecute. Opening the file works great, but i can't save the edited file, which is essential in my case. It always says "Check, whether the file is opened in another program". It seems like the file is still opened in another thread or process ... When I close the main program, I can save the file. I tried to close the handle, but it doesn't work.


This is the code i use:


HANDLE hOpenedFile = ShellExecute(0, "open", "myFile.txt", 0, 0, SW_SHOWDEFAULT);

CloseHandle(hOpenedFile);


I also tried other verbs like "edit" or even NULL ... But every time with the same result.
Is there something I'm doing wrong? Or is there another way to perform that action?

Thanks in advance for your help!

KroKoGer

Share this post


Link to post
Share on other sites
Advertisement
I'm not sure the returned HINSTANCE is what you think it is — it's just an error code, not a file handle.

Are you sure you're not opening the file elsewhere in your application first and forgetting to close it? I use very similar code in my own software (using the edit verb, admittedly, rather than the open verb) and it works fine.

Share this post


Link to post
Share on other sites
ShellExecuteEx gives more information about the spawned process, including handle and all.

Try sending a WM_CLOSE message to the notepad. something like:

sendMessage(findWindow(nil, 'notepad'), WM_CLOSE, 0, 0);

Share this post


Link to post
Share on other sites
Hey!

Thank you for the answers ... I found the error: In fact, I did open the file in my main program. I just couldn't find out where, so I thought I closed it again, but that wasn't the case.

Thanks!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!