进程的实例句柄

WinMain函数中的句柄值实际上是个基本内存地址,它指出系统在何处把EXE文件装载入进程的地址空间。例如,如果系统打开了可执行文件,在地址0X400000处装载了它的内容,那么进程实例句柄的值就是0X400000。这个基本地址是连接器决定的,当然,我们也可以自己设定,不同版本的连接器使用不同的缺省值,对于VC,可以使用/BASE:address开关来改变该值

每个加载进进程地址空间的EXE或DLL文件都被赋予了一个唯一的实例句柄,在调用装入资源的时候需要该句柄值。WIN32 API并不区分一个进程的HMODULE,HINSTANCE值---它们是同一个。

我们可以通过下面这个函数来获取调用进程中某个模块的基地址。

HMODULE GetModuleHandle( LPCTSTR lpModuleName);

Parameters
lpModuleName
[in] Pointer to a null-terminated string that contains the name of the module, which must be a DLL file.

If the file name extension is omitted, the default library extension .dll is appended.

The file name string can include a trailing point character (.) to indicate that the module name has no extension.

If this parameter is NULL, GetModuleHandle returns a handle to the file used to create the calling process. All paths are ignored; only the file name and extension are used.

The file extensions .dll and .cpl are treated as identical when comparing module names.

Return Values

A handle to the specified module indicates success.

NULL indicates failure.

To get extended error information, call GetLastError.

该函数有2个需要注意的地方,首先,它只是查看进程的地址空间,如果要查询的模块不在进程的地址空间(模块根本未加载或者加载在其它进程的地址空间),返回的都是NULL,其次,如果参数传递为NULL时,返回的是EXE的基本地址,因此在进程的任何一个模块中调用该函数,返回的都是EXE的基地址。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值