Limiting Applications to a Single Instance

Here is a very simple example of how to implement a "One Instance" Application. If the application is yet running and the user tries to launch another instance, the running app is activated and line command parameter parsed to the runnin app.

The following code is added to the CWinApp-derived class in the 'InitInstance()' function, after the main window has been initialized but not displayed (m_uUserMessage is a UINT member variable of the same class)

// Define a user message indicating that another instance of
// the app is trying to run:
m_uUserMessage=RegisterWindowMessage("ANOTHER_INSTANCE");

// Test for the presence of another instance from an
// existing main window whose handle has previously
// be written to the Registry
CString strHand=GetProfileString("Control","MainWndHwnd","0");
HWND hWnd;
sscanf(strHand,"%ld",&hWnd);

// Test existing window
if( IsWindow(hWnd) )	// There is an instance running
{
 // Copy to the registry the command line data, as
 // for example a file name
 WriteProfileString("Control","CmdLineFile",cmdInfo.m_strFileName);

 // Sent user message to the running app
 PostMessage(hWnd,m_uUserMessage,0,0);

 // Push running app to the foreground
 SetForegroundWindow(hWnd);

 // Exit current instance
 return FALSE;
}

// No instance running, so save to Registry
// the handle of the current app main window

hWnd=m_pMainWnd->GetSafeHwnd();	
strHand.Format("%ld",hWnd);
WriteProfileString("Control","MainWndHwnd",strHand);
A 'PreTranslateMessage()' function is surcharged in the CWinApp-derived class ('COneInstanceAppApp', in the present example):
BOOL COneInstanceAppApp::PreTranslateMessage(MSG* pMsg) 
{
 if( pMsg->message == m_uUserMessage )
 {
  // Retrive relevant command line info
  CString file=GetProfileString("Control","CmdLineFile","");

  if( !file.IsEmpty() )
  {
   // Do what you want with this info!
  }
  return TRUE; // User message has been handled
 }
	
 return CWinApp::PreTranslateMessage(pMsg);
}

Downloads

Download source - 15 Kb


Comments

  • What I was searching for...

    Posted by drnb on 12/07/2004 06:50pm

    This article answers the question I was just asking myself. It is simple, and understandable in first reading. However, as one of the comments corretly mention, the author has forgotten the important step of cleaning the registy. If you don't do that, you are unable to make the program run again!!!

    Reply
  • Multiple Instances

    Posted by Legacy on 08/04/2002 12:00am

    Originally posted by: JOSE MENDOZA

    How we can to limit that one progran can be unning by a determined number of instances. This example limits to one instance of the exe. But if I want to let the program to run 2 or 3 (a number determined) times simultaneously.

    Reply
  • mutex isn't possible?

    Posted by Legacy on 06/27/2001 12:00am

    Originally posted by: Renato Czar Tome

    Why don't you try to use mutex instead?

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: May 18, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT While the idea of using facial and or gesture recognitions to create a modern, intuitive game seems attractive, some developers may want to leverage Unity 3D as a way to accelerate their development. There are many different ways in which Intel and Unity Technologies have been working together to helps speed the develop of games with the Intel® RealSense™ SDK (Software Developer Kit), so come hear from a panel of experts on what we've done …

  • You may not realize the complexity you could be creating when developing mobile apps – many companies don't initially. You could be doubling your IT costs for development and delivery; not to mention, risking sales, productivity and brand satisfaction. Read this IBM-commissioned Forrester Study to understand the key cost drivers of mobile app delivery -- for both customer-facing and enterprise applications. Find out how you could lower costs and increase success with the right strategy and investment.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date