using a dll with node-ffi

时光总嘲笑我的痴心妄想 提交于 2021-02-10 07:07:55

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!