GetModuleHandle

获取已经载入进程空间的模块句柄。

函数声明:

HMODULE WINAPI GetModuleHandle(
  _In_opt_ LPCTSTR lpModuleName
);

参数:

LPCTSTR lpModuleName

需要获取句柄的模块名。(.dll或.exe文件),若无后缀,默认为.dll。不必指定所在模块路径,若需指定,则需用反斜杠\。模块名必须与当前映射到调用进程空间的模板名一致。不会获取使用标志LOAD_LIBRARY_AS_DATAFILE加载的模块句柄。


返回:

若成功,则返回指定模块的句柄值;反之,返回null。使用GetLastError获取错误信息。


头文件:

winbase.h(包含了windows.h)

lib库文件:

kernel32.lib

dll库文件:

kernel32.dll


实例:

#include "stdafx.h"

#include <windows.h>

typedef int (CALLBACK * PMessageBox)(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT UType);
void main()
{
	HMODULE hModule = GetModuleHandle("user32.dll");
	printf("LoadLibrary front: hModule = 0x%x\n", hModule);
	//hModule = NULL;
	if (NULL == hModule)      //若源程序没载入user32.dll则LoadLibrary它
	{
		hModule = LoadLibrary("user32.dll");
	}

	printf("LoadLibrary rear: hModule = 0x%x\n", hModule);

	PMessageBox MESS = (PMessageBox)GetProcAddress(hModule, "MessageBoxA");
	if (MESS)
	{
		(*MESS)(NULL, "MessageBoxText(内容)", "Title(标题)", MB_OK);
	}
}


输出:



参考网址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值