如何获得C#中的所有过程模块?

时间:2020-04-06 22:10:42

标签: c# c++ winapi

我知道System.Diagnostics中有一个process.Modules列表,其中包含一些过程模块,但由于在这里找不到一些dll而遇到了一些麻烦:

Process process = Memory.GetProcessByName("csgo");

foreach (ProcessModule pm in process.Modules) 
    Console.WriteLine(pm.ModuleName);

enter image description here

所以我一直在寻找一种解决方案来使它起作用,我在pinvoke.net上发现了一些有趣的东西,但它也没有起作用:

var snapshot = CreateToolhelp32Snapshot(SnapshotFlags.Module, (uint)process.Id);

MODULEENTRY32 mod = new MODULEENTRY32() { 
    dwSize = (uint)Marshal.SizeOf(typeof(MODULEENTRY32)) 
};

if (!Module32First((IntPtr)snapshot, ref mod))
    return;

do { Console.WriteLine(mod.szModule); } while (Module32Next((IntPtr)snapshot, ref mod));

enter image description here

然后我搜索了C ++中相同代码的实现,并且有效

#include <windows.h> 
#include <iostream>
#include <TlHelp32.h> 
#include <string> 
#include <sstream> 

using namespace std;
int main() {
    int a;
    cin >> a;
    HANDLE snapshot = CreateToolhelp32Snapshot(0x00000008, a);
    MODULEENTRY32 mod;
    mod.dwSize = sizeof(mod);
    if (!Module32First(snapshot, &mod))
        return 0;
    do { cout << mod.szModule << endl; } while (Module32Next(snapshot, &mod));
    system("pause");
    return 0;
}

enter image description here

所以我的问题是:第二个和第三个变体之间有什么区别,我该如何使其在C#中工作?我还尝试过使用user32.dll中的OpenProcess()和kernel32.dll中的GetModuleHandle(),但这没有用。

1 个答案:

答案 0 :(得分:0)

您必须将代码编译为x64并以管理员身份运行才能获取所有模块。

或者将其用作快照类型:

TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32

这是C#中CreateToolHelp32Snapshot()模块循环的样子:

const Int64 INVALID_HANDLE_VALUE = -1;
[Flags]

private enum SnapshotFlags : uint
{
    HeapList = 0x00000001,
    Process = 0x00000002,
    Thread = 0x00000004,
    Module = 0x00000008,
    Module32 = 0x00000010,
    Inherit = 0x80000000,
    All = 0x0000001F,
    NoHeaps = 0x40000000
}

[StructLayout(LayoutKind.Sequential, CharSet = System.Runtime.InteropServices.CharSet.Ansi)]
public struct MODULEENTRY32
{
    internal uint dwSize;
    internal uint th32ModuleID;
    internal uint th32ProcessID;
    internal uint GlblcntUsage;
    internal uint ProccntUsage;
    internal IntPtr modBaseAddr;
    internal uint modBaseSize;
    internal IntPtr hModule;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
    internal string szModule;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
    internal string szExePath;
}

[DllImport("kernel32.dll")]
static extern bool Module32First(IntPtr hSnapshot, ref MODULEENTRY32 lpme);

[DllImport("kernel32.dll")]
static extern bool Module32Next(IntPtr hSnapshot, ref MODULEENTRY32 lpme);

[DllImport("kernel32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool CloseHandle([In] IntPtr hObject);

[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr CreateToolhelp32Snapshot(SnapshotFlags dwFlags, IntPtr th32ProcessID);

public static IntPtr GetModuleBaseAddress(IntPtr procId, string modName)
{
    IntPtr modBaseAddr = IntPtr.Zero;
    IntPtr hSnap = CreateToolhelp32Snapshot(SnapshotFlags.Module | SnapshotFlags.Module32, procId);

    if (hSnap.ToInt64() != INVALID_HANDLE_VALUE)
    {
        MODULEENTRY32 modEntry = new MODULEENTRY32();
        modEntry.dwSize = (uint)Marshal.SizeOf(typeof(MODULEENTRY32));

        if (Module32First(hSnap, ref modEntry))
        {
            do
            {
                if (modEntry.szModule.Equals(modName))
                {
                    modBaseAddr = modEntry.modBaseAddr;
                    break;
                }
            } while (Module32Next(hSnap, ref modEntry));
        }
    }
    CloseHandle(hSnap);

    return modBaseAddr;
}