[Windows API learning] getprocaddress Learning

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.