Solved

How to keep only one program running

Posted on 1997-10-28
6
188 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
error email attachment VB2010 5 58
WPF MainWindow update textbox from another class... 6 205
Example of WebAPI 7 36
What .NET website keeps me current? 9 80
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
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…

738 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