4

$ raku -v This is Rakudo version 2019.07.1 built on MoarVM version 2019.07.1

The following was done on Raku REPL. What am I doing wrong here? How do I assign values to CArray[WCHAR]?

I want $lpData[0] to be 0xABCD and $lpData[1] to be 0xEF12.

> use NativeCall;
Nil

> constant WCHAR := uint16;
(uint16)

> my $ValueData = 0xABCDEF12;
2882400018

> my CArray[WCHAR]  $lpData;
(CArray[uint16])

> $lpData[ 0 ] =  ( $ValueData +& 0xFFFF0000 ) +> 0x10;
Type check failed in assignment to $lpData; expected NativeCall::Types::CArray[uint16] but got Array ($[])
  in block <unit> at <unknown file> line 1

> $lpData[ 1 ] =    $ValueData +& 0x0000FFFF;
Type check failed in assignment to $lpData; expected NativeCall::Types::CArray[uint16] but got Array ($[])
  in block <unit> at <unknown file> line 1

Many thanks, -T

1 Answer 1

5

The problem is stated clearly in the error message: in the way you declare it, it's expecting every item to be a CArray[WCHAR]. Declare it this way, as is indicated in the documentation:

use NativeCall;
constant WCHAR = uint16; # No need to bind here also
my $native-array = CArray[WCHAR].new(); 
$native-array[0] = 0xABCDEF12 +& 0x0000FFFF;
say $native-array.list; # OUTPUT: «(-4334)␤»

CArray is not exactly a Positional, but it does have AT-POS defined so you can use square brackets to assign values. The error arises when, you try to assign to an non-initialized Scalar (which contains any) as if it were an array. The minimal change from your program is just initializing to a CArray[WCHAR]:

use NativeCall;
constant WCHAR = uint16; # No need to bind here also
my CArray[WCHAR] $native-array .= new; 
$native-array[0] = 0xABCDEF12 +& 0x0000FFFF;
say $native-array.list; # OUTPUT: «(-4334)␤»
3
  • 1
    @Todd please remember to vote up and/or accept one answer as correct (in this and in the other questions)
    – jjmerelo
    Jan 7, 2020 at 6:34
  • 1
    There is a certain waiting period if I answer my own question.
    – Todd
    Jan 7, 2020 at 14:48
  • On Windows, Mac and Linux, this outputs (61202), not (-4334).
    – StevieD
    Sep 2, 2022 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.