DWORD GetProcessIDByName( LPCTSTR szProcessName )
{
STARTUPINFO st;
PROCESS_INFORMATION pi;
PROCESSENTRY32 ps;
HANDLE hSnapshot;
DWORD dwPID;
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 0;
}
if(!Process32First(hSnapshot,&ps))
{
return 0;
}
do
{
// 比较进程名
if(lstrcmpi(ps.szExeFile,szProcessName)==0)
{
// 找到了
dwPID = ps.th32ProcessID;
CloseHandle(hSnapshot);
return dwPID;
}
}
while(Process32Next(hSnapshot,&ps));
// 没有找到
CloseHandle(hSnapshot);
return 0;
}