Basic principle of Java Access COM

zhaozj2021-02-12  193

As a cross-platform language, Java has been successful in many environments. However, under the Windows platform, the development of Java has been limited by a certain degree. It is important reason that Java is currently not enough support for Windows component models, making Java programs difficult to multiplex rich components resources under Windows platforms, such as calendar, tab, Word, etc. (COM / ActiveX). The Windows component model is COM-based. Currently JDK does not provide any libraries that directly access COM. Therefore, we must be implemented through JNI if you need to access these resources. JNI is a bridge between the Java world and other languages, Java gets the service by accessing the JNI defined interface. On the other side of the JNI, we can implement these interfaces via C / C or other languages. We can create COM components through local language C / C , and use COM's services, finally returns the result to the Java program. Here, we involve several key issues. 1) Conversion of data types. The data type defined by Java and other languages ​​is endless, which makes we need to convert these data. In Windows, automated COM objects use variant as its primary data type. The Variant Type is a package of normal types, and we can easily convert it into Java corresponding types. For example, Variant_BOOL in Variant can directly correspond to Boolean in Java. However, some other data types of data are more troublesome, such as SafeArray and some pointers. Therefore, some Wrapper types are typically defined in Java in the implementation. 2) GUI handles a large number of ActiveX controls under Windows, and both interface services are provided. These classes are very good, with high reuse. These classes implements the IDispatch interface, so their use is relatively simple. However, Java's window management has great differences in Windows window management. Windows uses the handle management window. Java is managed by window class, for heavy components (AWT windows), each component has a homotropic body, that is, there is a local window corresponding to. For lightweight components (most of Swing), they have no bodies. Therefore, we can consider placing an ActiveX control on heavy components. Here we give an example description explanation how to use the homotubies technology to place an IE control on the Java's Panel. First, in the Java program, we receive a panel's homotropic window handle. The handle is represented by an INT. public int getHWND () {int hwnd = 0; DrawingSurfaceInfo drawingSurfaceInfo = ((DrawingSurface) (getPeer ())) getDrawingSurfaceInfo ();. // Get peer information if (null = drawingSurfaceInfo!) {drawingSurfaceInfo.lock (); Win32DrawingSurface win32DrawingSurface = (Win32DrawingSurface) drawingSurfaceInfo.getSurface (); hwnd = win32DrawingSurface.getHWnd (); // Get the window handle peer drawingSurfaceInfo.unlock ();} return hwnd;} we then through JNI method, pass this handle to C / C program.

转载请注明原文地址:https://www.9cbs.com/read-7204.html

New Post(0)