GetProcAddress function

Expand
22 out of 100 rated this helpful - Rate this topic

GetProcAddress function

Applies to: desktop apps | Metro style apps

Retrieves the address of an exported function or variable from the specified dynamic-link library (DLL).

Syntax

FARPROC WINAPI GetProcAddress(
  __in  HMODULE hModule,
  __in  LPCSTR lpProcName
);

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.

The GetProcAddress function does not retrieve addresses from modules that were loaded using the LOAD_LIBRARY_AS_DATAFILE flag. For more information, see 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.

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, call GetLastError.

Remarks

The spelling and case of a function name pointed to by lpProcName must be identical to that in the EXPORTS 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, see Conventions for Function Prototypes.

The lpProcName parameter can identify the DLL function by specifying an ordinal value associated with the function in the EXPORTS 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.

If the .def file does not number the functions consecutively from 1 to N (where N is the number of exported functions), an error can occur where GetProcAddress returns an invalid, non-NULL address, even though there is no function with the specified ordinal.

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.



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.

Examples

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

Requirements

Minimum supported client

Windows XP

Minimum supported server

Windows Server 2003

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

 

 

Send comments about this topic to Microsoft

Build date: 5/5/2012

Did you find this helpful?
(1500 characters remaining)
Community Additions ADD
Make sure to use extern "C"!
For your function to be able to be loaded by GetProcAddress you need to make sure you use extern "C" when DLL exporting the function. $0$0 $0 $0Eg. $0 $0extern "C" __declspec(dllexport) void foo() {}$0 $0 $0
8/2/2011
Casting from int to resource string
Use the WinUser.h defined macro MAKEINTRESOURCE to pass an ordinal value.  e.g. GetProcAddress(hInst, MAKEINTRESOURCE(2));
5/27/2011
C++ Class

A sample C++ class for dynamically loading and operating dll files can be found here:
Header:
http://www.asyncop.com/MTnPDirEnum.aspx?treeviewPath=%5bo%5d+Open-Source%5cWinModules%5cInfrastructure%5cSystemAPI.h
Source:
http://www.asyncop.com/MTnPDirEnum.aspx?treeviewPath=%5bo%5d+Open-Source%5cWinModules%5cInfrastructure%5cSystemAPI.cpp
This includes loading the library, calling functions and reading resources

12/30/2009
GetProcAddress returns null under x86
Hi,
I have C# library Foo. The Foo calls method Test from C++ library Bar with help of GetProcAddress.
Under x86 it works fine.
When C++ library Bar compiled as x64 and Foo works under x64 then GetProcAddress returns null.
I checked with help of Depends.exe and found method Test in the both versions of Bar (x86 and x64).

LoadLibrary finds Bar C++ library under x86 and x64 (at least results of LoadLibrary !=0).

How to solve the problem?
11/25/2009
vb.net syntax
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True)> _
Public Shared Function GetProcAddress(<[In]> ByVal hModule As IntPtr, <[In], MarshalAs(UnmanagedType.LPStr)> ByVal lpProcName As String) As IntPtr
End Function
5/6/2009
C# syntax
[DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
internal static extern IntPtr GetProcAddress([In] IntPtr hModule, [In, MarshalAs(UnmanagedType.LPStr)] string lpProcName);
5/6/2009
GetSymbol'sAddress?

Unlike the function's name suggests and like the summary does - the function:

Retrieves the address of an exported function or variable from the specified dynamic-link library (DLL). This means, that not only function, but also variables can be found this way, which is often overlooked - even in most of Microsoft's own documentation, like here: http://msdn2.microsoft.com/en-us/library/64tkc9y5(VS.71).aspx .

2/22/2007