gpt4 book ai didi

node.js - 与 node-gyp 的 python ctypes 等效的 javascript

转载 作者:搜寻专家 更新时间:2023-10-31 22:33:53 24 4
gpt4 key购买 nike

我想将 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

测试.js

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

插件.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)

绑定(bind).gyp

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

dll 在 Release 文件夹 /build/Release/processings2D.dll

我的方向正确吗?

最佳答案

解决方案非常简单:

我的 dll 是 32 位版本,所以我应该使用好的 arch 构建我的模块并使用好的 Node 版本执行我的测试。

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

关于node.js - 与 node-gyp 的 python ctypes 等效的 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40426417/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com