The Win32 API is quite large--it contains thousands of functions, constants, and data types. Of course, we don't want to write each WIN32 API function as a corresponding Java form. With this in mind, Microsoft has released a Java package that maps the WIN32 API to Java classes via J/direct. The name of the bag is called Com.ms.win32. When installing Java SDK 2.0, the package will be installed into our classpath if the appropriate settings are set in the installation options. This package consists of a large number of Java classes that fully reproduce the constants, data types, and functions of the Win32 API. The three categories with the greatest capacity for containment are user32.class,kernel.class and gdi32.class. They contain the core content of the Win32 API. To use them, simply import them in your Java code. The previous Showmsgbox example can be com.ms.win32 rewritten as follows (this also takes into account the use of unsatisfiedlinkerror in a more appropriate way):
Import com.ms.win32.*;
public class Usewin32package {public
static void Main (String args[]) {
try {
user32.messagebeep (
winm). mb_iconexclamation);
User32.messagebox (0,
"Created by the MessageBox () Win32 func",
"thinking in Java",
winm. Mb_okcancel |
Winm. mb_iconexclamation);
} catch (Unsatisfiedlinkerror e) {
System.out.println ("Can ' t link Win32 API");
System.out.println (e);
}
}
The Java package was imported on the first line. You can now invoke the MessageBeep () and MessageBox () functions without making other declarations. In MessageBeep (), we can see that the WIN32 constant is also declared when the package is imported. These constants are defined in a large number of Java interfaces, all named Winx (x represents the first letter of the constant to be used).
When writing this book, the development of the COM.MS.WIN32 package has not yet been formally completed, but it can be used.