c++ - 列出 Windows 进程和模块

标签 c++ windows api

所以我最近一直在研究 Windows API,并且遇到了一个问题,我需要一些帮助。其实准确的说是两个问题。

我将首先展示代码,然后解释我遇到的困难:

#pragma once
#pragma comment(lib, "Psapi.lib")   

#include <stdlib.h>
#include <stdio.h>
#include <Windows.h>
#include <TlHelp32.h>
#include <Psapi.h>

void ProcessError(DWORD error);

int main() {
    FILE* file = fopen("C:\\Users\\Administrator\\Desktop\\processes.txt", "w");
    DWORD count, i, modulesCount;
    WCHAR buffer[128] = {0};
    HMODULE modules[128] = {0};
    HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
    PROCESSENTRY32 pEntry;

    pEntry.dwSize = sizeof(PROCESSENTRY32);
    count = 0;

    if(snapshot != INVALID_HANDLE_VALUE) {
        if(!Process32First(snapshot, &pEntry)) {
            ProcessError(GetLastError());
            CloseHandle(snapshot);
            ExitProcess(EXIT_FAILURE);
        }

        do {
            HANDLE hProc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pEntry.th32ProcessID);
            GetModuleFileNameEx(hProc, NULL, (LPWSTR)memset(buffer, 0, 128), 128);

            fwprintf(file, L"-------------------------------------------------------\n");
            fwprintf(file, L"%s\t pid: %d\n", pEntry.szExeFile, pEntry.th32ProcessID);
            fwprintf(file, L"%s\n", buffer);

            if(hProc != INVALID_HANDLE_VALUE) {
                if(EnumProcessModules(hProc, (HMODULE*)memset(modules, 0, 128), 128, &modulesCount)) {
                    modulesCount = modulesCount <= 128 ? modulesCount : 128;
                    for(i = 0; i < modulesCount; ++i) {
                        GetModuleFileName(modules[i], (LPWSTR)memset(buffer, 0, 128), 128);
                        if(wcslen(buffer) > 0) {
                            fwprintf(file, L"\t\t0x%X Module: %s\n", modules[i], buffer);
                        }
                    }
                }
                else {
                    ProcessError(GetLastError());
                }
                CloseHandle(hProc);
            }
            else {
                ProcessError(GetLastError());
            }

            count++;
        } while(Process32Next(snapshot, &pEntry));
        fwprintf(file, L"Process count: %d\n", count);

    }
    else {
        ProcessError(GetLastError());
        CloseHandle(snapshot);
        ExitProcess(EXIT_FAILURE);
    }

    fclose(file);
    CloseHandle(snapshot);
    ExitProcess(EXIT_SUCCESS);
}

void ProcessError(DWORD error) {
    printf("Error in thread 0x%X, code: 0x%X\n", GetThreadId(GetCurrentThread()), error);
}

因此,第一个问题与以下内容有关:

if(EnumProcessModules(hProc, (HMODULE*)memset(modules, 0, 128), 128, &modulesCount))

有时我会收到 INVALID_HANDLE 错误,但我真的不知道为什么。 Process 句柄不是无效的,传递给该函数的任何其他参数也不是无效的。如果有人可以向我解释或至少为我指出某个方向(这是更可取的解决方案,因为我对学习更感兴趣 :D),那对我有好处。

其次,出于某种原因,当我枚举进程的模块和 GetModuleFileName() 时,它还包括当前进程的位置。

当我写入文件时,我会得到以下信息:

TuneUpUtilitiesApp32.exe   pid: 2744
D:\Program Files\TuneUp Utilities 2012\TuneUpUtilitiesApp32.exe
      0x76F60000 Module: C:\Windows\SYSTEM32\ntdll.dll
      0x75FE0000 Module: C:\Windows\system32\kernel32.dll
      0x75370000 Module: C:\Windows\system32\KERNELBASE.dll
      0x761A0000 Module: C:\Windows\system32\USER32.dll
      0x770D0000 Module: C:\Windows\system32\GDI32.dll
      0x77130000 Module: C:\Windows\system32\LPK.dll
      0x76EC0000 Module: C:\Windows\system32\USP10.dll
      0x75F20000 Module: C:\Windows\system32\msvcrt.dll
      0x755D0000 Module: C:\Windows\system32\ADVAPI32.dll
      0x75590000 Module: C:\Windows\SYSTEM32\sechost.dll
      0x757D0000 Module: C:\Windows\system32\RPCRT4.dll
      0x77120000 Module: C:\Windows\system32\PSAPI.DLL
      0x755B0000 Module: C:\Windows\system32\IMM32.DLL
      0x75670000 Module: C:\Windows\system32\MSCTF.dll
      0x10000000 Module: C:\Windows\system32\guard32.dll
      0x750D0000 Module: C:\Windows\system32\VERSION.dll
      0x750C0000 Module: C:\Windows\system32\fltlib.dll
      0x0 Module: C:\Users\Administrator\documents\visual studio 2010\Projects\FunWithWindowsAPI\Release\FunWithWindowsAPI.exe
      0x0 Module: C:\Users\Administrator\documents\visual studio 2010\Projects\FunWithWindowsAPI\Release\FunWithWindowsAPI.exe
      0x0 Module: C:\Users\Administrator\documents\visual studio 2010\Projects\FunWithWindowsAPI\Release\FunWithWindowsAPI.exe
      0x0 Module: C:\Users\Administrator\documents\visual studio 2010\Projects\FunWithWindowsAPI\Release\FunWithWindowsAPI.exe
      0x0 Module: C:\Users\Administrator\documents\visual studio 2010\Projects\FunWithWindowsAPI\Release\FunWithWindowsAPI.exe
      0x0 Module: C:\Users\Administrator\documents\visual studio 2010\Projects\FunWithWindowsAPI\Release\FunWithWindowsAPI.exe

它确实为我可以从中提取模块的每个进程执行此操作。任何帮助将不胜感激!

最佳答案

您没有正确使用返回的“modulesCount”。它不是模块的数量,它是以字节为单位的数组大小。您需要除以 sizeof(HMODULE) 以获得模块数。

第二个问题是您希望在开始迭代进程条目时 pEntry.th32ProcessID 仍然有效。那是徒劳的希望,一个过程可以在您迭代时终止。你不检查这个,你不验证 OpenProcess() 是否返回有效句柄。它也可能会失败,因为您没有足够的权限来访问该进程。

关注CreateToolhelp32Snapshot()能做什么,它还支持Module32First/Next()。由于该函数能够创建快照,因此它会更加可靠。

关于c++ - 列出 Windows 进程和模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13999136/

相关文章:

javascript - 从 eBay 中的查找商品高级 API 返回商品属性

javascript - 在 Express.js 中构建 API,获取 "Cannot GET/api"

c++ - 归并排序计数错误 : Counting Inversions

windows - 使用 BAT 从网络共享复制

c++ - 处理 WM_ENDSESSION 的正确方法?

windows - Grep 和 SED 在 PowerShell 中不工作

api - 在什么情况下代理会删除 HTTP 请求 header ?

c++ - 如何衰减所有可变模板参数?

c++ - 我如何使用 WTL::CHyperLink?

c++ - std::runtime_error::runtime_error(const std::string&) 如何满足 std::exception 对 throw() 的要求?