我有一个问题,读取扫描仪通过USB连接。方法(LSConnect)的返回值总是相同的“未找到设备”。通过阅读.NET中的示例,我发现它们使用了其他参数,如IntPtr、IntPtr.Zero、ref Int .我必须在JAVA中使用JNA来读取本机代码。
这就是LSApi.dll文档在C#中的例子:
[DllImport(@"lsapi.dll")]内部extern静态int LSConnect(int hWnd,int hInst,short外设,ref short hConnect),内部extern静态int LSDocHandle (short hConnect,int hWnd,short Stamp,short CodeLine,字节侧,空ScanMode,short馈线,short Sorter,short WaitTimeout,short Beep,ref int NrDoc,short Reserved1,int Reserved2);
但当我看到他们以前在.NET上所做的事情时:
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中声明了我的方法,就像Doc示例中提到的那样,wich在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“的返回值,因为”-1“指的是连接,2-我不知道IntPtr、IntPtr.Zero和ref int的不同参数的等效值是多少?我在IntPtr和ref中都使用了IntPtr。
发布于 2013-12-18 12:26:34
为此目的,IntPtr.Zero等同于null。IntPtr是一个足以容纳指针的整数类型。避免使用它,只需使用Pointer或PointerType。
在本例中,如果要传递句柄,则可以安全地使用HANDLE;如果函数要为您填充值,则可以使用HANDLEByReference。
正如Medinoc所指出的,ref int与IntByReference是相同的。
如果您可以为调用的API找到 example,则必须减少所需类型的转换。通常,如果C#引用DLL,您应该能够找到C的原始C声明。
发布于 2013-12-17 16:24:45
显然,IntPtr的等价物是com.sun.jna.Pointer类。
对于ref int来说,IntByReference应该是好的。
https://stackoverflow.com/questions/20635894
复制相似问题