Mapping pointer to UInt64

195 Views Asked by At

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

1

There are 1 best solutions below

2
On

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);