0

I can't understand the following compile error:

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
}

Error is "cannot convert argument 5 from 'void (__cdecl *) DWORD,DWORD,LPOVERLAPPED)' to 'LPOVERLAPPED_COMPLETION_ROUTINE' ". I use MSVC2015.

Could somebody please tell me what I'am doing wrong? Why it compiles with lambda but not with identical free function?

Dmitry J
  • 867
  • 7
  • 20

1 Answers1

3

Callbacks in windows have special requirements for the calling convention. Windows provides a handy macro named CALLBACK for specifying the calling convention when you define the function

void CALLBACK foo(DWORD dwErrorCode, DWORD dwNumberOfBytesTransfered, LPOVERLAPPED lpOverlapped)
{
  return;
}
Captain Obvlious
  • 19,754
  • 5
  • 44
  • 74