JNA 没有来自 Windows API 的函数 [英] JNA does not have a function from the Windows API

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

问题描述

我负责将 Windows JNI 代码移植到 Java,并使用 JNA.使用该库非常简单,显然它试图遵循 Windows API 的结构和风格(我不太了解 API,我遵循原始 JNI 代码).
我能够找到与大多数 Windows API 函数等效的 JNA,但找不到 EnableWindow.

I am in charge of porting Windows JNI code to Java, and have gone with JNA. Using the library is simple enough, as apparently it attempts to follow the structure and style of Windows' API (I do not know the API well, I follow the original JNI code).
I was able to find the JNA equivalent of most Windows API functions, but not EnableWindow.

这个函数定义在winuser.h 从逻辑上讲,应该在 com.sun.jna.platform.win32.WinUser 对吧?但是没有这样的功能,并且在com.sun.jna.platform.win32.WinUser.WS_DISABLED的文档中唯一提到了EnableWindow:

This function is defined in winuser.h and logically, one should find it under com.sun.jna.platform.win32.WinUser, right? However there is no such function, and the only mention of EnableWindow is in the documentation of com.sun.jna.platform.win32.WinUser.WS_DISABLED:

[...] 要在创建窗口后更改此设置,请使用 EnableWindow 函数.

[...] To change this after a window has been created, use the EnableWindow function.

就是这样,没有其他关于该功能的参考、提及或指示.文档的其余部分同样简洁,当人们不知道确切地知道该看什么和在哪里看时,它并不是很有帮助.

That's it, no other reference, mention or indication towards the function. The rest of the documentation is equally terse and not very helpful when one does not know exactly what and where to look.

那么 JNA 的 EnableWindow 在哪里(如果存在)?如果没有,可以用什么代替?

So where is JNA's EnableWindow, if it exists? And if it doesn't, what can be used in replacement?

推荐答案

JNA 有两个部分:核心功能(在 jna 工件中)和用户贡献的平台映射(在 jna 工件中)code>jna-platform 工件).当用户贡献函数和常量的映射时,他们通常将 Windows API 文档直接复制到 javadoc 中,因此经常引用尚未(尚未)映射的值.

There are two parts to JNA: the core functionality (in the jna artifact) and user-contributed platform mappings (in the jna-platform artifact). When users contribute mappings for functions and constants, they usually copy the Windows API documentation directly into the javadocs, so there are frequent references to values which have not (yet) been mapped.

正如您所观察到的,还没有用户为 EnableWindow 函数提供映射.那个用户可能就是你!

As you have observed, no user has (yet) contributed a mapping for the EnableWindow function. That user could be you!

JNA 常见问题解答 包含此花絮:

JNA 在其平台库映射中缺少函数 XXX

JNA is missing function XXX in its platform library mappings

不,不是,它只是在等你添加它:)

No, it's not, it's just waiting for you to add it :)

public interface MyUser32 extends User32 {
   // DEFAULT_OPTIONS is critical for W32 API functions to simplify ASCII/UNICODE details
   MyUser32 INSTANCE = (MyUser32)Native.load("user32", W32APIOptions.DEFAULT_OPTIONS);
   void ThatFunctionYouReallyNeed();
}

这基本上是您自己添加函数的模板:扩展现有库(如果它被部分映射)或创建一个新库,如果它没有被映射(在这种情况下,扩展 Library),然后添加你需要的功能.

That's basically the template for adding a function on your own: extend the existing library (if it's partially mapped) or create a new library if it hasn't been mapped (in which case, extend Library), then add the function you need.

WinUser 与大多数映射略有不同;它不包含库加载语句,因为它只是头文件,而在 JNA 中,DLL 加载库扩展了 WinUser.

WinUser is slightly different than most mappings; it doesn't include the library loading statement as it's just the header file, and in JNA, the DLL-loading library extends WinUser.

所以你需要做更多的研究,看看加载哪个 DLL 来本地访问该函数.文档表明它是 user32.dll,就像 JNA 常见问题示例一样!

So you need to do a little bit more research to see which DLL to load to access the function natively. The docs indicate it's the user32.dll, just like the JNA FAQ example!

所以模板在上面,你只需要函数映射.Windows BOOL 映射到 Java 的 boolean,因此您只需要在您自己的代码库中执行此操作:

So the template is above, you just need the function mapping. Windows BOOL maps to Java's boolean so you just need to do this in your own codebase:

public interface MyUser32 extends User32 {
    MyUser32 INSTANCE = (MyUser32) Native.load("user32", W32APIOptions.DEFAULT_OPTIONS);

    boolean EnableWindow(HWND hWnd, boolean bEnable);
}

这将解决您在自己的项目中的紧迫需求.

That will solve your immediate needs in your own project.

JNA 是一个用户维护的项目.请考虑将您的映射贡献给 JNA,以便下一个人可以使用您的映射,而无需自己创建它!

JNA is a user-maintained project. Please consider contributing your mapping to JNA so the next person can use your mapping and won't need to create it themselves!

这篇关于JNA 没有来自 Windows API 的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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