系统、开发环境:win7、eclipse 32位、jdk 32位、jre 32位
由于这里使用的dll文件是32位的,而我本身的环境是64位的,包括eclipse、jdk、jre都是64位,所以这里需要开发环境共存(32位、64位共存),如果本来就是32位环境就不用重新搭建环境了。从以下连接分别下载32位软件:
1.eclipse,不用安装,解压后即可使用,解压目录:D:\eclipseEclipse IDE for Java EE Developers, 247 MB Windows 32 Bit
http://www.eclipse.org/downloads
Windows x86 123.49MB jdk-7u45-windows-i586.exe
http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html
Windows Offline(32-bit) fileSize: 27.7MB
http://www.java.com/en/download/manual.jsp
至于JNA是什么,它本身是基于JNI技术的,然后进行封装以方便dll调用。这里不再详述,网上很多。
int System_Init (int version, char* encoding);
import com.sun.jna.Library;
import com.sun.jna.Native;
public class getSDK {
public interface Function extends Library {
Function instanceDll = (Function)Native.loadLibrary("libSDK.dll",Function.class);
public int System_Init(int version, String encoding);
}
}
result = getSDK.Function.instanceDll.System_Init(1, "");
typedef void (*Callback_Status)(char *station_id, char *station_ip, int status);
int Start_Server(char *ip, int port, Callback_Status fun);
import com.sun.jna.win32.StdCallLibrary.StdCallCallback;
public class getSDK {
public interface Callback_Status extends StdCallCallback {
public void Status(String station_id, String station_ip, int status);
}
}
public class getSDK {
public static class Status_Realize implements Callback_Status{
public void Status(String station_id, String station_ip, int status) {
if (status == 2) {
GUI.Station_online(station_id, station_ip, 1, 0);
} else if (status == 3) {
GUI.Station_online(station_id, station_ip, 2, 0);
}
}
}
}
public static getSDK.Callback_Status callback_status = new getSDK.Status_Realize();
result = getSDK.Function.instanceDll.Start_Server("0.0.0.0", 1234, callback_status);
typedef struct result_data {
char Station_id[3];
char Label_id[7];
int Signal;
int Power;
int Status;
}Tstatus, *PTstatus;
typedef void (*Callback_Data)(char *station_id, char *station_ip, Tstatus ret_data[], int count);
int Start_Server (char *server_ip, int listen_port, Callback_Status callback_status, Callback_Data callback_data);
import com.sun.jna.Structure;
public class getSDK {
public interface Function extends Library {
public static class Tstatus extends Structure {
public byte[] Station_id = new byte[3];
public byte[] Label_id = new byte[7];
public int Signal;
public int Power;
public int Status;
public static class ByReference extends Tstatus implements
Structure.ByReference {}
public static class ByValue extends Tstatus implements
Structure.ByValue {}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
protected List getFieldOrder() {
List a = new ArrayList();
a.add("Station_id");
a.add("Label_id");
a.add("Signal");
a.add("Power");
a.add("Status");
return a;
}
}
public int Start_Server(String server_ip, int port, getSDK.Callback_Status Callback_Status,getSDK.Callback_Data Callback_Data);
}
public interface Callback_Data extends StdCallCallback {
public void Data(String station_id, String station_ip, Pointer data, int count);
}
}
public class getSDK {
public static class Data_Realize implements Callback_Data{
public void Data(String station_id, String station_ip, Pointer data, int count) {
int srow, frow;
srow = 0;
frow = 0;
for (int i = 0; i < count; i++) {
byte[] stationbuf = new byte[2];
byte[] labelbuf = new byte[6];
int[] signal = new int[1];
int[] power = new int[1];
int[] status = new int[1];
data.read(0 + i * 24, stationbuf, 0, 2);
data.read(3 + i * 24, labelbuf, 0, 6);
data.read(12 + i * 24, signal, 0, 1);
data.read(16 + i * 24, power, 0, 1);
data.read(20 + i * 24, status, 0, 1);
String labelID = new String(labelbuf);
String stationID = new String(stationbuf);
GUI.message.append(String.valueOf(power[0]));
GUI.message.append(String.valueOf(signal[0]));
GUI.message.append(String.valueOf(status[0]));
}
}
}
}
public static getSDK.Callback_Status callback_status = new getSDK.Status_Realize();
public static getSDK.Callback_Data callback_data = new getSDK.Data_Realize();
result = getSDK.Function.instanceDll.Start_Server("0.0.0.0", 1234, callback_status, callback_data);
typedef unsigned int UINT;
typedef struct code_word {
char Feild[8];
BOOL Inverse;
UINT Length;
CHAR Payload[255];
}Cword, *pCword;
typedef struct label_data {
char Label_id[7];
UINT Count;
Cword NCword[50];
}Ldata, *pLdata;
int Start_Update (char *station_id, int count, Ldata data[]);
public class getSDK {
public interface Function extends Library {
public static class Cword extends Structure {
public byte[] Feild = new byte[8];
public boolean Inverse;
public int length;
public byte[] Payload = new byte[255];
public static class ByValue extends Cword implements
Structure.ByValue {}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
protected List getFieldOrder() {
List a = new ArrayList();
a.add("Feild");
a.add("X_coord");
a.add("Y_coord");
a.add("Font");
a.add("Inverse");
a.add("length");
a.add("Payload");
return a;
}
}
public static class Ldata extends Structure {
public byte[] Label_id = new byte[7];
public int Count;
public Cword.ByValue[] NCword = new Cword.ByValue[50];
public static class ByReference extends Ldata implements
Structure.ByReference {
}
public static class ByValue extends Ldata implements
Structure.ByValue {
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
protected List getFieldOrder() {
List a = new ArrayList();
a.add("Label_id");
a.add("Patten");
a.add("Count");
a.add("NCword");
return a;
}
}
public int Start_Update(String station_id, int count, Ldata.ByValue[] data);
}
}
public class GUI {
public static int labelcount = 5;
public static String[] labellist = new String[]{"000834","000835","041908",
"041909","04190A","04190B","04190C","04190D","04190E","04190F","041910",
"041911","041912","041913","041914","041915","041916","041917","041918"};
bsend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int i;
int result;
getSDK.Function.Ldata.ByValue tdata = new getSDK.Function.Ldata.ByValue();
getSDK.Function.Ldata.ByValue[] data = (getSDK.Function.Ldata.ByValue[])tdata.toArray(labelcount);
for (i = 0; i < labelcount; i++) {
data[i].Label_id = labellist[i].getBytes();
}
for (i = 0; i < labelcount; i++) {
data[i].Count = 4;
}
getSDK.Function.Cword.ByValue cdata = new getSDK.Function.Cword.ByValue();
getSDK.Function.Cword.ByValue[] ctdata = (getSDK.Function.Cword.ByValue[])cdata.toArray(50);
ctdata[0].Feild = "Text".getBytes();
ctdata[0].Inverse = false;
ctdata[0].length = "test".getBytes().length;
ctdata[0].Payload = "test".getBytes();
ctdata[1].Feild = "Text".getBytes();
ctdata[1].Inverse = false;
ctdata[1].length = "hello".getBytes().length;
ctdata[1].Payload = "hello".getBytes();
ctdata[2].Feild = "Text".getBytes();
ctdata[2].Inverse = false;
ctdata[2].length = "单位".getBytes().length;
ctdata[2].Payload = "单位".getBytes();
ctdata[3].Feild = "Text".getBytes();
ctdata[3].Inverse = false;
ctdata[3].length = "规格".getBytes().length;
ctdata[3].Payload = "规格".getBytes();
for (i = 0; i < labelcount; i++) {
data[i].NCword = ctdata;
}
result = getSDK.Function.instanceDll.Start_Update((String)station.getSelectedItem(), labelcount, data);
}
});
}
getSDK.Function.Ldata.ByValue[] tdata = new getSDK.Function.Ldata.ByValue[labelcount];
的方式,得到的内存是不连续的,当你传递这种方式定义的变量给dll时,dll只能取到第一个结构体元素的值,造成数据丢失。