使用node-ffi回调函数中文乱码不显示
我的代码:
const ffi = require('ffi');
const ref = require("ref");
let ReadIdcard = ffi.Library('./ReadIdcard.dll', {
'initReadIdcard': ['int', ['pointer']]
});
let callback = ffi.Callback('void', ['string'],
function(data) {
console.log("data: ", data);
});
console.log("registering the callback");
let z = ReadIdcard.initReadIdcard(callback);
console.log('done');
// Make an extra reference to the callback pointer to avoid GC
process.on('exit', function() {
callback
});
DLL声明:
READIDCARD_API typedef void(*PIDDataCB)(char * pData);
READIDCARD_API int initReadIdcard(PIDDataCB func);
32位DLL下载: 链接: https://pan.baidu.com/s/1KcKlLobW_TKw8r288hlBzA 密码: g7d3
应该是编码的问题,我尝试转码,发现都不能正常打印中文,DLL是可用的,在QT中用local8bit转换可以正常显示中文
后边的英文sorry可以正常显示
另外用了解到有fastcall这个模块,写了一下,发现好像没有调用
const fastcall = require('fastcall');
const Library = fastcall.Library;
const ref = fastcall.ref;
const lib = new Library('ReadIdcard.dll')
.callback({ PIDDataCB: ['void', ['string']] })
.function({ initReadIdcard: ['int', ['PIDDataCB']] });
function callback(data) {
console.log("data: ", data);
};
lib.interface.initReadIdcard(callback);
请会的朋友帮忙看看是哪里的问题,无论是乱码或者fastcall使用,先谢谢了
1 回复
try data.toString(‘utf8’) or ‘ucs2’