我嘗試使用 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();
添加回答
舉報
0/150
提交
取消