Calling C function from returned pointer in NodeJS

Jardel Candido

I'm trying to call a function in NodeJS that was returned as a pointer by a function called via ffi. I'm getting segmentation fault. What am I doing wrong?

The ffi imported function gets a pointer to char as argument (that informs to what function the returned pointer should point to) and then return a pointer to void. In ffi.Library declaration, I've tried many return types, I.E.: ref.types.void; ref.reftype(ref.types.void) and now I'm using ref.refType(getDevList), that is the type of my function.

C Function:

void *GetFunctionAddress(const char* fctName)
{
  void *address;
  ...
  return address;
}

NodeJS code:

var ffi = require('ffi');
var Gateway;

var DeviceList = StructType({
    deviceNumber: ref.types.uint16,
}) 
DeviceList.defineProperty('device', 
ArrayType(ref.refType(DeviceList)))

var deviceListPtr = ref.refType(DeviceList);
var getDevList = ffi.Function(deviceListPtr, []);

Gateway = ffi.Library('./GatewayManager.so', {

  'InitializeGateway': ['bool',['void']],
  'ReloadConfig' : ['void',['void']],
  'ReadPar' : ['uint',['uint','uint','uint', dwordType]],
  'WritePar' : ['uint',['uint','uint','uint', dwordType]],
  'GetDatablockAddress' : [wordType,['uint','uint','uint']],
  'GetFunctionAddress' :  [ref.refType(getDevList),['char *']]

});

var retPtr =                 
Gateway.GetFunctionAddress(ref.allocCString('GetDevList'));
var GetDevList = retPtr.deref();
var DeviceListPtr = GetDevList();
res.send(DeviceListPtr);
console.log(DeviceListPtr);

I'm getting segmentation fault when the following line is in code:

var DeviceListPtr = GetDevList();
Tiller Belotti Júnior

You do not need ref.refType() in the following line:
'GetFunctionAddress' : [ref.refType(getDevList),['char *']]
Change it to:
'GetFunctionAddress' : [getDevList,['char *']]
It should work.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C - values is struct pointer are not set after pointer is returned from function

From Dev

c++ Calling a function from a class pointer

From Dev

c - using a pointer returned from function in a function call

From Dev

Calling C function with array pointer and int pointer from Swift

From Dev

Assigning a pointer returned from a function

From Dev

unexpected integer returned from a C function by calling it using ctypes

From Dev

Pointer is being being masked when calling a C function from Fortran

From Dev

Calling a C style function pointer in a WebAssembly from JavaScript

From Dev

Calling a function from a funtion pointer in a map C++

From Dev

Calling function in C dll from C# which returns somekind of pointer to function pointer

From Dev

Calling nodeJS from angularJS function

From

Assign value returned from function to pointer

From Javascript

Calling a JavaScript function returned from an Ajax response

From Java

Calling a function when an activity is returned to from fragment

From Dev

Calling a function from a function pointer in an iterator

From Dev

C++: Calling a function on a returned class

From Dev

Calling function from dll with pointer not working

From Dev

C: malloc'ed pointer changes location after being returned from function?

From Dev

Calling C++ DLL function with pointer

From Dev

Calling pointer-to-member function C++

From Dev

Compiling error when calling a function pointer in C

From Dev

Allocation function and pointer to returned

From Dev

Calling a returned function

From Dev

pointer being returned from a function from an SDL library

From Dev

NodeJs Javascript Calling a Function Dynamically From an Array

From Dev

calling websocket send function from within .then() in nodejs

From Dev

Calling to a pointer from a c++ thread

From Dev

Calling a C function from Julia and passing a 2D array as a pointer of pointers as argument

From

Null pointer whenever referencing a slice or map returned from a function

Related Related

  1. 1

    C - values is struct pointer are not set after pointer is returned from function

  2. 2

    c++ Calling a function from a class pointer

  3. 3

    c - using a pointer returned from function in a function call

  4. 4

    Calling C function with array pointer and int pointer from Swift

  5. 5

    Assigning a pointer returned from a function

  6. 6

    unexpected integer returned from a C function by calling it using ctypes

  7. 7

    Pointer is being being masked when calling a C function from Fortran

  8. 8

    Calling a C style function pointer in a WebAssembly from JavaScript

  9. 9

    Calling a function from a funtion pointer in a map C++

  10. 10

    Calling function in C dll from C# which returns somekind of pointer to function pointer

  11. 11

    Calling nodeJS from angularJS function

  12. 12

    Assign value returned from function to pointer

  13. 13

    Calling a JavaScript function returned from an Ajax response

  14. 14

    Calling a function when an activity is returned to from fragment

  15. 15

    Calling a function from a function pointer in an iterator

  16. 16

    C++: Calling a function on a returned class

  17. 17

    Calling function from dll with pointer not working

  18. 18

    C: malloc'ed pointer changes location after being returned from function?

  19. 19

    Calling C++ DLL function with pointer

  20. 20

    Calling pointer-to-member function C++

  21. 21

    Compiling error when calling a function pointer in C

  22. 22

    Allocation function and pointer to returned

  23. 23

    Calling a returned function

  24. 24

    pointer being returned from a function from an SDL library

  25. 25

    NodeJs Javascript Calling a Function Dynamically From an Array

  26. 26

    calling websocket send function from within .then() in nodejs

  27. 27

    Calling to a pointer from a c++ thread

  28. 28

    Calling a C function from Julia and passing a 2D array as a pointer of pointers as argument

  29. 29

    Null pointer whenever referencing a slice or map returned from a function

HotTag

Archive