Pregunta

I have a problem to read a scanner connected via USB. the return value of the method (LSConnect) is always the same "Device not found". by reading the example in .NET, I found that they used other parameter as IntPtr, IntPtr.Zero, ref Int ... and i have to do it in JAVA using JNA to read native code.

so this is the LSApi.dll doc example in C#:

[DllImport(@"lsapi.dll")]
internal extern static int LSConnect(int hWnd, int hInst, short Peripheral,ref short hConnect);
internal extern static int LSDocHandle (short hConnect, int  hWnd, short Stamp, short Validate, short CodeLine,byte Side,short ScanMode,short Feeder,short Sorter, short WaitTimeout,short Beep,ref int NrDoc,short Reserved1,int   Reserved2);

But when i see what they did before on .NET: 1/ Declaration:

        public static extern int LSConnect(IntPtr hwnd, IntPtr HInst, int Peripheral, ref int hConnect);

        public static extern int LSDocHandle(int hConnect,
                            IntPtr hWnd,
                            int Stamp,
                            int Validate,
                            int CodeLine,
                            char Side,
                            int ScanMode,
                            int Feeder,
                            int Sorter,
                            int WaitTimeout,
                            int Beep,
                            ref uint NrDoc,
                            int Reserved1,
                            int Reserved2);

2/Main always in .Net:

 int b = -2;
        uint c = 0;
        IntPtr frontimg = IntPtr.Zero;
        IntPtr backimg = IntPtr.Zero;
        IntPtr R1 = IntPtr.Zero;
        IntPtr R2 = IntPtr.Zero;
        LsApi.LSConnect(IntPtr.Zero, IntPtr.Zero, 502, ref b);
        LsApi.LSDocHandle(b, IntPtr.Zero, LsApi.NO_STAMP, LsApi.NO_PRINT_VALIDATE, (int)LsApi.Codeline.NO_READ_CODELINE, (char)LsApi.Side.SIDE_FRONT_IMAGE, (int)LsApi.ScanMode.SCAN_MODE_COLOR_100, LsApi.AUTO_FEED, (int)LsApi.Sorter.SORTER_BAY1, LsApi.WAIT_NO, (int)LsApi.Beep.NO_BEEP, ref c, 0, 0).ToString();
        LsApi.LSReadImage(b, IntPtr.Zero, LsApi.CLEAR_ALL_BLACK, (char)LsApi.Side.SIDE_FRONT_IMAGE, 0, 0, ref frontimg, ref backimg, ref R1, ref R2);
        LsApi.LSDisconnect(b, IntPtr.Zero);

i declared my Method in JAVA like mentionned in the Doc Example wich is in C#, but i think the correct way is to follow the .Net Example

here is my JAVA Code:

public int LSConnect(int hWnd, int hInst, short i, ShortByReference hConnect);

public int LSDisconnect(short hConnect, IntByReference hWnd);

public int LSDocHandle(short hConnect, int hWnd, short Stamp,
        short Validate, short CodeLine, byte Side, short ScanMode,
        short Feeder, short Sorter, short WaitTimeout, short Beep,
        IntByReference NrDoc, short Reserved1, int Reserved2);

and the Main Class:

public class ConnectExample {

public static void main(String[] args) {
    String libName = "lsApi";
    JNAUser32 jnaUser32 = (JNAUser32) Native.loadLibrary(libName,
            JNAUser32.class);
    ShortByReference hConnect = new ShortByReference();
    hConnect.setValue((short) 55);

    int state = jnaUser32.LSConnect(0, 0, (short) 502, hConnect);
    System.out.println(state);
}

}

i just used the LSConnect example because : 1- i have to get the return value as "-1" wich refer to connection is OK 2- i don't know the equivalent of the different param as IntPtr, IntPtr.Zero and ref int ? i used intByReference for both IntPtr and ref int.

¿Fue útil?

Solución

IntPtr.Zero is equivalent to null for this purpose. IntPtr is an integer type large enough to hold a pointer. Avoid using it and just use a Pointer or PointerType.

In this case, you can safely use HANDLE if you're passing in a handle or HANDLEByReference if the function is going to fill in a value for you.

As indicated by Medinoc, ref int is the same as IntByReference.

If you can find a straight C example for the API you're calling, you'll have to do less translation of required types. Generally if the C# references a DLL you should be able to find raw C declarations for the API.

Otros consejos

Apparently the equivalent of IntPtr is the com.sun.jna.Pointer class.

For ref int, IntByReference should be good.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top