C++ 判断进程是否存在

来源:互联网 发布:计算机编程语言分类 编辑:程序博客网 时间:2024/05/17 23:17


[cpp]
 view plaincopyprint?
  1. #include <windows.h>    
  2. #include "psapi.h"    
  3. #include"stdio.h"  
  4. #include <tlhelp32.h>  
[cpp] view plaincopyprint?
  1. //  
  2. //判断进程是否存在  
  3. //2012-09-10  
  4. BOOL FindProcess()  
  5. {  
  6.     int i=0;  
  7.     PROCESSENTRY32 pe32;  
  8.     pe32.dwSize = sizeof(pe32);   
  9.     HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);  
  10.     if(hProcessSnap == INVALID_HANDLE_VALUE)  
  11.     {  
  12.         i+=0;  
  13.     }  
  14.     BOOL bMore = ::Process32First(hProcessSnap, &pe32);  
  15.     while(bMore)  
  16.     {  
  17.         //printf(" 进程名称:%s \n", pe32.szExeFile);  
  18.         if(stricmp("进程名",pe32.szExeFile)==0)  
  19.         {  
  20.             //printf("进程运行中");  
  21.             i+=1;  
  22.         }  
  23.         bMore = ::Process32Next(hProcessSnap, &pe32);  
  24.     }  
  25.     if(i>1){           //大于1,排除自身  
  26.         return true;  
  27.     }else{  
  28.         return false;  
  29.     }  
  30. }  


调用这个函数,如果存在则自动退出,如果不存在则正常运行。



转自:http://blog.csdn.net/zhensoft163/article/details/7967993

0 0
原创粉丝点击