5

https://docs.perl6.org/language/nativecall

 "As you may have predicted by now, a NULL pointer
 is represented by the type object of the struct type."

https://docs.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regqueryvalueexw

 C++
 LSTATUS RegQueryValueExW(
   HKEY    hKey,
   LPCWSTR lpValueName,
   LPDWORD lpReserved,
   LPDWORD lpType,
   LPBYTE  lpData,
   LPDWORD lpcbData
 );

 lpReserved
 This parameter is reserved and must be NULL.

使用“native”,如何满足“NULL”要求?

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

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

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

“int32”返回:

Cannot unbox a type object (int32) to int in method
CALL-ME at C:\rakudo\share\perl6\sources \947BDAB9F96E0E5FCCB383124F9
23A6BF6F8D76B (NativeCall) line 587

非常感谢,-T

4

2 回答 2

5

要将指针传递给 aDWORD您可以使用 a CArray[DWORD]。例如,在这里我创建了一个libmylib.so带有(aka ) 参数的foo()函数的测试库:DWORD *int32_t *

#include <stdio.h>
#include <stdint.h>

void foo (int32_t *bar) {
    if ( bar == NULL ) {
        printf( "Got NULL pointer\n" ); 
    }
    else {
        printf("Got bar: %d\n", bar[0]);
    }
}

然后使用以下命令测试该库的 Raku 接口:

use v6;
use NativeCall;

constant DWORD := int32;
sub foo(CArray[DWORD]) is native("./libmylib.so") { * };
my @bar := CArray[DWORD].new;
@bar[0]  = 1;
foo(@bar);
foo(CArray[DWORD]);  # <-- Use a type object to pass a NULL pointer

输出

Got bar: 1
Got NULL pointer
于 2019-12-29T01:18:30.930 回答
0

JJ and Brad on the Perl6 mailing list were correct. For a NULL, just pass it a zero. I had a booboo somewhere else.

于 2019-12-29T09:33:49.040 回答