问题
I'm using node-ffi to access a dll supplied by a custom hardware i bought, the dll uses device driver to do things, they don't supply dll docs but they have a sample app in c#, the dll is used in c# like this:
[DllImport("POS_CIDR.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr CIDR_Info();
...
result = Marshal.PtrToStringUni(CIDR.CIDR_Info());
...
now i try to access CIDR_Info
function of dll with following node code:
var ffi = require('ffi')
var ref = require('ref')
var int = ref.types.int;
var libprime = ffi.Library('POS_CIDR.dll', {
'CIDR_Info': [ int ,[]],
});
console.log(libprime.CIDR_Info()); // 73402156
now from what i understad the function returns an integer containing the address of a string, how can i dereference that integer to string in node? i couldnt find any way to set address of a buffer in node.
update
ok i found how to read that string with this code :
var ffi = require('ffi')
var ref = require('ref')
var stringPtr = ref.refType(ref.types.CString);
var int = ref.types.int
var libprime = ffi.Library('POS_CIDR.dll', {
'CIDR_Info': [ stringPtr ,[]],
});
buf = new Buffer(255);
buf=libprime.CIDR_Info();
console.log(ref.readCString(buf, 0));
now the console outputs p
which is the first char of string, if i set the offset to 2 it gives o
which is the second char, i think there is a problem with encoding that readCString sees a null char after each char.
any idea how to fix that?
来源:https://stackoverflow.com/questions/52802642/using-a-dll-with-node-ffi