Java calls windows APIs

Source: Internet
Author: User

Use jinvoke to call windowsapi. JNA is difficult to use and requires C code and parameter conversion. jinvoke is used just like a JDK package.

Official website reference: http://www.jinvoke.com/calling-the-win32-api-from-java

 

Example of a pop-up box (this code is used to call any DLL, not just windows, but also self-written DLL)

Import com. jinvoke. jinvoke;
Import com. jinvoke. nativeimport;

Public class hellowindows
{
@ Nativeimport (library = "USER32 ")
Public static native int MessageBox (INT hwnd,
String text,
String caption,
Int type); // Windows API MessageBox function prototype

Public static void main (string [] argc)
{
Jinvoke. initialize ();
MessageBox (0, "thisi MessageBox is a Native WIN32 MessageBox ",
"Hellow world", 0 );
}
}

Running effect:

 

 

*
You can use the above Code to call local DLL files.
Note: 1. Import com. jinvoke. jinvoke; import com. jinvoke. nativeimport; // reference the package
2. @ nativeimport (library = "USER32") // specify the local DLL file
3. Public static native int MessageBox (INT hwnd, string text, string caption, int type); // The Windows API MessageBox function prototype declares a method that can be used in the current Java class
4. jinvoke. initialize (); // instantiate jinvoke
5. MessageBox (0, "thisi MessageBox is a Native WIN32 MessageBox", "Hellow world", 0); // then you can call the functions in the DLL.

 

*
To use functions in user32.dll, you can directly use jinvoke. the USER32 class provided in jar does not need to use @ nativeimport (library = "USER32") to include a DLL, and then declare a method for converting the function type.

 

 

*

Jinvoke. jar must be included in the compilation: Java-classpath.; jinvoke. Jar hellowindows

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.