Objective
Recently studied the Google browser kernel. Google Browser kernel has been open source, and maintain the update, its open source project in the kernel update speed and Chrome browser version update the same progress! And it's different from WebKit (it's worth noting that Google Chrome doesn't use the WebKit kernel anymore), it provides more than just page rendering, but rather a full browser solution and plug-in rules.
Easy to use: we give it a "form" (the local form in the operating system or System Explorer, this series uses the Win32 platform as an example) and some configuration parameters, it will be able to make the page you need to render a perfect display in a given window.
Plug-in support: Adobe and Google jointly developed Pepperflashplayer features, and we can install it as an out-of-process plugin without having to worry about its automatic upgrades to users or the version changes in our development. And you just need a code to complete the plug-in, and it's easy to get and upgrade the plugin (install a Chrome browser on your computer and go to the installation directory copy:-_-). Google's plugin for PDFs can do the same.
This essay series mainly use Java to Google Browse set a shell. Because CEF (ie the "Google Chrome kernel chromium embedded framework", the latter uses CEF as the abbreviation, and this series uses the CEF3) written in C + +, does not directly provide the Java language API, although there is a maintenance version of the Java version, But I think it's not good.
Get AWT form Handle
What we are going to do today is not very much related to the CEF kernel, let's fix a problem: Get a handle to the Java form.
As we all know: the GUI support provided by the Java language is built on the support of the operating system resource management system (or the desktop environment) (in Java 2d/gui, the outermost form is definitely operating system related), then it is very simple, We can use some of the JNI APIs to get the form handle.
JNI is a localized code invocation interface provided by the Java language (in the Java Virtual machine actually does not care whether the next method entry is an internal pointer or an external-operating system pointer), we can write a C + + function to find the form handle, and then return to the Java virtual machine, Let us also know within the virtual machine that a Java form is assigned a handle by the operating system.
Java official has taken into account our needs, it provides an interface: JAWT. Includes a series of C/C + + containing (header file. h, platform-dependent) and a series of C/C + + static libraries.
Specifically, Jawt.h, Jawt_md.h, Jawt.lib (additional jni.h and jni_md.h are required for use with JNI)
Writing a dynamic-link library (DLL) requires a co-compilation with the c/++ source file using a C + + header file. We first use the Javah tool (Javah.exe) that comes with the JDK to generate a header file and implement it.
Of course, let's first write a Java class and annotate its native method.
1 /*2 * Copyright Jootmir Project3 * Licensed under the Apache License, Version 2.0 (the "License");4 * You are not a use this file except in compliance with the License.5 * Obtain a copy of the License at6 7 http://www.apache.org/licenses/LICENSE-2.08 9 * unless required by applicable or agreed to writing, softwareTen * Distributed under the License is distributed on a "as is" BASIS, One * without warranties or CONDITIONS of any KIND, either express or implied. A * See the License for the specific language governing permissions and - * Limitations under the License. - * the * Support:http://www.cnblogs.com/johness - */ - PackageJohness.jcef3.util; - + ImportJavax.swing.JFrame; - + /** A * AWT Tool Set at * - * @authorShawryan - */ - Public Final classAwtutil { - /** - * Get a handle to a form (under Windows platform) in * - * @paramwindow to * Form object that needs to get handle + * @returnform Application handle - */ the Public Static native intgetwindowhandleinwindows (JFrame window); *}
Awtutil
It then uses Javah to generate its corresponding header file.
The contents of the header file are as follows:
/*Do not EDIT this file-it are machine generated*/#include<jni.h>/*Header for Class Johness_jcef3_util_awtutil*/#ifndef _included_johness_jcef3_util_awtutil#define_included_johness_jcef3_util_awtutil#ifdef __cplusplusextern "C" {#endif/** Class:johness_jcef3_util_awtutil * method:getwindowhandleinwindows * Signature: (ljavax/swing/jframe;) I
*/jniexport jint jnicall java_johness_jcef3_util_awtutil_getwindowhandleinwindows (jnienv*, Jclass, jobject); #ifdef __cplusplus}#endif#endif
I will not repeat the details of the JNI.
Next we use C + + code to implement the interface function and compile it into a dynamic-link library.
Classic vc++6.0 or Good dev C + + I don't like to use it, I write and compile it with visual Studio 2012来.
Build a project
Delete everything we don't need (we don't need the-_-) file
Copy the JNI and JAWT related header files and library files into the project ( it's worth saying that you copied and pasted into the VS window and actually copied it into your C + + project folder )
%java_home%\include\jni.h
%java_home%\include\jawt.h
%java_home%\include\jni_md.h
%java_home%\include\jawt_md.h
%java_home%\lib\jawt.lib
That header file in your project.
Configuration Items
Configuration item is release
Configuration item using Jawt.lib Static library
Change the header file, make # include <jni.h> change to # include "Jni.h"
(The trailing bar is the cursor)
Write source file
( Remove the source file precompiled header before you create the source file and prepare to start writing code. )
Start coding (There are examples of use in jawt.h, we change)
1#include"jni.h"2#include"jawt_md.h"3#include"johness_jcef3_util_awtutil.h"4 5Jniexport jint jnicall java_johness_jcef3_util_awtutil_getwindowhandleinwindows (jnienv *env, Jclass sender, Jobject window) {6HWND hwnd =NULL;7 8Jawt_drawingsurface *ds;9Jawt_drawingsurfaceinfo *DSi;TenJawt_win32drawingsurfaceinfo *win; One A jawt AWT; -Awt.version =jawt_version_1_3; - theJboolean result = JAWT_GETAWT (env, &AWT); - if(Result = =jni_true) { -DS =AWT. Getdrawingsurface (env, window); -JintLock= ds-Lock (DS); + if(Lock!=jawt_lock_error) { -DSI = dsGetdrawingsurfaceinfo (DS); +Win = (Jawt_win32drawingsurfaceinfo *) DSiPlatformInfo; A athwnd = Win-hwnd; - -DS-Freedrawingsurfaceinfo (DSI); -DS-Unlock (DS); - AWT. Freedrawingsurface (DS); - returnJint (HWND); in } - return 0; to } + return 0; -}
Johness_jcef3_util_awtutil
Compile Build
Copy the generated DLL into the Java project, configure LibraryPath in the project configuration, and write the test code.
The test code is also simple:
PackageJohness.jcef3.util;ImportJavax.swing.JFrame; Public classMain { Public Static voidMain (string[] args) {system.loadlibrary ("Jawt"); System.loadlibrary ("JCEF3"); JFrame Frame=NewJFrame (); Frame.setsize (400, 300); Frame.setvisible (true); Frame.setdefaultcloseoperation (Jframe.exit_on_close); System.out.println (Awtutil.getwindowhandleinwindows (frame)); }}
Final summary
Use Visual Studio compile-time to be careful not to use precompiled file headers. Of course, if you know a little bit about VC, it may not be so troublesome.
Compiling a generated DLL using Visual Studio requires a runtime environment, such as Msvcr110.dll. If you don't have one installed on your machine, you might get the "can ' t find dependent libraries" error, and you can package the libraries of these runtime environments in the past.
...... Also, think about it again.
Contact me, communicate with each other
Welcome to our Communication group, when we are bored together to pass the time:
or through QQ to contact me:
(last edit time 2015-01-26 20:03:51)
jcef3--Google Browser core Java implementation (a): Use JAWT to get the form handle