“回调"是什么意思在 Windows API 函数声明中是什么意思? [英] What does "CALLBACK" in a Windows API function declaration mean?

查看:33
本文介绍了“回调"是什么意思在 Windows API 函数声明中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
C 中的CALLBACK"声明有什么作用?

WindowProc() 函数的原型是这个

The prototype of a WindowProc() function is this

LRESULT CALLBACK WindowProc(HWND hWnd,UINT message , WPARAM wParan, LPARAM lParam);

但我从来没有遇到过 CALLBACK 关键字,我只知道回调函数是你传递一个指向另一个函数的指针,那么这里的 CALLBACK 到底是什么?它有什么作用?

But I've never come across the CALLBACK keyword, I only know that a callback function is one you pass a pointer to another function to, so what exactly is this CALLBACK over here? and what does it do?

推荐答案

#defined 为 __stdcall,因为这是 Win32 API 对回调函数的假设.这是一个调用约定——它描述了函数调用在底层的排列方式——参数如何在 CPU 堆栈上排列,等等.关于预期堆栈布局(即调用约定)的假设必须在调用者和被调用者之间匹配,否则可能会出现各种有趣的后果.

It's #defined as __stdcall, because that's what the Win32 API assumes about callback functions. It's a calling convention - it describes the way a function call is arranged on the low level - how are parameters arranged on the CPU stack, and suchlike. The assumptions about the expected stack layout (i. e. the calling convention) must match between the caller and the callee, otherwise all kinds of fun consequences might ensue.

过去,在 Intel CPU 上有多种调用约定;如果算上非 Microsoft 编译器,则更多.如果您计算 C[++] 以外的能够调用和被 Windows API 调用的语言,则更是如此.确保您的 Windows API 回调显式标记为 __stdcall 以匹配 WinAPI 期望是一个很好的做法.在某些情况下,根据编译器和设置,__stdcall 是默认调用约定(即您可以安全地省略 CALLBACK),但并非总是如此.

Historically, on the Intel CPUs there were multiple calling conventions; more if you count non-Microsoft compilers. Even more if you count languages other than C[++] that are capable of calling and getting called by Windows API. Making sure your Windows API callbacks are explicitly marked as __stdcall to match the WinAPI expectation is a good practice. In some cases, depending on compiler and settings, __stdcall is the default calling convention (i. e. you can safely omit CALLBACK), but not always.

在 Win16 中,CALLBACK 被定义为 far pascal.这更不可能成为用户函数的默认值,尤其是在 C 程序中.假设回调具有 Pascal 调用约定这一事实是一个历史产物.它也起到了微优化的作用.

Back in Win16, CALLBACK was defined as far pascal. That's even less likely to be the default for user functions, especially in a C program. The fact that callbacks were assumed to have a Pascal calling convention was an historical artifact. It also worked as a microoptimization.

这篇关于“回调"是什么意思在 Windows API 函数声明中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆