void IsExistInstance(LPCTSTR lpszName);
//
void CMFCApplication2TestApp::IsExistInstance(LPCTSTR lpszName)
{
HANDLE m_hMutex = ::CreateMutexA(NULL, TRUE, lpszName);
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
AfxMessageBox(_T("该程序已经运行"));
CloseHandle(m_hMutex);
m_hMutex = NULL;
exit(0);
}
}
//*****************************************************************************//
// 唯一的一个 CMFCApplication2TestApp 对象
CMFCApplication2TestApp theApp;
// CMFCApplication2TestApp 初始化
//位置
BOOL CMFCApplication2TestApp::InitInstance()
{
IsExistInstance("MFCApplication2Test.exe");
//
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
// 将它设置为包括所有要在应用程序中使用的
// 公共控件类。
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);
CWinApp::InitInstance();
……
……
}
//*********************************************************************************//