获得进程的名字和ID(psapi)
EnumProcess.h
#pragma once
#pragma comment(lib, "psapi.lib")
#include <windows.h>
#include <vector>
#include <string>
#include <psapi.h>
using std::vector;
using std::string;
struct ProcessInfro
{
public:
string ProcessName;
int ProcessID;
};
class MyEnumProcess //
{
public:
vector<ProcessInfro> GetProcessInfo();
string GetProcessName(DWORD processID, UINT index);
};
#pragma comment(lib, "psapi.lib")
#include <windows.h>
#include <vector>
#include <string>
#include <psapi.h>
using std::vector;
using std::string;
struct ProcessInfro
{
public:
string ProcessName;
int ProcessID;
};
class MyEnumProcess //
{
public:
vector<ProcessInfro> GetProcessInfo();
string GetProcessName(DWORD processID, UINT index);
};
EnumProcess.cpp
#include "EnumProcess.h"
//-------------------------------------
//返回进程名字
vector<ProcessInfro> MyEnumProcess::GetProcessInfo()
{
vector<ProcessInfro> mVectorProcessInfo;
DWORD aProcesses[1024], cbNeeded, cProcesses; //cbNeeded表示有多少个进程x8,因为一个id占位,aProcesses是进程ID的数组
UINT index; //无符号整形
if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded)) //遍历进程 cbNeeded为返回了多少byte
{
return mVectorProcessInfo;
}
// 有多少个进程
cProcesses = cbNeeded / sizeof(DWORD); //除以双字节宽度,没个进程ID占一个DWORD
for ( index = 0; index < cProcesses; index++ )
{
ProcessInfro mProcessInfo;
mProcessInfo.ProcessName=GetProcessName(aProcesses[index], index);
mProcessInfo.ProcessID=aProcesses[index];
mVectorProcessInfo.push_back(mProcessInfo);
}
return mVectorProcessInfo;
}
//=========================================================================
string MyEnumProcess::GetProcessName(DWORD processID, UINT index)
{
string strProcess;
char szProcessName[MAX_PATH];
HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, processID); //aProcesses[index]为进程ID
if (hProcess)
{
HMODULE hMod;
DWORD cbNeededM;
if(EnumProcessModules( hProcess, &hMod, sizeof(hMod), &cbNeededM))
{
GetModuleBaseNameA(hProcess, hMod, szProcessName, sizeof(szProcessName));
}
}
//字符赋给字符串
strProcess=szProcessName;
int i=strProcess.size();
if(i<100)
{
}
else
{
strProcess="名字未知";
}
CloseHandle(hProcess);
return strProcess;
}
//-------------------------------------
//返回进程名字
vector<ProcessInfro> MyEnumProcess::GetProcessInfo()
{
vector<ProcessInfro> mVectorProcessInfo;
DWORD aProcesses[1024], cbNeeded, cProcesses; //cbNeeded表示有多少个进程x8,因为一个id占位,aProcesses是进程ID的数组
UINT index; //无符号整形
if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded)) //遍历进程 cbNeeded为返回了多少byte
{
return mVectorProcessInfo;
}
// 有多少个进程
cProcesses = cbNeeded / sizeof(DWORD); //除以双字节宽度,没个进程ID占一个DWORD
for ( index = 0; index < cProcesses; index++ )
{
ProcessInfro mProcessInfo;
mProcessInfo.ProcessName=GetProcessName(aProcesses[index], index);
mProcessInfo.ProcessID=aProcesses[index];
mVectorProcessInfo.push_back(mProcessInfo);
}
return mVectorProcessInfo;
}
//=========================================================================
string MyEnumProcess::GetProcessName(DWORD processID, UINT index)
{
string strProcess;
char szProcessName[MAX_PATH];
HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, processID); //aProcesses[index]为进程ID
if (hProcess)
{
HMODULE hMod;
DWORD cbNeededM;
if(EnumProcessModules( hProcess, &hMod, sizeof(hMod), &cbNeededM))
{
GetModuleBaseNameA(hProcess, hMod, szProcessName, sizeof(szProcessName));
}
}
//字符赋给字符串
strProcess=szProcessName;
int i=strProcess.size();
if(i<100)
{
}
else
{
strProcess="名字未知";
}
CloseHandle(hProcess);
return strProcess;
}