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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

check process list

Status
Not open for further replies.

jbs

Programmer
Feb 19, 2000
121
US
I have a program that needs to check the workstation task list to make sure that it will be the only instance of the task running.

Basically I'm trying to make sure that the user doesn't accidently start program execution twice by accident.

Let's say my program name is "abc.exe" How do I check the process list to make sure that an instance of "abc.exe" isn't already running?

I'm assuming there is a simple API to solve this one. Please assume I'm not using MFC and need to be Unicode compliant.

thanks,

jbs
 
Search for Q175030 on search.microsoft.com - You will find good samples.
 
The normal procedure for this is creating or opening a named mutex with the CreateMutex API. The CreateMutex of the second instance does succeed, but GetLastError returns ERROR_ALREADY_EXISTS after that call.

Example:

HANDLE hMutex = CreateMutex ( NULL, FALSE, "SOME_UNIQUE_STRING" );

if ( hMutex != NULL &&
GetLastError ( ) == ERROR_ALREADY_EXISTS )
{ // There is already an instance of the program running
CloseHandle ( hMutex );
MessageBox ( NULL, "This program is already running",
&quot;<Program-name>&quot;,
MB_OK | MB_ERROR );
exit ( 0 ); }


// Now, CreateMutex failed or it is the first instance

// here the code of the program

if ( hMutex != NULL )
{ CloseHandle ( hMutex );
hMutex = NULL; }
exit ( 0 ); }

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top