Link to home
Start Free TrialLog in
Avatar of gilbert_chang
gilbert_chang

asked on

Disable double execution of a program

I am writing an MFC dialog based program, and wouldn't like the user to open multiple copies of the program at once.
(only one at a time).
How is this done?
Thanks
Avatar of The_Brain
The_Brain

Do you want only the user to use that program ONLY, or what?
If it is the only application that needs attention then you could force the cursor to remain within the dialog, else you could use a detect which I am not fimiliar with.  (All though I would prob. say don't open two apps.

I would write a file which holds a value 1 or 0, and that will tell me if the application is allready running.

when you are in INITDialog read the value and use

if (!value)
   return;

this will quit the second app before it even comes up.  hope this helps.
Do you want only the user to use that program ONLY, or what?
If it is the only application that needs attention then you could force the cursor to remain within the dialog, else you could use a detect which I am not fimiliar with.  (All though I would prob. say don't open two apps.

I would write a file which holds a value 1 or 0, and that will tell me if the application is allready running.

when you are in INITDialog read the value and use

if (!value)
   return;


You will say on Create, make value=1 and writeover the file.
and at the end rewrite to 0.
this will quit the second app before it even comes up.  hope this helps.
if you want me to eleb, then I will, but just create a file and add char Value=1;  when the app starts.  (OnCreate)
then when it closes (fp.write("0");  (something to that effect);


Here is the "official" way of limiting a program to only one instance:

    // Beginning of program
    HANDLE hMutex;
    hMutex = CreateMutex(NULL, FALSE, "Some unique string");
    if (GetLastError() == ERROR_ALREADY_EXISTS)
    {
        // Application already running, display an error or something
        CloseHandle(hMutex);
        return 0;
    }

    // The rest of the program goes here...

    // End of program
    CloseHandle(hMutex);
    return 1;

If this is what you need, you'll need to reject the currently pending answer so you can accept mine.
Avatar of gilbert_chang

ASKER

alexo's answer works great. Thanks The_Brain too.
ASKER CERTIFIED SOLUTION
Avatar of alexo
alexo
Flag of Antarctica image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks.
Thanks.