rani2005
asked on
Single instance...
Hello,
How to have single instace of my application?
Thnaks in advance,
Rani.
How to have single instace of my application?
Thnaks in advance,
Rani.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Axter.
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::InitInsta
{
// 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;
}
// (...)
}