使用命名对象防止运行一个程序的多个实例【转】

来源:互联网 发布:php的面向对象 编辑:程序博客网 时间:2024/06/06 01:38

1.要点

  • 在程序的入口处调用CreateXXX函数创建一个命名对象(如Mutex,Event等均可),然后调用GetLastError()函数检查返回值,看此对象是否已存在,如果已存在则说明已存在此程序的实例
  • 在程序的出口点调用CloseHandle()关闭在入口处创建的命名对象

2.实现代码

  1: //At the entry point, such aa the beginning of WinMain in Win32 app,
  2: //or CWinApp::InitInstance() in MFC app
  3: LPCTSTR instanceName = 
  4:      _T("AD83912A-43F2-4BF6-B0CF-02BF6589FFF7");  //Generated with GuidGen tool
  5: HANDLE h = ::CreateMutex(NULL,FALSE,instanceName);
  6: DWORD error = GetLastError();
  7: if (error == ERROR_ALREADY_EXISTS)
  8: {
  9: ::CloseHandle(h);
 10: return FALSE;  //Exist
 11: }
 12: 
 13: //.......
 14: 
 15: //At the exist point, close the named object and then exist
 16: ::CloseHandle(h);
 17: return FALSE; //Exist
 18: 
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(288) | 评论(0) | 转发(0) |
0

上一篇:什么是Swap Chain【转自MSDN】

下一篇:VC编译器选项【转】

相关热门文章
  • 关于ip_conntrack跟踪连接满导...
  • 我的第一个python程序之续集...
  • 一款已上市MMO手游地图同步方...
  • Error:未在本地计算机上注册“...
  • 我的第一个python程序
  • test123
  • 编写安全代码——小心有符号数...
  • 使用openssl api进行加密解密...
  • 一段自己打印自己的c程序...
  • sql relay的c++接口
  • linux dhcp peizhi roc
  • 关于Unix文件的软链接
  • 求教这个命令什么意思,我是新...
  • sed -e "/grep/d" 是什么意思...
  • 谁能够帮我解决LINUX 2.6 10...
给主人留下些什么吧!~~
原创粉丝点击