Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EnumChildWindows and lambda

When I try to compile the following on MinGW 4.6.2

EnumChildWindows(hwnd, [](HWND, LPARAM) -> BOOL { return TRUE; }, 0);

I get

error: cannot convert '<lambda(HWND, LPARAM)>'
to 'ENUMWINDOWSPROC {aka int (*)(HWND__*,long int)}'
for argument '2' to 'BOOL EnumChildWindows(HWND, ENUMWINDOWSPROC, LPARAM)'

Am I declaring the lambda incorrectly, or is this simply not going to work? The MS definition of the callback is

BOOL CALLBACK EnumChildProc(
  __in  HWND hwnd,
  __in  LPARAM lParam
);

http://msdn.microsoft.com/en-us/library/windows/desktop/ms633493%28v=vs.85%29.aspx

Is the calling convention causing the issue?

like image 885
user657267 Avatar asked May 10 '12 04:05

user657267


1 Answers

BOOL CALLBACK EnumChildProc(...)

That's the problem, the CALLBACK macro applies the __stdcall attribute to the function to change the calling convention from the default setting. Which is __cdecl in most programs. You cannot control the calling convention for your lambda, other than by changing the global setting (/Gz compile option). That can have a lot more side-effects than you'd care about, give up on this lamda.

Not a problem in 64-bit code btw.

like image 169
Hans Passant Avatar answered Oct 09 '22 03:10

Hans Passant