简体   繁体   English

在Java JNA中将本机函数传递给参考变量

[英]Passing Native Function a reference variable in Java JNA

I am trying to write a Java applet to connect to a bar-code scanner and read the data. 我正在尝试编写Java小程序以连接到条形码扫描仪并读取数据。 The bar-code scanner comes with a dll file with functions to access it. 条形码扫描仪随附一个dll文件,该文件具有访问它的功能。 I don't have a lot of experience with Java but I've figured out how to use JNA to load the dll and call the native functions. 我没有Java的丰富经验,但是我已经弄清楚了如何使用JNA加载dll并调用本机函数。 Some of the functions work fine but the one that returns the actual bar-codes I can't seem to get to work. 某些功能可以正常工作,但可以返回我似乎无法使用的实际条形码的功能。 It takes a character array as an argument and pass by reference is not in JNA, I've tried using the Pointer class and can't get anything to work. 它需要一个字符数组作为参数,并且在JNA中不是通过引用传递的,我已经尝试过使用Pointer类,并且什么也无法工作。 The native functions definition is as follows. 本机函数定义如下。

NoMangle long DLL_IMPORT_EXPORT csp2GetPacket ( char szBarData[],long nBarcodeNumber,long nMaxLength ) NoMangle long DLL_IMPORT_EXPORT csp2GetPacket(char szBarData [],long nBarcodeNumber,long nMaxLength)

After the function call the szBarData[] variable is supposed to contain the bar-code data. 函数调用后,szBarData []变量应该包含条形码数据。

I've tried setting up the JNA map several different ways, making the szBarData[] argument a char array, string, a byte array, and a pointer, and have had no luck. 我尝试以几种不同的方式设置JNA映射,使szBarData []参数成为char数组,字符串,字节数组和指针,并且没有运气。

in visual basic you can pass the szBarData[] a string and it works, just not sure how to accomplish this in Java using JNA. 在Visual Basic中,您可以传递szBarData []一个字符串,它可以工作,只是不确定如何使用JNA在Java中完成此操作。

Since you're expecting the target function to fill in your buffer, you cannot pass a String (which is read-only) but must instead pass a byte[] . 由于您期望目标函数填充缓冲区,因此您不能传递String (只读),而必须传递byte[]

Also make sure you're not using Java long to represent native long ; 还要确保您没有使用Java long来表示本地long the class NativeLong exists for that purpose. NativeLong存在NativeLong类。

If what you get back is a NUL-terminated C string, then you can use Native.toString(byte[]) to convert to a String . 如果返回的是NUL终止的C字符串,则可以使用Native.toString(byte[])转换为String

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM