HarmonyOS ArkTS调用index.d.ts里面的C接口有一个方法中的参数类型是Uint8Array,C侧改如何接收

ArkTS调用index.d.ts里面的C接口有一个方法中的参数类型是Uint8Array,C侧改如何接收?

接口如下:

export const SymEncrypt:(IV:Uint8Array,ucPlain:Uint8Array,ucKey:Uint8Array,iAlgorithm:number, mode:number,ulPlainLen:number,ulKeyLen:number)=>Object;
  • 1.

Uint8Array类型的参数,C的代码中怎么接收?

HarmonyOS
2025-01-09 14:33:32
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Excelsior_abit

示例参考如下:

// ArkTS传递Uint8Array参数 
import testNapi from 'libentry.so';

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            let temp = new Uint8Array(2);
            temp[0] = 1;
            temp[1] = 2;
            console.info(`Pure inputBuffer length: ${temp.length}`);
            let res = testNapi.uintArr(temp);
            console.info(`Pure outputBuffer: ${res}`);
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.

Native侧可以使用napi_get_typedarray_info方法获取Uint8Array的详细信息。

// Native侧获取Uint8Array参数并返回到ArkTS侧
napi_value UintArr(napi_env env, napi_callback_info info) {
    size_t requireArgc = 1;
    size_t argc = 1;
    napi_value args[1] = {nullptr};

    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);

    napi_value inputArray = args[0];

    // 获取ArrayBuffer类型
    napi_typedarray_type type;
    napi_value inArrayBuffer;
    size_t byteOffset;
    size_t length;
    napi_get_typedarray_info(env, inputArray, &type, &length, nullptr, &inArrayBuffer, &byteOffset);
    if (type != napi_uint8_array) {
        return nullptr;
    }
    // 获取ArrayBuffer信息
    void *data = nullptr;
    size_t byte_length;
    napi_get_arraybuffer_info(env, inArrayBuffer, &data, &byte_length);

    // 构造ArrayBuffer并赋值
    napi_value output_buffer;
    void *output_ptr = nullptr;
    napi_create_arraybuffer(env, byte_length, &output_ptr, &output_buffer);
    napi_value outputArray;
    napi_create_typedarray(env, type, length, output_buffer, byteOffset, &outputArray);
    uint8_t *input_bytes = (uint8_t *)(data) + byteOffset;
    uint8_t *array = (uint8_t *)(output_ptr);
    for (size_t idx = 0; idx < length; idx++) {
        array[idx] = 3;
    }
    return outputArray;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
// Native侧获取Uint8Array参数并返回到ArkTS侧
napi_value UintArr(napi_env env, napi_callback_info info) {
    size_t requireArgc = 1;
    size_t argc = 1;
    napi_value args[1] = {nullptr};

    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);

    napi_value inputArray = args[0];

    // 获取ArrayBuffer类型
    napi_typedarray_type type;
    napi_value inArrayBuffer;
    size_t byteOffset;
    size_t length;
    napi_get_typedarray_info(env, inputArray, &type, &length, nullptr, &inArrayBuffer, &byteOffset);
    if (type != napi_uint8_array) {
        return nullptr;
    }
    // 获取ArrayBuffer信息
    void *data = nullptr;
    size_t byte_length;
    napi_get_arraybuffer_info(env, inArrayBuffer, &data, &byte_length);

    // 构造ArrayBuffer并赋值
    napi_value output_buffer;
    void *output_ptr = nullptr;
    napi_create_arraybuffer(env, byte_length, &output_ptr, &output_buffer);
    napi_value outputArray;
    napi_create_typedarray(env, type, length, output_buffer, byteOffset, &outputArray);
    uint8_t *input_bytes = (uint8_t *)(data) + byteOffset;
    uint8_t *array = (uint8_t *)(output_ptr);
    for (size_t idx = 0; idx < length; idx++) {
        array[idx] = 3;
    }
    return outputArray;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.

index.d.ts声明接口。

export const uintArr: (a: Uint8Array) => object;
  • 1.
分享
微博
QQ
微信
回复
2025-01-09 17:21:46
相关问题
HarmonyOS Uint8Array
836浏览 • 1回复 待解决
Uint8Array@Sendable类吗?
980浏览 • 1回复 待解决
Uint8Array如何转成ArrayBuffer
2281浏览 • 1回复 待解决
HarmonyOS ArrayBuffer如何转成Uint8Array
939浏览 • 1回复 待解决
HarmonyOS string转Uint8Array
930浏览 • 2回复 待解决
HarmonyOS 录音发送Uint8Array
605浏览 • 1回复 待解决
ArrayBuffer怎么转Uint8Array
1242浏览 • 1回复 待解决
C++同步调ArkTS里面的方法
1731浏览 • 1回复 待解决
HarmonyOS Uint8Array格式转字符串方法
1583浏览 • 1回复 待解决
HarmonyOS Uint8Array转16进制
802浏览 • 2回复 待解决
如何Uint8Array转ArrayBuffer?
1079浏览 • 1回复 待解决
Uint8Array 如何直接转为String or Json
3776浏览 • 1回复 待解决
c++可以直接调用tsstatic方法吗?
3152浏览 • 1回复 待解决
HarmonyOS 如何Uint8Array转为ArrayBuffer
630浏览 • 1回复 待解决
HarmonyOS 如何Uint8Array转成ArrayBuffer
1086浏览 • 1回复 待解决