检测所有进程

来源:互联网 发布:杨澜人工智能观后感 编辑:程序博客网 时间:2024/06/05 05:11

#include <Tlhelp32.h> 

 

DWORD GetProcessIdFromName(LPCTSTR name) //如果有运行,返回进程的PID,没运行返回0  
{
 CString cs="";
 PROCESSENTRY32 pe;
 DWORD id = 0;
 HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
 pe.dwSize = sizeof(PROCESSENTRY32);
 if( !Process32First(hSnapshot,&pe) )
  return 0;
 while(1)
 {
  pe.dwSize = sizeof(PROCESSENTRY32);
  if( Process32Next(hSnapshot,&pe)==FALSE )
   break;
  cs.Format("%s",(pe.szExeFile));
  TRACE(cs);
  if(strcmp(pe.szExeFile,name) == 0)
  {
   
   id = pe.th32ProcessID;
   break;
  }
  
 }
 CloseHandle(hSnapshot);
 return id;
}