// VC/MFC应用禁止多开
// 编译环境:VS2017
// MFC在 InitInstance 中创建Mutex来防止多开
HANDLE hMutex;
//hMutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, theApp.m_pszAppName);
// 创建互斥体
hMutex = CreateMutex(NULL, FALSE, _T("Microsoft"));
DWORD dwError = GetLastError();
if (NULL == hMutex)
{
CString szMsg;
szMsg.Format(_T("CreateMutex error:%d\n"), dwError);
OutputDebugString(szMsg);
}
else
{
if (ERROR_ALREADY_EXISTS == dwError)
{ //互斥体已存在,发送WM_QUIT消息退出
OutputDebugString(_T("CreateMutex opened an existing mutex.\n"));
CloseHandle(hMutex);
//PostQuitMessage(0);
//PostMessage(theApp.m_pMainWnd->m_hWnd, WM_QUIT, 0, 0);
//PostMessage(AfxGetMainWnd()->GetSafeHwnd(), WM_QUIT, 0, 0);
PostMessage(NULL, WM_QUIT, (WPARAM)1, 0);
}
else
{
// 正常流程
OutputDebugString(_T("CreateMutex created a new mutex.\n"));
CMutexDlg dlg;
m_pMainWnd = &dlg;
INT_PT
VC/MFC应用禁止多开
最新推荐文章于 2023-02-01 08:48:15 发布