ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

在动态库里获取动态库的模块句柄

2019-02-20 11:53:53  阅读:363  来源: 互联网

标签:GetCurrentModule name HMODULE 句柄 ATL DLL 7.0 库里 动态


/****************************************************************************
使用下面的HMODULE GetCurrentModule()可以获取dll自己的句柄。
接着使用
TCHAR lib_name[MAX_PATH]; 
::GetModuleFileName( GetCurrentModule(), lib_name, MAX_PATH );
就可以获取dll的路径了
 
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
 
static
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:
    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
}

  

标签:GetCurrentModule,name,HMODULE,句柄,ATL,DLL,7.0,库里,动态
来源: https://www.cnblogs.com/manongdashu/p/10405621.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有