-1

我正在使用 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 操作系统版本:6.1.7601.2.1.0.256.1
区域设置 ID:1033
附加信息 1:7bc2
附加信息 2:7bc24d73a5063367529b81d28aecc01c
附加信息 3:5bea
附加信息 4:5beaa1c0441c3adb39d196a170a61c

在线阅读我们的隐私声明:http:
//go.microsoft.com/fwlink/ ?linkid=104288&clcid=0x0409

如果在线隐私声明不可用,请离线阅读我们的隐私声明:C:\Windows\system32\en-US\erofflps.txt

4

1 回答 1

0

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<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 int year;  
    public int month;  
    public int day;
    @Override
    protected List<Object> getFieldOrder() {
        List<Object> list = new ArrayList<>();
        list.add("year");
        list.add("month");
        list.add("day");
        return list;
    }
}
于 2015-01-04T20:35:08.323 回答