0

We are using jniwrapper to communicate from JAVA with a 3rd party DLL. And the DLL wants us to pass the pointer to a callback function, as an uint64_t.

typedef struct random_struct { 
...
uint64_t callback;  //!< A function pointer to the callback
..
}

So from the jniwrapper I have tried using Void, Pointer etc to map from Java, but none of those works out. The DLL complains that the callback set up is invalid. So my question is how do I communicate the callback as an uint64_t. Has anyone used Jniwrapper for a requirement like this? Thanks

Jack
  • 11
  • 3
  • 3
    Please [edit] your question and add some background information about what you want to achieve and a [mre]. What do you want to do with the pointer that was converted/mapped to an `uint64_t`? What exactly means "none of those works out"? Do you get an error message? Or an unexpected result? Your question might be an [XY problem](https://meta.stackexchange.com/a/66378) – Bodo Oct 06 '21 at 11:22
  • 3
    That's not how a function pointer looks in C++. Not in C either. – Ted Lyngmo Oct 06 '21 at 11:33

1 Answers1

1

A proper callback function will be:

[returnType] (*[nameOftheFunctionPtr])([parameters...]);

Example:


typedef uint8_t (*dataReader_t)(uint8_t* const buffer, uint8_t length);


typedef struct random_struct { 

    dataReader_t callback;  //!< A function pointer to the callback

}


And you can have the following function which will be assigned to the callback:

uint8_t ucReadFromBuffer(uint8_t* const buffer, uint8_t length){

// do stuff ...
// return uint8_t variable

}

Then you can assign it in your code:

random_struct myStruct = {.callback = ucReadFromBuffer};

// and if you want to call it
myStruct.callback(someBuffer, length);
// similar to
ucReadFromBuffer(someBuffer, length);
S.Murtadha
  • 42
  • 10
  • So the issue is that it's a 3rd party DLL. I mean we can't make changes to their DLL. It expects a JAVA object to be communicated to a uint64_t field. – Jack Oct 13 '21 at 09:17
  • @Jack Try casting the pointer to `uint64_t`, the library may cast it back to a pointer internally since this is the only reason I'm thinking about why they'll pass a `uint64_t` instead of a proper function pointer. Example: `SomeFunctionPtr_t funcPtr; /* use it in the struct -> */ randomStructObject.callback = (uint64_t)funcPtr;` – S.Murtadha Oct 13 '21 at 12:47