我有下面的配置,我試圖創建一個測試C函數返回一個指向字符串數組的指針,然後使用SWIG的carrays .i和array_functions,以便我可以訪問Java中的Array元素。我不確定哪種%array_class或%array_functions最適合這種情況。此示例是一個構建塊,用於封裝返回動態創建的數組的C函數。SWIG和Java使用carrays.i和array_functions爲C數組字符串
不確定性:
- %array_functions(炭,SWIGArrayUtility); - 不確定字符是否正確
- 內嵌字符* getCharArray() - 不確定C函數簽名是否正確
- String result = getCharArray(); - 字符串返回看起來很奇怪,但這就是SWIG生成的內容
- 不確定是否內聯char * getCharArray()創建一個適合包裝的結構數組。
SWIG.i:
%module Test
%{
#include "test.h"
%}
%include <carrays.i>
%array_functions(char, SWIGArrayUtility);
%include "test.h"
%pragma(java) modulecode=%{
public static char[] getCharArrayImpl() {
final int num = numFoo();
char ret[] = new char[num];
String result = getCharArray();
for (int i = 0; i < num; ++i) {
ret[i] = SWIGArrayUtility_getitem(result, i);
}
return ret;
}
%}
內聯部首C功能:
#ifndef TEST_H
#define TEST_H
inline static unsigned short numFoo() {
return 3;
}
inline char *getCharArray(){
static char* foo[3];
foo[0]="ABC";
foo[1]="5CDE";
foo[2]="EEE6";
return foo;
}
#endif
爪哇主測試儀:
public class TestMain {
public static void main(String[] args) {
System.loadLibrary("TestJni");
char[] test = Test.getCharArrayImpl();
System.out.println("length=" + test.length);
for(int i=0; i < test.length; i++){
System.out.println(test[i]);
}
}
}
Java的主要測試儀輸出:
length=3
?
?
,
痛飲生成的Java API:
public class Test {
public static String new_SWIGArrayUtility(int nelements) {
return TestJNI.new_SWIGArrayUtility(nelements);
}
public static void delete_SWIGArrayUtility(String ary) {
TestJNI.delete_SWIGArrayUtility(ary);
}
public static char SWIGArrayUtility_getitem(String ary, int index) {
return TestJNI.SWIGArrayUtility_getitem(ary, index);
}
public static void SWIGArrayUtility_setitem(String ary, int index, char value) {
TestJNI.SWIGArrayUtility_setitem(ary, index, value);
}
public static int numFoo() {
return TestJNI.numFoo();
}
public static String getCharArray() {
return TestJNI.getCharArray();
}
public static char[] getCharArrayImpl() {
final int num = numFoo();
char ret[] = new char[num];
String result = getCharArray();
System.out.println("result=" + result);
for (int i = 0; i < num; ++i) {
ret[i] = SWIGArrayUtility_getitem(result, i);
System.out.println("ret[" + i + "]=" + ret[i]);
}
return ret;
}
}
感謝幫幫我! – c12 2012-03-23 17:40:10