Archived

This topic is now archived and is closed to further replies.

SikCiv

Stopping multiple app instances

Recommended Posts

S1CA    1418
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

Share this post


Link to post
Share on other sites