我正在使用 Node.js ffi插件来调用 C++ DLL。
我遇到的问题是我提供的结构 - 它包含一个 char 数组 - 我不相信我设置正确。结果我无法访问内容。
例程在 C++ 头文件中的定义:
int GetSysConfig(MyConfig * config);
MyConfig结构在C++ 中定义如下:
typedef struct{
int attribute;
char path[256];
}MyConfig;
我对应的 Node.js 结构定义:
var ffi = require('ffi');
var ref = require('ref');
var StructType = require('ref-struct');
var ArrayType = require('ref-array');
// This seems to be the problematic part?
var charArray = ArrayType('char');
charArray.length = 256;
var MyConfig = StructType({
'attribute' : 'int',
'path' : charArray
})
注意:下面是我从 Node.js 调用 DLL 的地方——尽管我可能是错的,但我认为这里没有问题。
// Create a pointer to the config - we know we expect to supply this to the C++ routine.
var myConfigPtr = ref.refType(MyConfig);
var lib = ffi.Library('my.dll', {
"GetSysConfig": ["int", [myConfigPtr]]
});
var myConfigObj = new MyConfig();
lib.GetSysConfig.async(myConfigObj.ref(), function(err, res) {
console.log("attribute: " + myConfigObj.attribute);
// This is always empty [] - when it shouldn't be.
console.log("path: " + JSON.Stringify(myConfigObj.path));
});
有谁知道我哪里错了?