我使用JNA运行dll函数:
以下是与该方式对应的所有代码:
原生声明:
//how the method declared
H264_Login (char *sIP, unsigned short wPort, char *sUserName, char *sPassword, LP_DEVICEINFO lpDeviceInfo, int *error, ,SocketStyle socketTyle=TCPSOCKET); // where LP_DEVICEINFO is a struct
//how the struct declared
typedef struct _H264_DVR_DEVICEINFO
{
SDK_SYSTEM_TIME tmBuildTime; // the "SDK_SYSTEM_TIME" is another struct
char sSerialNumber[64];
int byChanNum;
unsigned int uiDeviceRunTime;
SDK_DeviceType deviceTye; // the "SDK_DeviceType" is a enum
}H264_DVR_DEVICEINFO,*LP_DEVICEINFO;
// this is how "SDK_SYSTEM_TIME" is defined
typedef struct SDK_SYSTEM_TIME{
int year;
int month;
int day;
}SDK_SYSTEM_TIME;
// this is how "SDK_DeviceType" is defined
enum SDK_DeviceType
{
SDK_DEVICE_TYPE_DVR,
SDK_DEVICE_TYPE_MVR,
SDK_DEVICE_TYPE_NR
};
// this is how "SocketStyle" is defined
enum SocketStyle
{
TCPSOCKET=0,
UDPSOCKET,
SOCKETNR
};
以下是相应的Java映射:
public class Test implements StdCallLibrary {
public interface simpleDLL extends StdCallLibrary {
long H264_Login(String sIP, short wPort, String sUserName, String sPassword,
Structure DeviceDate, int error, int TCPSOCKET);
}
static
{
System.loadLibrary("NetSdk");
}
// the struct implementation
public static class DeviceDate extends Structure{
public SDK_SYSTEM_TIME tmBuildTime;
public String sSerialNumber;
public IntByReference byChanNum;
public IntByReference uiDeviceRunTime;
public IntByReference deviceTpye;
@Override
protected List<Object> getFieldOrder() {
List<Object> list = new ArrayList<>();
list.add("tmBuildTime");
list.add("sSerialNumber");
list.add("byChanNum");
list.add("uiDeviceRunTime");
list.add("deviceTpye");
return list;
}
}
public static class SDK_SYSTEM_TIME extends Structure{
public IntByReference year;
public IntByReference month;
public IntByReference day;
@Override
protected List<Object> getFieldOrder() {
List<Object> list = new ArrayList<>();
list.add("year");
list.add("month");
list.add("day");
return list;
}
}
// and then how I called it through the main function
public static void main(String args[]) throws FileNotFoundException{
simpleDLL INSTANCE = (simpleDLL) Native.loadLibrary( ("NetSdk"), simpleDLL.class);
DeviceDate dev = new DeviceDate() // where DeviceDate is a static class inherits com.sun.jna.Structure
int err = (int) INSTANCE.H264_GetLastError();
long result = INSTANCE.H264_Login("255.255.255.255", (short) 33333, "admin", "admin", dev, err, 0);
}
}
在运行应用程序时,Java崩溃了:
这是完整的问题签名:
问题签名:
问题事件名称:APPCRASH
应用程序名称:javaw.exe
应用版本:7.0.600.19
申请时间戳:536a95c6
故障模块名称:jna3976113557901128571.dll
故障模块版本:4.0.0.215
故障模块时间戳:52d3949a
例外代码:c0000005
异常偏移:0000e3a2 OS 版本:6.1.7601.2.1.0.256.1
地区ID:1033
附加信息1:7bc2
附加信息2:7bc24d73a5063367529b81d28aecc01c
附加信息3:5bea
附加信息4:5beaa1c0441c3adb156a170a61c93d19在线阅读我们的隐私声明:
http://go.microsoft.com/fwlink/?linkid=104288&clcid=0x0409如果没有在线隐私声明,请阅读我们的 脱机隐私声明:C:\ Windows \ system32 \ en-US \ erofflps.txt
答案 0 :(得分:0)
您的映射存在许多错误。您的结构应如下所示(IntByReference表示您将传递int
的地址的地方,并且您不能将String
替换为原始本地{{1数组)。请参阅the JNA mapping documentation以确保您了解本机类型如何映射到Java类型:
char