【问题标题】:Intptr, Intptr.Zero and ref int in java equivalent :Java 中的 Intptr、Intptr.Zero 和 ref int 等效项:
【发布时间】:2023-03-26 07:49:01
【问题描述】:

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

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

[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# 文档示例中提到的那样,但我认为正确的方法是遵循 .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。

【问题讨论】:

    标签: c# java .net jna


    【解决方案1】:

    IntPtr.Zero 等同于 null 用于此目的。 IntPtr 是一个足以容纳指针的整数类型。避免使用它,只使用PointerPointerType

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

    Medinoc所示,ref intIntByReference相同。

    如果您可以找到您正在调用的 API 的 a straight C example,您将需要减少对所需类型的翻译。一般来说,如果C# 引用了一个DLL,您应该能够找到API 的原始C 声明。

    【讨论】:

    • "IntPtr 只是指向int 的指针" - 等等,你确定吗?我的印象是IntPtr 只是一个允许基本算术的不起眼的指针,而不是专门指向 int 的指针。
    【解决方案2】:

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

    对于ref intIntByReference 应该不错。

    【讨论】:

      猜你喜欢
      • 2017-09-06
      • 2018-07-22
      • 2012-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-22
      • 2011-11-29
      • 1970-01-01
      相关资源
      最近更新 更多