JNA类型映射实例__结构体中包含字符串数组

转载请注明出处,http://blog.csdn.net/rainteen

作者邮箱:rainteen@163.com


C代码中结构体定义:

typedef struct STRU_FILE_STRU
{
	char* keyStoreFile[2];
	char* keyFile[2];
}KEYSTORE_FILE_STRU;
C代码中使用此结构体的函数声明:

extern "C" __declspec(dllexport) int __stdcall writeStruct(KEYSTORE_FILE_STRU* fileStruct);
C代码中对应声明的函数声明:
int _stdcall writeStruct(KEYSTORE_FILE_STRU* fileStruct)
{
	try{
		char* keystoreFile01="D:/testOK/keystoreFile01";
		int len = ::strlen(keystoreFile01);
		::memcpy(fileStruct->keyStoreFile[0], keystoreFile01, len);

		char* keystoreFile02="D:/testOK/keystoreFile02";
		len = ::strlen(keystoreFile02);
		::memcpy(fileStruct->keyStoreFile[1], keystoreFile02, len);

		char* keyFile01="D:/testOK/keyFile01";
		len = ::strlen(keyFile01);
		::memcpy(fileStruct->keyFile[0], keyFile01, len);

		char* keyFile02="D:/testOK/keyFile02";
		len=::strlen(keyFile02);
		::memcpy(fileStruct->keyFile[1], keyFile02, len);
	}catch(char *){
		return 0;
	}
	return 1;
}


JNA映射到 Java中的结构体:

package rainteen.jnadll;

import java.util.Arrays;
import java.util.List;

import rainteen.ByteArray;

import com.sun.jna.Pointer;
import com.sun.jna.Structure;

public class KEYSTORE_FILE_STRU extends Structure {
	public ByteArray.ByReference[] keyStoreFile = new ByteArray.ByReference[2];
	public ByteArray.ByReference[] keyFile = new ByteArray.ByReference[2];

	public KEYSTORE_FILE_STRU() {
		this.setAlignType(Structure.ALIGN_DEFAULT);
		this.keyStoreFile[0] = new ByteArray.ByReference();
		this.keyStoreFile[1] = new ByteArray.ByReference();
		this.keyFile[0] = new ByteArray.ByReference();
		this.keyFile[1] = new ByteArray.ByReference();
	}

	public KEYSTORE_FILE_STRU(Pointer pointer) {
		this.setAlignType(Structure.ALIGN_DEFAULT);
		this.keyStoreFile[0] = new ByteArray.ByReference();
		this.keyStoreFile[1] = new ByteArray.ByReference();
		this.keyFile[0] = new ByteArray.ByReference();
		this.keyFile[1] = new ByteArray.ByReference();
	}

	public static class ByReference extends KEYSTORE_FILE_STRU implements
			Structure.ByReference {
	}

	public static class ByValue extends KEYSTORE_FILE_STRU implements
			Structure.ByValue {
	}

	@SuppressWarnings("unchecked")
	protected List getFieldOrder() {
		return Arrays.asList(new String[] { "keyStoreFile", "keyFile" });
	}
}

其中的字节数组自定义结构体JNA定义如下:

package rainteen;

import java.util.Arrays;
import java.util.List;

import com.sun.jna.Pointer;
import com.sun.jna.Structure;

public class ByteArray extends Structure {
	public static final int MAX_LENGTH = 512;

	public byte[] data = new byte[MAX_LENGTH];

	public static class ByReference extends ByteArray implements
			Structure.ByReference {
	}

	public static class ByValue extends ByteArray implements Structure.ByValue {
	}

	public ByteArray() {
		super();
		this.setAlignType(Structure.ALIGN_DEFAULT);
	}

	public ByteArray(Pointer pointer) {
		super(pointer);
		this.setAlignType(Structure.ALIGN_DEFAULT);
	}

	public void init(byte[] data) {
		if (data != null) {
			int minLen = (data.length > MAX_LENGTH) ? MAX_LENGTH : data.length;
			System.arraycopy(data, 0, this.data, 0, minLen);
		}
	}

	public byte[] getData() {
		return this.data;
	}

	@SuppressWarnings("unchecked")
	protected List getFieldOrder() {
		return Arrays.asList(new String[] { "data" });
	}
}


Java中调用C函数的方法示例:

	@Test
	public void testWriteStruct() {
		KEYSTORE_FILE_STRU.ByReference fileStruct = new KEYSTORE_FILE_STRU.ByReference();

		int res = this.jnaDll.writeStruct(fileStruct);
		fileStruct.autoRead();
		byte[] data0 = fileStruct.keyStoreFile[0].getData();
		System.out.println(TypeUtil.toString(data0));
		byte[] data1 = fileStruct.keyStoreFile[1].getData();
		System.out.println(TypeUtil.toString(data1));

		byte[] key0 = fileStruct.keyFile[0].getData();
		System.out.println(TypeUtil.toString(key0));
		byte[] key1 = fileStruct.keyFile[1].getData();
		System.out.println(TypeUtil.toString(key1));

		Assert.assertTrue(res == 1);
	}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值