- #include <windows.h>
- #include "psapi.h"
- #include"stdio.h"
- #include <tlhelp32.h>
- //
- //判断进程是否存在
- //2012-09-10
- BOOL FindProcess()
- {
- int i=0;
- PROCESSENTRY32 pe32;
- pe32.dwSize = sizeof(pe32);
- HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
- if(hProcessSnap == INVALID_HANDLE_VALUE)
- {
- i+=0;
- }
- BOOL bMore = ::Process32First(hProcessSnap, &pe32);
- while(bMore)
- {
- //printf(" 进程名称:%s \n", pe32.szExeFile);
- if(stricmp("进程名",pe32.szExeFile)==0)
- {
- //printf("进程运行中");
- i+=1;
- }
- bMore = ::Process32Next(hProcessSnap, &pe32);
- }
- if(i>1){ //大于1,排除自身
- return true;
- }else{
- return false;
- }
- }
调用这个函数,如果存在则自动退出,如果不存在则正常运行。
转自:http://blog.youkuaiyun.com/zhensoft163/article/details/7967993