Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Hosting .NET Controls in Java : Page 2

Using JNI and some COM interop magic, you can host .NET Windows controls directly in your Java applications.


advertisement
Designing the Java Container Control
You need a Java control to host the .NET control. Both AWT and Swing controls provide a window handle, or HWND, to which you can attach a Windows control. That control can be a simple java.awt.Canvas to which the Java Virtual Machine (JVM) provides an HWND.

Because you'll be using JNI to attach the .NET control to the Java control's HWND, you need to add the following line into the class's static constructor:

public class COMControl { static { System.loadLibrary("JNIBridge"); } // See sample code for implementation. }

That line tells the JVM to load the JNIBridge.dll (or JNIBridge.so library in BSD or Linux) when it loads the class. The system uses the JNIBridge library whenever your code calls any native methods in the class. The library must exist in a directory specified in the PATH environment variable as with most libraries. The JVM will not, however, look for libraries in the current working directory. You'll see a workaround for that later in this article.

Declaring the native methods is trivial. They are simply declarations that use the native Java keyword. Later, you'll create a C header file from these definitions. Two native methods are required to initialize and destroy the .NET control. The JVM calls the addNotify and removeNotify methods to allow native code to attach controls to a window handle and to destroy those controls, so the native methods will be called from the implementation of these two methods. The native methods are declared below, and the two methods are overridden that call these two native methods:

public class COMControl { // See sample code for implementation. public void addNotify() { super.addNotify(); initialize(); } public void removeNotify() { destroy(); super.removeNotify(); } private native void initialize(); private native void destroy(); }

Other native methods are similarly called by overridden methods.

Compile the class using javac.exe from the Java Development Kit (JDK), for example:



javac.exe *.java

Creating the C Header for Native Methods
After the Java class is compiled, you must create a C header file that declares the native functions to be implemented. The javah.exe application makes this procedure easy by reading the class information and automatically writing a C header file using the specially named functions for each declared native method:

javah.exe -jni -o JNIBridge.h COMControl

The preceding code line creates functions in the form Java_<Class>_<Method>. You can find out more in the JNI tutorial. For example, here's the generated COMControl.initialize method:

JNIEXPORT void JNICALL Java_COMControl_initialize(JNIEnv *, jobject);

The JNIEnv parameter references the Java environment and you can use it to access classes, methods, and properties exposed by the JVM. The second parameter references the object itself—an instance of COMControl class in this case.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap