C语言定义
struct SerialNum
{
int id;
char msg[32];
};
struct Staff
{
SerialNum * pSNs;
int nCount;
};
java 调用,
把结构体用class 调用
//包含字符数组的结构
public static class SerialNum extends Structure
{
public static class ByReference extends SerialNum implements Structure.ByReference {};
public static class ByValue extends SerialNum implements Structure.ByValue{};
public int id;
public byte msg[] = new byte[32];
public SerialNum(){
this.allocateMemory();
}
public int setMSG(String str){
return StringToFixedBytes(str, msg);
}
public String getMsg(){
return BytesToString(msg);
}
//用于带数组指针的结构
public static class Staff extends Structure{
public static class ByReference extends Staff implements Structure.ByReference {};
public SerialNum.ByReference pSN;
public int nCount;
public Staff() {nCount=0;}
public SerialNum.ByReference[] toArray() {
return (SerialNum.ByReference[]) pSN.toArray(nCount);
}
}
调用的时候:
SerialNum sn = new SerialNum();
sn.id = 9000;
sn.msg = "192.168.1.252".getBytes();
// sn.setMSG("abc你好");
DllInterface.INSTANCE.AssignSN(sn);
System.out.println(sn.id + ", " + sn.getMsg());
调用的时候
System.out.println("---------9.带数组指针的结构-----------");
Staff.ByReference pStaff9 = new Staff.ByReference();
DllInterface.INSTANCE.GetStaff(pStaff9);
SerialNum.ByReference [] sns9 = pStaff9.toArray();
for(int i=0; i<pStaff9.nCount; i++){
System.out.println(sns9[i].getMsg());
}
DllInterface.INSTANCE.FreeMemory(pStaff9.pSN.getPointer());
参考:
https://blog.csdn.net/gwd1154978352/article/details/55097376
Java 类型 | C 类型 | 原生表现 | |
boolean | int | 32位整数(可定制) | |
byte | char | 8位整数 | |
char | wchar_t | 平台依赖 | |
short | short | 16位整数 | |
int | int | 32位整数 | |
long | long long, __int64 | 64位整数 | |
float | float | 32位浮点数 | |
double | double | 64位浮点数 | |
Buffer/Pointer | pointer | 平台依赖(32或64位指针) | |
<T>[] (基本类型的数组) | pointer/array | 32或64位指针(参数/返回值) 邻接内存(结构体成员) | |
String | char* | /0结束的数组 (native encoding or jna.encoding) | |
WString | wchar_t* | /0结束的数组(unicode) | |
String[] | char** | /0结束的数组的数组 | |
WString[] | wchar_t** | /0结束的宽字符数组的数组 | |
Structure | struct*/struct | 指向结构体的指针(参数或返回值) (或者明确指定是结构体指针)结构体(结构体的成员) (或者明确指定是结构体) | |
Union | union | 等同于结构体 | |
Structure[] | struct[] | 结构体的数组,邻接内存 | |
Callback | <T> (*fp)() | Java函数指针或原生函数指针 | |
NativeMapped | varies | 依赖于定义 | |
NativeLong | long | 平台依赖(32或64位整数) | |
PointerType | pointer | 和Pointer相同 |