Library tutorials & articles
Avoiding Multiple Instances of an Application
By Joseph M. Newcomer, published on 13 Oct 2001
The source code
exclusion.h
#define UNIQUE_TO_SYSTEM 0 #define UNIQUE_TO_DESKTOP 1 #define UNIQUE_TO_SESSION 2 #define UNIQUE_TO_TRUSTEE 3 CString createExclusionName(LPCTSTR GUID, UINT kind = UNIQUE_TO_SYSTEM);
|
|
exclusion.cpp
#include "stdafx.h" #include "exclusion.h"
/****************************************************************************
* createExclusionName
* Inputs:
* LPCTSTR GUID: The GUID for the exclusion
* UINT kind: Kind of exclusion
* UNIQUE_TO_SYSTEM
* UNIQUE_TO_DESKTOP
* UNIQUE_TO_SESSION
* UNIQUE_TO_TRUSTEE
* Result: CString
* A name to use for the exclusion mutex
* Effect:
* Creates the exclusion mutex name
* Notes:
* The GUID is created by a declaration such as
* #define UNIQUE _T("MyAppName-{44E678F7-DA79-11d3-9FE9-006067718D04}")
****************************************************************************/
CString createExclusionName(LPCTSTR GUID, UINT kind)
{
switch(kind)
{ /* kind */
case UNIQUE_TO_SYSTEM:
return CString(GUID);
case UNIQUE_TO_DESKTOP:
{ /* desktop */
CString s = GUID;
DWORD len;
HDESK desktop = GetThreadDesktop(GetCurrentThreadId());
BOOL result = GetUserObjectInformation(desktop, UOI_NAME, NULL, 0, &len);
DWORD err = ::GetLastError();
if(!result && err == ERROR_INSUFFICIENT_BUFFER)
{ /* NT/2000 */
LPBYTE data = new BYTE[len];
result = GetUserObjectInformation(desktop, UOI_NAME, data, len, &len);
s += _T("-");
s += (LPCTSTR)data;
delete [ ] data;
} /* NT/2000 */
else
{ /* Win9x */
s += _T("-Win9x");
} /* Win9x */
return s;
} /* desktop */
case UNIQUE_TO_SESSION:
{ /* session */
CString s = GUID;
HANDLE token;
DWORD len;
BOOL result = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token);
if(result)
{ /* NT */
GetTokenInformation(token, TokenStatistics, NULL, 0, &len);
LPBYTE data = new BYTE[len];
GetTokenInformation(token, TokenStatistics, data, len, &len);
LUID uid = ((PTOKEN_STATISTICS)data)->AuthenticationId;
delete [ ] data;
CString t;
t.Format(_T("-%08x%08x"), uid.HighPart, uid.LowPart);
return s + t;
} /* NT */
else
{ /* 16-bit OS */
return s;
} /* 16-bit OS */
} /* session */
case UNIQUE_TO_TRUSTEE:
{ /* trustee */
CString s = GUID;
#define NAMELENGTH 64
TCHAR userName[NAMELENGTH];
DWORD userNameLength = NAMELENGTH;
TCHAR domainName[NAMELENGTH];
DWORD domainNameLength = NAMELENGTH;
if(GetUserName(userName, &userNameLength))
{ /* get network name */
// The NetApi calls are very time consuming
// This technique gets the domain name via an
// environment variable
domainNameLength = ExpandEnvironmentStrings(_T("%USERDOMAIN%"),
domainName,
NAMELENGTH);
CString t;
t.Format(_T("-%s-%s"), domainName, userName);
s += t;
} /* get network name */
return s;
} /* trustee */
default:
ASSERT(FALSE);
break;
} /* kind */
return CString(GUID);
} // createExclusionName
|
|
Back in the original example, replace the string which I hardwired into the ::CreateMutex call with a call on createExclusionName with the desired specification to get a correctly-formatted unique name to use for the Mutex.
Related articles
Related discussion
-
VS2005 app's won't run on another machine
by ted4444 (0 replies)
-
VB.NET: Hide and show table using radio buttons
by converter2009 (1 replies)
-
Convert C++ code to VB6
by mawcot (4 replies)
-
How to create a games like FIFA08
by mawcot (0 replies)
-
Binary Studio | software development outsourcing Ukraine
by shane124 (4 replies)
I am making a single instance appilcation and have followed all your suggestions . And i am really thankful to you for writing such an informative article.But i have a problem. It is my requirement that user can close my running application from the command prompt.To impliment this i am sending a message (WM_COPYDATA) from new instance which tries to launch but since it finds that an instance is already running it passes the cmdLine parameter from new instance to already existing instance and if the cmdLine parameter is /exit the application is closed. Everything works Like a charm.But problem is this. If i keep the exe in the folder named similar to the window name of my application then the second instance is not able to find the first instance and thus the application does not exit when it should have exited. If i change the name of the folder in which the exe is residing then everything works fine.
I just want to understand why is it happening and would love to have your views on this one.
Brilliant article!! Here it is again, boiled down to cookbook code for your MyApp.cpp file.
Many thanks to Joseph Newcomer & Daniel Lohmann. // Mark Malyj
//Avoiding Multiple Instances of an Application
//Step 1 of 5.
//Include exclusion.h at the top of app.cpp.
include "exclusion.h"
//Step 2 of 5.
//Define UNIQUE_GUID after the includes
//Run Visual Studio tool GUIDGEN.EXE to generate a worldwide-unique GUID for this app;
//choose option 4: registry format, Copy, and define here (DON'T USE this SAMPLE GUID!):
define UNIQUE_GUID _T("{088FA840-B10D-11D3-BC36-006067709674}")
//Step 3 of 5.
//Use a shared variable between all instances of the application.
//This can be done by creating a shared data segment.
//Place this code block following all includes in the App .cpp file
pragma comment(linker, "/SECTION:.shr,RWS")
pragma data_seg(".shr")
HWND g_hWnd = NULL;
pragma data_seg()
//Step 4 of 5.
//Place this code block at the top of App::InitInstance
bool AlreadyRunning;
HANDLE hMutexOneInstance = ::CreateMutex( NULL, TRUE,
//Choose UNIQUETODESKTOP, UNIQUETOSYSTEM,
//UNIQUETOSESSION, or UNIQUETOTRUSTEE here:
createExclusionName(UNIQUEGUID, UNIQUETODESKTOP));
AlreadyRunning = (GetLastError() == ERRORALREADYEXISTS);
if (hMutexOneInstance != NULL)
{
::ReleaseMutex(hMutexOneInstance);
}
if ( AlreadyRunning )
{ /* kill this */
HWND hOther = ghWnd;
if (hOther != NULL)
{ /* pop up /
:: SetForegroundWindow(hOther);
if (IsIconic(hOther))
{ / restore /
:: ShowWindow(hOther, SW_RESTORE);
} / restore /
} / pop up /
return FALSE; // terminates the creation
} / kill this */
// ... continue with InitInstance
//Step 5 of 5.
//After Creating the Main Frame, get the handle and store it on the shared variable below.
//Place this code block at the bottom of App::InitInstance, after the mainframe has been created or loaded
ghWnd = mpMainWnd->m_hWnd;
//Remember to add exlusion.h, exclusion.cpp to your project (see the original article)!!
I found this information useful for an implementation that did not involve "Avoiding Multiple Instances of an Application," but did involve use of an EnumWindows() callback loop which was freezing up before all of the windows were enumerated. I was able to find a window that was blocking a call to GetWindowText() (it was busy with an I/O operation). Once I discontinued using GetWindowText(), the enumeration of all windows succeeded. I was then able to find the handle to a specific password prompt (launched by a third-party application) and automatically set the password (as well as post an IDOK message to make the window disapear).
This thread is for discussions of Avoiding Multiple Instances of an Application.