Java使用JNA來調用C++,需要返回String,有三種方式。
第一種:簡單粗暴,自由散漫
/// C定義接口(字符串當作參數傳入,返回結果也由此傳出):
void getString( char* pRetStr);
/// Java聲明接口
void getString( Pointer strPointer);
/// Java調用接口
Pointer strPointer = new Memory( 1024 );
clib.getString( strPointer );
缺點:不知道new多大的Memory,少了不夠,多了浪費。于是有了第二種方法。
第二種:兩段式調用,同一個函數調用兩次。第一次獲取到需要分配多大內存,new 出對應大小的Memory,再調用一次得到結果。以下代碼參考下面文章
Java 調用 C/C++ 之 JNA 系列實戰篇 —— 輸出char * (六)
/// C定義接口
/// result:返回結果 resultLength:返回字符串的長度
int getString( char* result, int * resultLength);
/// Java聲明接口
int getString( POinter result, IntByReference resultLength);
/// Java調用接口
Pointer resultv = Pointer.NULL; //char* resul 指定一個空指針
IntByReference resultLength = new IntByReference(); //int* resultlength
//調用兩次,第一次獲取結果的長度
//第二次再根據長度去獲取字節數組
int getVersionInfo = CFJna.library.GetVersionInfo(resultv, resultLength);
System.out.println("getVersionInfo:"+getVersionInfo);
if(getVersionInfo == SUCCESS) {//表示獲取成功
//第一次獲取結果的長度
int vesionLen = resultLength.getValue();
System.out.println("versionLen:"+vesionLen);
//第二次再根據長度去獲取字節數組
resultv = new Memory(vesionLen);
getVersionInfo = CFJna.library.GetVersionInfo(resultv, resultLength);
System.out.println("getVersionInfo:"+getVersionInfo);
//獲取字節數組
byte[] byteArray = resultv.getByteArray(0, vesionLen);
//轉化成字符
System.out.println(new String(byteArray,"GB2312"));
}
缺點:需要調用兩次,C++要保證兩次結果返回一致,效率比較低。
第三種:自我管理,自己管理內存。在C里面malloc內存,java調用完函數,手動再調用C++的free函數
/// C定義接口(字符串當作參數傳入):
void example2_getString(char** ppszVal)
{
*ppszVal = (char*)malloc(sizeof(char) * 6);
memset(*ppszVal, 0, sizeof(char) * 6);
strcpy(*ppszVal, "hello");
}
void example2_cleanup(char* pszVal)
{
free(pszVal);
}
/// Java聲明接口
public void example2_getString(PointerByReference val);
public void example2_cleanup(Pointer p);
/// Java調用接口
// get string from C
// allocate a void**
final PointerByReference ptrRef = new PointerByReference();
// call the C function
clib.example2_getString(ptrRef);
// extract the void* that was allocated in C
final Pointer p = ptrRef.getValue();
// extract the null-terminated string from the Pointer
final String val = p.getString(0);
System.out.println("example 2b: " + val);
// clean up memory allocated in C
clib.example2_cleanup(p);
缺點:調用完接口,需要自己手動釋放。
沒有銀彈,自己挑選!