Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node-ffi code convert to fastcall #30

Open
sincerefly opened this issue Apr 10, 2018 · 0 comments
Open

node-ffi code convert to fastcall #30

sincerefly opened this issue Apr 10, 2018 · 0 comments

Comments

@sincerefly
Copy link

sincerefly commented Apr 10, 2018

Hello, My DLL declare:

READIDCARD_API typedef void(*PIDDataCB)(char * pData);   
READIDCARD_API int initReadIdcard(PIDDataCB func);

and with node-ffi, I can call it with:

const ffi = require('ffi');

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");
ReadIdcard.initReadIdcard(callback);
console.log('done');

// Make an extra reference to the callback pointer to avoid GC
process.on('exit', function() {
  callback
});

now, I hope use fastcall rewrite it, and read the doc, I load DLL, but don't know how to callback, maybe I can use ffi.Callback to create a callback function, But without node-ffi, Is there a way only with fastcall to use callback

const fastcall = require('fastcall');
const Library = fastcall.Library;
const ffi = fastcall.ffi;

const lib = new Library('ReadIdcard.dll')

lib.callback({ PIDDataCB: ['void', ['string']] })
   .function({ initReadIdcard: ['int', ['PIDDataCB']] });

let callback = ffi.Callback('void', ['string'],
  function(data) {
    console.log(data)
});

lib.interface.initReadIdcard(callback);

error:

        throw new TypeError('Cannot make callback from: ' + value);
        ^

TypeError: Cannot make callback from: [object Object]
    at FastCallback.makePtr (E:\code\node-project\node___callback\node_modules\fastcall\lib\FastCallback.js:82:15)
    at Ctx.(anonymous function).value [as argSetter0] (E:\code\node-project\node___callback\node_modules\fastcall\lib\Fa
stFunction.js:130:52)
    at Ctx.eval (eval at _makeSyncFunction (E:\code\node-project\node___callback\node_modules\fastcall\lib\FastFunction.
js:159:25), <anonymous>:3:26)
    at Object.func [as initReadIdcard] (E:\code\node-project\node___callback\node_modules\fastcall\lib\FastFunction.js:1
66:30)
    at Object.<anonymous> (E:\code\node-project\node___callback\run.js:27:15)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Function.Module.runMain (module.js:684:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3

thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant