Search code examples
javac++java-native-interfaceusbnative

How to call C method (of Scanner API) with java using JNI


I have a Scanner API that contains a DLL (LsApi.dll) and I have to use its methods with JAVA using JNI. I tried to use the first method (LSConnect) wich return an integer LS_OKAY if the device is correctly connected or LS_PERIPHERAL_NOT_FOUND if not , and unfortunately, i always get errors.

this is my code in Java:

public class Scanner {

public final static short LS_515_USB = 502;
private static final short hconnect = 0;
public final int LS_OKAY = 0;

public native static int LSConnect(int hWnd, int hlnst, short Peripheral,
        short hConnect);

static {
    System.loadLibrary("lsApi");
}

/**
 * @param args
 */
public static void main(String[] args) {
    new Scanner();
    System.out.println("connexion établie: "
            + Scanner.LSConnect(0, 0, LS_515_USB, hconnect));

}

}

and this is a example of C# defines of all the methods :

C# Defines

namespace WindowsApplication1

{

public partial class Form1 : Form

{      



    // from lsapi.h

    [DllImport(@"lsapi.dll")]

    internal extern static int LSConnect(int hWnd, int hInst, short Peripheral,ref short hConnect);



    [DllImport(@"lsapi.dll")]

    internal extern static int LSDisconnect (short hConnect,int hWnd);


    [DllImport(@"lsapi.dll")]

    internal extern static int LSSaveJPEG( int hwnd, int hImage,int quality, string filename );



    [DllImport(@"lsapi.dll")]

    internal extern static int LSSaveDIB(int hWnd, int hImage, string filename);


    // Parameter Type peripheral

    public const short LS_5xx_SCSI    =          500;

    public const short LS_515_USB     =          502;

    public const short LS_800_USB     =          801;



    // ------------------------------------------------------------------------

    //                          REPLY-CODE

    // ------------------------------------------------------------------------



    public const int                 LS_OKAY                                                        =                      0;



    // ------------------------------------------------------------------------

    //                  ERRORS

    // ------------------------------------------------------------------------

    public const int                 LS_SYSTEM_ERROR                                       =                      -1;

    public const int                 LS_USB_ERROR                                             =                      -2;

    public const int                 LS_PERIPHERAL_NOT_FOUND                       =                      -3;

    public const int                 LS_HARDWARE_ERROR                                 =                      -4;

    public const int                 LS_PERIPHERAL_OFF_ON                              =                      -5;

    public const int                 LS_RESERVED_ERROR                                  =                      -6;

    public const int                 LS_BOURRAGE                                               =                      -7;

    public const int                 LS_PAPER_JAM                                              =                      -7;

    public const int                 LS_TARGET_BUSY                                          =                      -8;

    public const int                 LS_INVALID_COMMAND                                  =                      -9;

    public const int                 LS_DATA_LOST                                              =                      -10;

    public const int                 LS_COMMAND_IN_EXECUTION_YET               =                      -11;

    public const int                 LS_JPEG_ERROR                                           =                      -12;

    public const int                 LS_COMMAND_SEQUENCE_ERROR               =                      -13;

    public const int                 LS_PC_HW_ERROR                                        =                      -14;

    public const int                 LS_IMAGE_OVERWRITE                                 =                      -15;

    public const int                 LS_INVALID_HANDLE                                     =                      -16;

    public const int                 LS_NO_LIBRARY_LOAD                                  =                      -17;

    public const int                 LS_BMP_ERROR                                             =                      -18;

    public const int                 LS_TIFF_ERROR                                             =                      -19;

    public const int                 LS_IMAGE_NO_MORE_AVAILABLE                 =                      -20;

    public const int                 LS_IMAGE_NO_FILMED                                  =                      -21;

    public const int                 LS_IMAGE_NOT_PRESENT                              =                      -22;

    public const int                 LS_FUNCTION_NOT_AVAILABLE                     =                      -23;

    public const int                 LS_DOCUMENT_NOT_SUPPORTED                 =                      -24;

    public const int                 LS_BARCODE_ERROR                        =                      -25;

    public const int                 LS_INVALID_LIBRARY                                     =                      -26;

    public const int                 LS_INVALID_IMAGE                                        =                      -27;

    public const int                 LS_INVALID_IMAGE_FORMAT             =                      -28;

    public const int                 LS_INVALID_BARCODE_TYPE            =                      -29;

    public const int                 LS_OPEN_NOT_DONE                         =                      -30;

    public const int                 LS_INVALID_TYPE_COMMAND                       =                      -31;

    public const int                 LS_INVALID_CLEARBLACK                             =                      -32;

    public const int                 LS_INVALID_SIDE                                           =                      -33;

    public const int                 LS_MISSING_IMAGE                                       =                      -34;

    public const int                 LS_INVALID_TYPE                                          =                      -35;

    public const int                 LS_INVALID_SAVEMODE                                =                      -36;

    public const int                 LS_INVALID_PAGE_NUMBER                          =                      -37;

    public const int                 LS_INVALID_NRIMAGE                                    =                      -38;

    public const int                 LS_INVALID_STAMP                                       =                      -39;

    public const int                 LS_INVALID_WAITTIMEOUT                            =                      -40;

    public const int                 LS_INVALID_VALIDATE                                   =                      -41;

    public const int                 LS_INVALID_CODELINE_TYPE                        =                      -42;

    public const int                 LS_MISSING_NRIMAGE                                   =                      -43;

    public const int                 LS_INVALID_SCANMODE                                =                      -44;

    public const int                 LS_INVALID_BEEP                                          =                      -45;

    public const int                 LS_INVALID_FEEDER                                     =                      -46;

    public const int                 LS_INVALID_SORTER                                     =                      -47;

    public const int                 LS_INVALID_BADGE_TRACK                          =                      -48;

    public const int                 LS_MISSING_FILENAME                                 =                      -49;

    public const int                 LS_INVALID_QUALITY                                     =                      -50;

    public const int                 LS_INVALID_FILEFORMAT                              =                      -51;

    public const int                 LS_INVALID_COORDINATE                             =                      -52;

    public const int                 LS_MISSING_HANDLE_VARIABLE                   =                      -53;

    public const int                 LS_INVALID_POLO_FILTER                             =                      -54;

    public const int                 LS_INVALID_ORIGIN_MEASURES                   =                      -55;

    public const int                 LS_INVALID_SIZEH_VALUE                             =                      -56;

    public const int                 LS_INVALID_FORMAT                                     =                      -57;

    public const int                 LS_STRINGS_TOO_LONGS                              =                      -58;

    public const int                 LS_READ_IMAGE_FAILED                              =                      -59;

    public const int                 LS_INVALID_CMD_HISTORY                           =                      -60;

    public const int                 LS_MISSING_BUFFER_HISTORY                     =                      -61;

    public const int                 LS_INVALID_ANSWER                                    =                      -62;

    public const int                 LS_OPEN_FILE_ERROR_OR_NOT_FOUND       =                      -63;

    public const int                 LS_READ_TIMEOUT_EXPIRED            =                      -64;

    public const int                 LS_INVALID_METHOD                                     =                      -65;

    public const int                 LS_CALIBRATION_FAILED                              =                      -66;

    public const int                 LS_INVALID_SAVEIMAGE                               =                      -67;

    public const int                 LS_INVALID_UNIT                                           =                      -68;

    public const int                 LS_INVALID_NRWINDOWS                             =                      -71;

    public const int                 LS_INVALID_VALUE                                        =                      -72;

    public const int                 LS_ILLEGAL_REQUEST                                   =                      -73;

    public const int                 LS_INVALID_NR_CRITERIA                             =                      -74;

    public const int                 LS_MISSING_CRITERIA_STRUCTURE  =                      -75;

    public const int                 LS_INVALID_MOVEMENT                                =                      -76;

    public const int                 LS_INVALID_DEGREE                                     =                      -77;

    public const int                 LS_R0TATE_ERROR                                        =                      -78;

    public const int                 LS_MICR_VALUE_OUT_OF_RANGE                 =                      -79;

    public const int                 LS_PERIPHERAL_RESERVED                         =                      -80;

    public const int                 LS_INVALID_NCHANGE                                   =                      -81;

    public const int                 LS_BRIGHTNESS_ERROR                               =                      -82;

    public const int                 LS_CONTRAST_ERROR                                   =                      -83;

    public const int                 LS_INVALID_SIDETOPRINT                             =                      -84;

    public const int                 LS_DOUBLE_LEAFING_ERROR                       =                      -85;

    public const int                 LS_INVALID_BADGE_TIMEOUT                       =                      -86;

    public const int                 LS_INVALID_RESET_TYPE                              =                      -87;

    public const int                 LS_MISSING_SET_CALLBACK             =                      -88;

    public const int                 LS_IMAGE_NOT_200_DPI                                =                      -89;

    public const int                 LS_DOWNLOAD_ERROR                                 =                      -90;

    public const int                 LS_INVALID_SORT_ON_CHOICE                     =                      -91;

    public const int                 LS_INVALID_FONT                                          =                      -92;

    public const int                 LS_INVALID_UNIT_SPEED                              =                      -93;

    public const int                 LS_INVALID_LENGTH                                      =                      -94;



    public const int                 LS_SCAN_NETTO_IMAGE_NOT_SUPPORTED=                       -521;

    public const int                 LS_256_GRAY_NOT_SUPPORTED                   =                      -522;

    public const int                 LS_INVALID_PATH                                          =                      -523;

    public const int                 LS_MISSING_CALLBACK_FUNCTION              =                      -526;

    public const int                 LS_INVALID_OCR_IMAGE_SIDE                      =                      -558;

    public const int                 LS_PERIPHERAL_NOT_ANSWER                    =                      -599;



    public const int                 LS_INVALID_CONNECTION_HANDLE              =                      -1000;

    public const int                 LS_INVALID_CONNECT_PERIPHERAL =                      -1001;

    public const int                 LS_PERIPHERAL_NOT_YET_INTEGRATE         =                      -1002;

    public const int                 LS_UNKNOW_PERIPHERAL_REPLY                =                      -1003;

Solution

  • You can't call a .dll directly through JNI just by calling LoadLibrary and defining a native method (unlike C#). You would need to write a "bridge" library (separate .dll) in C that wraps the lsapi.dll and converts the JNI calling conventions to the lsapi calling conventions.

    I highly recommend using JNA rather than writing your own JNI DLL. Calling a .dll using JNA is pretty near trivial. There are plenty of resources on the web to help, and searching for Java + JNA on stackoverflow will yield lots of help.