Intptr,Intptr.Zero和java等效的ref int:

时间:2013-12-17 13:47:23

标签: c# java .net jna

我在阅读通过USB连接的扫描仪时遇到问题。方法的返回值(LSConnect)始终相同“未找到设备”。通过阅读.NET中的示例,我发现他们使用其他参数作为IntPtr,IntPtr.Zero,ref Int ...我必须在JAVA中使用JNA来读取本机代码。

所以这是C#中的LSApi.dll doc示例:

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

但是当我看到他们之前在.NET上所做的事情时: 1 /声明:

        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总是在.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);

我在JAVA中声明了我的方法,就像在C#中提到的Doc示例一样,但我认为正确的方法是遵循.Net示例

这是我的JAVA代码:

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

和主类:

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

}

我刚刚使用了LSConnect示例,因为: 1-我必须得到返回值为“-1”,这是指连接正常 2-我不知道IntPtr,IntPtr.Zero和ref int等同于不同的参数? 我对intPtr和ref int使用了intByReference。

2 个答案:

答案 0 :(得分:1)

为此,

IntPtr.Zero相当于nullIntPtr是一个足以容纳指针的整数类型。避免使用它,只需使用PointerPointerType

在这种情况下,如果您传入句柄,则可以安全地使用HANDLE;如果函数要为您填写值,则可以HANDLEByReference

Medinoc所示,ref intIntByReference相同。

如果您可以找到要调用的API的a straight C example,则必须减少对所需类型的翻译。通常,如果C#引用了DLL,您应该能够找到API的原始C声明。

答案 1 :(得分:0)

显然相当于IntPtrcom.sun.jna.Pointer类。

对于ref intIntByReference应该是好的。