Using the bridge, Java calls any DLL

Source: Internet
Author: User

As you know, there is a special place for Java to Call DLL, that is, the DLL output function requires a special name:
Jniexport void jnicallJava _Classname_function name
(Jnienv *, jclass, other parameters)

{

}

DLL functions are always requiredJava _Start,How can I call getwindowtext in user32.dll? Direct calling is impossible. The following describes how to call any API in Java or any DLL similar to an API through a bridge.

First look at the running effect:

This is to useThe setjavaswrgn function is changed to a rounded corner window.

For the preparation of this bridge adapter, refer to the source code written by VC. Here we mainly introduce the Java SectionCode:

1. API

 

Public   Class Myapi {
Public   Static Dobject4j OS _api = New Dobject4j ( " Osapi " );
Public   Static Dvalue getwindowtext (dvalue hwnd ){
Dobject4j Param
= New Dobject4j ( " Apiparams " );

Param. Invoke (
" Appendhandlepointer " , Hwnd );
Param. Invoke (
" Allocasciistringbuffer " , New Dvalue ( 260 ));
Param. Invoke (
" Appendunsignedlong " , New Dvalue ( 260 ));

OS _api.invoke (
" Callosapi " , New Dvalue ( " User32.dll " ), New Dvalue ( " Getwindowtexta " ), New Dvalue (PARAM ));
Return Param. Invoke ( " Readasciistring " , New Dvalue ( 4 ));
}

Public Static Dvalue set1_wrgn (dvalue hwnd, dvalue hrgn, dvalue bredraw ){
Dobject4j Param =
New Dobject4j ("apiparams ");

Param. Invoke ("appendhandlepointer", hwnd );
Param. Invoke ("appendhandlepointer", hrgn );
Param. Invoke ("appendunsignedlong", bredraw );

Dvalue ret = OS _api.invoke ("callosapi ",
New Dvalue ("user32.dll "), New Dvalue ("setmediawrgn "), New   Dvalue (PARAM ));
Param = NULL;
Return RET;
}
// You can download the code for other APIs. The downloaded .dwidgetfile is in the zipformat. You can unzip it by renaming it.
}

 

2. Application Implementation

Public   Void Startupme (){
Frame
= New Jframe ( " Swing Application " );
................
Createcomponents (FRAME );
Frame. setlocation (
300 , 220 );
Frame. setsize (
300 , 130 );
String title
= " Round jframe " ;
Frame. settitle (title );
Frame. setvisible (
True );

................

Dvalue Desktop = Myapi. getmediatopwindow ();
Dvalue H
= Myapi. finddomainwex (desktop, Null , New Dvalue ( " Javax. Swing. jframe " ), New Dvalue (title ));
Dvalue hparentrgn = Myapi. createroundrectrgn ( New Dvalue ( 13 ), New Dvalue ( 3 ),
New Dvalue ( 290 ), New Dvalue ( 120 ), New Dvalue ( 15 ), New Dvalue ( 15 ));
Myapi. setjavaswrgn (H, hparentrgn,
New Dvalue ( True ));
Myapi. deleteobject (hparentrgn );
}

 

The two important classes of the bridge adapter are: Osapi And Apiparams . Osapi Responsible for API call implementation, Apiparams Organizes parameters. Osapi Callosapi The first parameter of the method is the DLL file name, such as user32.dll. The second parameter is the method name. Note that many windows APIs distinguish ANSI and Unicode, just like the familiar getwindowtext (), there are Getwindowtexta () And Getwindowtextw () The second parameter corresponds to ANSI and Unicode, and the third parameter is the API parameter. Apiparams There are many methods that support various types of parameters, such as appendunsignedlong () The method supports DWORD and appendhandlepointer. () Supports various handles. Apiparams It can also be nested, with appendstructurepointer () The method supports nested struct pointers, using appendstructure () Method Supports nested struct, and so on.

Click here to download the example code, and unzip the. dwidgetrename. ZIP file.

Click here to download the entire operationProgram

Click here to download this ie kernel BrowserSource code(The source code of the msscript. ocx call mechanism is not included, which is provided in the DLL form)

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.