gpt4 book ai didi

Raku/Perl6 : how do you code a NULL with NativeCall

转载 作者:行者123 更新时间:2023-12-03 02:56:06 24 4
gpt4 key购买 nike

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://learn.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

最佳答案

要将指针传递给DWORD,您可以使用CArray[DWORD]。例如,在这里我创建了一个测试库 libmylib.so ,其中的 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 与该库的接口(interface):

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

关于Raku/Perl6 : how do you code a NULL with NativeCall,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59500796/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com