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的基地址。