develooper Front page | perl.perl6.users | Postings from December 2019

My Native Call notes

Thread Next
ToddAndMargo via perl6-users
December 29, 2019 22:56
My Native Call notes
Message ID:
Hi All,

This is my keeper on Native Call.



Raku: Native Call notes:

Really poor reference:

1) Kernel32.dll call variables with a "p" in them are "C" pointers.

    LSTATUS RegOpenKeyExW(
        HKEY    hKey,
        LPCWSTR lpSubKey,
        DWORD   ulOptions,
        REGSAM  samDesired,
        PHKEY   phkResult

     LPCWSTR and PHKEY in the above would be "C" pointers.

       A) Native Call converts Raku variables into C Pointer for you

       B) NativeCall converts the pointers to their contents of a
          returned “C” variable for you

2) to pass a "C" NULL to Native Call, send it a numerical zero.
    Note: in “C”, a NULL is a pointer whose value is zero

3) a “C” string is an array of characters terminated with chr(0)

4) Kernel32.dll calls with a “W” in them use UTF16 `uint16` values
    in their strings.  The rest you can use UTF8 `uint8`.  Most of
    the time they are interchangeable if you are using standard

    Use the following to convert a Raku strings, `abcdefg` below,
    into a “C” string

       UTF8:  my $L =  CArray[uint8].new("abcdefg".encode.list);
       UTF16: my $M = CArray[uint16].new("abcdefg".encode.list);

    Native Call is tack the chr(0) at the end for you.

5) create two line to pass a call to Native Call (see 1) above for
    the call definition):

    constant WCHAR   := uint16;
    constant DWORD   := int32;

    sub RegQueryValueExW(
       DWORD is rw,
       DWORD is rw
       is native("Kernel32.dll")
       is symbol("RegQueryValueExW")
       returns DWORD
    { * };

    $RtnCode = RegQueryValueExW(
        $Handle, $lpValueName, 0, 0, $lpData, $lpcbData );

    $RtnCode is the Windows return error code.  Note: 0 is success

    $lpData is the actual data converted from the “C” pointer

    $lpcbData is the converted number of bytes in $lpData’s
    converted data


Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About