c/c++语言开发共享在动态库里获取动态库的模块句柄

/**************************************************************************** 使用下面的HMODULE GetCurrentModule()可以获取dll自己的句柄。 接着使用 TCHAR lib_name[MAX_PAT… …

/**************************************************************************** 使用下面的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 } 

  

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/605138.html

(0)
上一篇 2021年5月13日
下一篇 2021年5月13日

精彩推荐