Re: How do I ensure only one version of my executable is running
"Joseph M. Newcomer" <newcomer@flounder.com> wrote in message
news:q6o0v2pgmcd2a3n9h0djmgrfqckep6jc55@4ax.com...
The ONLY reliable way is to create a named kernel object, such as an even,
mutex,
or semaphore.
Another reliable way is to put a simple BOOL global variable in a shared
data segment (yes, data segments can be shared for .exe's although they are
mostly used in DLL's).
#pragma data_seg(".sdata")
BOOL g_bAppRunning = FALSE;
#pragma data_seg()
// This comment makes the above data segment shareable
// It replaces having to add a linker option to the project
#pragma comment(linker, "/SECTION:.sdata,rws")
Then:
CMyApp::InitInstance()
{
if ( g_bAppRunning )
return FALSE; // another instance is running
g_bAppRunning = TRUE; // record app is running
...
}
This works great and is lighter weight than a mutex. The only negative I've
heard is that if you have 2 copies of the .exe (say one in RELEASE and
another in DEBUG), then the global variable is not shared between these
disparate copies of the .exe, so it is possible to launch both the release
and debug versions simultaneously. However, this doesn't happen in deployed
solutions.
-- David (MVP)