raku - Raku/Perl6:如何使用 NativeCall 编写 NULL
问题描述
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
解决方案
要将指针传递给 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
推荐阅读
- python - 总结dict中值列表中的时间戳
- java - 如何创建可变数量的变量?
- swift - 上传图片的预览页面
- azure - 使用 MSAL 库 java 获取 Azure 应用服务主体
- python - 从不同目录导入文件
- java - 使用 JsonPath-Jayway 检索 JSON 元素
- javascript - Javascript 表单 onsubmit 总是在刷新时运行
- reactjs - 如何执行内部包含反应钩子的函数
- reactjs - 为什么这个组件在反应中渲染两次?
- javascript - Google Map API navigator.geolocation.getCurrentPosition() 给出了错误的坐标