Solved

How to keep only one program running

Posted on 1997-10-28
6
185 Views
Last Modified: 2013-12-03
How can I keep only one windows program running in memory?
What should I check when this program starts running?
0
Comment
Question by:hbh
6 Comments
 
LVL 15

Expert Comment

by:Tommy Hui
ID: 1407979
Do you want only one Windows program running? Regardless of what that program is?

Which version of Windows are you referring to? 16-bit or 32-bits?
0
 
LVL 11

Accepted Solution

by:
alexo earned 50 total points
ID: 1407980
If you mean one instance of a given program, there are several ways of accomplishing this.

In Win16, the preferred way is to check the previous instance parameter to WinMain().

In Win32 you cannot do that so you can create a global object (like semaphore or event) with a hardcoded name and check the return value to see if the object already existed.

0
 

Author Comment

by:hbh
ID: 1407981
Yes, I just want one window program running under windows 95,
a 32bit platform. An example called Spy++ in MSVisual C++ is
such as this kind of program, which has only one copy when it
is running.

0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 3

Expert Comment

by:vinniew
ID: 1407982
Here's how I'd do it for Win32:

#pragma data_seg("myapplication")
BOOL g_bHasStarted;
#pragma

Then, in the initialization somewhere:

if(g_bHasStarted == 0) {
   g_bHasStarted = 1;
} else {
   Exit();   /* or your cleanup routine. */
}


0
 
LVL 11

Expert Comment

by:alexo
ID: 1407983
that works too but only if you instruct the linker to make the "myapplication" segment shared, *and* initialize the global variable.

Something like:

#pragma comment(linker, "/section:myapplication,rws")
#pragma data_seg("myapplication")
BOOL g_bHasStarted = 0;
#pragma data_seg()

However, it relies on #pragmas specific to the MSVC compiler and thus is less portable.

I'd suggest relying on a named object or a global atom.

Something like:

    hMutex = CreateMutex (NULL, FALSE, "Some unique name");
    if ((hMutex != NULL) && (GetLastError () == ERROR_ALREADY_EXISTS)) {
        MessageBox (NULL, "Another instance of this application is running",
            "Error", MB_OK | MB_ICONSTOP);
        CloseHandle (hMutex);
        return -1;
    }

In the beginning of your program, and:

    CloseHandle (hMutex);

At the end.

0
 

Author Comment

by:hbh
ID: 1407984
I have adopted alexo's method. It is simple and effictive.
Thanks.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

777 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