获取句柄的方法总结(尤其是对于dll而言)

/************************************************************
*测试获取文件所在的模块的方法,其中GetModuleHandle是获取exe的
*句柄,而后两者还可以用来获取dll模块的句柄
*By IT05
*2012-8-6 16:24:27
*************************************************************/
#include "stdafx.h"
#include "windows.h"
#include "_vcclrit.h"//定义了__ImageBase

int _tmain(int argc, _TCHAR* argv[], TCHAR* env[])
{
	int i;

	HMODULE hModule = ::GetModuleHandle(NULL);

	_tprintf( TEXT("GetModuleHandle(NULL):0x%x\r\n"), hModule );

	_tprintf( TEXT("__ImageBase=0x%x\r\n"), (HINSTANCE)&__ImageBase );

	//_tmain处是说明本函数所在模块
	//当是dll文件时,那么写dll模块内德函数即可得到dll的句柄
	::GetModuleHandleEx( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (PCTSTR)_tmain, &hModule );

	_tprintf( TEXT("GetModuleHandleEx:0x%x\r\n"), hModule );
	
	getchar();
	return 0;
}

原文地址:https://www.cnblogs.com/arbboter/p/4225251.html