第一步先安装nodejs , 这个很简单就不用我就不多说了。
第二步 安装 python 2.7
第三步 安装 Visual Studio 2017
上面这些环境装好我们再安装需要的模块,直接 cmd 命令
npm install node-gyp
yarn add ffi-napi
安装完成后,我们用Visual Studio创建c++ 动态库
创建c++ 头文件
之后 myAddDll.cpp 写我们要导出的函数体
右键项目生成dll
我里面调用了 MessageBox ,这个要注意字符集问题,字符集是unicode 字符串需要加L,多字符不需要L
(属性–>常规–>项目默认项–>字符集改为未设置)
生成完成后去项目目录中 x64\Debug\myAddDll.dll 拷贝出来放到我们的nodejs项目中
node项目:
const ffi = require(‘ffi-napi’); // 引入ffi-napi
// 下面 引入 我们 myAddDll.dll 路径 ,并导入c++ 函数
const myAddDll = new ffi.Library(__dirname+’/dll/myAddDll’, {
‘testInt’:[‘int’, [‘int’, ‘int’]],
‘testBool’: [‘bool’, [‘int’]],
‘testString’:[‘string’,[‘string’]],
‘testString1’:[‘string’,[]],
‘testVoid’:[‘void’,[]]
});
调用成功,搞定!!!