简体   繁体   中英

JNA's equivalent to Windows 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 .

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 :

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

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

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.

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

The JNA FAQ includes this tidbit:

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();
}

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, then add the function you need.

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

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 is a user-maintained project. Please consider contributing your mapping to JNA so the next person won't need to create it themselves!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM