Solved

How to keep only one program running

Posted on 1997-10-28
6
187 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Application vs Workbook.applicaiton 3 45
Is COM supported from Apache 1 74
Having a different time zone on your task bar 3 77
How do ASP.NET and MVC work together? 4 70
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

685 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