只允许一个实例运行,定义一个全局句柄
HANDLE g_hMutex;
在主窗体的构造函数中
g_hMutex = CreateMutex(NULL, FALSE, L"MyAppName");
if(ERROR_ALREADY_EXISTS == GetLastError()){
CloseHandle(g_hMutex);
PostQuitMessage(0);//关闭所有窗口
}
在主窗体的OnDestory函数中关闭句柄
CloseHandle(g_hMutex);