Java JNA Mapping in Delphi Dll函数 [英] Java JNA Mapping in Delphi Dll function

查看:171
本文介绍了Java JNA Mapping in Delphi Dll函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用JNA映射此功能:

How do i map this function with JNA:

Delphi Dll功能代码

function send_command (const command : byte; var size : byte; var data : pbyte) : integer; stdcall external 'comunication.dll';

在Delphi中使用示例示例程序:

Use example in Delphi Example Program:

send_command(cmdCLOCK_ADJUST,tam,pb);

其中:

const 
    cmdCLOCK_ADJUST = $18;

var
   tam : byte;
   pb, p : pbyte;

begin
   ...
    tam = 7;
    p:= pb;
       for i:= 1 to tam do
          begin
            p^:= Dados [i];
            inc (p)
          end;
    send_command (cmdCLOCK_ADJUST, tam, pb);
    freemem (pb);
   ...
end

返回的int值可以为0对于错误或1执行正确;

The int value that is returned can be 0 for Error or 1 for right execution;

我的建议是:

JNA Java功能代码:

JNA Java Function Code:

int send_command(byte command, byte size, byte[] data);

问题是dll的函数返回0.我还尝试过其他数据类型,但它hasn工作喷气机。我认为问题是dll函数无法写入参数数据。

The problem is that the function of the dll returns 0. I also tried other Datatypes, but it hasn't worked jet. I think the problem is that the dll function can't write to the parameter data.

推荐答案

看起来像Delphi代码实际上是错误的。

It looks like your Delphi code is actually wrong.

size 参数似乎是将来自调用者的数据长度传递给被调查者因此,它不应该是 var 参数。

The size parameter appears to be a means to pass the length of the data from the caller to the callee. As such it should not be a var parameter.

数据参数看起来是一个指向调用者分配的缓冲区的指针。再次,这不应该是一个 var 参数。只是一个简单的 PByte ,通过值传递。

The data parameter appears to be a pointer to a buffer allocated by the caller. Again, that should not be a var parameter. Just a plain PByte, passed by value.

这将使Delphi代码如下所示:

Which would make the Delphi code be like this:

function send_command(const command: Byte; size: Byte; data: PByte): Integer; 
  stdcall external 'comunication.dll';

然后,Delphi代码将与您的Java代码相匹配。

And then the Delphi code would match your Java code.

当然,我不得不做一些猜测来写这个答案。猜测根据我的经验这样的问题,但猜测是一样的。您应该从中避开的真正课程是,接口不是由其参数的类型指定的。您还需要指定参数传递的语义。

Of course, I've had to make a number of guesses to write this answer. Guesses based on my experience of such problems, but guesses all the same. The real lesson that you should take away from this is that an interface is not specified by the types of its parameters. You also need to specify the semantics of the parameter passing.

所以,当你有一个 PByte paramerer,是指向一个字节的指针,或一个指向数组?如果后者,阵列有多大。等等。您需要指定所有这些信息来定义界面。

So, when you have a PByte paramerer, is that a pointer to a single byte, or a pointer to an array? If the latter, how large is the array. And so on. You need to specify all of this information to define an interface.

如果您真的无法更改DLL,则需要将大小数据参数,即使它们似乎具有值语义。这个答案涵盖了在JNA中的引用:如何获取java JNA调用DLL以获取参数中返回的数据?

If you really cannot change the DLL, then you'll need to pass the size and data parameters by reference, even though they appear to have value semantics. This answer covers passing by reference in JNA: How do I get a java JNA call to a DLL to get data returned in parameters?

这篇关于Java JNA Mapping in Delphi Dll函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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