[Reprint] Considerations for using API function GetProcAddress

Source: Internet
Author: User

Windows API function GetProcAddress is widely used to obtain function pointer addresses. For example:

typedef BOOL (WINAPI *pfngetproductinfo) (DWORD, DWORD, DWORD, DWORD, Pdword);

PGPI = (pfngetproductinfo) GetProcAddress (Getmodulehandlew (L "kernel32.dll"), "Getproductinfo");

Note that the function defines the WINAPI in the TypeDef, which specifies the calling protocol of the function, which is necessary and critical, and should be noted when writing the code.

For example, if you want to get the strcpy function in the standard C + + runtime (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 how function parameters are passed and how the stack is managed. If you do not specify a calling protocol here , the compiler will use the default calling protocol. The result may be different from the original protocol specified by the DLL function, which directly causes the program to crash when it is called .

The main methods of function call are _cdecl, _stdcall, _fastcall, _thiscall .

_cdecl is the default function call protocol for the C language: All parameters are entered in the stack from right to left, and the parameters in the stack are purged by the caller.

_stdcall is the default function call protocol of the Pascal language, and all parameters are entered into the stack from right to left, and the parameters in the stack are cleared by the called function after it is returned. The Windows API is all _stdcall, and the WINAPI in the example above is actually ___stdcall

#define WINAPI __stdcall
__fastcall is the first two (x86 machine) or 4 (x64 machine) parameters passed by the register, and the remaining parameters are passed through the stack. The parameters in the stack are cleared by the called function after it is returned. Borland Delphi, C + + Builder uses this method of invocation by default.

_thiscall and _stdcall are similar, just _thiscall put the this pointer of the class in a particular register, such as Visual C + + in ECX, Borland C + + in EAX.

By comparison, several invocation protocols are quite different, so the functions GetProcAddress get need to specify the correct calling protocol

[Reprint] Considerations for using API function GetProcAddress

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.