天天看點

GetProcAddressGetProcAddress function

GetProcAddress function

Retrieves the address of an exported function or variable from the specified dynamic-link library (DLL).從指定的DLL中獲得導出函數的位址或者變量。

Syntax

C++

FARPROC WINAPI GetProcAddress(
  _In_  HMODULE hModule,//DLL的句柄
  _In_  LPCSTR lpProcName//隻能是ANSI
);

      

Parameters

hModule [in]
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。
lpProcName [in]
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.

Return value

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。

Remarks

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中不可用——那麼必須指定函數名而不是序号,并且在程式中處理這種情形,示例代碼如下:

C++

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.

Examples

For an example, see Using Run-Time Dynamic Linking.

Requirements

Minimum supported client Windows XP [desktop apps | Windows Store apps]
Minimum supported server Windows Server 2003 [desktop apps | Windows Store apps]
Header
Winbase.h (include Windows.h)
Library
Kernel32.lib
DLL
Kernel32.dll

See also

Dynamic-Link Library Functions FreeLibrary GetModuleHandle LoadLibrary LoadLibraryEx LoadPackagedLibrary Run-Time Dynamic Linking