Retrieves the address of an exported function or variable from the specified dynamic-link library (DLL).从指定的DLL中获得导出函数的地址或者变量。
FARPROC WINAPI GetProcAddress( _In_ HMODULE hModule,//DLL的句柄 _In_ LPCSTR lpProcName//只能是ANSI );
A handle to the DLL module that contains the function or variable. The LoadLibrary, LoadLibraryEx, LoadPackagedLibrary, or GetModuleHandle function returns this handle.包含函数或者变量的DLL句柄。使用LoadLibrary、LoadLibraryEx、LoadPackagedLibrary和GetModuleHandle都可以获得这个句柄。
The GetProcAddress function does not retrieve addresses from modules that were loaded using theLOAD_LIBRARY_AS_DATAFILE flag. For more information, seeLoadLibraryEx.如果句柄是通过LOAD_LIBRARY_AS_DATAFILE加载的,那么GetProcAddress不会从模块中获得地址。更多信息参见LoadLibraryEx。
The function or variable name, or the function's ordinal value. If this parameter is an ordinal value, it must be in the low-order word; the high-order word must be zero.函数或者变量的名字,或者函数的序号。如果这个参数是一个序号,那么必须是在一个字的低字段上,这个字的高字段必须为0.
If the function succeeds, the return value is the address of the exported function or variable.成功返回导出函数或者导出变量的地址。
If the function fails, the return value is NULL. To get extended error information, callGetLastError.失败返回NULL。详细信息需调用GetLastError。
The spelling and case of a function name pointed to by lpProcName must be identical to that in theEXPORTS statement of the source DLL's module-definition (.def) file. The exported names of functions may differ from the names you use when calling these functions in your code. This difference is hidden by macros used in the SDK header files. For more information, seeConventions for Function Prototypes.由lpProcName表示的导出函数名的拼写以及大小写,必须在DLL的导出表中唯一定义。
The lpProcName parameter can identify the DLL function by specifying an ordinal value associated with the function in theEXPORTS statement.GetProcAddress verifies that the specified ordinal is in the range 1 through the highest ordinal value exported in the .def file. The function then uses the ordinal as an index to read the function's address from a function table.lpProcName参数也可以使用在导出表中关联的序号来标识DLL函数。GetProcAddress检验指定的序号是否在.def文件的从1到最大序号里。GetProcAddress函数随后使用这个序号作为索引读取函数表中的函数地址。
If the .def file does not number the functions consecutively from 1 to N (whereN is the number of exported functions), an error can occur whereGetProcAddress returns an invalid, non-NULL address, even though there is no function with the specified ordinal.如果.def文件中没有这个序号,即使序号没有对应一个函数,使用GetProcAddress返回的无效的非NULL地址可能会引发错误。
If the function might not exist in the DLL module—for example, if the function is available only on Windows Vista but the application might be running on Windows XP—specify the function by name rather than by ordinal value and design your application to handle the case when the function is not available, as shown in the following code fragment.如果DLL中可能不存在这个函数——例如,函数在Vista中可用但在XP中不可用——那么必须指定函数名而不是序号,并且在程序中处理这种情形,示例代码如下:
typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO); // Call GetNativeSystemInfo if supported or GetSystemInfo otherwise. PGNSI pGNSI; SYSTEM_INFO si; ZeroMemory(&si, sizeof(SYSTEM_INFO)); pGNSI = (PGNSI) GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetNativeSystemInfo"); if(NULL != pGNSI) { pGNSI(&si);//有就调用 } else { GetSystemInfo(&si);//没有就用个备选方案 }
For the complete example that contains this code fragment, see Getting the System Version.
Windows Phone 8: This API is supported.
Windows Phone 8.1: This API is supported.
For an example, see Using Run-Time Dynamic Linking.
Minimum supported client |
Windows XP [desktop apps | Windows Store apps] |
---|---|
Minimum supported server |
Windows Server 2003 [desktop apps | Windows Store apps] |
Header |
|
Library |
|
DLL |
|