带有node-gyp的python ctypes的javascript等价物

9wbgstp7  于 2023-02-15  发布在  Node.js
关注(0)|答案(1)|浏览(116)

我想把一个python脚本转换成javascript脚本。我的python脚本加载一个dll并使用它的API。

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

我试着用node-gyp做同样的事情,但是我的脚本找不到dll。

console.log(processings2D.ImageProcessor2DCreate());
                      ^
TypeError: Cannot load processings2D.dll library

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
"我走的方向对吗"

erhoui1w

erhoui1w1#

解决办法其实很简单:
我的dll是32位版本,所以我应该使用32位arch标志构建模块,并使用32位版本的node执行测试。

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

相关问题