楼上的别误导人了。 MSDN 说法: Handle to the module whose path is being requested. If this parameter is NULL, GetModuleFileName retrieves the path of the executable file of the current process. GetModuleFileName(NULL, szFilePath, MAX_PATH); 返回的是可执行程序的路径,一般来说是Exe的路径。
TCHAR szFilePath[MAX_PATH];
GetModuleFileName(NULL, szFilePath, MAX_PATH);有了路径就可以用GetModuleHandle获得HModule
HMODULE hszFilePath = GetModuleHandle(szFilePath);
GetModuleFileName(NULL, szFilePath, MAX_PATH);
这个得到的是调用他的exe的路径。不是dll的路径。
MSDN 说法:
Handle to the module whose path is being requested. If this parameter is NULL, GetModuleFileName retrieves the path of the executable file of the current process. GetModuleFileName(NULL, szFilePath, MAX_PATH); 返回的是可执行程序的路径,一般来说是Exe的路径。
HMODULE GetYourModule()
{ static HMODULE hModule = NULL;
if ( hModule == NULL )
{
BOOL b = GetModuleHandleEx(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(LPCTSTR)(&hModule),
&hModule
);
}return hModule ;
}
也可以用VirtualQuery(pAddress,lpMBI,dwLength);
其中pAddress是DLL内的任一地址(比如DllMain地址),然后lpMBI->AllocationBase里面就是此Dll的HMODULE了。(在winCE里面此值不是模块句柄。)
方便,又简单.