获取每个流程的流程信息

时间:2018-05-17 11:17:57

标签: windows winapi process wc

我正在尝试创建一个程序,任何普通用户都可以在Windows上运行并生成所有进程的进程列表,包括可执行位置。我使用CreateToolhelp32Snapshot()来获取所有进程名称,pid,ppid。但是在获取图像路径时遇到问题。我做的每件事都会导致拒绝访问。

我尝试过ZwQueryInformationProcess,GetProcessImageFileName等,并使用OpenProcess来获取每个进程的句柄。我可以通过使用PROCESS_QUERY_LIMITED_INFORMATION来获取句柄,但任何其他选项都不起作用。我迷路了,已经在这里待了几天。有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:0)

这是适用于Windows上非管理员用户的代码。使用PROCESSENTRY32的szExeFile成员获取路径:

HANDLE hProcessSnap = NULL;
HANDLE hProcess = NULL;
PROCESSENTRY32 pe32;
DWORD dwPriorityClass = 0;

// Take a snapshot of all processes in the system.
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == INVALID_HANDLE_VALUE)
{
    return;
}

// Set the size of the structure before using it.
pe32.dwSize = sizeof(PROCESSENTRY32);

// Retrieve information about the first process,
// and exit if unsuccessful
if (!Process32First(hProcessSnap, &pe32))
{
    CloseHandle(hProcessSnap);          // clean the snapshot object
    return;
}

// Now walk the snapshot of processes, and
// display information about each process in turn
do
{
    // do something with the pe32 struct.
    // pe32.szExeFile -> path of the file

} while (Process32Next(hProcessSnap, &pe32));

CloseHandle(hProcessSnap);