This article was previously published under Q106385
This article has been archived. It is offered "as is" and will no longer be updated.
The entry point of both 16-bit (Win16) and 32-bit (Win32) Windowsapplications 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 mayhave the same ID as another application.
To determine whether another instance of the application is running, use anamed mutex. If opening the mutex fails, then there are no other instancesof the application running. FindWindow() can be used with the class andwindow name. However, note that a second instance of the application couldbe started, and could execute the FindWindow() call before the firstinstance has created its window. Use a named object to ensure that thisdoes not happen.
The fact that hPrevInstance is set to NULL simplifies porting Win16applications. Most 16-bit Windows-based applications contain the followinglogic:
Under Win16, window classes only are registered by the first instance ofan application. Consequently, if hPrevInstance is not NULL, then the windowclasses 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 correctlyregister its window classes.