[英]Java JNA u32t return value of Pointer (Memory)
我嘗試使用 JNA 訪問 C++ DLL 的方法。
定義如下:
u32t OpenPort(u8t valueA, char* valueB, u32t* handle);
我不確定如何使用 map u32t 以及如何使用或指針或 Memory 獲取返回值?
我是這樣設計的:
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);
歡迎來到 StackOverflow。
Pointer
實際上指向本地 memory,其中有一個 32 位值。 但是僅映射到Pointer
並不能告訴您所指向的位置是什么。
您應該使用IntByReference
class 到 model 一個*uint32_t
或指向 32 位值的類似指針。 該方法將返回一個指針,但您可以使用getValue()
方法來檢索您想要的實際值。
我還注意到您使用NativeLong
作為返回類型,但它被明確指定為 32 位,因此您想使用int
。 僅在long
定義為 32 位或 64 位的情況下使用NativeLong
,具體取決於操作系統位數。
請注意,Java 沒有有符號整數與無符號整數的概念。 雖然該值將是 32 位int
,但您需要通過將負值轉換為無符號對應項來處理您自己的代碼中的負值。
所以你的映射應該是:
int MethodName(byte valueA, String valueB, IntByReference returnValue);
然后調用:
IntByReference returnValue = new IntByReference();
MethodName(valueA, ValueB, returnValue);
int theU32tValue = returnValue.getValue();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.