我无法理解以下编译错误:
unsigned char buf[1000];
const DWORD maxBytes = 1000;
OVERLAPPED o;
void foo(DWORD dwErrorCode, DWORD dwNumberOfBytesTransfered, LPOVERLAPPED lpOverlapped) {
return;
}
void bar(HANDLE hFile) {
auto lambda_foo = [](DWORD dwErrorCode, DWORD dwNumberOfBytesTransfered, LPOVERLAPPED lpOverlapped){return;};
ReadFileEx(hFile, buf, 1000, &o, lambdafoo); //compiles
ReadFileEx(hFile, buf, 1000, &o, foo); //doesn't compile
}
错误是“无法将参数5从'void(__ cdecl *)DWORD,DWORD,LPOVERLAPPED''转换为'LPOVERLAPPED_COMPLETION_ROUTINE'”。我使用的是MSVC2015。
有人可以告诉我我做错了什么吗?为什么它与lambda编译但没有相同的自由函数?
答案 0 :(得分:3)
Windows中的回调对调用约定有特殊要求。 Windows提供了一个方便的宏CALLBACK
,用于在定义函数时指定调用约定
void CALLBACK foo(DWORD dwErrorCode, DWORD dwNumberOfBytesTransfered, LPOVERLAPPED lpOverlapped)
{
return;
}