判断程序是否已经运行

#include

#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);
 }
}

你可能感兴趣的:(软件技术)