BOOL CMyApp::InitInstance()
{
::CreateMutex(NULL,FALSE,"MyApp1");
if(::GetLastError() == ERROR_ALREADY_EXISTS) return FALSE;
{
::CreateMutex(NULL,FALSE,"MyApp1");
if(::GetLastError() == ERROR_ALREADY_EXISTS) return FALSE;
{
m_hMutex = ::CreateMutex(NULL,true,"test");
if(GetLastError() == ERROR_ALREADY_EXISTS)
return false; //表明已经运行
ReleaseMutex(m_hMutex);
}
app初始化实例的时候
用FindWindow可以查当前运行程序类名或标题的名字,
如果判断标题名字有重复的就可以返回了,例如
CWnd *pWndPrev, *pWndChild;
if (pWndPrev = CWnd::FindWindow(NULL,_T("***")))
{
pWndChild = pWndPrev->GetLastActivePopup();
if (pWndPrev->IsIconic())
pWndPrev->ShowWindow(SW_RESTORE);
pWndChild->SetForegroundWindow();
return FALSE;
}如果是判断类名
判断类名方法同上;
AfxRegisterClass之前,为lpszClassName起一个名字;
退出的时候用UnregisterClass;
{
HWND hWnd;
HANDLE hMutex; hInst = hInstance;
hMutex = CreateMutex(NULL,FALSE,"watchdog");
if (WaitForSingleObject(hMutex,0) ==WAIT_TIMEOUT)
{
return FALSE;
}
..
}