• Advertisement

Archived

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

Stopping multiple app instances

This topic is 6184 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do you stop more than one instance of an application?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
In VB that''s simple:

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

Share this post


Link to post
Share on other sites

  • Advertisement