在DLL中怎样获得所在DLL的名称? 在线ing

housisong 2003-08-25 03:16:51
在DLL中代码怎样动态获得所在DLL的名称?


...全文
173 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
housisong 2003-08-25
  • 打赏
  • 举报
回复
VirtualQuery这个函数居然没有查到
再次谢谢approach
结帖
housisong 2003-08-25
  • 打赏
  • 举报
回复
to: approach()

谢谢,你的方法测试通过;
housisong 2003-08-25
  • 打赏
  • 举报
回复
to: luogucai(萝卜)
我的代码中没有办法获得DllMain()函数中的HINSTANCE hInstance
下面的代码执行也不正确:
AFX_EXTENSION_MODULE ECadObjectsDLL;
new CDynLinkLibrary(ECadObjectsDLL);
char szFile[512];
GetModuleFileName(ECadObjectsDLL.hModule, szFile, 512);
kingzai 2003-08-25
  • 打赏
  • 举报
回复
use GetCommandLine API
approach 2003-08-25
  • 打赏
  • 举报
回复
Most DLL developers have faced the challenge of detecting a HMODULE/HINSTANCE handle within the module you're running in. It may be a difficult task if you wrote the DLL without a DLLMain() function or you are unaware of its name. For example:

Your DLL was built without ATL/MFC, so the DLLMain() function exists, but it's hidden from you code and you cannot access the hinstDLL parameter. You do not know the DLL's real file name because it could be renamed by everyone, so GetModuleHandle() is not for you.

This small code can help you solve this problem:

#if _MSC_VER >= 1300 // for VC 7.0
// from ATL 7.0 sources
#ifndef _delayimp_h
extern "C" IMAGE_DOS_HEADER __ImageBase;
#endif
#endif

HMODULE GetCurrentModule()
{
#if _MSC_VER < 1300 // earlier than .NET compiler (VC 6.0)

// Here's a trick that will get you the handle of the module
// you're running in without any a-priori knowledge:
// http://www.dotnet247.com/247reference/msgs/13/65259.aspx

MEMORY_BASIC_INFORMATION mbi;
static int dummy;
VirtualQuery( &dummy, &mbi, sizeof(mbi) );

return reinterpret_cast<HMODULE>(mbi.AllocationBase);

#else // VC 7.0

// from ATL 7.0 sources

return reinterpret_cast<HMODULE>(&__ImageBase);
#endif
}
luogucai 2003-08-25
  • 打赏
  • 举报
回复
DllMain()函数中有一个HINSTANCE hInstance,这就是这个Dll的Handle,
VC的扩展DLL中使用了static AFX_EXTENSION_MODULE xxxDLL = { NULL, NULL };来记录动态库的Moudle和Resource Handle


new CDynLinkLibrary(ECadObjectsDLL);
char szFile[512];
GetModuleFileName(xxxDll.hModule, szFile, 512);

test7979 2003-08-25
  • 打赏
  • 举报
回复
关注!
housisong 2003-08-25
  • 打赏
  • 举报
回复
//上面写错了一点,更正:GetModuleFileName(GetModuleHandle(NULL))

我想到的一种方法是:
1.在DLL的代码中获得当前被执行的代码所在的地址p
2.得到整个进程的模块分布区间
3.查询地址p属于哪一个模块区间内
4.得到所在区间模块的基地址d
5.GetModuleFileName(d);

太麻烦了!
housisong 2003-08-25
  • 打赏
  • 举报
回复

DLL中调用GetModuleFileName(NULL)得到的是DLL被调用的EXE的名称;
GetModuleFileName(GetModuleFileName(NULL))也是所在EXE的名称;


给出这两个函数的中文参数详细说明也可以得部分分(先有鸡还是先有蛋的问题):
GetModuleHandle
GetModuleFileName

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧