# native中的struct

``````typedef struct _Point {
int x, y;
} Point;
``````

struct的使用有两种情况，一种是值传递，一种是引用传递。先来看下这两种情况在native方法中是怎么使用的：

``````Point* translate(Point* pt, int dx, int dy);
``````

``````Point translate(Point pt, int dx, int dy);
``````

# Structure

``````class Point extends Structure { public int x, y; }
Point translate(Point pt, int x, int y);
...
Point pt = new Point();
Point result = translate(pt, 100, 100);
``````

``````class Point extends Structure {
public static class ByValue extends Point implements Structure.ByValue { }
public int x, y;
}
Point.ByValue translate(Point.ByValue pt, int x, int y);
...
Point.ByValue pt = new Point.ByValue();
Point result = translate(pt, 100, 100);
``````

Structure内部提供了两个interface,分别是ByValue和ByReference:

``````public abstract class Structure {

public interface ByValue { }

public interface ByReference { }
``````

# 特殊类型的Structure

## 结构体数组作为参数

``````void get_devices(struct Device[], int size);
``````

``````int size = ...
Device[] devices = new Device[size];
lib.get_devices(devices, devices.length);
``````

## 结构体数组作为返回值

``````struct Display* get_displays(int* pcount);
void free_displays(struct Display* displays);
``````

get_displays方法返回的是一个指向结构体数组的指针，pcount是结构体的个数。

``````Display get_displays(IntByReference pcount);
void free_displays(Display[] displays);
``````

``````IntByReference pcount = new IntByReference();
Display d = lib.get_displays(pcount);
Display[] displays = (Display[])d.toArray(pcount.getValue());
...
lib.free_displays(displays);
``````

## 结构体中的结构体

``````typedef struct _Point {
int x, y;
} Point;

typedef struct _Line {
Point start;
Point end;
} Line;
``````

``````class Point extends Structure {
public int x, y;
}

class Line extends Structure {
public Point start;
public Point end;
}
``````

``````typedef struct _Line2 {
Point* p1;
Point* p2;
} Line2;
``````

``````class Point extends Structure {
public static class ByReference extends Point implements Structure.ByReference { }
public int x, y;
}
class Line2 extends Structure {
public Point.ByReference p1;
public Point.ByReference p2;
}
``````

``````class Line2 extends Structure {
public Pointer p1;
public Pointer p2;
}

Line2 line2;
Point p1, p2;
...
line2.p1 = p1.getPointer();
line2.p2 = p2.getPointer();
``````

## 结构体中的数组

``````typedef struct _Buffer {
char buf1[32];
char buf2[1024];
} Buffer;
``````

``````class Buffer extends Structure {
public byte[] buf1 = new byte[32];
public byte[] buf2 = new byte[1024];
}
``````

``````typedef struct _Header {
int flags;
int buf_length;
char buffer[1];
``````

``````class Header extends Structure {
public int flags;
public int buf_length;
public byte[] buffer;
buffer = new byte[bufferSize];
buf_length = buffer.length;
allocateMemory();
}
}
``````

## 结构体中的可变字段

``````class Data extends com.sun.jna.Structure {
public volatile int refCount;
public int value;
}
...
Data data = new Data();
``````

## 结构体中的只读字段

``````class ReadOnly extends com.sun.jna.Structure {
public final int refCount;
{
// 初始化
refCount = -1;
// 从内存中读取数据
}
}
``````

# 总结

原文作者：flydean
原文地址: https://www.cnblogs.com/flydean/p/16249687.html
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。