You can limit a 32 bit application to one instance at a time by saving in the system registry the window class name for the first instance, and then checking to see if an application is running with that class name when you attempt to launch the second instance of the application.
In your CFrameWnd derived object implementation file ( usually "mainfrm.cpp" ) and in the OnCreate() over-ride, get the window class name and save it in the system registry. The following lines should be entered immediately after the code that creates the CFrameWnd derived object.
char szBuffer[ 256 ];
GetClassName( m_hWnd, szBuffer, 255 )
AfxGetApp()->WriteProfileString( "Initialization", "ClassName", szBuffer );
In the InitInstance() over-ride in your application object, retrieve the window class name from the system registry and check whether an application is already running that shares the class name. If there is such an instance, notify the user and bring the already running application to the top of the Z order.
Return FALSE from InitInstance to abort the second instance of the application.
SetRegistryKey( "CustomSoft" );
CString csClassName;
csClassName = GetProfileString("Initialization","ClassName","None");
HWND hWnd;
hWnd = ::FindWindow( ( LPCSTR )csClassName, NULL );
if(hWnd) {
AfxMessageBox( "MyApplication.exe is already running..." );
::SetForegroundWindow( hWnd );
return FALSE;
Bookmark
Email This
Hits: 3508
Comments (0)

Write comment



