2016-11-04 101 views
5

我想將python腳本轉換爲javascript腳本。我的python腳本加載一個DLL並使用它的API。javascript等效於python ctypes與node-gyp

processings2D = ctypes.CDLL('processings2D.dll') 
print(processings2D.ImageProcessor2DCreate()) 

我試着對node-gyp做同樣的事情,但是我的腳本沒有找到dll。

test.js

var processings2D = require('./build/Release/processings2D.node'); 
console.log(processings2D.ImageProcessor2DCreate()); 

addon.cc

#include <nan.h> 
#include "processings2D/processings2D.h" 

HINSTANCE hDLL = NULL; 
typedef int(*Fn)(); 

void ImageProcessor2DCreate(const Nan::FunctionCallbackInfo<v8::Value>& info) { 
    hDLL = LoadLibrary("processings2D.dll"); 
    if(!hDLL) 
    { 
     Nan::ThrowTypeError("Cannot load processings2D.dll library"); 
     return; 
    } 

    Fn fn = (Fn)GetProcAddress(hDLL, "ImageProcessor2DCreate"); 
    if (!fn) { 
     Nan::ThrowTypeError("Could not load ImageProcessor2DCreate function"); 
     FreeLibrary(hDLL); 
     return; 
    } 

    info.GetReturnValue().Set(Nan::New(fn())); 
} 

void Init(v8::Local<v8::Object> exports) { 
    exports->Set(Nan::New("ImageProcessor2DCreate").ToLocalChecked(), Nan::New<v8::FunctionTemplate>(ImageProcessor2DCreate)->GetFunction()); 
} 

NODE_MODULE(twoD, Init) 

binding.gyp

{ 
    "targets": [ 
    { 
     "target_name": "processings2D", 
     "sources": [ 
     "addon.cc" 
     ], 
     "include_dirs": [ 
     "<!(node -e \"require('nan')\")" 
     ] 
    } 
    ] 
} 

DLL是在發行文件夾/build/Release/processings2D.dll

我在正確的方向嗎?

回答

2

的解決方案是非常簡單的:

我的DLL是一個32位版本,所以我應該建立自己的模塊與良弓和用好版本節點的執行我的考驗。

node-gyp clean configure --arch=ia32 build 
"C:\Program Files (x86)\nodejs\node.exe" test.js