C# java等价物中的Intptr、Intptr.Zero和ref int:

C# java等价物中的Intptr、Intptr.Zero和ref int:,c#,java,.net,jna,C#,Java,.net,Jna,我无法读取通过USB连接的扫描仪。方法(LSConnect)的返回值总是相同的“未找到设备”。通过阅读.NET中的示例,我发现他们使用了其他参数,如IntPtr、IntPtr.Zero、ref Int。。。我必须在JAVA中使用JNA读取本机代码 这是C#中的LSApi.dll文档示例: 但当我看到他们以前在.NET上所做的事情时: 1/声明: public static extern int LSConnect(IntPtr hwnd, IntPtr HInst, int Pe

我无法读取通过USB连接的扫描仪。方法(LSConnect)的返回值总是相同的“未找到设备”。通过阅读.NET中的示例,我发现他们使用了其他参数,如IntPtr、IntPtr.Zero、ref Int。。。我必须在JAVA中使用JNA读取本机代码

这是C#中的LSApi.dll文档示例:

但当我看到他们以前在.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#声明的,但我认为正确的方法是遵循.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。

显然,
IntPtr
的等价物是
com.sun.jna.Pointer


对于
ref int
IntByReference
应该是好的。

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

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

如所示,
ref int
IntByReference
相同


如果你能找到你正在调用的API,你就必须少翻译所需的类型。通常,如果
C#
引用了DLL,您应该能够找到API的原始
C
声明。

IntPtr
只是指向
int
”的指针-等等,确定吗?我的印象是,
IntPtr
只是一个允许基本算术运算的普通指针,而不是专门指向int的指针。
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);
}