判断程序是否已经运行

来源:互联网 发布:怎么更新网络电影 编辑:程序博客网 时间:2024/05/03 03:53

#include <stdio>

#if defined(WIN32)
  #include "windows.h"
  #include "wincon.h"
  #include "stdlib.h"
  #include "stdio.h"
  #include "time.h"
  #include "nb30.h"
  #include "Tlhelp32.h"
#elif defined(SOLARIS)
#endif

bool hasRun (const char* name)
{
#if defined(WIN32)                     
 DWORD id = GetCurrentProcessId();
 
 PROCESSENTRY32 pe;
 HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
 pe.dwSize = sizeof(PROCESSENTRY32);
 if (Process32First(hSnapshot, &pe))
 {
 while (Process32Next(hSnapshot, &pe))
 {
   if (ACE_OS::strcasecmp(pe.szExeFile, name) == 0)
   {
     if (id != pe.th32ProcessID)
     {
       CloseHandle(hSnapshot);
       return true;
     }
   }
 }
 }
 
 CloseHandle(hSnapshot);
 return false;
 
#elif define(SOLARIS)
 return false;

void main(void)
{
 const char* prog = argv[0];
 if (hasRun(prog))
 {
  printf("%s already has run!", prog);
 }
 else
 {
  printf("%s does not exist!", prog);
 }
}