簡體   English   中英

Intptr,Intptr.Zero和ref int in Java等效項:

[英]Intptr, Intptr.Zero and ref int in java equivalent :

我在讀取通過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 /主要總是在.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);

我用Doc Example中提到的方式在JAVA中聲明了我的Method,它在C#中,但是我認為正確的方法是遵循.Net Example

這是我的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.Zero等於null IntPtr是一個足夠容納指針的整數類型。 避免使用它,而僅使用PointerPointerType

在這種情況下,如果要傳遞HANDLE則可以安全地使用HANDLE如果函數要為您填充一個值,則可以安全地使用HANDLEByReference

Medinoc所示ref intIntByReference相同。

如果您可以找到要調用的API 的直接C示例 ,則不必進行必需類型的轉換。 通常,如果C#引用DLL,則您應該能夠找到API的原始C聲明。

顯然, com.sun.jna.Pointer類與IntPtr等效。

對於ref intIntByReference應該很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM