//开发中常用的只允许一个程序运行的办法
//程序以单例模式运行 常用办法,创建一个互斥量
//由于互斥量只允许一个进程或者线程占用 会创建失败,利用这个特性可以做到单例运行改程序
#include "stdafx.h"
#include<windows.h>
#include <stdio.h>
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE m_hMutex = CreateMutex(NULL,TRUE,L"{D5A500DF-E48C-4ba9-A91D-AA6DD4C309A3}");
DWORD dwRet = GetLastError();
if (m_hMutex)
{
if (ERROR_ALREADY_EXISTS == dwRet)
{
printf("程序已经在运行中了,程序退出!\n");
CloseHandle(m_hMutex);
return 0;
}
}
else
{
printf("创建互斥量错误,程序退出!\n");
CloseHandle(m_hMutex);
return 0;
}
printf("RUNING....!\n");
system("pause");
CloseHandle(m_hMutex);
return 0;
}
版权声明:本文为jangdong原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。