在动态库里获取动态库的模块句柄
程序员文章站
2022-04-09 18:55:09
/**************************************************************************** 使用下面的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
}
推荐阅读