Sign in to follow this  
HaywireGuy

HOWTO: Enforce single app instance in registry?

Recommended Posts

HaywireGuy    208
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.

Share this post


Link to post
Share on other sites
Sandman    2210
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.

Share this post


Link to post
Share on other sites
Endurion    5408
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.

Share this post


Link to post
Share on other sites
HaywireGuy    208
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?

Share this post


Link to post
Share on other sites
TDragon    679
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.

Share this post


Link to post
Share on other sites
Anon Mike    1098
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.

Share this post


Link to post
Share on other sites
Fruny    1658
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.

Share this post


Link to post
Share on other sites
HaywireGuy    208
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this