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

  • 相关阅读:
    hdu 2842 Chinese Rings
    Codeforces Round #118 (Div. 1) A 矩阵快速幂
    hdu2604 Queuing
    支付宝 生活号 获取 userId 和 生活号支付
    maven 项目使用本地jar
    nexus 私有 maven 仓库的搭建
    linux jdk 安装
    gitlab 可以上传代码,但是 不能 上传 tag 问题
    maven 内置变量
    mysql 不允许分组的问题 this is incompatible with sql_mode=only_full_group_by
  • 原文地址:https://www.cnblogs.com/adylee/p/9935059.html
Copyright © 2011-2022 走看看