创建了个MFC对话框程序,怎么运行一次

席戛戛 2014-09-16 11:39:19
BOOL CtestkeyboardApp::InitInstance()
{
if (!oneinstance())
{
return FALSE;
}//我在InitInstance函数一开始就用互斥对象判断是否存在
。。。
}

//oneinstance如下
BOOL CtestkeyboardApp::oneinstance()
{
/****/
//唯一
HANDLE hMutex = NULL;
TCHAR *lpszName = "TestMutex";

hMutex = ::CreateMutex(NULL, FALSE, lpszName);
DWORD dwRet = ::GetLastError();
if (hMutex)
{
if (ERROR_ALREADY_EXISTS == dwRet)
{
AfxMessageBox("Has been running");
CloseHandle(hMutex); // should be closed
return FALSE;
}
}
else
{
//创建失败
}
::CloseHandle(hMutex);

// Add this code in Destruction function
/****/
return TRUE;
}


为什么我的程序还是能够 打开好多个。
...全文
133 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
老王爱上猫 2014-09-16
  • 打赏
  • 举报
回复

 if (!oneinstance())
 {
        return TRUE;
 }
试一下
席戛戛 2014-09-16
  • 打赏
  • 举报
回复
引用 5 楼 zhao4zhong1 的回复:
 HANDLE hMutex;
    hMutex = CreateMutex(NULL, FALSE, "YourProgramName" );
    if ( WaitForSingleObject(hMutex, 1000) == WAIT_TIMEOUT ) {
        // There is another instance out there, but it is taking to long to
        // locate, just exit
        return FALSE;
    }
    if ( GetLastError() == ERROR_ALREADY_EXISTS ) {
        ReleaseMutex(hMutex);
        return FALSE;
    }
……
    ReleaseMutex(hMutex);
    CloseHandle(hMutex);
//oneinstance如下 BOOL CtestkeyboardApp::oneinstance() { /****/ //唯一 HANDLE hMutex = NULL; TCHAR *lpszName = "TestMutex"; hMutex = ::CreateMutex(NULL, FALSE, lpszName); DWORD dwRet = ::GetLastError(); if (hMutex) { if (ERROR_ALREADY_EXISTS == dwRet) { AfxMessageBox("Has been running"); CloseHandle(hMutex); // should be closed return FALSE; } } else { //创建失败 } // ::CloseHandle(hMutex); // Add this code in Destruction function /****/ return TRUE; } CloseHandle 应该在CWinApp退出的时候关闭 互斥对象,每次调用oneinstance后,互斥对象被关闭了,难怪第二次运行,还能运行。 感谢楼上的各位
赵4老师 2014-09-16
  • 打赏
  • 举报
回复
 HANDLE hMutex;
    hMutex = CreateMutex(NULL, FALSE, "YourProgramName" );
    if ( WaitForSingleObject(hMutex, 1000) == WAIT_TIMEOUT ) {
        // There is another instance out there, but it is taking to long to
        // locate, just exit
        return FALSE;
    }
    if ( GetLastError() == ERROR_ALREADY_EXISTS ) {
        ReleaseMutex(hMutex);
        return FALSE;
    }
……
    ReleaseMutex(hMutex);
    CloseHandle(hMutex);
赵4老师 2014-09-16
  • 打赏
  • 举报
回复
HANDLE hMutex; hMutex = CreateMutex(NULL, FALSE, "RECcap" ); if ( WaitForSingleObject(hMutex, 1000) == WAIT_TIMEOUT ) { // There is another instance out there, but it is taking to long to // locate, just exit return FALSE; } if ( GetLastError() == ERROR_ALREADY_EXISTS ) { ReleaseMutex(hMutex); return FALSE; } …… ReleaseMutex(hMutex); CloseHandle(hMutex);
老王爱上猫 2014-09-16
  • 打赏
  • 举报
回复
自己调试下,写法大体没问题..
席戛戛 2014-09-16
  • 打赏
  • 举报
回复
引用 1 楼 bjym1987 的回复:

 if (!oneinstance())
 {
        return TRUE;
 }
试一下
还是能运行多个。

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧