Limiting Applications to a Single Instance | CodeGuru

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, […]

Written By
CodeGuru Staff
CodeGuru Staff
Aug 31, 2000
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.