我如何将这个 Delphi 函数与 JNA 映射

发布于 2024-07-15 22:52:09 字数 374 浏览 2 评论 0 原文

我有以下 Delphi 函数:

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

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

我已经成功地使用 JNA 从 dll 映射了一些我需要的函数,并且它们工作正常,但是这个函数给我带来了问题,任何帮助将不胜感激。

I have the following Delphi function:

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

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).

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画中仙 2024-07-22 22:52:09

大多数 JNA 文档假设您使用的是 C,而不是 Delphi,因此从与该函数等效的 C 开始:

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

您还需要正确的调用约定。 Delphi 的默认设置是注册,这可能不是您想要的。 使用stdcall来代替; 这就是其他所有 DLL 所使用的。

Java 没有与您使用的类型等效的无符号类型,因此首先忽略无符号。 这使得 InputBufferSize 成为 int。 您的函数返回布尔结果,因此请使用 boolean 作为其返回类型。 JNA 支持通过 的后代通过引用传递类型ByReference 类,因此使用 OutputBufferSizeIntByReference

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

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

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

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

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 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.

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文