C++可以调HarmonyOS鸿蒙Next中ets里面的方法吗?

C++可以调HarmonyOS鸿蒙Next中ets里面的方法吗? C++可以调ets里面的方法吗?

3 回复
import testNapi from 'libentry.so';

@Entry
@Component
struct Index {
@State message: string = 'Hello World';
//private resmgr = getContext().resourceManager; // 获取js的资源对象

build() {
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.onClick(() => {
//hilog.info(0x0000, 'testTag', 'Test NAPI 2 + 3 = %{public}d', testNapi.add(2, 3));
//let rawfileContet = testNapi.getRawFileContent(this.resmgr, "rawfile1.txt");
//console.log("rawfileContet" + rawfileContet);
testNapi.addSync(3, 4, (result)=>{
this.message = `result is $(result)`
});
})
}
.width('100%')
}
.height('100%')
}
}

export const addSync: (a: number, b: number, callback:(result:number)=>void) => number;
static napi_value AddSync(napi_env env, napi_callback_info info) {
size_t requireArgc = 3;
size_t argc = 3;
napi_value args[3] = {nullptr};
napi_value context = nullptr;

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

SampleTheread();

napi_valuetype valuetype0;
napi_typeof(env, args[0], &valuetype0);

napi_valuetype valuetype1;
napi_typeof(env, args[1], &valuetype1);

double value0;
napi_get_value_double(env, args[0], &value0);

double value1;
napi_get_value_double(env, args[1], &value1);

napi_value callBackArg[1] = {nullptr};
napi_create_double(env, value0 + value1, &callBackArg[0]);

napi_value sum;
napi_call_function(env, context, args[2], 1, callBackArg, &sum);

return sum;
}
static napi_value Init(napi_env env, napi_value exports)
{
napi_property_descriptor desc[] = {
{"add", nullptr, Add, nullptr, nullptr, nullptr, napi_default, nullptr},
{"addSync", nullptr, AddSync, nullptr, nullptr, nullptr, napi_default, nullptr},
{"getRawFileContent", nullptr, GetRawFileContent, nullptr, nullptr, nullptr, napi_default, nullptr}};
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
return exports;
}

参考上述实现

更多关于C++可以调HarmonyOS鸿蒙Next中ets里面的方法吗?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,C++可以通过NDK(Native Development Kit)调用ETS(Extended TypeScript)中的方法。具体实现需要借助鸿蒙提供的Native API和JS/ETS交互机制。

首先,ETS是鸿蒙Next中用于开发UI和业务逻辑的脚本语言,基于TypeScript扩展。C++代码可以通过鸿蒙的Native API与ETS层进行交互。鸿蒙提供了Native APIJS/ETS Binding机制,允许C++代码调用ETS层暴露的接口。

要实现C++调用ETS方法,通常需要以下步骤:

  1. 在ETS层定义需要暴露给C++的方法,并使用@Native@Export等注解标记这些方法。
  2. 在C++层使用鸿蒙提供的Native API,通过napi_get_propertynapi_call_function等函数获取并调用ETS层的方法。
  3. 通过napi_value等数据类型在C++和ETS层之间传递参数和返回值。

需要注意的是,C++调用ETS方法时,数据类型需要在C++和ETS层之间进行转换,例如将C++的std::string转换为ETS的string类型。此外,调用过程中需要处理可能的异常和错误。

总结来说,C++可以通过鸿蒙的Native API和JS/ETS Binding机制调用ETS层的方法,但需要遵循鸿蒙的交互规则和数据类型转换机制。

目前,C++ 直接调用 HarmonyOS 鸿蒙 Next 中 ETS(Extensible TypeScript)的方法尚不支持。ETS 是鸿蒙系统的开发语言,主要用于应用层开发,而 C++ 通常用于系统层或性能敏感模块。若需交互,可通过 JSI(JavaScript Interface)或 Native API 进行桥接,但需注意跨语言调用的性能开销和兼容性问题。建议根据具体需求选择合适的交互方案。

回到顶部