[英]Why calling SetConsoleCtrlHandler() triggers a warning?
#include <windows.h>
BOOL MyCtrlHandler(DWORD ctrlType) {
return TRUE;
}
int main(void) {
SetConsoleCtrlHandler(MyCtrlHandler, TRUE);
return 0;
}
The function signature matches thedoc : function 签名与文档匹配:
The PHANDLER_ROUTINE type defines a pointer to this callback function. PHANDLER_ROUTINE 类型定义了指向此回调 function 的指针。 HandlerRoutine is a placeholder for the application-defined function name. HandlerRoutine 是应用程序定义的 function 名称的占位符。
If I cross-compile it with MinGW in 64 bits, it works:如果我用 64 位 MinGW 交叉编译它,它可以工作:
$ x86_64-w64-mingw32-gcc ctrl.c
$
But in 32 bits, I get a warning:但在 32 位中,我收到警告:
$ i686-w64-mingw32-gcc ctrl.c
ctrl.c: In function ‘main’:
ctrl.c:8:27: warning: passing argument 1 of ‘SetConsoleCtrlHandler’ from incompatible pointer type [-Wincompatible-pointer-types]
8 | SetConsoleCtrlHandler(MyCtrlHandler, TRUE);
| ^~~~~~~~~~~~~
| |
| BOOL (*)(DWORD) {aka int (*)(long unsigned int)}
In file included from /usr/share/mingw-w64/include/windows.h:74,
from ctrl.c:1:
/usr/share/mingw-w64/include/wincon.h:249:68: note: expected ‘PHANDLER_ROUTINE’ but argument is of type ‘BOOL (*)(DWORD)’ {aka ‘int (*)(long unsigned int)’}
249 | WINBASEAPI WINBOOL WINAPI SetConsoleCtrlHandler(PHANDLER_ROUTINE HandlerRoutine,WINBOOL Add);
|
$
I don't understand why, because MyCtrlHandler
should have the same type as PHANDLER_ROUTINE
.我不明白为什么,因为MyCtrlHandler
应该与PHANDLER_ROUTINE
具有相同的类型。
The problem here is the difference between __cdecl and __stdcall.这里的问题是 __cdecl 和 __stdcall 之间的区别。 You must have lucked out with the default calling convention on your one successful attempt to compile.在一次成功的编译尝试中,您一定很幸运地使用了默认调用约定。 You need:你需要:
BOOL WINAPI MyCtrlHandler(DWORD ctrlType) {
return TRUE;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.