VC++: How to ensure that at a time only one instance of an application runs in the memory?
How to ensure that at a time only one instance of an application runs in the memory?
Some applications like MS-Power Point allow only one instance of the application to run in the memory. It means that if PowerPoint application is already loaded and if we try to open another '.ppt' file the file gets opened in the same instance of PowerPoint. We can also achieve this using the following code.
In the frame window class write a function as shown below:
int CMainFrame::oncheck( )
{
return 0xA1B2C3D4 ;
}
Nothing special about the number 0xA1B2C3D4. We can give any unique number.
Make the following entry in message map of frame window class.
ON_MESSAGE ( WM_USER, oncheck )
BOOL CMyApp::InitInstance( )
{
CWnd *check = CWnd::FindWindow ( 0, "Hello World" ) ;
if ( check && check -> SendMessage ( WM_USER ) == 0xA1B2C3D4 )
{
check -> SetForegroundWindow( ) ;
check -> ShowWindow ( SW_SHOWNORMAL ) ;
}
else
{
// Code added by AppWizard
m_pMainWnd -> ShowWindow ( SW_SHOW ) ;
m_pMainWnd -> UpdateWindow( ) ;
return TRUE ;
}
return 0 ;
}In the above code "Hello World" must be the title of the window.




Comments
Log in or create a user account to comment.