2013-12-17 121 views
0

我有一個讀取通過USB連接的掃描儀的問題。該方法的返回值(LSConnect)始終與「未找到設備」相同。通過閱讀.NET中的示例,我發現他們使用其他參數,如IntPtr,IntPtr.Zero,ref Int ...並且我必須使用JNA來使用JNA來讀取本地代碼。Intptr,Intptr.Zero和ref int in java等價物:

,所以這是在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 /主營總是在.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方法就像mentionned在文檔實例至極是在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」 OK 2 - 我不知道相同的不同參數作爲IntPtr,IntPtr.Zero和ref int? 我用intByReference爲IntPtr和ref int。

回答

1

IntPtr.Zero相當於null爲此目的。 IntPtr是一個足夠容納指針的整數類型。避免使用它,只使用PointerPointerType

在這種情況下,如果您要傳遞句柄或HANDLEByReference(如果函數要爲您填充值),則可以安全地使用HANDLE

Medinoc所示,ref intIntByReference相同。

如果您可以找到a straight C example作爲您打電話給的API,那麼您將不得不少量翻譯所需的類型。通常,如果C#引用DLL,則應該能夠找到該API的原始C聲明。

+0

「'IntPtr'只是一個指向'int'的指針」 - 等等,你確定嗎?我的印象是,「IntPtr」僅僅是一個允許基本算術的非描述性指針,而不是一個特別指向int的指針。 – Smallhacker

+0

你是對的,回答編輯反映。 – technomage

0

顯然相當於IntPtr的是com.sun.jna.Pointer類。

對於ref intIntByReference應該是好的。