Re: How do I ensure only one version of my executable is running

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



"Joseph M. Newcomer" <newcomer@xxxxxxxxxxxx> wrote in message
news:q6o0v2pgmcd2a3n9h0djmgrfqckep6jc55@xxxxxxxxxx
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)


.



Relevant Pages

  • Re: How do I ensure only one version of my executable is running
    ... data segment. ... #pragma data_seg ... 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. ...
    (microsoft.public.vc.mfc)
  • Re: How to spy global mouse & keyboard event?
    ... the #pragma is only a fraction of the problem. ... you have to cause the data segment to be shared ... know if I should disconnect the hook if the DLL is being unloaded. ... shared DLL, as this can cause injection of the entire MFC DLL into each process, so MFC ...
    (microsoft.public.vc.mfc)
  • Re: #pragma data_seg
    ... you need to avoid race conditions by using mutex or critical ... to ensure that any reader process will read ... When using #pragma data_seg, you need to make sure to mark the data segment ...
    (microsoft.public.vc.mfc)
  • Re: Why use #pragma data_seg?
    ... therefore the second pragma data_seg is irrelevant since it merely says ... to revert to the default data segment, which is the only data segment currently being ... initialized variables in a header file. ... The default segment in the .obj file for initialized ...
    (microsoft.public.vc.mfc)
  • Re: #pragma data_seg
    ... you need to avoid race conditions by using mutex or critical ... to ensure that any reader process will read ... When using #pragma data_seg, you need to make sure to mark the data segment ...
    (microsoft.public.vc.mfc)