在动态库里获取动态库的模块句柄
程序员文章站
2022-07-05 16:03:46
/**************************************************************************** 使用下面的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
}
推荐阅读
-
Python在Windows和在Linux下调用动态链接库的教程
-
ASP.NET MVC 在控制器中获取某个视图动态的HTML代码
-
c# json转换成dynamic对象,然后在dynamic对象中动态获取指定字符串列表中的值
-
在动态库里获取动态库的模块句柄
-
ajax动态获取数据库中的数据方法
-
mysql存储过程 在动态SQL内获取返回值的方法详解_MySQL
-
Python在Windows和在Linux下调用动态链接库的教程
-
Yii操作数据库实现动态获取表名的方法_PHP
-
Python在Windows和在Linux下调用动态链接库的教程
-
ASP.NET MVC 在控制器中获取某个视图动态的HTML代码