user-0 提问: 关于GetModuleHandle函数用法的疑问？还请大牛解惑！

回复:
user: user-1
content: #ifndef __JEMALLOC___INCLUDED__ #define __JEMALLOC___INCLUDED__   #include <windows.h>  typedef void*    (__cdecl *_LPMALLOC)		(unsigned int size); typedef void*    (__cdecl *_LPCALLOC)		(unsigned int number, unsigned int size); typedef int      (__cdecl *_LPPOSIX_MEMALIGN)( void **ptr, unsigned int alignment, unsigned int size); typedef void*    (__cdecl *_LPALIGNED_ALLOC)(unsigned int alignment, unsigned int size); typedef void*    (__cdecl *_LPREALLOC)		(void *ptr, unsigned int size); typedef void     (__cdecl *_LPFREE)			(void *ptr); typedef int      (__cdecl *_LPSTRDUP)		(char *Source); typedef int      (__cdecl *_LPWCSDUP)		(wchar_t * Source); typedef int      (__cdecl *_LPSTRNDUP)		(char *Source, int); typedef unsigned int    (__cdecl *_LPMALLOC_USABLE_SIZE)(const void *ptr); typedef void*    (__cdecl *_LPFREX)			(void); typedef void*    (__cdecl *_LPMEMALIGN)		(unsigned int boundary, unsigned int size); typedef void*    (__cdecl *_LPVALLOC)		(unsigned int size);  typedef struct { 	_LPMALLOC	malloc; 	_LPCALLOC	calloc; 	_LPPOSIX_MEMALIGN posix_memalign; 	_LPALIGNED_ALLOC	align_alloc; 	_LPREALLOC	realloc; 	_LPFREE		free; 	_LPSTRDUP	strdup; 	_LPWCSDUP	wcsdup; 	_LPSTRNDUP	strndup; 	_LPMALLOC_USABLE_SIZE malloc_usable_size; 	_LPFREX		frex; 	_LPMEMALIGN	memalign; 	_LPVALLOC	valloc; } MOZILLA_JEMALLOC, *LPMOZILLA_JEMALLOC;  HINSTANCE hJeMod;  VOID LoadJemallocLibrary (const char* version_name, LPMOZILLA_JEMALLOC lpmz) { 	BOOL mask = 0; 	{ 		hJeMod = LoadLibraryA(version_name);   		if(!hJeMod) MessageBoxA(nullptr, "LoadJemalloc Failed", "ERROR", MB_OK), exit(-1);	 		 		{ 			lpmz->malloc				= (_LPMALLOC) 				GetProcAddress(hJeMod, "malloc"); 			lpmz->calloc				= (_LPCALLOC) 				GetProcAddress(hJeMod, "calloc"); 			lpmz->posix_memalign		= (_LPPOSIX_MEMALIGN) 		GetProcAddress(hJeMod, "posix_memalign"); 			lpmz->align_alloc			= (_LPALIGNED_ALLOC) 		GetProcAddress(hJeMod, "align_alloc"); 			lpmz->realloc				= (_LPREALLOC) 				GetProcAddress(hJeMod, "realloc"); 			lpmz->free					= (_LPFREE) 				GetProcAddress(hJeMod, "free"); 			lpmz->strdup				= (_LPSTRDUP) 				GetProcAddress(hJeMod, "strdup"); 			lpmz->wcsdup				= (_LPWCSDUP) 				GetProcAddress(hJeMod, "wcsdup"); 			lpmz->strndup				= (_LPSTRNDUP) 				GetProcAddress(hJeMod, "strndup"); 			lpmz->malloc_usable_size	= (_LPMALLOC_USABLE_SIZE) 	GetProcAddress(hJeMod, "malloc_usable_size"); 			lpmz->frex					= (_LPFREX) 				GetProcAddress(hJeMod, "frex"); 			lpmz->memalign				= (_LPMEMALIGN)				GetProcAddress(hJeMod, "memalign"); 			lpmz->valloc				= (_LPVALLOC) 				GetProcAddress(hJeMod, "valloc"); 		} 	} }  BOOL FreeJemallocLibrary () { 	return FreeLibrary(hJeMod); } #endif   这是我写的读取火狐jemalloc dll的代码(不想用lib。。) 希望对lz有所帮助

user: user-2
content: Map到内存 也可以 Windows exe和 dll 加载方式都只这么做的， 注意加载参数选择， 内存映像方式

user: user-3
content: 如果DLL没有加载到内存，怎么会有句柄？如果不想显示加载，那就使用与DLL相关的lib进行隐式加载就是。

user: user-4
content: 不好意思，目前我只知道LoadLibrary，为何不用它呢，挺方便的

user: user-0
content: 引用 1 楼 ant2012 的回复:MSDN The handles returned by GetModuleHandle and LoadLibrary can be used in the same functions — for example, GetProcAddress, FreeLibrary, orLoadResource. The difference between the two functions involves the reference count. LoadLibrary maps the module into the address space of the calling process, if necessary, and increments the module's reference count, if it is already mapped. GetModuleHandle, however, returns the handle to a mapped module without incrementing its reference count.   也就是说如果动态链接库已经加载到地址空间中，就可直接用GetModuleHandle ，否则得通过LoadLibrary 将库映射到进程的地址空间中请问除了LoadLibrary 还有其他的办法吗？

user: user-4
content: MSDN The handles returned by GetModuleHandle and LoadLibrary can be used in the same functions — for example, GetProcAddress, FreeLibrary, orLoadResource. The difference between the two functions involves the reference count. LoadLibrary maps the module into the address space of the calling process, if necessary, and increments the module's reference count, if it is already mapped. GetModuleHandle, however, returns the handle to a mapped module without incrementing its reference count.   也就是说如果动态链接库已经加载到地址空间中，就可直接用GetModuleHandle ，否则得通过LoadLibrary 将库映射到进程的地址空间中