如何获取java JNA调用DLL以获取参数中返回的数据? [英] How do I get a java JNA call to a DLL to get data returned in parameters?

查看:3065
本文介绍了如何获取java JNA调用DLL以获取参数中返回的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个java测试

package ftct;

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;
import com.sun.jna.win32.StdCallLibrary;
import java.util.Date;

public class LibFTCT {


public LibFTCT() {
 }
 public interface LibFTCTLib extends StdCallLibrary {
          LibFTCTLib INSTANCE = (LibFTCTLib) Native.loadLibrary(
             "FTCTLib", LibFTCTLib.class);
      int a(int x);
      int DoCommand(int Command, int Param);
    int GetDataRecord(int RecordNum, int StreamNum, Date ReadingTime,
   double AIN1, double AIN2, double AIN3, double AIN4);

 }
}

它调用Delphi DLL。如果在Delphi中将参数设置为var,则Java会崩溃。否则它们是只读的。

It calls a Delphi DLL. If put the parameters as var in Delphi the Java crashes. Otherwise they are read only.

我希望GetDataRecord在RecordNum中返回数据。我如何在java中执行此操作?

I want GetDataRecord to return data in RecordNum etc. How do I do this in java?

推荐答案

您需要将参数声明为特殊类型,表示通过引用传递的事实。所以如果Delphi参数是这样的:

You need to declare the parameters as special types that convey the fact that they are passed by reference. So if the Delphi parameters are like this:

procedure Foo(var i: Integer; var d: Double);

您将把它映射到这样的Java函数:

you would map that to a Java function like this:

void Foo(IntByReference i, DoubleByReference d);

并调用函数:

IntByReference iref = new IntByReference();
DoubleByReference dref = new DoubleByReference();
INSTANCE.Foo(iref, dref);
int i = iref.getValue();
double d = dref.getValue():

这一切都在文档


使用ByReference参数

当一个函数接受一个指针型参数时,你可以使用
的ByReference类型中的一个来捕获返回的值,或者将
自己的子类化。例如:

When a function accepts a pointer-to-type argument you can use one of the ByReference types to capture the returned value, or subclass your own. For example:

// Original C declaration
void allocate_buffer(char **bufp, int* lenp);

// Equivalent JNA mapping
void allocate_buffer(PointerByReference bufp, IntByReference lenp);

// Usage
PointerByReference pref = new PointerByReference();
IntByReference iref = new IntByReference();
lib.allocate_buffer(pref, iref);
Pointer p = pref.getValue();
byte[] buffer = p.getByteArray(0, iref.getValue());

或者,您可以使用具有
所需类型的单个元素的Java数组,但是ByReference约定更好地传达了代码的意图
。指针类提供了许多访问器方法
,除了getByteArray(),它有效地作为一个类型转换
在内存上。

Alternatively, you could use a Java array with a single element of the desired type, but the ByReference convention better conveys the intent of the code. The Pointer class provides a number of accessor methods in addition to getByteArray() which effectively function as a typecast onto the memory.

Type-可以通过从PointerType
类派生来声明安全指针。

Type-safe pointers may be declared by deriving from the PointerType class.

这篇关于如何获取java JNA调用DLL以获取参数中返回的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆