我如何获得当前执行code中的HMODULE? [英] How do I get the HMODULE for the currently executing code?

查看:120
本文介绍了我如何获得当前执行code中的HMODULE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有可能会链接到是 .exe文件的.dll 静态库。在运行时,我想ONY我的库函数来获得 HMODULE 对力所能及的事静态库code已被链接到。

我目前使用下面的技巧(从这个论坛启发):

 常量HMODULE GetCurrentModule()
{
    MEMORY_BASIC_INFORMATION MBI = {0};
    :: VirtualQuery来(GetCurrentModule,&安培; MBI,siz​​eof的(MBI));    返回reinter pret_cast< HMODULE>(mbi.AllocationBase);
}

有没有更好的方式来做到这一点看起来不那么哈克?

(注:这样做的目的是加载,我知道我的用户会在同一时间作为我的静态库已发现,在某些Win32的资源)


解决方案

  HMODULE GetCurrentModule()
{//注意:XP +解决方案!
  HMODULE HMODULE = NULL;
  GetModuleHandleEx(
    GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
    (LPCTSTR)GetCurrentModule,
    &安培; HMODULE);  返回HMODULE;
}

I have a static library that may get linked into either a .exe or a .dll. At runtime I want ony of my library functions to get the HMODULE for whatever thing the static library code has been linked into.

I currently use the following trick (inspired from this forum):

const HMODULE GetCurrentModule()
{
    MEMORY_BASIC_INFORMATION mbi = {0};
    ::VirtualQuery( GetCurrentModule, &mbi, sizeof(mbi) );

    return reinterpret_cast<HMODULE>(mbi.AllocationBase);
}

Is there a better way to do this that doesn't look so hacky?

(Note: The purpose of this is to load some Win32 resources that I know my users will have linked in at the same time as my static library.)

解决方案

HMODULE GetCurrentModule()
{ // NB: XP+ solution!
  HMODULE hModule = NULL;
  GetModuleHandleEx(
    GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
    (LPCTSTR)GetCurrentModule,
    &hModule);

  return hModule;
}

这篇关于我如何获得当前执行code中的HMODULE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆