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!

Holding only one instance of the prog started 1

Status
Not open for further replies.

firelex

Programmer
Jan 10, 2002
118
DE
Hello, all!
The question is in the subj.
How can I block all other tries to start my prog if it is already started?
 
You need to create a named mutex semaphore when you start your application. When the second one starts it tries to get access to the mutex but will fail...
Code:
// app.h
class CYourApp : public CWinApp
{
  ...

private:
  HANDLE hMutex;
};

// app.cpp
BOOL CYourApp::InitInstance()
{
  // Create mutex
  hMutex = ::CreateMutex(NULL, TRUE, "GlobalMutex");

  switch(::GetLastError())
  {
    case ERROR_SUCCESS:
      // Mutex created successfully. There is no instance running
      break;

    case ERROR_ALREADY_EXISTS:
      // Mutex already exists so there is a running instance of our app.
      return FALSE;

    default:
      // Failed to create mutex by unknown reason
      return FALSE;
  }
}

Standa K.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top