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

  • 相关阅读:
    js03 案例驱动表单的验证
    js02 案例驱动1 定时弹出广告 Brower对象
    js基础01
    ScalarHandler对象获取 数据库中的数据是注意转换
    java中写模糊查询2
    mvc与三层结构终极区别
    JSP和El表达式和JSTL标签库使用
    SQL注入学习笔记——盲注
    SQL注入学习笔记——联合语句查询
    Linux PHP版本7下布置sqli-labs
  • 原文地址:https://www.cnblogs.com/adylee/p/9935059.html
Copyright © 2011-2022 走看看