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

My Native Call notes

Thread Next
From:
ToddAndMargo via perl6-users
Date:
December 29, 2019 22:56
Subject:
My Native Call notes
Message ID:
f4f7433f-dce5-4e99-3a30-78891f539e59@zoho.com
Hi All,

This is my keeper on Native Call.

-T

<perl6.Native.Call.Notes.txt>

Raku: Native Call notes:

Really poor reference: https://docs.raku.org/language/nativecall


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
    characters.

    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,
       WCHARS,
       DWORD,
       DWORD,
       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

</perl6.Native.Call.Notes.txt>

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About