分享 2013-01-14 17:52183****4371
|
浏览 583 次
2013-01-14 18:06
提问者采纳
#include <windows.h> #include <tlhelp32.h> #include <stdio.h> BOOL FindProcess(char *pProcess) { int i=0; PROCESSENTRY32 pe32; pe32.dwSize = sizeof(PROCESSENTRY32); HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if(hProcessSnap == INVALID_HANDLE_VALUE) { printf("不能创建进程快照句柄!\n"); return false; } BOOL bMore = ::Process32First(hProcessSnap, &pe32); while(bMore) { if(stricmp(pProcess,pe32.szExeFile) == 0) { i += 1; //找到,可能不只一个进程 } bMore = ::Process32Next(hProcessSnap, &pe32); } if( i >= 1) { return true; } else { return false; } } int main() { if (FindProcess("qq.exe")) { printf("qq.exe正在运行\n"); } else { printf("没有找到qq.exe"); } return 0; }
追问:
能不能换成全才c++的代码,你这里面还有c语言
追答:
#include <windows.h> #include <tlhelp32.h> #include <iostream> #include <string> using namespace std; BOOL FindProcess(string strProc) { int i=0; PROCESSENTRY32 pe32; pe32.dwSize = sizeof(PROCESSENTRY32); HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if(hProcessSnap == INVALID_HANDLE_VALUE) { cout<<"创建进程快照句柄失败!"<<endl; return false; } BOOL bMore = ::Process32First(hProcessSnap, &pe32); while(bMore) { //这里还是使用stricmp函数最方便,否则还要统一转换成大写或者小写 if(0 == stricmp(strProc.c_str(), pe32.szExeFile)) { i += 1; //找到 } bMore = ::Process32Next(hProcessSnap, &pe32); } if( i >= 1) { return true; } else { return false; } } int main() { if (FindProcess("qq.exe")) { cout<<"qq.exe正在运行"<<endl; } else { cout<<"未找到qq.exe"<<endl; } return 0; }