Unable to open the port by calling Native method f

2019-01-20 06:31发布

问题:

This is native method from ITLSSPProc.dll

NOMANGLE int CCONV OpenSSPComPort (SSP_COMMAND * cmd);

Here, SSP_COMMAND is structure in ITLSSPProc.dll which is in C++ Language.

struct SSP_COMMAND
{
unsigned long BaudRate;
unsigned char PortNumber;
};

So, I have to access OpenSSPComPort (SSP_COMMAND * cmd) in java using JNI. Here is a code i have written,

public class Main {
    public interface ITLSSPProc extends Library {
     ITLSSPProc INSTANCE = (ITLSSPProc) Native.loadLibrary(
            (Platform.isWindows() ? "ITLSSPProc" : "simpleDLLWindowsPort"), ITLSSPProc.class);

        int OpenSSPComPort(Pointer param); 
        int CloseSSPComPort();                    
    }

    public static void main(String[] args)throws IOException {

     ITLSSPProc sdll = ITLSSPProc.INSTANCE;

        Memory intMem = new Memory(10); // allocating space
        intMem.setLong(0,9600);
        intMem.setString(1,"com7");        

        if(sdll.OpenSSPComPort(intMem)==1)
        {// calling function with int parameter&result
            System.out.println("connected");
        }
        else
        {
            System.out.println("failed");
        }
     }
}

Output : failed

Port number is COM7 on which we are working. So, when i run this application and as i passing baud rate as manually as given in user manual and if port number is correct it has to print "connected" on console. So, anybody know where i am going wrong, i dont understand where is actual problem..

回答1:

JNA documentation for basic types (long, char).

JNA documentation for aggregate types (struct, struct *).

// tl;dr
class SSP_COMMAND extends Structure {
    public NativeLong BaudRate;
    public byte PortNumber;
}

int OpenSSPComPort(SSP_COMMAND param)


标签: java dll jni jna