Java crashes when calling dll function using JNA

谁说我不能喝 提交于 2019-12-13 08:24:44

问题


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

}
}

upon running the app, the Java crashes:

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

Read our privacy statement online:
http://go.microsoft.com/fwlink/?linkid=104288&clcid=0x0409

If the online privacy statement is not available, please read our privacy statement offline: C:\Windows\system32\en-US\erofflps.txt


回答1:


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;
    }
}


来源:https://stackoverflow.com/questions/27729672/java-crashes-when-calling-dll-function-using-jna

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!