MqxM
Denoβ€’2mo agoβ€’
11 replies
Mqx

FFI callback with multiple parameters

Hey could someone please help me with this interface definition:

MODULE_API auto getDevices(
  void (*callback_fn)(
      const char *name,
      const char *path,
      const char *manufacturer,
      const char *serial_number
  ),
  ErrorCallbackT error_callback = nullptr
) -> int;
const getDevices : Deno.ForeignFunction = {
  parameters: [
    'function',
    'function'
  ],
  result: 'i32',
  nonblocking: true
}


const {symbols} = Deno.dlopen('', {
  getDevices
})

await symbols.getDevices(
  (
    name,
    path,
    manufacturer,
    serial_number
  ) => {
    devices.push({
      name,
      path,
      manufacturer,
      serial_number
    });
  },
  (error) => {console.log(error);}
)

console.log(devices);


I would like to know on how to properly define my FFI interface for that kind of C++ function. I want to understand how this works correctly, because I think my current approach is completely wrong πŸ˜…

I want to transmit kind of an β€žobjectβ€œ from C++ to TypeScript. And I read somewhere that this is the preferred method, using a callback function.

Thanks 😊
Was this page helpful?