如何获得我自己的代码的模块句柄? [英] How to get my own code's module handle?

查看:20
本文介绍了如何获得我自己的代码的模块句柄?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
如何获取当前正在执行的 HMODULE代码?

我正在尝试在我自己的模块中查找资源.如果这个模块是一个可执行文件,那就很简单了 - GetModuleHandle(NULL) 返回主"模块的句柄.

I'm trying to find a resource in my own module. If this module is an executable, that's trivial - GetModuleHandle(NULL) returns the handle of the "main" module.

然而,我的模块是一个由另一个可执行文件加载的 DLL.所以 GetModuleHandle(NULL) 会将模块句柄返回给那个可执行文件,这显然不是我想要的.

My module, however, is a DLL that is loaded by another executable. So GetModuleHandle(NULL) will return the module handle to that executable, which is obviously not what I want.

有没有办法确定包含当前运行代码的模块的模块句柄?在对 GetModuleHandle() 的调用中使用 DLL 的名称对我来说似乎是一种黑客攻击(如果将有问题的代码移植到不同的 DLL 中,则不容易维护).

Is there any way to determine the module handle of the module that contains the currently running code? Using the DLL's name in a call to GetModuleHandle() seems like a hack to me (and is not easily maintainable in case the code in question is transplanted into a different DLL).

推荐答案

将模块句柄保存在 DllMain 中,然后在您实际需要时使用它.许多框架(例如 MFC)会自动执行此操作.

Store the module handle away when it is given to you in DllMain and then use it later when you actually need it. A lot of frameworks (e.g., MFC) do this automatically.

这篇关于如何获得我自己的代码的模块句柄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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