Friends is there any way to detect two instances of our program executing.I mean multiple instances of the application running so that i could prompt a message to the user
Printable View
Friends is there any way to detect two instances of our program executing.I mean multiple instances of the application running so that i could prompt a message to the user
i got it
// 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;
}
}
i got the code from previous faq
hi ,
what will happen , if another program resort to the same technique ?
(with mutex name "GlobalMutex" )
why one cannot use GUIDs for this ? any thoughts ?
Praseed Pai
HANDLE hdl;
hdl = CreateMutex(NULL, TRUE, "MyTestMutex");
if(hdl)
{
DWORD dwRetCode = GetLastError();
if(dwRetCode == ERROR_ALREADY_EXISTS)
{
AfxMessageBox("Another instance is already running@!!!");
return FALSE;
}
}
The FAQ presents a simple technique to achive the goal. Of course "GlobalMutex" is not a very good name. You can come with something like "GlobalMutex@ProgramName#Author@Year". I doubt that you'll have two indentical identifiers...Quote:
Originally Posted by SOCM_FP_CPP
... or better use GUIDGEN.EXE provided with Visual C++ to generate a unique string as for example "{3690872D-DAB2-4243-B5F8-1832567BC25E}".
Take a look at the following FAQ...
but this is not working in vc.net ,its working in vc++ only
Hi anand ,
It seems that ur writing a managed application using VC.NET. if ur trying to write unmanged ( win32 console or Win32 gui ) code , the above scheme should work.
Praseed Pai
What do you mean with "it not working in vc.net"? This will work disregarding whether it is .NET or not in the first place...Quote:
Originally Posted by anand123