[Windows programming] Precautions for using the API function getprocaddress

Source: Internet
Author: User

Windows API function getprocaddress is widely used to obtain the function pointer address. For example:

 

Typedef bool (winapi * pfngetproductinfo) (DWORD, pdword );

 

Pgpi = (pfngetproductinfo) getprocaddress (getmodulehandlew (L "kernel32.dll"), "getproductinfo ");

 

Note that the function defines the winapi in typedef, which specifies the function call protocol. This part is very necessary and critical. Pay attention to it when writing code.

 

For example, if you want to obtain the strcpy function in the Standard C ++ Runtime Library (msvcrt. dll), the function definition should be:

Typedef char * (_ cdecl * pfnstrrchr) (const char * string, int C );

Psrs = (pfnstrrchr) getprocaddress (hmsvcrt, "strrchr ");

 

The function call Protocol specifies the function parameter transfer method and stack management method. If you do not specify the call protocol, the compiler uses the default call protocol. The result may be different from the Protocol specified by the DLL function. When the function is called, the program will crash.

 

Mainstream function calls include _ cdecl, _ stdcall, _ fastcall, and _ thiscall.

_ Cdecl is the default function call protocol for C language: All parameters are pushed to the stack from right to left, and the parameters in the stack are cleared by the caller.

_ Stdcall is the default function call protocol of Pascal. All parameters are written to the stack from right to left. The parameters in the stack are cleared after the called function is returned. Windows APIs all adopt the _ stdcall method. In the above example, winapi is actually ___ stdcall

# Define winapi _ stdcall
_ Fastcall is the first two (x86 machines) or four (x64 machines) parameters are passed by registers, and other parameters are passed through stacks. The parameters in the stack are cleared after the called function returns. Borland Delphi and C ++ builder use this call method by default.

_ Thiscall is similar to _ stdcall, but _ thiscall puts this pointer of the class in a specific register, such as Visual C ++ in ECx and Borland C ++ in eax.

 

After comparison, several call protocols are quite different. Therefore, the function obtained by getprocaddress must specify the correct call protocol.

 

--

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.