本文介紹了如何獲取當(dāng)前正在執(zhí)行的代碼的 HMODULE?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我有一個靜態(tài)庫,可以鏈接到 .exe
或 .dll
.在運行時,我希望我的庫函數(shù)之一為靜態(tài)庫代碼已鏈接到的任何內(nèi)容獲取 HMODULE
.
I have a static library that may get linked into either a .exe
or a .dll
. At runtime I want one of my library functions to get the HMODULE
for whatever thing the static library code has been linked into.
我目前使用以下技巧(靈感來自這個論壇)::>
I currently use the following trick (inspired from this forum):
const HMODULE GetCurrentModule()
{
MEMORY_BASIC_INFORMATION mbi = {0};
::VirtualQuery( GetCurrentModule, &mbi, sizeof(mbi) );
return reinterpret_cast<HMODULE>(mbi.AllocationBase);
}
有沒有更好的方法來做到這一點,而且看起來不那么笨拙?
Is there a better way to do this that doesn't look so hacky?
(注意:這樣做的目的是加載一些我知道我的用戶將在與我的靜態(tài)庫同時鏈接的 Win32 資源.)
推薦答案
HMODULE GetCurrentModule()
{ // NB: XP+ solution!
HMODULE hModule = NULL;
GetModuleHandleEx(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
(LPCTSTR)GetCurrentModule,
&hModule);
return hModule;
}
這篇關(guān)于如何獲取當(dāng)前正在執(zhí)行的代碼的 HMODULE?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!