HarmonyOS鸿蒙Next中FFI C++调用ArkTS定义的一个方法

HarmonyOS鸿蒙Next中FFI C++调用ArkTS定义的一个方法

鸿蒙提供了FFI,目标是C++调用JS arkTS方法,通过napi接口napi_call_function调用arkts 的方法,本地试了get_named_property拿不到对应的property,是哪有问题么? 附执行代码块: testAdd() 定义在ArkTS内一个方法 C++ 部分:

static napi_value runFunction(napi_env env, napi_callback_info info)
{
    napi_status napi_status;
    napi_valuetype valuetype;
    napi_value global;
    napi_status = napi_get_global(env, &global);
    napi_value addFunc;
    // 获取add函数
    napi_status = napi_get_named_property(env, global, "testAdd", &addFunc);
    napi_status = napi_typeof(env, addFunc, &valuetype);
    if (valuetype != napi_function) {
        napi_value sum;
        napi_create_double(env, 404, &sum);
        return sum;
    }
    napi_value result;
    napi_status = napi_call_function(env, global, addFunc, 0, nullptr, &result);
    return result;
}

更多关于HarmonyOS鸿蒙Next中FFI C++调用ArkTS定义的一个方法的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

解决措施 现提供两种方式调用artTS内函数

//User.ts export default class User { name: string = ‘username’

location: object = {
    country: 'china',
    city: 'shanghai'
}

oncAll() {
    console.log("testTag user oncAll")
}

}

function oncAllGlobal() { console.log(“testTag user oncAll Global”) } globalThis.oncAllGlobal = oncAllGlobal; // hello.cpp // 传入实例对象,在C++侧调用对象中的函数 static napi_value CallFunction(napi_env env, napi_callback_info info) { size_t argc = 1; napi_value args[1];

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

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

napi_value oncAll;
napi_status status = napi_get_named_property(env, args[0], "oncAll", &oncAll);
if (status != napi_ok)
    return oncAll;
napi_value return_val;
status = napi_call_function(env, args[0], oncAll, 0, NULL, &return_val);
return oncAll;

} // 使用全局对象,调用artTS内函数 static napi_value CallFunction_global(napi_env env, napi_callback_info info) { size_t argc = 1; napi_value args[1];

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

napi_value global;
napi_status status = napi_get_global(env, &global);

napi_value oncAll;
status = napi_get_named_property(env, global, "oncAllGlobal", &oncAll);
if (status != napi_ok)
    return global;
napi_value return_val;
status = napi_call_function(env, global, oncAll, 0, NULL, &return_val);
return oncAll;

} // index.ets build() { Row() { Column() { Text(this.message) .fontSize(50) .fontWeight(FontWeight.Bold) .onClick(() => { let user = new User(); hilog.info(0x0000, ‘testTag’, ‘Test NAPI call function: %{public}s’, testNapi.callFunction(user)); hilog.info(0x0000, ‘testTag’, ‘Test NAPI call function global : %{public}s’, testNapi.callFunctionGlobal()); }) } .width(‘100%’) } .height(‘100%’) }


napi使用方式可参考文档:  
[https://nodejs.cn/api/n-api.html#napi_call_function](https://nodejs.cn/api/n-api.html#napi_call_function)  
napi_get_global函数使用可参考:  
[https://nodejs.cn/api/n-api.html#napi_get_global](https://nodejs.cn/api/n-api.html#napi_get_global)  
其中napi_value 代表 JavaScript global 对象

更多关于HarmonyOS鸿蒙Next中FFI C++调用ArkTS定义的一个方法的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,通过FFI(Foreign Function Interface)实现C++调用ArkTS定义的方法,可以使用Native API进行桥接。首先,在ArkTS中定义一个方法,并使用@Native注解标记,以便在C++中调用。例如:

// ArkTS
@Native
function myMethod(param: string): string {
    return "Hello, " + param;
}

在C++中,使用napi接口调用ArkTS方法。首先,加载ArkTS模块,然后通过napi_get_named_property获取方法,最后使用napi_call_function调用该方法。例如:

// C++
#include <napi.h>

void CallArkTSMethod(Napi::Env env, Napi::Object exports) {
    // 加载ArkTS模块
    Napi::Object arkTSModule = Napi::Object::New(env);
    // 获取ArkTS方法
    Napi::Function myMethod = arkTSModule.Get("myMethod").As<Napi::Function>();
    // 调用ArkTS方法
    Napi::Value result = myMethod.Call(arkTSModule, { Napi::String::New(env, "World") });
    // 处理返回值
    std::string resultStr = result.As<Napi::String>().Utf8Value();
}

通过这种方式,C++可以调用ArkTS中定义的方法,实现跨语言调用。

在HarmonyOS鸿蒙Next中,通过FFI(Foreign Function Interface)机制,可以从C++代码调用ArkTS定义的方法。首先,在ArkTS中使用@FFI注解导出方法,然后在C++中使用napi_get_named_propertynapi_call_function等NAPI接口调用该方法。确保在C++代码中正确加载ArkTS模块,并通过NAPI上下文进行跨语言调用。

回到顶部