转载请注明出处,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" });
}
}
@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);
}