HOWTO: Enforce single app instance in registry?
Greetings everyone,
I have created a utility that views proprietary files of my own format. This
utility is capable of viewing multiple files at the same time (something like
MDI). I have this file type associated with my utility program in the system
registry, but everytime I double click on files with the said extension, the
system runs a new instance of the utility instead of an existing running copy.
How do I enforce this single-instanceness through the system registry?
Thanks in advance for any help! :)
Regards,
Ben.
Not even sure you can do it in the registry alone, but you can certainly do it fairly easily in code.
You can do a search using FindWindow() to see if you already have a window up. If you have, send it a custom message (using RegisterWindowMessage() and PostMessage() or SendMessageTimeout() ) to tell it to open the file instead - and then exit the second instance instead of creating the window.
You can do a search using FindWindow() to see if you already have a window up. If you have, send it a custom message (using RegisterWindowMessage() and PostMessage() or SendMessageTimeout() ) to tell it to open the file instead - and then exit the second instance instead of creating the window.
A safer way than using FindWindow would be a Mutex. You can call CreateMutex with a user defined name (make sure you take a rather unique one).
If CreateMutex returns ERROR_ALREADY_EXISTS you know your app is already running. In that case use your favourite way of notifying your other instance of the new file and shut down the current one.
If CreateMutex returns ERROR_ALREADY_EXISTS you know your app is already running. In that case use your favourite way of notifying your other instance of the new file and shut down the current one.
Thanks people.
I understand how FindWindow() and Mutex work in this context, but that's not
the solution I am looking for. WinMain() of my app gets executed once for each
new instance created, the subsequent instance can quit without any problem
(FindWindow, Mutex, etc). But I'm hoping the system to "tell" my first app
instance that the file is being double-clicked on, and it will open the file.
Surely there's some flags to be used in the registry for this?
I understand how FindWindow() and Mutex work in this context, but that's not
the solution I am looking for. WinMain() of my app gets executed once for each
new instance created, the subsequent instance can quit without any problem
(FindWindow, Mutex, etc). But I'm hoping the system to "tell" my first app
instance that the file is being double-clicked on, and it will open the file.
Surely there's some flags to be used in the registry for this?
Quote:Original post by HaywireGuy
Thanks people.
I understand how FindWindow() and Mutex work in this context, but that's not
the solution I am looking for. WinMain() of my app gets executed once for each
new instance created, the subsequent instance can quit without any problem
(FindWindow, Mutex, etc). But I'm hoping the system to "tell" my first app
instance that the file is being double-clicked on, and it will open the file.
Surely there's some flags to be used in the registry for this?
I'm not aware of any such registry settings, sorry. I'd recommend you use the CreateMutex function to detect if there is a previous version running; if so, use FindWindow() to get its handle and send it a message saying you want it to open the file, then close.
Why exactly do you care if multiple instances of your app are running? I have to confess I've never understood this sort of behavior. I've been annoyed by it far more often than I've been glad of it.
Quote:Original post by HaywireGuy
But I'm hoping the system to "tell" my first app
instance that the file is being double-clicked on, and it will open the file.
Well, that's exactly what you have to do. Have the new instance pass a message to the old one telling it to open the file.
Quote:Surely there's some flags to be used in the registry for this?
AFAIK, not unless you implement it and check it yourself.
Thanks everyone for your help, appreciate it. Guess I'm left with no choice but to use SendMessage() this case.
Ben.
Ben.
Look for some instructions for DDE... It's kind of hard to get working, but I believe it's the "proper" way to do what you want to do.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement