How can I make this work?

Rumbu rumbu at rumbu.ro
Sun Feb 28 09:18:56 UTC 2021


On Sunday, 28 February 2021 at 09:04:49 UTC, Rumbu wrote:
> On Sunday, 28 February 2021 at 07:05:27 UTC, Jack wrote:
>> I'm using a windows callback function where the user-defined 
>> value is passed thought a LPARAM argument type. I'd like to 
>> pass my D array then access it from that callback function. 
>> How is the casting from LPARAM to my type array done in that 
>> case?
>>
>> for example, I need something like this to work:
>>
>> int[] arr = [1, 2, 3];
>> long l = cast(long) cast(void*) arr.ptr;
>> int[] a = cast(int[]) cast(void*) l;
>
> LPARAM is not long on 32 bits, it's int. Use LPARAM instead of 
> long.

And you are passing only the address of the first element this 
way, loosing the array/slice length. This should work, but keep 
in mind that you have no warranty that the array stays in memory 
and it is not garbage collected.

int[] arr = [1, 2, 3];
LPARAM l = cast(LPARAM)cast(void*)&arr;
int[] a = *cast(int[]*)(cast(void*)l);




More information about the Digitalmars-d-learn mailing list