【问题标题】:Getting process information of every process获取每个进程的进程信息
【发布时间】:2018-05-17 11:17:56
【问题描述】:

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

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

【问题讨论】:

    标签: windows winapi process wc


    【解决方案1】:

    这是适用于 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);
    

    【讨论】:

    • 你不应该在调用OpenProcess()时使用PROCESS_ALL_ACCESS。仅请求您实际需要的权利。例如,GetPriorityClass() 只需要 PROCESS_QUERY_INFORMATIONPROCESS_QUERY_LIMITED_INFORMATION。但是,这个例子没有使用hProcess 做任何事情,所以你甚至根本不需要调用OpenProcess()
    猜你喜欢
    • 2023-03-03
    • 2020-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-25
    • 2020-05-06
    • 2012-02-12
    相关资源
    最近更新 更多