NODEJS調用C的dll

1、安裝運行環境
npm install -g windows-build-tools
####
##建議使用cnpm(cnpm install -g windows-build-tools)
####
2、該命令會自動安裝,python 及 visio studioC++等環境


3、編寫C++應用程序,調用
1)創建工作目錄文件夾(test)
1.1在test目錄中創建test.cc 和binding.gyp
CMD命令中:
第一步:執行 node-gyp configure
第二步:執行 node-gyp build
第三步:node app

C++代碼示例:
#include <node.h>
namespace demo {
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;
void Method(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
args.GetReturnValue().Set(String::NewFromUtf8(isolate, "world"));
}
void init(Local<Object> exports) {
NODE_SET_METHOD(exports, "hello", Method);
}
NODE_MODULE(addon, init)
}

binding.gyp示例:
{
'targets':[
{
'target_name':'hello',
'sources':['hello.cc'],
}]
}

app.js示例:
const addon = require('./build/Release/hello');
console.log(addon.hello()); // 'world'

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章