Click here to Skip to main content
6,295,667 members and growing! (15,652 online)
Email Password   helpLost your password?
Languages » C / C++ Language » General     Intermediate

CSingleInstance - Single Instance Apps

By PJ Naughter

An MFC class to implement single instance apps.
VC6, MFC, Dev
Posted:3 Mar 2000
Views:164,184
Bookmarked:39 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
24 votes for this article.
Popularity: 6.17 Rating: 4.47 out of 5

1

2
1 vote, 10.0%
3
1 vote, 10.0%
4
8 votes, 80.0%
5

Introduction

Welcome to CSingleInstance, a freeware MFC class to make your app into a single instance. Limiting your app to single instance is more tricky in Win32 with the removal of the hPrevInstance parameter from WinMain and the introduction of separate address spaces.

There are numerous examples already out there of making your app single instance, but some such as the one I found on http://www.codeguru.com/ require knowledge of undocumented or heavy duty MFC.

Other methods such as using FindWindow or FindWindowEx are not 100% bullet proof due to the preemptive Win32 environment.

This class internally uses a memory mapped file (MMF) into which the handle of the main window of your application is stuffed. When a second instance of your app is run, it detects that the memory mapped file exists, gets the window handle of the first instance from the MMF and reactivates the old window.

The source zip file contains the CSingleInstance source code and a simple MFC app (without Doc/View) which demonstrates all the functionality of the class. For further details on how to use the class, have a look at the BOOL CMyApp::InitInstance() function and the included documentation.

Features
Usage
History
API Reference
Contacting the Author

Features

  • Very simple API, only 2 functions exposed.
  • Does not require any messing around with window styles and your mainfrm source code.
  • Much more robust implementation than other methods as already mentioned.
  • The classes are fully Unicode compliant and include Unicode built options in the workspace file.

Usage

To use the class in your code, simply include sinstance.cpp in your project and #include sinstance.h in your app's source file. Create your mainfrm window as normal but before you show it, simply include the following code:

CMyApp::InitInstance()
{
    CInstanceChecker instanceChecker;

    if (instanceChecker.PreviousInstanceRunning())
    {
      AfxMessageBox(_T("Previous version detected, will now restore it"), 
                                                                   MB_OK);
      instanceChecker.ActivatePreviousInstance();
      return FALSE;
    }

    ....

    // create main MDI Frame window

    CMainFrame* pMainFrame = new CMainFrame;
    m_pMainWnd = pMainFrame;

    if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
        return FALSE;

    // If this is the first instance of our App then

    // track it so any other instances can find it.

    if (!instanceChecker.PreviousInstanceRunning())
        instanceChecker.TrackFirstInstanceRunning();

    .....
}

That's all there is to it. Your application should now be single instance. For those interested in the nitty gritty of the code, have a look at sinstance.cpp.

History

  • V1.0 (29th July 1998)
    • Initial public release.
  • V1.01 (27th March 2000)
    • Now ships with a VC 5 workspace.
    • Fixed a potential handle leak where the file handle m_hPrevInstance was not being closed under certain circumstances.
    • The remaining changes were made by Neville Franks. Contact nevf@getsoft.com, http://www.getsoft.com/.
    • Changed #pragma error in instance header file to #pragma message. Former wouldn't compile under VC6
    • Replaced above #pragma with #include.
    • Added TrackFirstInstanceRunning(), MakeMMFFilename()
    • Split PreviousInstanceRunning() up into separate functions so we can call it without needing the MainFrame window.
    • Changed ActivatePreviousInstance() to return hWnd.
  • V1.02 (28th March 2000)
    • Minor update to the documentation.

API Reference

The API consists of the the two public member functions of the class CSingleInstance:

CSingleInstance::PreviousInstanceRunninging

BOOL CSingleInstance::PreviousInstanceRunning()

Return Value:

TRUE if a previous instance of this application is running, otherwise FALSE.

Remarks:

In response to TRUE being sent back the program, should call ActivatePreviousInstance() and abort prematurely from the current instance of the app.

CSingleInstance::ActivatePreviousInstance

BOOL CSingleInstance::ActivatePreviousInstance()

Remarks:

Activates the previous instance of the app as detected in a call to PreviousInstanceRunning().

Contacting the Author

PJ Naughter

Email: mailto:pjn@indigo..ie

Web: http://www.naughter.com/

29th July 1998

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

About the Author

PJ Naughter


Member

Location: United States United States

Other popular C / C++ Language articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 43 (Total in Forum: 43) (Refresh)FirstPrevNext
GeneralCrashes (altough under esoteric circumstances) PinmemberBernhard21:09 22 Nov '06  
GeneralDoesn't work! Pinmemberalmc8:28 21 Nov '06  
GeneralCrashed on restoring previous instance PinmemberBruce Antman4:49 9 May '02  
GeneralRe: Crashed on restoring previous instance Pinmemberpjnaughter0:28 4 Apr '03  
Generalhow to restore my app from system tray? Pinmemberhunglin19:46 21 Mar '02  
GeneralRe: how to restore my app from system tray? PinmemberGopalan0:19 4 Apr '03  
GeneralCode doesn't work, fails assertion PinmemberBokonon7913:09 6 Mar '02  
GeneralRe: Code doesn't work, fails assertion Pinmemberpjnaughter0:30 4 Apr '03  
GeneralWTL SDI Application PinmemberAlpesh Makwana0:27 16 Oct '01  
GeneralPrevious Instance will not restore PinmemberDebbie Christie3:38 12 Jun '01  
GeneralRe: Previous Instance will not restore Pinmemberpjnaughter0:30 4 Apr '03  
GeneralDialog Application PinmemberAnonymous1:02 11 May '01  
GeneralRe: Dialog Application Pinmemberdk0:07 21 Jun '01  
GeneralRe: Dialog Application Pinmemberpjnaughter0:31 4 Apr '03  
GeneralKill previous instrance PinmemberP1:46 11 Apr '01  
GeneralRe: Kill previous instrance Pinmemberpjnaughter0:32 4 Apr '03  
GeneralKill previous instrance PinmemberP1:46 11 Apr '01  
GeneralNeedless Line of Code PinmemberEugene2:23 22 Nov '00  
GeneralDialog based application Pinsussakram3:02 26 Oct '00  
GeneralRe: Dialog based application Pinmembervietdoorgroup18:06 18 Dec '06  
GeneralYeah I have similar prob PinsussSteve8:00 5 Sep '00  
GeneralWhat if the App terminates unexpectably (Crash) PinsussEli13:30 26 Aug '00  
GeneralRe: When the App terminates unexpectably (Crash) PinsussStiewie6:41 28 Aug '00  
GeneralTrackFirstInstanceRunning() not in source code Pinsussleowlf19:57 9 Apr '00  
GeneralRe: TrackFirstInstanceRunning() not in source code PinsussNeville Franks22:17 9 Apr '00  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 3 Mar 2000
Editor: Smitha Vijayan
Copyright 2000 by PJ Naughter
Everything else Copyright © CodeProject, 1999-2009
Web10 | Advertise on the Code Project