简体   繁体   English

映射指针到 UInt64

[英]Mapping pointer to UInt64

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.我们正在使用 jniwrapper 从 JAVA 与第三方 DLL 进行通信。DLL 希望我们将指针作为 uint64_t 传递给回调 function。

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.因此,我尝试从jniwrapper使用 Void、Pointer 等从 Java 到 map,但这些都没有用。 The DLL complains that the callback set up is invalid. DLL 投诉回调设置无效。 So my question is how do I communicate the callback as an uint64_t.所以我的问题是如何将回调作为 uint64_t 进行通信。 Has anyone used Jniwrapper for a requirement like this?有没有人使用 Jniwrapper 来满足这样的要求? Thanks谢谢

A proper callback function will be:正确的回调 function 将是:

[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:您可以将以下 function 分配给回调:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM