通过进程快照的方式查找指定程序
1 BOOL FindProcessByName(LPCTSTR szFileName, PROCESSENTRY32& pe) 2 { 3 HANDLE hProcess; 4 PROCESSENTRY32 lpe = 5 { 6 sizeof(PROCESSENTRY32) 7 }; 8 9 CString strFileName(szFileName); 10 strFileName.MakeLower(); 11 12 hProcess = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); 13 if (hProcess == INVALID_HANDLE_VALUE) 14 { 15 return FALSE; 16 } 17 18 BOOL isExist = ::Process32First(hProcess, &lpe); 19 BOOL isRuning = FALSE; 20 CString strName; 21 while(isExist) 22 { 23 strName = lpe.szExeFile; 24 strName.MakeLower(); 25 if (strName == strFileName) 26 { 27 isRuning = TRUE; 28 break; 29 } 30 31 isExist = ::Process32Next(hProcess,&lpe); 32 } 33 34 if (isRuning) 35 { 36 memcpy(&pe, &lpe, sizeof(PROCESSENTRY32)); 37 } 38 39 CloseHandle(hProcess); 40 return isRuning; 41 }
需要头文件 #include <TlHelp32.h>