鲁大师2011官方下载-ubuntu10 04源

js调用代码
2023年4月4日发(作者:怎样生成自己的二维码)

NodeJS调⽤C++(Node-ffi)

本⽂介绍如何⽤Nodejsd调⽤C++代码

⽤node-ffi实现

如果调⽤的C++dll是32位接⼝,则NodeJS也需要确保是32位。

⽤ffi,则NodeJS必须是V10及以下的版本

NodeJS查看版本和位数:

node-v//查看版本号

node-p'process'//在返回的arch和platform可以看详细信息

⾸先安装node-gyp(nodejs默认安装,若没有则⽤⼀下命令)

npminstall-gnode-gyp

安装ffi和ref

npminstallffi

npminstallref

默认安装完会⽤node-gyp编译

教程上说nodejsv11以上可以通过⼀下安装,但是我还是会报错。。。npminstall@saleae/ffi

代码测试:

npm调⽤windowsApi:

1varffi=require('ffi');

2

3varc_txt=text=>{

(`${text}0`,"ucs2");

5};

6

7varcurrent=y("user32",{

8"MessageBoxW":["int32",["int32","string","string","int32"]]

9});

10

11constok_or_cancel=eBoxW(0,c_txt("HellofromNodeJs"),c_txt("node-ffitest"),1);

12

(ok_or_cancel);

新建⼀个C++dll⼯程:

1#ifdefined(WIN32)||defined(_WIN32)

2#defineEXPORT__declspec(dllexport)

3#else

4#defineEXPORT

5#endif

6

7#ifdef__cplusplus

8extern"C"{

9#endif

10

11EXPORTintfoo(intparam){

12intret=param+1;

13//doC++things

14returnret;

15}

16

17EXPORTintbar(){

18intret=0;

19//doC++things

20returnret;

21}

22

23#ifdef__cplusplus

24}

25#endif

NodeJS中调⽤:

1varbasic=y("./Project_dll_1",{

2"foo":["int",["int"]],

3"bar":["int",[]]

4})

5

6varv=(1);

(v);

更多推荐

js调用代码