zoukankan      html  css  js  c++  java
  • Windows API 之 GetModuleHandle

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

    HMODULE WINAPI GetModuleHandle(
      _In_opt_ LPCTSTR lpModuleName
    );

    Parameters

    lpModuleName [in, optional]

    The name of the loaded module (either a .dll or .exe file). If the file name extension is omitted, the default library extension .dll is appended. The file name string can include a trailing point character (.) to indicate that the module name has no extension. The string does not have to specify a path. When specifying a path, be sure to use backslashes (), not forward slashes (/). The name is compared (case independently) to the names of modules currently mapped into the address space of the calling process.

    If this parameter is NULL, GetModuleHandle returns a handle to the file used to create the calling process (.exe file).

    Return value

    If the function succeeds, the return value is a handle to the specified module.

    If the function fails, the return value is NULL. To get extended error information, call GetLastError.

    Remarks

    The returned handle is not global or inheritable. It cannot be duplicated or used by another process.

    The GetModuleHandle function returns a handle to a mapped module without incrementing its reference count. However, if this handle is passed to the FreeLibrary function, the reference count of the mapped module will be decremented. Therefore, do not pass a handle returned by GetModuleHandle to the FreeLibrary function. Doing so can cause a DLL module to be unmapped prematurely.

    例如:

    GetModuleHandle(NULL); //  这将返回自身应用程序句柄

    GetModuleHandle("kernel32");//这将返回kernel32.dll的句柄

    模块句柄实际上就是模块在当前进程空间的装入地址。即,进程地址空间中可执行文件的基址。例如:

    #include <windows.h>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        HMODULE hModule = GetModuleHandle(NULL);
        PIMAGE_DOS_HEADER pDosHeader = (PIMAGE_DOS_HEADER)hModule;
        cout << (char*)pDosHeader << endl;
        system("pause");
        return 0;
    }

    输出结果:

    可以看出hModule实际上就是装入内存的PE结构的首地址(指向字符“MZ”)。

    参考:

    https://msdn.microsoft.com/en-us/library/windows/desktop/ms683199%28v=vs.85%29.aspx

    http://blog.csdn.net/guzhou_diaoke/article/details/8826558

  • 相关阅读:
    Python基础知识篇
    Django框架
    Django REST Framework框架
    NoSQL
    MySQL恩恩怨怨
    Python奇技淫巧
    一文搞定Flask
    数据结构与算法(Python)
    学习数据分析
    项目杂项
  • 原文地址:https://www.cnblogs.com/predator-wang/p/4959277.html
Copyright © 2011-2022 走看看