我如何将这个Delphi函数与JNA映射 [英] How would I map this Delphi function with JNA

查看:198
本文介绍了我如何将这个Delphi函数与JNA映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Delphi函数:

I have the following Delphi function:

function DoX(const InputBuffer: Pointer; const InputBufferSize: longword; OutputBuffer: Pointer; var OutputBufferSize: longword): longbool;

OutputBuffer和OutputBufferSize将作为结果的一部分在函数中设置,布尔值返回指出方法是否成功(InputBuffer& OutputBuffer将是字节数组)。

The OutputBuffer and OutputBufferSize would be set in the function as part of the result, with a boolean return to indicate whether the method was successful (InputBuffer & OutputBuffer would be byte arrays).

我已经设法将一些我需要的函数从dll映射到JNA,它们是工作确定,但是这个给我的问题,任何帮助将不胜感激。

I have managed to map some of my required functions from the dll with JNA and they are working ok, however this one is giving me issues, any help would be appreciated.

推荐答案

大多数JNA文档假定您正在使用C,不是Delphi,所以从C开始等同于该功能:

Most JNA documentation assumes you're using C, not Delp so start with the C equivalent to that function:

int DoX(const void* InputBuffer,
        unsigned int InputBufferSize,
        void* OutputBuffer,
        unsigned int* OutputBufferSize);

您还需要获得调用约定。 Delphi的默认是注册,这可能不是你想要的。改用 stdcall 这就是每个其他DLL使用的。

You'll also want to get the calling convention right. Delphi's default is register, which probably isn't what you want. Use stdcall instead; that's what every other DLL uses.

Java不具有与您使用的类型相同的无符号类型,因此首先忽略无符号。这使得 InputBufferSize 一个 int 。您的函数返回一个布尔结果,因此使用 boolean 作为其返回类型。 JNA支持通过 <$ c的后代引用参考传递类型$ c> ByReference 类,所以使用 OutputBufferSize ,rel =noreferrer> IntByReference

Java doesn't have unsigned type equivalents to the ones you used, so start by ignored the unsignedness. That makes InputBufferSize an int. Your function returns a Boolean result, so use boolean for its return type. JNA supports passing types by reference through descendants of the ByReference class, so use IntByReference for OutputBufferSize.

最后是指针。你说他们是字节数组,所以我很困惑为什么你不在Delphi代码中声明它们。或者使用 PByte ,或者声明一个新的 PByteArray 类型并使用它。 (这种变化将使实现功能更加方便。)在Java中,尝试将其声明为字节数组。所以,最终的产品:

Finally are the pointers. You said they're byte arrays, so I'm puzzled why you don't declare them that way in your Delphi code. Either use PByte, or declare a new PByteArray type and use that. (That change will make implementing that function much more convenient.) In Java, try declaring them as byte arrays. So, the final product:

boolean DoX(byte[] InputBuffer,
            int IntputBufferSize,
            byte[] OutputBuffer,
            IntByReference OutputBufferSize);

这篇关于我如何将这个Delphi函数与JNA映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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