Restricting your application to one instance only

Submitted by date of submission user level 

It's easy to restrict your application to one instance only. There are two ways. 

Simple Method : Write one registry entry with a True/False values. When your run your application, set this value to True and Set this value to False again at ExitInstance of your application.

Other Method:

You can save class instance of your application. Write class name in from mainframe.

char szBuff[ 255 ];

GetClassName( m_hWnd, szBuffer, 254 )

AfxGetApp()->WriteProfileString( "mcInit", "mcClsName", szBuff );  

  

Now check on InitInstance if instance of this class is already running of not.

HWND hWnd;

SetRegistryKey( "something" );

CString csClassName;

csClassName = GetProfileString("mcInit","mcClsName","None");

hWnd = ::FindWindow( ( LPCSTR )csClassName, NULL );

if(hWnd) {

AfxMessageBox( "Application is already running..." );

::SetForegroundWindow( hWnd );

if (::IsIconic (hWnd) != FALSE) ::ShowWindow (hWnd, SW_RESTORE);

return FALSE;

}