跳轉到主要內容
CString 包裝一個原始 C 字符串指針(const char*)並提供安全讀取。

示例

import { dlopen, FFIType, CString } from 'arkffi';

const lib = dlopen('libffi_target.so', {
  getVersion: { args: [], returns: FFIType.int64 },
});

const ptr: number = lib.symbols.getVersion();
const cstr = new CString(ptr);

cstr.toString(); // → "1.0.0"
cstr.length;     // → 5

lib.close();

構造函數

constructor(ptr: number, byteOffset?: number, byteLength?: number);
參數類型默認值說明
ptrnumberC 字符串的內存地址
byteOffsetnumber0跳過前 N 個字節再開始讀取
byteLengthnumber自動檢測 null 結尾顯式指定讀取長度
從原始內存地址創建 CString。指針必須指向有效的 C 字符串。可選地指定偏移量和長度。
const cstr = new CString(ptr, 0, 5);
cstr.toString(); // 讀取前 5 個字節

屬性

length

get length(): number;
返回 C 字符串的長度(字符數)。

方法

toString()

toString(): string;
讀取 C 字符串並以 ArkTS 字符串返回。

空指針

傳入 0(空指針)是安全的,返回空字符串:
const cstr = new CString(0);
cstr.toString(); // → ""
cstr.length;     // → 0