Click here to Skip to main content
11,579,662 members (71,798 online)
Click here to Skip to main content

CSingleInstance - Single Instance Apps

, 3 Mar 2000 229.8K 2K 65
Rate this:
Please Sign up or sign in to vote.
An MFC class to implement single instance apps.

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

Share

About the Author

PJ Naughter
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralCrashes (altough under esoteric circumstances) Pin
Bernhard22-Nov-06 20:09
memberBernhard22-Nov-06 20:09 
GeneralDoesn't work! Pin
almc21-Nov-06 7:28
memberalmc21-Nov-06 7:28 
GeneralCrashed on restoring previous instance Pin
Bruce Antman9-May-02 3:49
memberBruce Antman9-May-02 3:49 
GeneralRe: Crashed on restoring previous instance Pin
pjnaughter3-Apr-03 23:28
memberpjnaughter3-Apr-03 23:28 
Questionhow to restore my app from system tray? Pin
hunglin21-Mar-02 18:46
memberhunglin21-Mar-02 18:46 
AnswerRe: how to restore my app from system tray? Pin
Gopalan3-Apr-03 23:19
memberGopalan3-Apr-03 23:19 
GeneralCode doesn't work, fails assertion Pin
Bokonon796-Mar-02 12:09
memberBokonon796-Mar-02 12:09 
GeneralRe: Code doesn't work, fails assertion Pin
pjnaughter3-Apr-03 23:30
memberpjnaughter3-Apr-03 23:30 
Make sure you have the latest version from my web site as there have been a number of updates since I posted to codeproject
GeneralWTL SDI Application Pin
Alpesh Makwana15-Oct-01 23:27
memberAlpesh Makwana15-Oct-01 23:27 
GeneralPrevious Instance will not restore Pin
Debbie Christie12-Jun-01 2:38
memberDebbie Christie12-Jun-01 2:38 
GeneralRe: Previous Instance will not restore Pin
pjnaughter3-Apr-03 23:30
memberpjnaughter3-Apr-03 23:30 
GeneralDialog Application Pin
Anonymous11-May-01 0:02
memberAnonymous11-May-01 0:02 
GeneralRe: Dialog Application Pin
dk20-Jun-01 23:07
memberdk20-Jun-01 23:07 
GeneralRe: Dialog Application Pin
pjnaughter3-Apr-03 23:31
memberpjnaughter3-Apr-03 23:31 
GeneralKill previous instrance Pin
P11-Apr-01 0:46
memberP11-Apr-01 0:46 
GeneralRe: Kill previous instrance Pin
pjnaughter3-Apr-03 23:32
memberpjnaughter3-Apr-03 23:32 
GeneralKill previous instrance Pin
P11-Apr-01 0:46
memberP11-Apr-01 0:46 
GeneralNeedless Line of Code Pin
Eugene22-Nov-00 1:23
memberEugene22-Nov-00 1:23 
GeneralDialog based application Pin
akram26-Oct-00 2:02
sussakram26-Oct-00 2:02 
GeneralRe: Dialog based application Pin
vietdoorgroup18-Dec-06 17:06
membervietdoorgroup18-Dec-06 17:06 
GeneralYeah I have similar prob Pin
Steve5-Sep-00 7:00
sussSteve5-Sep-00 7:00 
QuestionWhat if the App terminates unexpectably (Crash) Pin
Eli26-Aug-00 12:30
sussEli26-Aug-00 12:30 
AnswerRe: When the App terminates unexpectably (Crash) Pin
Stiewie28-Aug-00 5:41
sussStiewie28-Aug-00 5:41 
GeneralTrackFirstInstanceRunning() not in source code Pin
leowlf9-Apr-00 18:57
sussleowlf9-Apr-00 18:57 
GeneralRe: TrackFirstInstanceRunning() not in source code Pin
Neville Franks9-Apr-00 21:17
sussNeville Franks9-Apr-00 21:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 4 Mar 2000
Article Copyright 2000 by PJ Naughter
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid