Link to home
Start Free TrialLog in
Avatar of rani2005
rani2005

asked on

Single instance...

Hello,
How to have single instace of my application?
Thnaks in advance,
Rani.
Avatar of Axter
Axter
Flag of United States of America image

How To Prevent Multiple copies of a Program Running at the same Time
If you only want to prevent a second instance to run, a mutex object will do the job. It must be placed in the InitInstance method of the CWinApp derived class. As a name for the mutex-object, we chose the name of the application's executable file (CWinApp::m_pszExeName) but you can choose your own unique name.

BOOL COneInstanceApp::InitInstance()
{
     // Standard initialization
     // If you are not using these features and wish to reduce the size
     //  of your final executable, you should remove from the following
     //  the specific initialization routines you do not need.

     HANDLE hSingle;

     hSingle = ::CreateMutex(NULL, TRUE, m_pszExeName);
     if (ERROR_ALREADY_EXISTS == GetLastError()) {
     
          // The system closes the handle automatically when the process
          // terminates
          return FALSE;
     }


     // (...)

}

ASKER CERTIFIED SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rani2005
rani2005

ASKER

Thanks Axter.