自定义一个进程类
class Process
{
public:
Process(std::wstring name, DWORD id);
~Process();
std::wstring getProcessName(void) const;
DWORD getProcessId(void) const;
private:
std::wstring processName;
DWORD processId;
};
获取进程信息的函数
bool GetProcessByProcessName(std::vector<Process>& processVec, const std::wstring& processName)
{
//创建系统快照,并返回进程句柄
HANDLE handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (handle == INVALID_HANDLE_VALUE)
return false;
//枚举系统进程
PROCESSENTRY32 info;
info.dwSize = sizeof(PROCESSENTRY32);
Process32First(handle, &info);
while (Process32Next(handle, &info) != FALSE)
{
if (_wcsicmp(processName, std::wstring(info.szExeFile)) == 0)
processVec.emplace_back(Process(processName, info.th32ProcessID));
}
CloseHandle(handle);
return true;
}
参考文档
MSDN - CreateToolhelp32Snapshot
MSDN - Process32First
我是江鸟,即将迈向社会的程序猿,欢迎交流~