×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Checking for the existence of other instances a project

Checking for the existence of other instances a project

Checking for the existence of other instances a project

(OP)
How can I make sure that a c++ program is the only instance (of itself) running on the machine?

thanks

D

RE: Checking for the existence of other instances a project

check to see if the handle hPrevInstance is NULL in WinMain. If it is, then the application is the first instance.
If not, then there is another copy of the program already running.

int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow)


Kim_Christensen@telus.net
Home Page

RE: Checking for the existence of other instances a project

zBuilder,

Can you expand on this a bit. I've seen the use of mutex to do this but some of the machines I work on do not have mutex running.

Donald,
I've also seen the use of creating a file in a certain directory when a program starts. If the program is started again, the program sees the file and knows that it has already been started. The downside to this is if the program crashes, the program is left open and someone has to manually delete the file before the program can start again.

James P. Cottingham
main@ivcusa.com
International Veneer Co., Inc.
All opinions are mine alone and do not necessarily reflect those of my employer.

RE: Checking for the existence of other instances a project

2ffat,

I'm thinking of machines with Win3.1, Win95/98 running as the GUI / OS
Under these environments, the OS supplies to the newly executed application/window a handle to any previous instance of itself. WinMain is the entry point for all MsWindows applications whether they use OWL, MFC, etc... It may be hidden in an object somewhere, if the programmer is using a OOPs interface to the windows API.
Under another OS, this would be done differently though...

Donald,

Let us know if you ARE compiling for the Windows environment or not....


Kim_Christensen@telus.net
Home Page

RE: Checking for the existence of other instances a project

(OP)
I am compiling for win 95 but I'm having trouble using WinMain. Where do I put it? How is it implemented? So far, it's been quietly ignored by the compiler.

D

RE: Checking for the existence of other instances a project

Ahhh... Ok. Win95. My example was for 16bit windows... I'm no expert on Win32 though. Below is an exerpt from a win32 help file (Hope it helps)

The WinMain function is called by the system as the initial entry point for a Win32-based application.

int WINAPI WinMain(

HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // pointer to command line
int nCmdShow // show state of window
);
Parameters

hInstance

Identifies the current instance of the application.

hPrevInstance

Identifies the previous instance of the application. For a Win32-based application, this parameter is always NULL. If you need to detect whether another instance already exists, create a named mutex using the CreateMutex function. If the GetLastError function returns ERROR_ALREADY_EXISTS, another instance of your application exists (it created the mutex).

lpCmdLine

Points to a null-terminated string specifying the command line for the application.

nCmdShow

Specifies how the window is to be shown. This parameter can be one of the following values:

Value Meaning
SW_HIDE Hides the window and activates another window.
SW_MINIMIZE Minimizes the specified window and activates the top-level window in the system’s list.
SW_RESTORE Activates and displays a window. If the window is minimized or maximized, Windows restores it to its original size and position (same as SW_SHOWNORMAL).
SW_SHOW Activates a window and displays it in its current size and position.
SW_SHOWMAXIMIZED Activates a window and displays it as a maximized window.
SW_SHOWMINIMIZED Activates a window and displays it as an icon.
SW_SHOWMINNOACTIVE Displays a window as an icon. The active window remains active.
SW_SHOWNA Displays a window in its current state. The active window remains active.
SW_SHOWNOACTIVATE Displays a window in its most recent size and position. The active window remains active.
SW_SHOWNORMAL Activates and displays a window. If the window is minimized or maximized, Windows restores it to its original size and position (same as SW_RESTORE).
Return Values

If the function succeeds, terminating when it receives a WM_QUIT message, it should return the exit value contained in that message’s wParam parameter. If the function terminates before entering the message loop, it should return 0.

Remarks

WinMain initializes an application, displays its main window, and then enters a message retrieval-and-dispatch loop that is the top-level control structure for the remainder of the application’s execution. The message loop terminates when a WM_QUIT message is received. At that point, WinMain exits the application, returning the value passed in the WM_QUIT message’s wParam parameter. If WM_QUIT was received as a result of calling PostQuitMessage, the value of wParam is the value of the PostQuitMessage function’s nExitCode parameter. For more information, see Creating a Message Loop.


Kim_Christensen@telus.net
Home Page

RE: Checking for the existence of other instances a project

zBuilder,

We are using WinNT 4.0. The snippet that I saw was from BCBDEV.COM site. It uses a mutex. I will try your example and see how it works. Thanks

James P. Cottingham
main@ivcusa.com
International Veneer Co., Inc.
All opinions are mine alone and do not necessarily reflect those of my employer.

RE: Checking for the existence of other instances a project

Here is a Solution using Mutex:

WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
int Mutex;
Mutex=(int)CreateMutex(NULL,true,GetPrevInstMutexName());
if((Mutex!=NULL)&& (GetLastError()==0))
    {
    try
        {
        Application->Initialize();
        Application->CreateForm(__classid(TForm1), &Form1);
        Application->Run();
        }
    catch (Exception &exception)
        {
        Application->ShowException(&exception);
        }
    }

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close