C:
					
typedef struct Example7Struct {
	int val;
} Example7Struct;
...
void example7_getStructArray(Example7Struct** ppVals, int* pNumVals)
{
	*pNumVals = 3;
	*ppVals = (Example7Struct*)malloc(sizeof(Example7Struct) * 3);
	memset(*ppVals, 0, sizeof(Example7Struct) * 3);
	(*ppVals)[0].val = 1;
	(*ppVals)[1].val = 2;
	(*ppVals)[2].val = 3;
}

void example7_cleanupStructArray(Example7Struct* pMem)
{
	printf("(C) cleaning up memory...\n");
	free(pMem);
}
					
				
Java:
					
public interface CLibrary extends Library {
	public static class Example7Struct extends Structure {
		public static class ByReference extends Example7Struct implements Structure.ByReference {}
		
		public int val;
		
		public Example7Struct() {}
		public Example7Struct(Pointer p) {
			super(p);
		}
	}
	...
	public void example7_getStructArray(PointerByReference valsRef, IntByReference numValsRef);
	public void example7_cleanupStructArray(Pointer p);
}
...
CLibrary clib = (CLibrary)Native.loadLibrary("testlib", CLibrary.class);
...
PointerByReference valsRefPtr = new PointerByReference();
IntByReference numValsRef = new IntByReference();
clib.example7_getStructArray(valsRefPtr, numValsRef);
int numVals = numValsRef.getValue();
// 获取指针值
Pointer pVals = valsRefPtr.getValue();
// 指针内存读入到Example7Struct
CLibrary.Example7Struct valsRef = new CLibrary.Example7Struct(pVals);
valsRef.read();
// Structure.toArray 复制数据块的内存
CLibrary.Example7Struct[] e7vals = (CLibrary.Example7Struct[])valsRef.toArray(numVals);
System.out.println("example 7: retrieved " + numVals + " values:");
for (CLibrary.Example7Struct e7val : e7vals) {
	System.out.println("\t" + e7val.val);
}
// call C code to clean up memory allocated in C
System.out.println("\t(example 7: cleanup)");
clib.example7_cleanupStructArray(pVals);
//www.sitcoder.com



	
色迷迷 哭 呕吐 大笑 口水 微笑 啵一个 发怒

Hi,您需要填写昵称和邮箱!

  • 必填项
  • 必填项