Solved

Disable double execution of a program

Posted on 1998-12-29
8
181 Views
Last Modified: 2010-04-02
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
0
Comment
Question by:gilbert_chang
  • 3
  • 3
  • 2
8 Comments
 
LVL 1

Expert Comment

by:The_Brain
ID: 1181210
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.
0
 
LVL 1

Expert Comment

by:The_Brain
ID: 1181211
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.
0
 
LVL 1

Expert Comment

by:The_Brain
ID: 1181212
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);


0
 
LVL 11

Expert Comment

by:alexo
ID: 1181213
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:gilbert_chang
ID: 1181214
alexo's answer works great. Thanks The_Brain too.
0
 
LVL 11

Accepted Solution

by:
alexo earned 50 total points
ID: 1181215
Details in my comment from December 29 1998 - 05:29AM
0
 

Author Comment

by:gilbert_chang
ID: 1181216
Thanks.
0
 

Author Comment

by:gilbert_chang
ID: 1181217
Thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now