ToolHelp 根据进程名 返回进程pid

#include <windows.h> #include <TLHELP32.H> #include <iostream> using namespace std; DWORD FindTargetProcess(LPCTSTR lpszProcess = NULL) { DWORD dwRtn = 0; HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); PROCESSENTRY32 pe32; pe32.dwSize = sizeof(PROCESSENTRY32); Process32First(hSnapShot,&pe32); do { if(0 == lstrcmpi(pe32.szExeFile,lpszProcess)) { dwRtn = pe32.th32ProcessID; break; } } while (Process32Next(hSnapShot,&pe32)); CloseHandle(hSnapShot); return dwRtn; } void main(int argc,TCHAR*argv[]) { DWORD dwProcessID = FindTargetProcess("hh.exe"); cout<<dwProcessID<<endl; HANDLE hProcess = OpenProcess(PROCESS_CREATE_THREAD|PROCESS_VM_OPERATION|PROCESS_VM_WRITE,FALSE,dwProcessID); if(NULL == hProcess) cout<<"Open Fail"<<endl; }

你可能感兴趣的:(ToolHelp 根据进程名 返回进程pid)