[英]Java JNA u32t return value of Pointer (Memory)
I try to access a method of a C++ DLL using JNA.我尝试使用 JNA 访问 C++ DLL 的方法。
The Definition is as follows:定义如下:
u32t OpenPort(u8t valueA, char* valueB, u32t* handle);
I am not sure how to map u32t and how to get the returnValue using or Pointer or Memory maybe?我不确定如何使用 map u32t 以及如何使用或指针或 Memory 获取返回值?
I made it like this:我是这样设计的:
int OpenPort(byte valueA, String valueB, IntByReference handle); //u32t OpenPort(u8t type, char* myString, u32t* handle);
and calling并打电话
IntByReference handle = new IntByReference();
byte i = 0;
int error = myClass.OpenPort(i, "my string", handle);
System.out.println(error + " - " + handle.getValue());
The result is "0 - 0".结果是“0 - 0”。
The error "0" is fine, but the returnValue should not be 0. As this is a value I need to pass to other methods like:错误“0”很好,但 returnValue 不应该为 0。因为这是我需要传递给其他方法的值,例如:
int ClosePort(IntByReference handle); //u32t ClosePort(u32t handle);
if I then start:如果我然后开始:
error = myClass.ClosePort(handle);
The return error says that the port handle is not valid.返回错误表示端口句柄无效。
A sample c# code from the DLL maker is like this:来自 DLL 制造商的示例 c# 代码如下所示:
UInt32 handle;
UInt32 error;
error= OpenPort(0, "teststring", out handle);
xError = ClosePort(handle);
Welcome to StackOverflow.欢迎来到 StackOverflow。
The Pointer
is actually pointing to native memory where there is a 32-bit value. Pointer
实际上指向本地 memory,其中有一个 32 位值。 But mapping just to Pointer
does not tell you what's at the pointed-to location.但是仅映射到
Pointer
并不能告诉您所指向的位置是什么。
You should use the IntByReference
class to model a *uint32_t
or similar pointer to a 32-bit value.您应该使用
IntByReference
class 到 model 一个*uint32_t
或指向 32 位值的类似指针。 The method will return a pointer, but you can use the getValue()
method to retrieve the actual value you want.该方法将返回一个指针,但您可以使用
getValue()
方法来检索您想要的实际值。
Also I notice that you've used NativeLong
for the return type, but it's explicitly specified as 32-bit, so you want to use an int
.我还注意到您使用
NativeLong
作为返回类型,但它被明确指定为 32 位,因此您想使用int
。 Only use NativeLong
for a situation where long
is defined as 32-bit or 64-bit depending on the operating system bitness.仅在
long
定义为 32 位或 64 位的情况下使用NativeLong
,具体取决于操作系统位数。
Note that Java does not have a concept for signed vs. unsigned integers.请注意,Java 没有有符号整数与无符号整数的概念。 While the value will be a 32-bit
int
you will need to handle negative values in your own code by converting them to their unsigned counterparts.虽然该值将是 32 位
int
,但您需要通过将负值转换为无符号对应项来处理您自己的代码中的负值。
So your mapping should be:所以你的映射应该是:
int MethodName(byte valueA, String valueB, IntByReference returnValue);
Then call with:然后调用:
IntByReference returnValue = new IntByReference();
MethodName(valueA, ValueB, returnValue);
int theU32tValue = returnValue.getValue();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.