Associate a file type to a running application
I am developing an application in windows using vs2005 c++ and want to associate a file extension to the application which I can do in the registry settings. However, when I click on the file I want to associate it with a running ins开发者_如何学Gotance of my application and not launch a new one. How can I achieve this behavior.
I can't give you an implementation off the top of my head but look into something called "interprocess communication" (typically using COM if you're on windows). What you can do is, when your program starts, check for other copies of itself, if it finds one then it tells that one what file you wanted open, then shuts itself down.
I found an implementation for you:
http://www.flounder.com/nomultiples.htm
This used to be done with a parameter in winmain but from msdn
A handle to the previous instance of the application. This parameter is always NULL. If you need to detect whether another instance already exists, create a uniquely named mutex using the CreateMutex function. CreateMutex will succeed even if the mutex already exists, but the function will return ERROR_ALREADY_EXISTS. This indicates that another instance of your application exists, because it created the mutex first. However, a malicious user can create this mutex before you do and prevent your application from starting. To prevent this situation, create a randomly named mutex and store the name so that it can only be obtained by an authorized user. Alternatively, you can use a file for this purpose. To limit your application to one instance per user, create a locked file in the user's profile directory.
精彩评论