Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript equivalent for python ctypes with node-gyp

I want to transform a python script into a javascript script. My python script loads a dll and use its API.

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

I try do to the same with node-gyp but my script doesn't find the 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 is in the Release folder /build/Release/processings2D.dll

Am I in the right direction ?

like image 213
Guillaume Vincent Avatar asked Nov 04 '16 15:11

Guillaume Vincent


People also ask

What version of Python does node-gyp need?

node-gyp requires that you have installed a compatible version of Python, one of: v3. 7, v3. 8, v3. 9, or v3.

Does node-gyp need Python?

node-gyp requires that you have installed a compatible version of Python, one of: v3.7, v3.8, v3.9, or v3.10.

What is API node JS?

Node-API is a toolkit introduced in Node 8.0. 0 that acts as an intermediary between C/C++ code and the Node JavaScript engine. It permits C/C++ code to access, create, and manipulate JavaScript objects as if they were created by JavaScript code. Node-API is built into Node versions 8.0.


1 Answers

The solution was really simple :

My dll was a 32 bit version, so I should build my module with the good arch and execute my test with the good version of node.

node-gyp clean configure --arch=ia32 build
"C:\Program Files (x86)\nodejs\node.exe" test.js
like image 82
Guillaume Vincent Avatar answered Nov 15 '22 09:11

Guillaume Vincent