简体   繁体   中英

Why use __stdcall for function pointer

I was looking at some code like the one mentioned here: Function pointers for winapi functions (stdcall/cdecl) , Function pointer and calling convention , etc.

What is the need, benefit for declaring the calling type of a function pointer as __stdcall ?

Why declare the calling type for a function pointer at all?

Embedding a calling convention specifier in a function pointer allows you to use that calling convention when calling functions through that pointer. __stdcall is the calling convention used to call Win32 API functions.

The benefit of specifying it in a function pointer is being able to match a different calling convention according to your code's needs (eg when loading an external library's function via late binding ). Library headers should specify them as a good programming practice.

There's a caveat though: if you embed a wrong calling convention in a function pointer, the compiler might be powerless to help you detect that and you might end up doing bad things at runtime.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM