资料收集站

SDL

Wednesday
Dec 03rd
Text size
  • Increase font size
  • Default font size
  • Decrease font size

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;

Comments (0)Add Comment

Write comment

busy
 

Google 搜索

在线用户

We have 74 guests online