Use nested functions as callbacks with Windows API functions?

spikespaz spikespaz at outlook.com
Mon Oct 1 20:27:43 UTC 2018


I have the following code, this works.

================================================================

import core.sys.windows.windows: EnumWindows;
import std.stdio: writeln;

void*[] hWndList;

extern (Windows) int callback(void* hWnd, long /* lParams */ ) 
nothrow {
     hWndList ~= hWnd;

     return true;
}

void main() {
     EnumWindows(&callback, 0);

     writeln(hWndList);
}

================================================================

I was hoping I could use something more akin to JavaScript's 
syntax: (void* hWnd, long) => {}.

I tried this but I'm getting errors with the signature, it says 
the function is a delegate and apparently Windows API can't 
accept a delegate.

================================================================

import core.sys.windows.windows: EnumWindows;
import std.stdio: writeln;

void main() {
     void*[] hWndList;

     EnumWindows((void* hWnd, long /* lParams */ ) nothrow {
         hWndList ~= hWnd; return true;
     }, 0);

     writeln(hWndList);
}

================================================================

I'm not going to even paste the compiler error because I am very 
clearly going about this the wrong way.

Of course there is nothing wrong with defining each callback as a 
separate function, but then comes the issue of naming them. I 
also don't like the way it makes my code look.

Thanks.


More information about the Digitalmars-d-learn mailing list