关于C#:GetModuleHandle()无法检索由” notepad.exe”加载的” advapi32.dll”的句柄

GetModuleHandle() cannot retrieve handle of “advapi32.dll” loaded by “notepad.exe”

本问题已经有最佳答案,请猛点这里访问。

我正在尝试获取notepad.exe处理的文件信息。

因此,我的程序执行以下步骤。

  • 为notepad.exe创建过程

    CreateProcess(NULL, szCmdLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);

  • 等待直到完成notepad.exe的初始化

    WaitForInputIdle(pi.hProcess, 10000);

  • 以Debugee身份将notepad.exe进程附加到我的程序中。

    DebugActiveProcess(dwPID)

  • 等待Debugee的调试事件。

  • 当程序接收到CREATE_PROCESS_DEBUG_EVENT时,请执行我需要的操作。

  • 这是我的功能出现问题。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    LPVOID g_pfHookingAdd = NULL;
    BOOL OnCreateProcessDebugEvent(LPDEBUG_EVENT pde)
    {
        DWORD dwLastErr;
        if (NULL == GetModuleHandleA("advapi32.dll"))   // Not able to get a handle here.
        {
            dwLastErr = GetLastError(); // dwLastErr => 126 => (0x7E)
        }
        g_pfHookingAdd = GetProcAddress(GetModuleHandleA("advapi32.dll"),"IsTextUnicode");
        return TRUE;
    }

    如您所见,我的目标是检索加载IsTextUnicode()函数的地址。

    但是,当我调用GetModuleHandleA("advapi32.dll")时,出现错误代码126,这是
    ERROR_MOD_NOT_FOUND

    我还检查了notepad.exe执行期间是否已加载advapi32.dll

    谁能告诉我为什么这不起作用?

    这是我的环境条件:

    Windows 10专业版1803(操作系统内部版本17134.165)


    那是行不通的,因为GetModuleHandle()...

    Retrieves a module handle for the specified module. The module must
    have been loaded by the calling process.

    获取GetModuleHandle()的答案,因为在另一个进程中使用DLL可能会对您有所帮助。