|
-
September 24th, 2004, 12:38 AM
#1
detect multiple instances of our program
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
Last edited by anand123; September 24th, 2004 at 12:46 AM.
-
September 24th, 2004, 01:07 AM
#2
Re: detect multiple instances of our program
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
-
September 24th, 2004, 01:13 AM
#3
Re: detect two instances of our program
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
-
September 24th, 2004, 01:16 AM
#4
Re: detect two instances of our program
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;
}
}
-
September 24th, 2004, 02:48 AM
#5
Re: detect two instances of our program
 Originally Posted by SOCM_FP_CPP
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
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...
-
September 24th, 2004, 03:11 AM
#6
Re: detect two instances of our program
... or better use GUIDGEN.EXE provided with Visual C++ to generate a unique string as for example "{3690872D-DAB2-4243-B5F8-1832567BC25E}".
Hokutata Yakubotu
-
September 24th, 2004, 09:16 AM
#7
Re: detect two instances of our program
Take a look at the following FAQ...
-
September 27th, 2004, 05:21 AM
#8
Re: detect two instances of our program
but this is not working in vc.net ,its working in vc++ only
-
September 27th, 2004, 05:33 AM
#9
Re: detect two instances of our program
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
-
September 27th, 2004, 10:48 AM
#10
Re: detect two instances of our program
 Originally Posted by anand123
but this is not working in vc.net ,its working in vc++ only
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...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|