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

  • 相关阅读:
    3. applicationCache
    9. 水平垂直居中的方法
    2. 贝塞尔曲线bezierCurveTo
    相识python 之小数据池 集合
    相识python --------- 列表 元祖 range 范围
    相识python while循环 代码块 编码初识 运算符
    相识python第二步:变量 注释 str int bool 用户交换 流程控制语句的解释用法
    python学习基础知识
    python的基础知识
    我对python的见解
  • 原文地址:https://www.cnblogs.com/adylee/p/9935059.html
Copyright © 2011-2022 走看看