Windows 10 中的 GetModuleInformation 链接失败

GetModuleInformation Fails on Linkage in Windows 10

我正在尝试创建一个 DLL 注入器,它将直接在目标进程的 DLL 中执行函数。我试图获取我注入的 DLL 的入口点,以便我可以获得函数的偏移量。我阅读了 Microsoft 文档以使用 GetModuleInfo()。我使用了 header psapi.h 并发现编译有效,但链接失败并提供以下错误:

g++ dll_injector.cpp -o dll_injector.exe

C:\Users\DELKAR~1\AppData\Local\Temp\ccOeKdhW.o:dll_injector.cpp:(.text+0x187): undefined reference to `GetModuleInformation@16'
C:\Users\DELKAR~1\AppData\Local\Temp\ccOeKdhW.o:dll_injector.cpp:(.text+0x1ae): undefined reference to `GetModuleInformation@16'
collect2.exe: error: ld returned 1 exit status

我已经尝试将 Psapi.libdll_injector.cpp 放在同一目录中,然后使用

进行编译

g++ dll_injector.cpp -o dll_injector.exe -Xlinker -L Psapi.lib

但它仍然抛出相同的错误。

这里是 dll_injector.cpp 代码:

#include <windows.h>
#include <iostream>
#include <psapi.h>
using namespace std;

int main() {
    DWORD pid;
    const char* dll_path = "C:\Users\Delkarix\Desktop\target_dll.dll";
    HWND hwnd = FindWindow(NULL, "C:\Users\Delkarix\Desktop\target_process.exe");
    GetWindowThreadProcessId(hwnd, &pid);
    HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);

    LPVOID addr = VirtualAllocEx(hProcess, NULL, strlen(dll_path) + 1, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
    WriteProcessMemory(hProcess, addr, dll_path, strlen(dll_path) + 1, NULL);
    CreateRemoteThread(hProcess, NULL, 0, LPTHREAD_START_ROUTINE(GetProcAddress(LoadLibrary("kernel32"), "LoadLibraryA")), addr, 0, NULL);

    HMODULE lib = LoadLibrary("target_dll.dll");
    FARPROC proc_addr = GetProcAddress(lib, "test_function");

    MODULEINFO info;
    MODULEINFO info_current;
    BOOL success1 = GetModuleInformation(GetCurrentProcess(), lib, &info_current, sizeof(info_current));
    BOOL success2 = GetModuleInformation(hProcess, lib, &info, sizeof(info));
    cout << success1 << endl; // Test if it works
    cout << success2 << endl; // Test if it works
}

这里是 target_dll.cpp 代码:

#include <iostream>
using namespace std;

extern "C" __declspec(dllexport) void test_function() {
    cout << "Hello World" << endl;
}

我希望 dll_injector.exe 打印说明函数是否成功的值。稍后我将更改代码,以便能够获取入口点值。现在,我只希望函数能够成功,而不必创建 Visual Studio C++ 控制台项目。我的问题:为什么联动失败,如何才能联动成功?

改变

g++ dll_injector.cpp -o dll_injector.exe -Xlinker -L Psapi.lib

g++ dll_injector.cpp -o dll_injector.exe -L. -lPsapi

-L(库路径)——指定一个lib目录。 . 指定当前路径。

-l(图书馆) - link 与图书馆。 Psapi指定库名。(省略windows中.lib的后缀名或linux中的lib前缀名和.a后缀名)

文档来自 Compiling with g++