BOOL GetProcessIdByName(LPSTR szProcessName, DWORD *lpPID)
{
STARTUPINFO st;
PROCESS_INFORMATION pi;
PROCESSENTRY32 ps;
HANDLE hSnapshot;
ZeroMemory(&st,sizeof(STARTUPINFO));
ZeroMemory(&pi,sizeof(PROCESS_INFORMATION));
st.cb = sizeof(STARTUPINFO);
ZeroMemory(&ps,sizeof(PROCESSENTRY32));
ps.dwSize = sizeof(PROCESSENTRY32);
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if(hSnapshot == INVALID_HANDLE_VALUE)
{
return FALSE;
}
if(!Process32First(hSnapshot,&ps))
{
return FALSE;
}
do
{
if(lstrcmpi(ps.szExeFile,szProcessName) == 0)
{
*lpPID = ps.th32ProcessID;
CloseHandle(hSnapshot);
return TRUE;
}
}while(Process32Next(hSnapshot,&ps));
CloseHandle(hSnapshot);
return FALSE;
}
通过进程名获取进程ID
最新推荐文章于 2023-07-06 15:50:54 发布