放在BOOL CServerApp::InitInstance()
HANDLE m_hMutex = CreateMutex(NULL, FALSE, "SilkServer");
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
MessageBox(NULL,"程序已运行","提示",MB_OK|MB_ICONSTOP);
HWND hProgramWnd = ::FindWindow(NULL,"Server");//"Server",窗口标题
if (hProgramWnd)
{
WINDOWPLACEMENT* pWndpl = NULL;
WINDOWPLACEMENT wpm;
pWndpl =&wpm;
GetWindowPlacement(hProgramWnd,&wpm);
if (pWndpl)
{
//将运行的程序窗口还原成正常状态
pWndpl->showCmd = SW_SHOWNORMAL;
::SetWindowPlacement(hProgramWnd,pWndpl);
SetWindowPos(hProgramWnd,HWND_NOTOPMOST,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE);
}
}
// 如果已有互斥量存在则释放句柄并复位互斥量
CloseHandle(m_hMutex);
m_hMutex = NULL;
// 程序退出
return FALSE;
}