How To Identify a Previous Instance of an Application

Article translations Article translations
Article ID: 106385 - View products that this article applies to.
This article was previously published under Q106385
Expand all | Collapse all


The entry point of both 16-bit (Win16) and 32-bit (Win32) Windows applications is documented as follows:
   int WinMain( hInstance, hPrevInstance, lpszCmdLine, nCmdShow )

   HINSTANCE hInstance;        /* Handle of current instance  */ 
   HINSTANCE hPrevInstance;    /* Handle of previous instance */ 
   LPSTR lpszCmdLine;          /* Address of command line     */ 
   int nCmdShow;               /* Show state of window        */ 
However, under Win32, hPrevInstance is documented to always be NULL. The reason is that each application runs in its own address space and may have the same ID as another application.

To determine whether another instance of the application is running, use a named mutex. If opening the mutex fails, then there are no other instances of the application running. FindWindow() can be used with the class and window name. However, note that a second instance of the application could be started, and could execute the FindWindow() call before the first instance has created its window. Use a named object to ensure that this does not happen.


The fact that hPrevInstance is set to NULL simplifies porting Win16 applications. Most 16-bit Windows-based applications contain the following logic:
   if( !hPrevInstance )
      if( !InitApplication(hInstance) )
         return FALSE;
Under Win16, window classes only are registered by the first instance of an application. Consequently, if hPrevInstance is not NULL, then the window classes have already been registered and InitApplication() is not called.

Under Win32, because hPrevInstance is always NULL, InitApplication() is always called, and each instance of an application will correctly register its window classes.


Article ID: 106385 - Last Review: July 11, 2005 - Revision: 1.3
  • Microsoft Platform Software Development Kit-January 2000 Edition
kbhowto KB106385

Give Feedback


Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from