第一种使用互斥体
BOOL IsAlreadyRun()
{
HANDLE hMutex = NULL;
hMutex = CreateMutex(NULL, FALSE, L"MYFLAG");
if (hMutex != NULL)
{
if (ERROR_ALREADY_EXISTS == GetLastError())
{
ReleaseMutex(hMutex);
return TRUE;
}
}
return FALSE;
}
int main()
{
if (IsAlreadyRun())
{
return 0;
}
}
第二种使用共享内存
#pragma data_seg("sharedata")
DWORD dwFlagId = 0xEE;
#pragma data_seg()
#pragma comment(linker, "/SECTION:sharedata,RWS")
BOOL IsAlreadyRun()
{
if (dwFlagId == 0xEF)
{
return TRUE;
}
return FALSE;
}
int main()
{
if (IsAlreadyRun())
{
return 0;
}
else
{
dwFlagId = 0xEF;
}
}