现在只知道两种方法;

1利用mutex对像:

 

#include "stdafx.h"
#include <iostream>
#include <Windows.h>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
 HANDLE lhMutex;


 lhMutex = CreateMutex(NULL, false, "myInstance");
 if (lhMutex)
 {
  if (ERROR_ALREADY_EXISTS == GetLastError())
  {

        MssageBox("程序已经运行!");

        exit();
     }
 }

//其它代码

...........


 ReleaseMutex(lhMutex);
 return 0;
}

 

第二种方法,利用Event对像:

 

#include "stdafx.h"
#include <iostream>
#include <Windows.h>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
 HANDLE lnEvent;

lnEvent = CreateEvent(NULL, false, false, "myInstance");
  if (lnEvent)
 {
  if (ERROR_ALREADY_EXISTS == GetLastError())
  {

     MessageBox("程序已经运行!");

     exit();
     }
 }

//其它代码:

...........

CloseHandle(lnEvent);
 return 0;
}

相关文章: