How do I open a new document at application startup without opening a new instance of the application?

I have a situation partially covered by other answers in SO, but I cannot find a complete answer. In short, we are trying to use a URL for our specific datatypes that, when double clicked, open our app and load those datasets into that app. We have this part of the job.

(for example, the url might look like this: resource: //shaders/basic_shader.hlsl)

What we would like to do is prevent new instances of the application from opening when we double click on a new URL. For example, let's say we have a URL that opens a shader in our shader editor. When you click that resource url, it will open our shader editor. When the new shader url is clicked, we would like to open the shader in the current application launch and open the new shader in a new tab in our editor.

We can easily detect if another instance of our application is running. A problem we don't know how to easily fix is ​​to tell the current application launch to open this new file for editing. This behavior is very similar to Apple Finder.

On unix, you can mimic this behavior by opening some named pipe and then new applications could check if that pipe is active and then send the document data down the pipe. Is there a more standard Windows way to achieve this behavior?

We need a C / C ++ solution. Thank.

+1


source to share


5 answers


Create a named mutex when the app starts as David Grant said, then before displaying the UI for the second url, check that mutex if it's already created, then just close by passing the new url to the first running app (Have an interface in the app to set the url and tell it to redirect programmatically)



+1


source


A named pipe is the best way. The first instance of your application opens a channel and listens to it (use PIPE_ACCESS_INBOUND as dwOpenMode and the same code will also allow you to detect running instances). All subsequent instances verify that they are not alone, send a command line argument to the pipe, and exit.



+2


source


You cannot avoid executing the program associated with the url that needs to be executed.

A "windows" solution would be to send a message (via DDE in the old days, but maybe there is something more "modern" now) to a previously running application with a URL, and then exit ...

0


source


You can get a named mutex on startup and make it execute that way.

0


source


I got this working very well for my C ++ MFC application by following Joseph Newcome's tutorial here . It uses a named mutex, which is checked at startup, and a message sent to an already running application with a new resource to open.

0


source







All Articles