Stopping multiple app instances
Assuming Visual C++/Windows
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
// 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
In VB that''s simple:
if app.previnstance=true then
msgbox app.name & " is already running"
end
end if
if app.previnstance=true then
msgbox app.name & " is already running"
end
end if
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement