vc进程和模块查询
来源:互联网 发布:淘宝购物车页面 编辑:程序博客网 时间:2024/06/10 03:41
李国帅编于2009-02-25 10:17
查询进程是否打开
#include <Psapi.h>#pragma comment(lib,"Psapi.lib")//enum process to find a certain moduleBOOL IsAppRun(CString modulename){ DWORD buf[4096]; DWORD num; TCHAR filenamebuf[_MAX_PATH + 1]; HMODULE hModule; DWORD cbReturned; BOOL bret = FALSE; BOOL bfound = FALSE; CString msg; bret = EnumProcesses(buf, 4095, &num); if (!bret) { printf("Error EnumProcesses"); return FALSE; } for (int i = 0; i < (int)num; i++) { HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, buf[i]); if (hProcess == NULL) continue; bret = EnumProcessModules(hProcess, &hModule, sizeof(hModule), &cbReturned); if (bret) { DWORD dwret = GetModuleFileNameEx(hProcess, hModule, filenamebuf, _MAX_PATH); CloseHandle(hProcess); if (dwret == 0) { msg.Format("%d", GetLastError()); printf(msg); break; } else { TCHAR* pfind = _tcsstr(filenamebuf, modulename); if (pfind) { bfound = TRUE; break; } } } } return bfound;}BOOL IsAppRun(CString modulename);int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]){ CString ModuleName("TSRun.exe"); BOOL bfound = IsAppRun(ModuleName); if (bfound) printf("Found it"); else printf("Not found"); system("pause"); return 0;}
阅读全文