【问题标题】:Mapping pointer to UInt64将指针映射到 UInt64
【发布时间】:2021-11-26 13:15:52
【问题描述】:

我们正在使用 jniwrapper 从 JAVA 与第 3 方 DLL 进行通信。 DLL 希望我们将指针作为 uint64_t 传递给回调函数。

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

因此,从jniwrapper 开始,我尝试使用 Void、Pointer 等从 Java 映射,但这些都不起作用。 DLL 抱怨回调设置无效。所以我的问题是如何将回调作为 uint64_t 进行通信。 有没有人使用 Jniwrapper 来满足这样的需求? 谢谢

【问题讨论】:

  • edit您的问题,并添加一些有关您想要实现的目标的背景信息和minimal reproducible example。你想对转换/映射到uint64_t 的指针做什么? “这些都不行”到底是什么意思?您收到错误消息吗?还是意想不到的结果?您的问题可能是XY problem
  • 这不是函数指针在 C++ 中的样子。也不在 C 中。

标签: java c++ c java-native-interface jniwrapper


【解决方案1】:

适当的回调函数是:

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

例子:


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

}


您可以拥有以下将分配给回调的函数:

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

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

}

然后你可以在你的代码中分配它:

random_struct myStruct = {.callback = ucReadFromBuffer};

// and if you want to call it
myStruct.callback(someBuffer, length);
// similar to
ucReadFromBuffer(someBuffer, length);

【讨论】:

  • 所以问题在于它是第 3 方 DLL。我的意思是我们不能更改他们的 DLL。它期望一个 JAVA 对象与一个 uint64_t 字段进行通信。
  • @Jack 尝试将指针转换为uint64_t,库可能会在内部将其转换回指针,因为这是我考虑为什么他们会传递uint64_t 的唯一原因一个适当的函数指针。示例:SomeFunctionPtr_t funcPtr; /* use it in the struct -&gt; */ randomStructObject.callback = (uint64_t)funcPtr;
猜你喜欢
  • 2011-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多