Click here to Skip to main content
15,886,095 members
Articles / Desktop Programming / MFC

Single Instance Application

Rate me:
Please Sign up or sign in to vote.
5.00/5 (10 votes)
31 Oct 20023 min read 117.4K   1.3K   39  
An example approach to solving the single instance application problem with command line argument passing.
//------------------------------------------------------------------------------

#include "stdafx.h"
#include "Singleton.h"
#include "SingletonDlg.h"

//------------------------------------------------------------------------------

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//------------------------------------------------------------------------------

BEGIN_MESSAGE_MAP(CSingletonApp, CWinApp)
END_MESSAGE_MAP()

//------------------------------------------------------------------------------

CSingletonApp theApp;

//------------------------------------------------------------------------------

CSingletonApp::CSingletonApp()
{
}

//------------------------------------------------------------------------------

CSingletonApp::~CSingletonApp()
{
}

//------------------------------------------------------------------------------

void CSingletonApp::WakeUp ( LPCTSTR aCommandLine ) const
// Name:        WakeUp 
// Type:        Function
// Description: virtual override for wake up, so we can process the command line
{
	// Call parent class to handle the basic functionality
	CSingleInstance::WakeUp ( aCommandLine ) ;	

	// Send new command line to the dialog
	CSingletonDlg* lSingletonDlg = (CSingletonDlg*) m_pMainWnd ;
	if ( lSingletonDlg )
	{
		lSingletonDlg -> SetCommandLine ( aCommandLine ) ;
	}
}

//------------------------------------------------------------------------------

BOOL CSingletonApp::InitInstance()
{
	// We use a GUID here, so that we are sure the string is 100% unique
	if ( CSingleInstance::Create ( _T("E435FC13-82C1-4f80-97C5-006FF4A4E302") ) == FALSE )
		return FALSE ;

	AfxEnableControlContainer();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	// of your final executable, you should remove from the following
	// the specific initialization routines you do not need.

	#ifdef _AFXDLL
	// Call this when using MFC in a shared DLL
	Enable3dControls () ;			
	#else
	// Call this when linking to MFC statically
	Enable3dControlsStatic () ;
	#endif

	CSingletonDlg lSingletonDlg ;
	m_pMainWnd = &lSingletonDlg ;
	lSingletonDlg . DoModal () ;

	// Since the dialog has been closed, return FALSE so that we exit the
	// application, rather than start the application's message pump.
	return FALSE ;
}

//------------------------------------------------------------------------------

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Australia Australia
Developing windows applications for over 15 years now starting on Win 3.1 with Object Oriented Pascal, progressed to C++ and OWL, in 1996 switch to MFC and never looked back, now focusing on .NET/Mono.

Comments and Discussions