jna java调用sdofiledll,使用JNA调用dll函数时,Java会崩溃

I'm using JNA to run a dll function:

Here is all the code corresponding to that manner:

The Native Declarations:

//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

};

The following is their corresponding Java mappings:

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 getFieldOrder() {

List 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 getFieldOrder() {

List 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);

}

}

upon running the app, the Java crashes:

odz6T.png

and this is the full problem signature:

Problem signature:

Problem Event Name: APPCRASH

Application Name: javaw.exe

Application Version: 7.0.600.19

Application Timestamp: 536a95c6

Fault Module Name: jna3976113557901128571.dll

Fault Module Version: 4.0.0.215

Fault Module Timestamp: 52d3949a

Exception Code: c0000005

Exception Offset: 0000e3a2 OS

Version: 6.1.7601.2.1.0.256.1

Locale ID: 1033

Additional Information 1: 7bc2

Additional Information 2: 7bc24d73a5063367529b81d28aecc01c

Additional Information 3: 5bea

Additional Information 4: 5beaa1c0441c3adb156a170a61c93d19

If the online privacy statement is not available, please read our

privacy statement offline: C:\Windows\system32\en-US\erofflps.txt

解决方案

Your mappings have a number of errors. Your structures should look like the following (IntByReference represents places where you would pass the address of an int, and you can't substitute String for a primitive native char array). Please refer to the JNA mapping documentation to ensure you understand how native types map to Java types:

public static class LP_DEVICE_INFO extends Structure{

public SDK_SYSTEM_TIME tmBuildTime;

public byte[] sSerialNumber = new byte[64];

public int byChanNum;

public int uiDeviceRunTime;

public int deviceType; // Assuming the size of the enum is int

@Override

protected List getFieldOrder() {

List 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 int year;

public int month;

public int day;

@Override

protected List getFieldOrder() {

List list = new ArrayList<>();

list.add("year");

list.add("month");

list.add("day");

return list;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值