SETT
SETT

JVM to .NET Interop

by Charles Calkins, Principal Software Engineer

May 2014

Introduction

Writing mixed-language applications is common in today's software development, such as using JavaScript in a web browser and Java or Scala on the server side of a web application, or a script-based data analysis process invoking C or FORTRAN-based mathematics routines for increased performance. This article describes one form of multi-language development, where software written for Microsoft's .NET Framework invokes functions executing in a Java Virtual Machine.

A Real-World Need

Over the last year, the author has been writing a web application in Scala to complement a well-established desktop application that is written in C++ and C#. The web application presents graphs and reports in a manner similar to the desktop application, but provides delivery to Internet-based mobile devices. Both applications share the same underlying database, where some tables contain records of packed binary blobs. Code could be written for each application — once in C# for the desktop application, and again in Scala for the web one — to pack and unpack these blobs, but, to avoid code duplication, it was decided that a single library written in Scala should be invoked by C#.

Perhaps surprisingly, there are several ways this can be accomplished, as shown here:

Scala Diagram

In this diagram, the relationships between the projects in this article are illustrated. The implementation language is identified by the shape: Java/Scala as a rectangle, C++ as an oval, and C# as a hexagon; and the runtime environment is shown by the border: the Java Virtual Machine as a dotted border, native code as a solid border, and the .NET Framework as a dashed border.

Starting on the left in the diagram, two ways to expose code on the JVM to the outside world are via the Java Native Interface (JNI), and by IKVM.NET:

This article will explore these methods in detail.

The Java and Scala to Invoke

For our examples, we will consider two classes: Custom and Adder. In Java, Custom is defined in the javalib package, consisting of a constructor which stores a value into a member variable, and a getter to retrieve it:

  1. // javalib\src\main\java\javalib\Custom.java
  2. package javalib;
  3.  
  4. public class Custom {
  5. private final int i;
  6.  
  7. public Custom(int i) {
  8. this.i = i;
  9. }
  10.  
  11. public int get() {
  12. return i;
  13. }
  14. }

Class Adder has two methods. The first, add() adds an integer to the contents of a Custom object. The second, name(), is a static method that returns a string that gives the name of the Adder.

  1. // javalib\src\main\java\javalib\Adder.java
  2. package javalib;
  3.  
  4. public class Adder {
  5. public int add(int a, Custom b) {
  6. return a + b.get();
  7. }
  8.  
  9. public static String name() {
  10. return "javalib Adder";
  11. }
  12. }

In Scala, the code is functionally the same, but is much more compact, with the entire scalalib package defined in half the number of lines of javalib. The class Custom is declared in a single line, where val automatically generates a getter for i. Method add() is on class Adder, but name() is part of Adder's companion object.

  1. // scalalib\src\main\scala\scalalib\scalalib.scala
  2. package scalalib
  3.  
  4. class Custom(val i: Int) {}
  5.  
  6. class Adder {
  7. def add(a: Int, b: Custom) =
  8. a + b.i
  9. }
  10.  
  11. object Adder {
  12. def name() =
  13. "scalalib Adder"
  14. }

The JAD Java Decompiler, applied to the CLASS files generated by the compilation of scalalib.scala show a representation of what Scala generates behind the scenes.

Class Custom is very similar to the class written by hand in Java, although the getter for i is named i(), rather than get().

  1. // Custom.jad
  2. package scalalib;
  3.  
  4. public class Custom
  5. {
  6. public int i()
  7. {
  8. return i;
  9. }
  10.  
  11. public Custom(int i)
  12. {
  13. this.i = i;
  14. super();
  15. }
  16.  
  17. private final int i;
  18. }

Class Adder is emitted as two separate classes, one for the main class and one for the companion object. The class corresponding to the companion object, Adder$, implements name() and contains a static reference to itself named MODULE$.

  1. // Adder$.jad
  2. package scalalib;
  3.  
  4. public final class Adder$
  5. {
  6. public String name()
  7. {
  8. return "scalalib Adder";
  9. }
  10.  
  11. private Adder$()
  12. {
  13. }
  14.  
  15. public static final Adder$ MODULE$ = this;
  16.  
  17. static
  18. {
  19. new Adder$();
  20. }
  21. }

The second emitted class, Adder, implements add(), as well as name() as a static method, delegating to class Adder$ for its behavior.

  1. // Adder.jad
  2. package scalalib;
  3.  
  4. public class Adder
  5. {
  6. public static String name()
  7. {
  8. return Adder$.MODULE$.name();
  9. }
  10.  
  11. public int add(int a, Custom b)
  12. {
  13. return a + b.i();
  14. }
  15.  
  16. public Adder()
  17. {
  18. }
  19. }

Part of the execution of buildikvm.bat invokes the sbt assembly command in both the javalib and scalalib directories. The Simple Build Tool is able to compile both Java and Scala code, and the sbt-assembly plugin for sbt packs all of the files in a project into a single JAR file, also making it a runnable JAR if applicable. Here, the result of sbt assembly creates javalib-assembly-1.0.jar and scalalib-assembly-1.0.jar, respectively.

Interop with Java Native Interface (JNI)

Our use of JNI consists of three phases. The first is the creation of a Java VM that includes the javalib and scalalib JAR files in its classpath. The second is obtaining references to the methods in the classes contained within. The previous section showed that, for Java, two classes need to be invoked through JNI — Custom and Adder. For Scala, there are three — CustomAdder, and Adder$. The third phase invokes the methods to create a Custom object, and then add() it to an integer.

To reduce complexity, we shall create a wrapper class, Context, in C++, to contain all of the JNI references that must be maintained. From the JDK, we include jni.h, and link to jvm.lib, for JNI functionality. The first pointers we must store are to the JVM and to the JNI environment.

  1. // JNI\Context.h
  2. #include <jni.h>
  3.  
  4. class Context {
  5. JavaVM *_jvm;
  6. JNIEnv *_env;
  7. ...

The function JNI_CreateJavaVM() creates a Java virtual machine. Although the API allows for multiple VMs to be created, implementations generally only allow for just one, and calling the function again returns an error. Arguments that must be supplied to JNI_CreateJavaVM() include the JNI version to use, as well as where the CLASS files are located that are to be used by the VM. Here, relative paths to the javalib and scalalib JARs are provided.

  1. // JNI\Context.cpp
  2. Context::Context() {
  3. ...
  4. JavaVMInitArgs vmArgs;
  5. JavaVMOption vmOptions;
  6. vmOptions.optionString =
  7. "-Djava.class.path="
  8. "..\\..\\javalib\\target\\scala-2.10\\javalib-assembly-1.0.jar;"
  9. "..\\..\\scalalib\\target\\scala-2.10\\scalalib-assembly-1.0.jar";
  10. vmArgs.version = JNI_VERSION_1_6;
  11. vmArgs.nOptions = 1;
  12. vmArgs.options = &vmOptions;
  13. vmArgs.ignoreUnrecognized = 0;
  14.  
  15. int success = JNI_CreateJavaVM(&_jvm, (void**)&_env, &vmArgs);
  16. if (success < 0)
  17. return;
  18. ...

If the call to JNI_CreateJavaVM() is successful, then valid pointers to the JVM and the environment are returned. The pointer to the JVM is used to shut down the system, via a call to _jvm->DestroyJavaVM() in the destructor, while the pointer to the environment is used for interaction with the VM.

We next must obtain references to the methods that we wish to invoke. Executing javap -s on each CLASS file reveals the function signatures that we must use. For the Java class Adder, the output is:

  1. public class javalib.Adder {
  2. public javalib.Adder();
  3. Signature: ()V
  4.  
  5. public int add(int, javalib.Custom);
  6. Signature: (ILjavalib/Custom;)I
  7.  
  8. public static java.lang.String name();
  9. Signature: ()Ljava/lang/String;
  10. }

For each method, the signature shows the types of the parameters within parentheses, followed by the return type. For example, the signature (ILjavalib/Custom;)I of add() indicates that it takes two parameters: an integer (the I inside the opening parenthesis), and a reference to an instance of Custom (Ljavalib/Custom;), and it returns an integer (the I after the parenthesis).

Class Context stores references to the class and individual methods that are obtained.

  1. // JNI\Context.h
  2. ...
  3.     jclass _clsJavaAdder;
  4.     jmethodID _midJavaAdderConstructor;
  5.     jmethodID _midJavaAdderAdd;
  6.     jmethodID _midJavaAdderName;
  7. ...

The class reference is returned via a call to FindClass(). As shown in the signatures, the full path to the class must be provided, with slashes separating the components. Given the class reference, calls to GetMethodID() and GetStaticMethodID() obtain references to instance and static methods, respectively. The name of the method to locate is passed as an argument (with <init> used to reference the class's constructor), as well as its signature, which allows overloaded methods to be uniquely determined.

  1. // JNI\Context.cpp
  2. ...
  3. _clsJavaAdder = _env->FindClass("javalib/Adder");
  4. if (_clsJavaAdder != 0) {
  5. _midJavaAdderConstructor =
  6. _env->GetMethodID(_clsJavaAdder, "<init>", "()V");
  7. _midJavaAdderAdd =
  8. _env->GetMethodID(_clsJavaAdder, "add", "(ILjavalib/Custom;)I");
  9. _midJavaAdderName =
  10. _env->GetStaticMethodID(_clsJavaAdder, "name", "()Ljava/lang/String;");
  11. }
  12. ...

References to other methods are obtained similarly. As shown by the output of the JED disassembler above, the Scala method name() can be either obtained as a static method of Adder, or as an instance method of Adder$, as we do here:

  1. // JNI\Context.cpp
  2. ...
  3. _clsScalaAdderDollar = _env->FindClass("scalalib/Adder$");
  4. if (_clsScalaAdderDollar != 0) {
  5. _midScalaAdderDollarName =
  6. _env->GetMethodID(_clsScalaAdderDollar, "name", "()Ljava/lang/String;");
  7. }

Once all references to classes and methods are successfully obtained, we can create methods of Context which wrap the functionality needed to invoke them. For example, an object is created via a call to NewObject(), as shown here, to create an instance of Custom. The first parameter to NewObject() is a reference to the class to instantiate an instance of, the second a reference to the constructor to invoke, and lastly any constructor parameters, as needed. Here, the parameter is a Java int, with the JNI type of jint, corresponding to a C++ long. A pointer to the newly-created object is returned as a void * to the caller.

  1. // JNI\Context.cpp
  2. void *Context::JavaCreateCustom(long i) {
  3. if (_env && _clsJavaCustom && _midJavaCustomConstructor)
  4. return _env->NewObject(_clsJavaCustom, _midJavaCustomConstructor, (jint)i);
  5. return 0;
  6. }

The way in which a method is invoked depends upon its parameters and return type. As add() is an instance method that returns an intCallIntMethod() is used. An invocation is encapsulated as:

  1. // JNI\Context.cpp
  2. long Context::JavaAdd(void *pObjAdder, long a, void *pObjCustom) {
  3. if (_env && pObjAdder && _midJavaAdderAdd && pObjCustom)
  4. return _env->CallIntMethod((jobject)pObjAdder, _midJavaAdderAdd, (jint)a, (jobject)pObjCustom);
  5. return 0;
  6. }

The add() method is not static, so requires both a reference to an instance of an object to call it on (here, pObjAdder), as well as a reference to the method itself. The parameters following are ones to pass to add() — an integer, and a reference to an instance of Custom.

The name() method is static, and returns a string object, so is invoked by a call to CallStaticObjectMethod(). A pointer to a character array is obtained from the string object through a call to GetStringUTFChars(). After the string is copied into the output buffer, the string is then freed via a call to ReleaseStringUTFChars().

  1. // JNI\Context.cpp
  2. void Context::JavaName(char *buf, int buflen) {
  3. if (_env && _clsJavaAdder && _midJavaAdderName) {
  4. jstring strName = (jstring)_env->CallStaticObjectMethod(_clsJavaAdder, _midJavaAdderName);
  5. const char *pName = _env->GetStringUTFChars(strName, 0);
  6. strncpy_s(buf, buflen, pName, _TRUNCATE);
  7. _env->ReleaseStringUTFChars(strName, pName);
  8. }
  9. }

Additional methods are written similarly to encapsulate the creation and invocation of the other classes and methods in javalib and scalalib.

The JNITest project is a C++ application which tests the JNI wrapper by instantiating a Context and calling its methods. For Java, it displays the object name, followed by creating an instance of Custom holding the value 3, and lastly add 9 to the value of the Custom object. It does the same for Scala, but using the values 4 and 7.

  1. // JNITest\JNITest.cpp
  2. int main(int argc, char* argv[])
  3. {
  4. char buf[30];
  5. Context C;
  6. C.JavaName(buf, sizeof(buf));
  7. std::cout << "Name: " << buf << std::endl;
  8. void *pJavaCustom = C.JavaCreateCustom(3);
  9. void *pJavaAdder = C.JavaCreateAdder();
  10. std::cout << C.JavaAdd(pJavaAdder, 9, pJavaCustom) << std::endl;
  11.  
  12. C.ScalaName(buf, sizeof(buf));
  13. std::cout << "Name: " << buf << std::endl;
  14. void *pScalaCustom = C.ScalaCreateCustom(4);
  15. void *pScalaAdder = C.ScalaCreateAdder();
  16. std::cout << C.ScalaAdd(pScalaAdder, 7, pScalaCustom) << std::endl;
  17.  
  18. return 0;
  19. }

The output of the test is:

Name: javalib Adder
12
Name: scalalib Adder
11

Platform Invocation Services (P/Invoke)

Platform Invocation Services, known as P/Invoke, is a method by which .NET Framework (Common Language Infrastructure) code can call native code, such as Windows API functions in operating system DLLs. The functions that are called are C-style, so we must convert Custom into a C-style API. While JNI functions could be used directly, this process provides greater encapsulation, as the JVM pointer, the environment and all of the class and method references would otherwise have to be marshalled separately.

We create the C-style wrapper of Context in the CPPWrapper_Native DLL project, which exports these functions for the caller to use.

Functions in a DLL can be exported in two ways: using a separate DEF file to list the functions to export, or to mark the functions where they are defined with __declspec(dllexport). We shall use the latter method.

The calling convention of each exported function must be specified, which determines such things as the order of arguments on the stack and name decoration conventions. P/Invoke, unless specified otherwise, uses the __stdcall calling convention, so each method is declared to use it.

As it is a C-style wrapper, the functions must have C, rather than C++ linkage, so are declared within an extern "C" block.

The rules above give annotated functions as follows. The first function is used to create a Context, and the remainder take a pointer to it as their first argument.

  1. // CPPWrapper_Native\CPPWrapper_Native.cpp
  2. extern "C" {
  3. __declspec(dllexport) void * __stdcall CreateContext() {
  4. return new Context();
  5. }
  6.  
  7. __declspec(dllexport) void __stdcall JavaName(void *pContext, char *buf, int buflen) {
  8. static_cast<Context *>(pContext)->JavaName(buf, buflen);
  9. }
  10.  
  11. __declspec(dllexport) void * __stdcall JavaCreateCustom(void *pContext, long i) {
  12. return static_cast<Context *>(pContext)->JavaCreateCustom(i);
  13. }
  14. ...

In the C# application which consumes this DLL (the CS_PINVOKE project), each function must be declared using the P/Invoke syntax. Each function is prefixed with a DllImport annotation, indicating where the native function can be found. Each function is declared extern, and uses types that are appropriate to the .NET Framework. The IntPtr type has the size of an integer on the platform, and is the equivalent of void *. The P/Invoke syntax does provide a few more complex types to aid in the interoperation, such as the .NET StringBuilder to be automatically populated by a character buffer that is copied into on the native side.

  1. // CS_PINVOKE\CS_PINVOKE.cs
  2. class CS_PINVOKE
  3. {
  4. [DllImport("CPPWrapper_Native.dll")]
  5. public static extern IntPtr CreateContext();
  6.  
  7. [DllImport("CPPWrapper_Native.dll")]
  8. public static extern void JavaName(IntPtr context, StringBuilder buf, int buflen);
  9.  
  10. [DllImport("CPPWrapper_Native.dll")]
  11. public static extern IntPtr JavaCreateCustom(IntPtr context, long i);
  12. ...

Once declared in this manner, they can be called as any other method in C#. The output of CS_PINVOKE.cs, as generated by the following, is the same as that of JNITest.

  1. // CS_PINVOKE\CS_PINVOKE.cs
  2. static void Main(string[] args)
  3. {
  4. IntPtr context = CreateContext();
  5. StringBuilder javaBuf = new StringBuilder(40);
  6. JavaName(context, javaBuf, javaBuf.Capacity);
  7. Console.WriteLine(javaBuf.ToString());
  8. IntPtr pJavaCustom = JavaCreateCustom(context, 3);
  9. IntPtr pJavaAdder = JavaCreateAdder(context);
  10. Console.WriteLine(JavaAdd(context, pJavaAdder, 9, pJavaCustom));
  11.  
  12. StringBuilder scalaBuf = new StringBuilder(40);
  13. ScalaName(context, scalaBuf, scalaBuf.Capacity);
  14. Console.WriteLine(scalaBuf.ToString());
  15. IntPtr pScalaCustom = ScalaCreateCustom(context, 4);
  16. IntPtr pScalaAdder = ScalaCreateAdder(context);
  17. Console.WriteLine(ScalaAdd(context, pScalaAdder, 7, pScalaCustom));
  18. }

P/Invoke is especially useful when a native DLL is to be used by C#, but the DLL itself cannot be modified. The P/Invoke syntax allows the methods of the DLL to be used directly, provided the appropriate declarations are made. There is even a web site that contains declarations to allow the use of Windows system libraries.

C++/CLI

An implicit form of P/Invoke is provided by C++/CLI. C++/CLI provides additional C++ keywords to allow C++ classes to be created as .NET types, as well as the /clr compiler flag, which allows the application to use features in the Common Language Runtime. Depending upon the particular /clr option selected, the resulting file may contain native code, only Common Intermediate Language bytecode, or a mix of the two.

The CPPWrapper_CPPCLI project provides a C++/CLI wrapper for the Context class. We begin by using the System namespace, to include CLR types. The using namespace syntax is similar to using in C# or import in Java and Scala.

In the namespace CPPWrapper_DotNET, we create a class with the same name to wrap Context. The ref keyword indicates that this is a managed .NET class to be allocated on the garbage-collected heap, rather than a native C++ class to be allocated on the native heap.

An unmanaged pointer to an instance of Context is stored — a ref class cannot contain an unmanaged object directly, but can contain pointers to them. The constructor instantiates a Context on the native heap and sets the pointer, and the destructor frees it.

  1. // CPPWrapper_CPPCLI\CPPWrapper_CPPCLI.cpp
  2. #include "Context.h"
  3.  
  4. using namespace System;
  5.  
  6. namespace CPPWrapper_DotNET {
  7.  
  8. public ref class CPPWrapper_DotNET {
  9. Context *_pContext;
  10. public:
  11.  
  12. CPPWrapper_DotNET() {
  13. _pContext = new Context();
  14. }
  15.  
  16. ~CPPWrapper_DotNET() {
  17. delete _pContext;
  18. }
  19. ...

The methods of the CPPWrapper_DotNET class wrap the calls to Context, as shown by this sample:

  1. // CPPWrapper_CPPCLI\CPPWrapper_CPPCLI.cpp
  2. String^ JavaName() {
  3. char buf[40];
  4. _pContext->JavaName(buf, sizeof(buf));
  5. return gcnew String(buf);
  6. }
  7.  
  8. IntPtr JavaCreateCustom(long i) {
  9. return IntPtr(_pContext->JavaCreateCustom(i));
  10. }
  11. ...
  12. Int64 JavaAdd(IntPtr pJavaAdder, long a, IntPtr pJavaCustom) {
  13. return _pContext->JavaAdd(pJavaAdder.ToPointer(), a, pJavaCustom.ToPointer());
  14. }
  15. ...

In the JavaName() method, the unmanaged buffer set by Context::JavaName() is used to construct a managed .NET string. The gcnew keyword indicates that the object should be allocated on the garbage-collected heap, and the resulting object reference (signified by the caret) is returned by the method to its caller.

As in the P/Invoke case, a void * becomes an IntPtr. The constructor for IntPtr accepts a void *, and the ToPointer() method returns it.

The CS_CPPCLI project uses the wrapper defined above. As the class CPPWrapper_CPPCLI is already a managed class, it is used directly by C#, without the need for additional declarations. The entire program consists of:

  1. // CS_CPPCLI\CS_CPPCLI.cs
  2. using System;
  3.  
  4. namespace CS_CPPCLI
  5. {
  6. class CS_CPPCLI
  7. {
  8. static void Main(string[] args)
  9. {
  10. CPPWrapper_DotNET.CPPWrapper_DotNET context = new CPPWrapper_DotNET.CPPWrapper_DotNET();
  11. Console.WriteLine(context.JavaName());
  12. IntPtr pJavaCustom = context.JavaCreateCustom(3);
  13. IntPtr pJavaAdder = context.JavaCreateAdder();
  14. Console.WriteLine(context.JavaAdd(pJavaAdder, 9, pJavaCustom));
  15. Console.WriteLine(context.ScalaName());
  16. IntPtr pScalaCustom = context.ScalaCreateCustom(4);
  17. IntPtr pScalaAdder = context.ScalaCreateAdder();
  18. Console.WriteLine(context.ScalaAdd(pScalaAdder, 7, pScalaCustom));
  19. }
  20. }
  21. }

Its output matches that of JNITest. C++/CLI is a very straightforward syntax, but does require code to be written in the .NET way – the use of ref class, compilation with the /clr flag, and such, which may not be appropriate for legacy projects. As long as a project can be compiled with the /clr flag, however, native classes and managed classes can coexist, so the addition of CLR types can be done incrementally. Adding /clr to a legacy C++ application is not necessarily an otherwise transparent change. For example, one executable that the author compiled in this manner would no longer run until string pooling was also enabled — by compiling with /clr, the structure of an internal string table changed such that there were more strings in the application than would fit in the table. Fortunately, once common strings were pooled together, the limit was no longer exceeded, and the application functioned normally.

Component Object Model (COM)

COM is an old Microsoft technology with roots in the early 1990s and Windows 3.1 days, although it is still used today in libraries such as DirectX. A COM server provides one or more COM objects for use. A server can be implemented as a DLL that is loaded into the same process that references the object (an in-process server), or implemented as a standalone executable (an out-of-process server). A COM object provides sets of functionality grouped into interfaces. One standard interface, IUnknown, must be implemented. The methods of IUnknown are AddRef() to increase the reference count of an interface, Release() to decrement the reference count, and QueryInterface() to obtain an interface pointer to a specifically requested interface. Objects and interfaces are identified by globally unique numeric identifiers. Additionally, objects also can be identified by a human-readable name.

For instance, if the Java and Scala example at the beginning of this article was implemented directly in COM, it would consist of two COM objects, each implementing IUnknown as well as an appropriate interface for the object's functionality, such as, perhaps, interface IAdd implementing the add() method. Rather than passing object references via void *, COM interface pointers would be used instead.

The methodology of requesting a pointer to an interface allows for decoupling clients and servers in that they can be upgraded independently. Consider a version 1 object implementing version 1 of an interface, and a version 1 client making use of that interface. Let the object be upgraded to version 2, with an additional version 2 interface. Provided that it still supports the version 1 interface, the client does not need to change — the client would still request the version 1 interface, which the object would still provide. If the client is upgraded to version 2, it can request the version 2 interface, and operate with enhanced behavior. If the client was upgraded, but not the object, then the client can operate with degraded functionality — the version 2 interface can be requested, and, when the client discovers that it is not available, can request the version 1 interface and still operate, but with degraded functionality.

Creating a COM server and COM object that is served by it is a cumbersome task if the code is written by hand. Fortunately, Visual Studio provides wizards for server and object creation.

For our wrapper, we create a single in-process COM server. Right-click on the name of the solution in the Solution Explorer, select Add->New Project... and choose the ATL Project. Name it CPPWrapper_COM and under Application Settings select the checkbox to Allow merging of proxy/stub code. Ensure that the DLL radio button is selected, and click Finish.

With the server created, a COM object can be added to it. Right-click on the CPPWrapper_COM project, and select Add->Class..., choose the ATL Simple Object, and click the Add button. Give it a Short name: of COMWrapper, and click Finish to accept all other settings.

Finally, switch from the Solution Explorer to the Class View, expand COMWrapper_COM, right-click on ICOMWrapper, and select Add->Add Method.... This can be done repeatedly to add each additional interface method. For example, give a Method name: of JavaName. Create one argument to it by providing a Parameter name: of pContext, a Parameter type: of LONGLONG, select the in checkbox, and press the Add button. Add a second argument named buf, with type  BSTR *, and set it as an out parameter. For both methods, leave the return type as HRESULT. Click Finish to add the method to the interface.

As seen in the above process to add a method, COM has its own set of types. A BSTR, for instance, encodes a string as a 4-byte length, a sequence of Unicode characters, and two NULL characters as a terminator. Methods return an HRESULT value, which encodes the success or failure of the method, as well as a specific numeric error on failure. Parameters must be marked as to whether they only are passed into a function (in), are written to by the function (out), or both. The wizard writes object and method definitions to an Interface Definition (IDL) file, and how parameters are marked affects the marshalling and code generation performed when the IDL file is compiled into C++ code. The IDL and associated files can be modified by hand, but using the wizard reduces the chance of error.

Methods are implemented in COMWrapper.cpp, the skeleton of which is one of the many files that is generated by the Visual Studio wizard and IDL compilation process. The JavaName() method that was added above is implemented as:

  1. // COMWrapper_COM\COMWrapper.cpp
  2. STDMETHODIMP CCOMWrapper::JavaName(LONGLONG pContext, BSTR* buf)
  3. {
  4. char b[30];
  5. Context *pC = reinterpret_cast<Context *>(pContext);
  6. pC->JavaName(b, sizeof(b));
  7. *buf = ToBSTR(b);
  8. return S_OK;
  9. }

As COM does not have the equivalent of void *, we pass the pointer as a LONGLONG, a type large enough to store 64-bit pointers, and cast the value to be a pointer to a Context. The buffer returned by Context::JavaName() is not Unicode, so must be converted before it can be returned. The function returns S_OK, indicating that it worked successfully. A professional application should add proper error checking, however, such as returning one of the many predefined error codes, for instance using E_OUTOFMEMORY if memory could not be allocated for the string, or E_FAIL on an unspecified error.

To convert the string buffer to Unicode, we can make use of the Windows function MultiByteToWideChar(). Calling it once with a zero-length destination buffer indicates that it should return the number of bytes that would be needed by the converted string. The function is called again with a destination buffer allocated to the correct size.

  1. // COMWrapper_COM\COMWrapper.cpp
  2. BSTR ToBSTR(char *buf) {
  3. int n = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, buf, -1, NULL, 0);
  4. wchar_t *wbuf = new wchar_t[n];
  5. MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, buf, -1, wbuf, n);
  6. BSTR bstr = SysAllocString((const OLECHAR*)wbuf);
  7. delete wbuf;
  8. return bstr;
  9. }

Compiling the COM server in Visual Studio registers it on the system, although registration can be performed by hand via the use of the regsvr32 command.

The project CS_COM uses the COMWrapper object. The COM Interop process converts failure HRESULT return values into exceptions automatically so return values do not need to be checked, but the syntax is a bit cumbersome with all values being returned via parameters. This code produces the same output as does JNITest:

  1. // CS_COM\CS_COM.cs
  2. using System;
  3.  
  4. namespace CS_COM
  5. {
  6. class CS_COM
  7. {
  8. static void Main(string[] args)
  9. {
  10. // invoke the COM object
  11. CPPWrapper_COMLib.ICOMWrapper w = new CPPWrapper_COMLib.COMWrapper();
  12. long pContext;
  13. w.CreateContext(out pContext);
  14.  
  15. string buf;
  16. w.JavaName(pContext, out buf);
  17. Console.WriteLine(buf);
  18.  
  19. long pJavaCustom, pJavaAdder;
  20. w.JavaCreateCustom(pContext, 3, out pJavaCustom);
  21. w.JavaCreateAdder(pContext, out pJavaAdder);
  22.  
  23. int result;
  24. w.JavaAdd(pContext, pJavaAdder, 9, pJavaCustom, out result);
  25. Console.WriteLine(result);
  26.  
  27. w.ScalaName(pContext, out buf);
  28. Console.WriteLine(buf);
  29.  
  30. long pScalaCustom, pScalaAdder;
  31. w.JavaCreateCustom(pContext, 4, out pScalaCustom);
  32. w.JavaCreateAdder(pContext, out pScalaAdder);
  33.  
  34. w.JavaAdd(pContext, pScalaAdder, 7, pScalaCustom, out result);
  35. Console.WriteLine(result);
  36. }
  37. }
  38. }

IKVM

In contrast to the above methods, with IKVM (so named because I and K are on either side of J) there is no need for an intermediate library in C/C++. The ikvmc compiler will directly convert Java bytecode into .NET Common Intermediate Language bytecode.

The commands:

ikvmc -target:library javalib-assembly-1.0.jar
ikvmc -target:library scalalib-assembly-1.0.jar

create two assemblies, corresponding to each of the JAR files. These assemblies can then be referenced by a .NET application. The CS_IKVM project contains the following, which produces the same output as TestJNI.

  1. // CS_IKVM\CS_IKVM.cs
  2. using System;
  3.  
  4. namespace CS_IKVM
  5. {
  6. class CS_IKVM
  7. {
  8. static void Main(string[] args)
  9. {
  10. Console.WriteLine(javalib.Adder.name());
  11. javalib.Custom javaCustom = new javalib.Custom(3);
  12. javalib.Adder javaAdder = new javalib.Adder();
  13. Console.WriteLine(javaAdder.add(9, javaCustom));
  14.  
  15. Console.WriteLine(scalalib.Adder.name());
  16. scalalib.Custom scalaCustom = new scalalib.Custom(4);
  17. scalalib.Adder scalaAdder = new scalalib.Adder();
  18. Console.WriteLine(scalaAdder.add(7, scalaCustom));
  19. }
  20. }
  21. }

Using IKVM is definitely the simplest method to use Java or Scala code in C#. There is one caveat — symbols used in the CLASS files are translated directly, which can be a problem in the case of Scala. In particular, the dollar sign is not a valid C# identifier, so the Adder$ class cannot be directly referenced. This has a wider impact than it may first appear, as special types, such as the None value of an Option in Scala are implemented as objects, so have a trailing dollar sign as part of their internal type name. The issue can be mitigated, however, by writing a Scala function which returns the object, and on the C# side receiving it as a var, rather than as an explicitly typed variable.

Building the Examples

Tools and Process

Source code and binaries for the examples in this article are available here, and were developed under 64-bit Microsoft Windows 7 using Microsoft Visual Studio 2010, Simple Build Tool 0.13.1, IKVM.NET 7.2, and the the 64-bit Java 7 Development Kit. MPC 3.9.60 was used for project file management.

To run the samples on a 64-bit architecture, only installation of the JDK and setting appropriate paths is necessary, but to build the samples, install the JDK, sbt, IKVM and Visual Studio. Set the environment variables JAVA_HOME to the top-level Java directory, IKVM_ROOT to the top-level IKVM directory, and MPC_ROOT to the top-level MPC directory. Ensure that sbt's bin directory, %IKVM_ROOT%\bin%JAVA_HOME%\bin and %JAVA_HOME%\jre\bin\server (or other location of jvm.dll) are in the system PATH. From a console prompt, execute the following command to generate project files for Visual Studio 2010:

%MPC_ROOT%\mwc.pl -type vc10 Interop.mwc

Also, execute this command to build the support libraries:

buildikvm.bat

Finally, build the Interop.sln solution in Visual Studio using the x64 build configuration. The output files will be in Output\Debug or Output\Release depending upon whether a debug or release build was performed. As relative paths are used, please run the examples from either directory for successful operation. Also, ensure that Visual Studio is run with administrative privilege, otherwise the COM server example, after it is built, will not be able to be registered automatically, and will have to be registered by hand.

The Makefile, Project, and Workspace Creator (MPC)

MPC is used for project file generation. Although it is particularly useful in a multi-platform, multi-tool environment where a single set of description files can be used to automatically generate Makefiles, Visual Studio solution files, Borland C++ Builder projects, etc., it is also handy for a single platform, single tool environment for improved project management. Base projects can be created to centralize settings, which are then inherited by project description files. This allows settings that apply to multiple projects to be set in only one place, as the same settings do not have to be applied by hand to each project individually, which reduces the chance of error.

The projects in this article inherit from one of two base projects, depending on whether they are in C++ or C#, and those base projects inherit from a base project that sets common paths. Base projects have an MPB file extension.

The base of all projects, Paths.mpb, sets the output, library and include directories for both the Debug and Release Windows builds. As these settings, in this case, are specific to a particular compiler feature, they are contained within a specific block. If this file were to be used on other architectures, options to the specific block could be supplied to restrict the definitions in various ways, such as to particular compiler verisons or platform types.

  1. // config\Paths.mpb
  2. project {
  3. specific {
  4. Release::lib_modifier =
  5. Debug::lib_modifier =
  6.  
  7. Release::install = ../Output/Release
  8. Debug::install = ../Output/Debug
  9. Release::libout = ../Output/Release
  10. Debug::libout = ../Output/Debug
  11. Release::libpaths += ../Output/Release
  12. Debug::libpaths += ../Output/Debug
  13. Release::includes += ../Output/Release
  14. Debug::includes += ../Output/Debug
  15. }
  16. }

Projects compiled for C++ inherit from CPPBase.mpb, which itself inherits from Paths.mpb. This base project sets include paths and linker properties to allow applications to use the JDK. The environment variable JAVA_HOME is expanded to a full path via the call to expand, as Visual Studio prefers to use absolute, rather than relative paths.

  1. // config\CPPBase.mpb
  2. project : Paths {
  3. expand(JAVA_HOME) {
  4. $JAVA_HOME
  5. }
  6.  
  7. includes += $(JAVA_HOME)\include
  8. includes += $(JAVA_HOME)\include\win32
  9.  
  10. libpaths += $(JAVA_HOME)\lib
  11. libs += jvm
  12.  
  13. includes += ../JNI
  14. }

The base project for C# projects is even smaller. Certain .NET assemblies are specified, as well as the architecture to use when generating code. The architecture is normally not needed, unless, as with this article, linking against a library with a particular architecture is being performed. As the C++ projects are compiled as 64-bit, compling C# code as 64-bit ensures that no incompatibilities (such as a BadFormatException) occur.

  1. // config\CSharpBase.mpb
  2. project : Paths {
  3. lit_libs += System System.Data System.Xml
  4.  
  5. specific {
  6. PlatformTarget = x64
  7. }
  8.  
  9. libpaths -= .
  10. }

By using base projects, MPC files that describe projects to build can be very short. For example, the MPC file that describes the CPPWrapper_Native project is as follows. The option managed = 0 ensures that native, rather than .NET, code is generated, while a nonzero value enables one of the/clr flag variants, depending on the value. The Source_Files section specifies the source files of the project, and if all source files were in the same directory as the MPC file, this section could even be eliminated.

  1. // CPPWrapper_Native\CPPWrapper_Native.mpc
  2. project : CPPBase {
  3. managed = 0
  4.  
  5. Source_Files {
  6. *.cpp
  7. ../JNI/*.cpp
  8. }
  9. }

The MPC file which describes the CS_CPPCLI project is simpler still. By default, a library would be generated (if a main() function is not found), but setting the exename both confirms that the project that is generated is an executable, and what its name should be. The after clause creates a build dependency, ensuring that the current project is built after what is listed in the after clause.

  1. // CS_CPPCLI\CS_CPPCLI.mpc
  2. project : CSharpBase {
  3. exename = CS_CPPCLI
  4.  
  5. after += CPPWrapper_CPPCLI
  6. }

MPC files can be as complex as needed, however. Due to the nature of COM server development, many options must be set, such as defined symbols via macros, COM server registration via RegisterOutput, specification of resource files, and the like. Additionally, files that are created by the IDL compilation process shouldn't be directly compiled into the project (they are automatically included by the COM framework itself) so are removed from consideration by prefixing their filename with a caret.

  1. // CPPWrapper_COM\CPPWrapper_COM.mpc
  2. project : CPPBase {
  3. managed = 0
  4.  
  5. macros += _MERGE_PROXYSTUB
  6.  
  7. specific {
  8. ModuleDefinitionFile = CPPWrapper_COM.def
  9. RegisterOutput = true
  10. useofatl = Dynamic
  11. unicode = 1
  12. }
  13.  
  14. Resource_Files {
  15. *.rc
  16. *.rgs
  17. }
  18.  
  19. Header_Files {
  20. *.h
  21. ../JNI/*.h
  22. ^CPPWrapper_COM.h
  23. ^CPPWrapper_COM_i.h
  24. }
  25.  
  26. Source_Files {
  27. *.c
  28. *.cpp
  29. *.idl
  30. *.def
  31. ../JNI/*.cpp
  32. ^dlldata.c
  33. ^CPPWrapper_COM_p.c
  34. }
  35. }

To use the COM object that is created by the definition above, the unique ID of the COM object is specified in a comreferences entry, as is done in the CS_COM project.

  1. // CS_COM\CS_COM.mpc
  2. project : CSharpBase {
  3. exename = CS_COM
  4.  
  5. after += CPPWrapper_COM
  6.  
  7. specific {
  8. comreferences += CPPWrapper_COMLib:embed=true,guid=1E1DB34F-024E-4D3B-BE9F-F94B66BD8BBB,majorver=1,minorver=0,wrapper=tlbimp
  9. }
  10. }

Lastly, all of the MPC files are tied together into a workspace, so they can all be built together. Directories containing MPC files (or MPC files by name explicitly) are specified in a workspace (MWC) file. Running mwc.pl on a MWC file creates the workspace project files, and the cmdline option adjusts the command-line options for the run. Because projects are compiled as C++ by default, certain ones are identified as needing to be compiled as C# via the -language command-line option. The -include command-line option indicates the directory where base projects can be found.

  1. // Interop.mwc
  2. workspace {
  3. cmdline += -include config
  4.  
  5. CPPWrapper_Native
  6. CPPWrapper_CPPCLI
  7. CPPWrapper_COM
  8. JNITest
  9.  
  10. csharp {
  11. cmdline += -language csharp
  12. CS_CPPCLI
  13. CS_PINVOKE
  14. CS_COM
  15. CS_IKVM
  16. }
  17. }

Summary

This article has demonstrated four ways of using code on the JVM from C#, both simple and complex. Each is useful, depending on the application, although, if C/C++ is not needed, using IKVM is by far the easiest method. It has also been shown that MPC is useful for build management, allowing simple build descriptions when possible, but supporting complexity as needed.

References

 

The Software Engineering Tech Trends is a monthly newsletter featuring emerging trends in software engineering.

Subscribe

© Copyright Object Computing, Inc. 1993, 2016. All rights reserved

WebSanity Top Secret