15.5 保证应用程序只有一个实例运行(Mutex)
#include <iostream>
#include <Windows.h>
using namespace std;
int main()
{
HANDLE hMutex = CreateMutex(NULL,TRUE,L"OnlyInstance");
if (hMutex)
{
if (ERROR_ALREADY_EXISTS == GetLastError())
{
cout<<"Already exist"<<endl;
return 0;
}
else
{
cout<<"An instance"<<endl;
}
}
else
{
cout<<"ERROR_INVALID_HANDLE"<<endl;
}
system("pause");
return 0;
}