Stopping multiple app instances

Started by
1 comment, last by SikCiv 23 years ago
How do you stop more than one instance of an application?

  Downloads:  ZeroOne Realm

Advertisement
Assuming Visual C++/Windows

  // at the start of your program, put this once, in one file// with the other global variables.#pragma data_seg( "SHARESEC" )volatile bool g_bAlreadyLoaded = false;#pragma comment( linker, "/SECTION:SHARESEC,RWS" )#pragma data_seg()int WINAPI WinMain( HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR lpszArgs, int nWinMode ){    // if application already running, show an error    // and exit with -1 return code    if (g_bAlreadyLoaded)    {      MyError( "Application is already running!!" );      return -1;    }    g_bAlreadyLoaded = true; ... do rest of application here ...  



The #pragmas tell the compiler/linker that the variables in the block of memory between them should be shared between all instances of the exe (usually each time its loaded, the globals get re-initialised for each instance).

The volatile is to prevent the compiler from trying to optimise the variable or turn it into a constant or anything else which may stop it working in the shared section.

--
Simon O''''Connor
Creative Asylum Ltd
www.creative-asylum.com

Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

In VB that''s simple:

if app.previnstance=true then
msgbox app.name & " is already running"
end
end if

This topic is closed to new replies.

Advertisement