简体   繁体   English

为什么使用__stdcall作为函数指针

[英]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. 我正在看一些像这里提到的代码: winapi函数的函数指针(stdcall / cdecl)函数指针和调用约定等。

What is the need, benefit for declaring the calling type of a function pointer as __stdcall ? 将函数指针的调用类型声明为__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. __stdcall是用于调用Win32 API函数的调用约定。

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. 但有一点需要注意:如果在函数指针中嵌入了错误的调用约定,编译器可能无法帮助您检测到它,并且您最终可能会在运行时执行错误操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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