HOWTO: Enforce single app instance in registry?

Started by
8 comments, last by HaywireGuy 18 years, 9 months ago
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.
Advertisement
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.
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.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

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?
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.
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
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.
-Mike
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Thanks everyone for your help, appreciate it. Guess I'm left with no choice but to use SendMessage() this case.


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.
Thank Nypyren, this DDE thing looked suspicious at first, but I didn't look
carefully. Now that you talked about it, I should read more in details.

This topic is closed to new replies.

Advertisement