Use JNA to access a function of a DLL (C #) from Java?

8

I have a DLL ( TransferImg.dll ) that is written in C# (provided by an external company), which tells me that I need to create an interface to access image transmission functions from JAVA, I was investigating of how, but I have not found something that understands or works for me.

The data that they provided me are: - Library TransferImg.dll
- Add reference to project.
- Add Using TorreyTransfer ;
- Create Object CommandTorrey myobj = new CommandTorrey();
- Call function myobj.TORREYSendImagesToScale which returns an integer with the error.

This is the function I want to use:

int SendImagesToScale(string Ip, string PathImage ,string Tipo, Socket iSock);

I do not have a clear idea of how to do it, this is my code taken from an example:

public class SendImg {
    public interface CLibrary extends Library {
        CLibrary INSTANCE = (CLibrary)
            Native.loadLibrary((Platform.isWindows() ? "C:\TransferImg.dll" : "c#"),                                   CLibrary.class);

        void TORREYSendImagesToScale(string Ip, string PathImage ,string Tipo, Socket iSock);
    }

    public static void main(String[] args) {
        CLibrary.INSTANCE.TORREYSendImagesToScale("192.168.1.111", "C:\img.jpg", "Splash", 3306);

    }
}

Dll: TransferImg.dll

    
asked by Angel Montes de Oca 27.07.2017 в 00:00
source

3 answers

5

I just created a test project in c # and I added the DLL that you uploaded to the created project.
And when inspecting the dll there is no function int SendImagesToScale(string Ip, string PathImage ,string Tipo, Socket iSock); as such but there is a similar one that is this: int TORREYSendImagesToScale(String sIpScale, String sPathImage, String sTypeData, Socket iSock);

The problem is that if you want to use it you can not do it directly since this is a function of a class and the function is not declared as static so you must first create an instance of the class and then call the function through the created instance.

TorreyTransfer.CommandTorrey commandTorrey = new TorreyTransfer.CommandTorrey();
commandTorrey.TORREYSendImagesToScale("IpScale", "pathImage", "TypeData", "UnSocket");

    
answered by 01.08.2017 / 11:33
source
3

Check the dll and I did not find any function void SendImagesToScale(string Ip, string PathImage ,string Tipo, Socket iSock); but if there is a int TORREYSendImagesToScale(String sIpScale, String sPathImage, String sTypeData, Socket iSock); that may be your error, decopile the dll with dotpeek ( link ) so maybe you can study the dll or do it again to your liking, well I could see all the code inside the dll, in the TorreyTransfer namespace the TORREYSendImagesToScale function is found.

I gave you some code that could help you:

import java.net.Socket;
import com.sun.jna.Library;
import com.sun.jna.Native;

public class Main {

    private final static int PUERTO = 1234;
    private final static String HOST = "localhost";
    protected static Socket mysocket;

    public interface MyObject extends Library {
        //sIpScale=ip de destino de la imagen
        //sPathImage= path de la imagen a enviar
        //sTypeData=varia entre "Splash", "TestUsb","Product" y "TestSdcard"
        //iSock= socket de conexion de tu aplicacion

        int TORREYSendImagesToScale(String sIpScale, String sPathImage, String sTypeData, Socket iSock);
    }

    public static void main(String[] args) {
        try {
            mysocket = new Socket(HOST, PUERTO);
            MyObject myObj = (MyObject) Native.loadLibrary("TransferImg.dll", MyObject.class);
            int resultado = myObj.TORREYSendImagesToScale("192.168.1.111", "C:/img.jpg", "Splash", mysocket);
            //resultado = {0=correcto windows}, otros datos error
        }
    }
}

Where TransferImg.dll should be in the direction of your project.

    
answered by 31.07.2017 в 21:16
3

You need to make several wrappers, call a class that calls a class that calls your DLL in C# , an example is:

Here is a page that could be of great help: LINK

    
answered by 02.08.2017 в 19:40