The process explicitly linked to the DLL calls getprocaddress to obtain
DLL export function address. Use the returned function pointer to call the DLL function. Getprocaddress will (returned by loadlibrary, afxloadlibrary, or getmodulehandle) DLL
The module handle and the function name to be called or the export sequence number of the function are used as parameters.
Because the DLL function is called through a pointer and there is no type check during compilation, you need to ensure that the function parameters are correct so that they do not exceed the memory allocated on the stack and will not cause access conflicts. One way to help provide type security is to view the function prototype of the exported function and create a function pointer match.
Typedef. For example:
typedef UINT (CALLBACK* LPFNDLLFUNC1)(DWORD,UINT);...HINSTANCE hDLL; // Handle to DLLLPFNDLLFUNC1 lpfnDllFunc1; // Function pointerDWORD dwParam1;UINT uParam2, uReturnVal;hDLL = LoadLibrary("MyDLL");if (hDLL != NULL){ lpfnDllFunc1 = (LPFNDLLFUNC1)GetProcAddress(hDLL, "DLLFunc1"); if (!lpfnDllFunc1) { // handle the error FreeLibrary(hDLL); return SOME_ERROR_CODE; } else { // call the function uReturnVal = lpfnDllFunc1(dwParam1, uParam2); }}
When getprocaddress is called, the method for specifying the required function depends on
DLL generation method.
Only when the DLL to be linked is defined by the module (. def) file generated, and the serial number is in the DLL. when the exports part of the def file is listed together with the function, the export sequence number can be obtained. If
DLL has many export functions. Compared with the function name, calling getprocaddress with the export sequence number is faster, because the export sequence number is the index of the DLL export table. Using the export sequence number, getprocaddress can directly locate the function, instead
Compare the function names in the DLL export table. However, getprocaddress should be called only when you have the right to control the sequence number distribution of the export function in the. Def file.