sProcessName 不能不写 “.exe”, 比如“taskmgr.exe”
见如下代码
int CProcessCheckDlg::GetProcessID(CString sProcessName)
{
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(pe32);
HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == INVALID_HANDLE_VALUE)
{
MessageBox(_T("create tool help 32 snapshot error"));
return 0;
}
int iRet = 0;
BOOL bProcess = Process32First(hProcessSnap, &pe32);
while (bProcess)
{
if ((CString)pe32.szExeFile == sProcessName)
{
iRet++;
}
bProcess = Process32Next(hProcessSnap, &pe32);
}
CloseHandle(hProcessSnap);
return iRet;
}