Use nested functions as callbacks with Windows API functions?

spikespaz spikespaz at outlook.com
Mon Oct 1 22:51:23 UTC 2018


On Monday, 1 October 2018 at 21:03:24 UTC, Boris-Barboris wrote:
> On Monday, 1 October 2018 at 20:27:43 UTC, spikespaz wrote:
>> 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.
>
> You can make it a non-delegate by passing a pointer to hWndList 
> in lParams as it was supposed to by WinApi devs, instead of 
> zero, and not implicitly capturing stack pointer by referencing 
> hWndList directly from the body.
>
> https://run.dlang.io/is/t4k4Nc

I don't know how to do this. I'm not the best with pointers, I'm 
still learning D and I'm unfamiliar with functional programming.

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

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

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

     return true;
}

void main() {
     void*[] hWndList;

     EnumWindows(&callback, &hWndList);

     writeln(hWndList);
}

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

Clearly I can't use &hWndList to pass the reference, how would I 
access the variable by the memory address inside the callback?



More information about the Digitalmars-d-learn mailing list