参看文档,完全是自己写的哦
#include "Psapi.h"
//导入库Psapi.lib
DWORD GetPIDbyProcessName(CString strProcessName)
{
TCHAR szProcessName[MAX_PATH] = _T("<unknown>");
DWORD aProcesses[1024]={0}, cbNeeded, cProcesses;
//枚举进程
if (EnumProcesses(aProcesses,sizeof(aProcesses),&cbNeeded))
{
for (int i = 0; i < cbNeeded/sizeof(DWORD); i++)
{
//打开进程
HANDLE hProcess = NULL;
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,
FALSE,aProcesses[i]);
if (hProcess != NULL)
{
//枚举进程模块
HMODULE hMod = NULL;
DWORD dwNeeded = 0;
CString strName;
if(EnumProcessModules(hProcess,&hMod,sizeof(hMod),&dwNeeded))
{
//获得名字
if(GetModuleBaseName(hProcess,NULL,szProcessName,sizeof(szProcessName)/sizeof(TCHAR)))
{
TRACE(_T("%-20s %d/r/n"),szProcessName,aProcesses[i]);
strName = szProcessName;
strName.MakeUpper();
strProcessName.MakeUpper();
if (strName == strProcessName)
return aProcesses[i];
}
}
}
}
}
}
BOOL KillProcessbyPID(DWORD pid)
{
DWORD dwExitCode = 0;
HANDLE hProcess = NULL;
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ|
PROCESS_TERMINATE ,
FALSE,pid);
if(NULL != hProcess)
{
if(GetExitCodeProcess(hProcess,&dwExitCode))
return TerminateProcess(hProcess,dwExitCode);
else
return TerminateProcess(hProcess,dwExitCode);
}
}