node-ffi-rs: How to obtain null-terminated utf8 string?
Descibe your problem in detail
How can I obtain a returned null-terminated utf8 string?
const char* MaaVersion();
According to comment in dts, DataType.String is representing char16* instead of char8*. But in document the demo function concatenateStrings uses char8*. I’m not sure if there has been some breaking changes.
I’ve checked if I can use external as a workaround, but it seems that I cannot iterate the pointer data to calculate the length.
Currently, I’ve managed to call strlen on it and got the length, but then retrieving the data using arrayConstructor cause a crash.
export function version() {
const ptr = load({
library: 'maafw',
funcName: 'MaaVersion',
paramsType: [],
paramsValue: [],
retType: DataType.External
})
const len = load({
library: 'libc',
funcName: 'strlen',
paramsType: [DataType.External],
paramsValue: [ptr],
retType: DataType.I32
})
console.log(ptr, len)
const result = load({
library: 'maafw',
funcName: 'MaaVersion',
paramsType: [],
paramsValue: [],
retType: arrayConstructor({
type: DataType.U8Array,
length: len
})
})
console.log('result', result)
return result
}
PS E:\Projects\MAA\maa-ffi-rs> tsx .\test\load.ts
[External: 1c0001bbee0] 40
PS E:\Projects\MAA\maa-ffi-rs>
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 27 (14 by maintainers)
This works. Also, I’ve noticed that I can use
DataType.Stringnow, withfreeResultMemory: false.restorePointer corresponds the result of createPointer.
Use createPointer to create a pointer point to string will return a pointer point to char* that is char**.
You can use
wrapPointerto create a multiple pointerOk,i will deal with that later