Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detecting already executing applications

Status
Not open for further replies.

FredrikE

Programmer
Apr 23, 2002
12
SE
How can I detect whether my application already is running (or not) ?

Theres no need to start one instance for every file double-clicked in the Explorer in an MDI-app. Compare Word etc. which only launches a single window (or at least used to). /Fredrik Eriksson
 
Check the second option of the WinMain function:

HINSTANCE hPrevInstance

The handle of the previous instance of this application. This value is NULL if this is the first instance.
 
Since I'm running Win98 all hPrevInstance are NULL... /Fredrik Eriksson
 
You are right, it is always null for win32 apps.
this is straight from the cbuilder help:

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).
 
This checks for a a previous instance of the program running.

CreateMutex(NULL, true,
"The name of the program + ver # + name");

if (GetLastError() != ERROR_ALREADY_EXISTS)
{
Application->Run ();
}

Put this in the winmain

tomcruz.net
 
The string paraameter can also be created by randomly
hitting the keyboard to obtain a random string.

such as

CreateMutex(NULL, true,
"o49p324853;u; asd;e98;reu; sesdfg;dfg9875bv873p");

tomcruz.net

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top