[英]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。
顯然, com.sun.jna.Pointer
類與IntPtr
等效。
對於ref int
, IntByReference
應該很好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.