Solved

How to keep only one program running

Posted on 1997-10-28
6
182 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 3

Expert Comment

by:vinniew
Comment Utility
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
Comment Utility
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
Comment Utility
I have adopted alexo's method. It is simple and effictive.
Thanks.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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

10 Experts available now in Live!

Get 1:1 Help Now