为了账号安全,请及时绑定邮箱和手机立即绑定

Java JNA u32t 指针(内存)的返回值

Java JNA u32t 指针(内存)的返回值

海绵宝宝撒 2023-08-16 09:56:11
我尝试使用 JNA 访问 C++ DLL 的方法。定义如下:u32t OpenPort(u8t valueA, char* valueB, u32t* handle);我不确定如何映射 u32t 以及如何使用指针或内存获取 returnValue?我是这样做的:int OpenPort(byte valueA, String valueB, IntByReference handle); //u32t OpenPort(u8t type, char* myString, u32t* handle);并打电话        IntByReference handle = new IntByReference();                    byte i = 0;        int error = myClass.OpenPort(i, "my string", handle);        System.out.println(error  + " - " + handle.getValue());结果是“0 - 0”。错误“0”很好,但 returnValue 不应该为 0。因为这是我需要传递给其他方法的值,例如:int ClosePort(IntByReference handle); //u32t ClosePort(u32t handle);如果我然后开始:error = myClass.ClosePort(handle);返回错误表明端口句柄无效。DLL 制造商提供的示例 C# 代码如下:UInt32 handle;UInt32 error;error= OpenPort(0, "teststring", out handle);xError = ClosePort(handle);
查看完整描述

1 回答

?
开心每一天1111

TA贡献1836条经验 获得超13个赞

Pointer实际上指向有 32 位值的本机内存。但仅映射到并Pointer不能告诉您所指向的位置有什么。


您应该使用该类IntByReference来模拟*uint32_t指向 32 位值的指针或类似指针。该方法将返回一个指针,但您可以使用该getValue()方法检索您想要的实际值。


我还注意到您已经使用了NativeLong返回类型,但它被明确指定为 32 位,因此您想要使用int. 仅用于根据操作系统位数定义为 32 位或 64 位的NativeLong情况。long


请注意,Java 没有有符号整数与无符号整数的概念。虽然该值是 32 位,但int您需要通过将负值转换为无符号对应项来处理您自己的代码中的负值。


所以你的映射应该是:


int MethodName(byte valueA, String valueB, IntByReference returnValue);

然后调用:


IntByReference returnValue = new IntByReference();

MethodName(valueA, ValueB, returnValue);

int theU32tValue = returnValue.getValue();


查看完整回答
反对 回复 2023-08-16
  • 1 回答
  • 0 关注
  • 109 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信