HarmonyOS 鸿蒙Next native如何创建ArkTS对象并传递构造函数的参数

HarmonyOS 鸿蒙Next native如何创建ArkTS对象并传递构造函数的参数 大家好,我的Napi模块里的cpp文件用到了第三方har包里的类,请问如何构造该类及调用其方法

第三方third.har包结构如下

![cke_5050.png](data-originheight=“405” data-originwidth=“291” src="https://alliance-communityfile-drcn.dbankcdn.com/FileServer/getFile/cmtybbs/306/209/223/0420086000306209223.20250226165043.74250955661693997273480031810831:50001231000000:2800:CC015242AE3D1A6CF086378F8973988500E7797411DE44FA30658F08137B2B52.png)

import third from 'libthird.so';
export class ThirdLib {
    constructor(h) {
        this.nativeInst = third.createInst(h);
    }
    funcA(e, f, g) {
        if (this.nativeInst) {
            return third.funcA(this.nativeInst, e, f, g);
        }
        return undefined;
    }
    funcB(b, c, d) {
        if (this.nativeInst) {
            return third.funcB(this.nativeInst, b, c, d);
        }
        return undefined;
    }
}

请问如何在我的native方法里创建第三方包里的ThirdLib以及调用funcA或funcB方法


更多关于HarmonyOS 鸿蒙Next native如何创建ArkTS对象并传递构造函数的参数的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

可以使用参考下面示例中的onCallBack3的方式,通过native层去调用arkts的方法,然后通过arkts来调用第三方har的接口:

//index.ets
import hilog from '@ohos.hilog';
import testNapi from 'libentry.so';

export class testCb{
  testNum:number = 0;
  testString:string = 'hello world';
};

export class tsClass {

  public onCallBack1(id: string){
    //操作id
    console.log("testcallback1" + id)
  }
  public onCallBack2(cnt: number){
    // 操作number
    console.log("testcallback2" + cnt)
  }
  public onCallBack3(cbClass: testCb){
    // 操作cbClass
    console.log("testcallback3" + cbClass.testNum)
  }
}
@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  TsOBJ:tsClass = new tsClass();
  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
             testNapi.transObject(this.TsOBJ)
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}
//index.d.ts
export const transObject: (a: Object) => void;
//napi_init.cpp
因为onCallBack3的入参是一个TS类,所以需要用status = napi_create_object(env, &arg_object)创建类的入参,并用napi_set_named_property()给类赋值
static napi_value TransObject(napi_env env, napi_callback_info info) {
   napi_status status;
   size_t argc = 1;
   napi_value js_cb;
   // 获取TS侧传入的参数
   status = napi_get_cb_info(env, info, &argc, &js_cb, nullptr, nullptr);
  
   // 获取TS 对象的方法 onCallBack1
   napi_value onCallBack1, str_arg;
   status = napi_get_named_property(env, js_cb, "onCallBack1", &onCallBack1);
   status = napi_create_string_utf8(env, "mangguo", strlen("mangguo"), &str_arg);
   // 调用TS 对象的方法 onCallBack1
   size_t cb1argc = 1;
   status = napi_call_function(env, js_cb, onCallBack1, cb1argc, &str_arg, nullptr);
    
   // 获取TS 对象的方法 onCallBack2
   size_t cb2argc = 1;
   napi_value onCallBack2, int_arg;
   status = napi_get_named_property(env, js_cb, "onCallBack2", &onCallBack2);
   status = napi_create_int32(env, 2, &int_arg);
   // 调用TS 对象的方法 onCallBack2
   status = napi_call_function(env, js_cb, onCallBack2, cb2argc, &int_arg, nullptr);

   // 获取TS 对象的方法 onCallBack3
   napi_value oncallback3, arg_object;
   status = napi_get_named_property(env, js_cb, "onCallBack3", &oncallback3);
   // native 层创建对象arg_object
   status = napi_create_object(env, &arg_object);
   napi_value testNum,testString,cb3argc;
   status = napi_create_int32(env, 123, &testNum);
   // 给上面创建的arg_object对象属性testNum赋值123
   status = napi_set_named_property(env, arg_object, "testNum", testNum);
   status = napi_create_string_utf8(env, "mangguo", strlen("mangguo"), &testString);

   // 给上面创建的arg_object对象属性testString赋值mangguo
   status = napi_set_named_property(env, arg_object, "testString", testString);

   // 调用TS 对象的方法 onCallBack3,并将上面创建的对象arg_object,作为方法参数传递
   status = napi_call_function(env, js_cb, oncallback3, cb2argc, &arg_object, nullptr);

   return nullptr;
}

更多关于HarmonyOS 鸿蒙Next native如何创建ArkTS对象并传递构造函数的参数的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,创建ArkTS对象并传递构造函数的参数可以通过以下步骤实现:

  1. 定义ArkTS类:首先需要在ArkTS中定义一个类,类中可以包含构造函数。例如:

    class MyClass {
        private value: number;
    
        constructor(value: number) {
            this.value = value;
        }
    
        getValue(): number {
            return this.value;
        }
    }
    
  2. 创建对象并传递参数:在需要使用该类的地方,可以通过new关键字创建该类的实例,并传递构造函数的参数。例如:

    let myObject = new MyClass(10);
    
  3. 使用对象:创建对象后,可以调用对象的方法或访问其属性。例如:

    let value = myObject.getValue();
    

通过以上步骤,可以在HarmonyOS鸿蒙Next中创建ArkTS对象并传递构造函数的参数。

回到顶部