使用自由函数作为LPOVELAPPED_COMPLETION_ROUTINE

Using free function as LPOVELAPPED_COMPLETION_ROUTINE

本文关键字:LPOVELAPPED COMPLETION ROUTINE 自由 函数      更新时间:2023-10-16

我无法理解以下编译错误:

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 编译但不使用相同的自由函数?

窗口中的回调对调用约定有特殊要求。Windows 提供了一个名为CALLBACK的方便宏,用于在定义函数时指定调用约定

void CALLBACK foo(DWORD dwErrorCode, DWORD dwNumberOfBytesTransfered, LPOVERLAPPED lpOverlapped)
{
return;
}