Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

java - JNA does not have a function from the Windows API

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 (in which case, extend Library), 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 WinUser.

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 can use your mapping and won't need to create it themselves!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...