Professional C__ - Marc Gregoire [436]
Once you have the library, you can call it from C# by using Interop services. First, you need to include the Interop namespace:
using System.Runtime.InteropServices;
Next, you define the function prototype, and tell C# where it can find the implementation of the function. This is done with the following line, assuming you have compiled the library as HelloCpp.dll:
[DllImport("HelloCpp.dll", CharSet = CharSet.Unicode)]
public static extern int FunctionInDLL(String s);
The first part of this line is saying that C# should import this function from a library called HelloCpp.dll, and that it should use Unicode strings. The second part specifies the actual prototype of the function, which is a function accepting a string as parameter and returning an integer. The following code shows a complete example on how to use the C++ library from C#:
using System;
using System.Runtime.InteropServices;
namespace HelloCSharp
{
class Program
{
[DllImport("HelloCpp.dll", CharSet = CharSet.Unicode)]
public static extern int FunctionInDLL(String s);
static void Main(string[] args)
{
Console.WriteLine("Writen by C#.");
int res = FunctionInDLL("Some string from C#.");
Console.WriteLine("C++ returned the value " + res);
}
}
}
Code snippet from CSharp\HelloCSharp.cs
The output will be as follows:
Writen by C#.
The following string was received by C++:
'Some string from C#.'
C++ returned the value 42
The details of the C# code are outside the scope of this C++ book, but the general idea should be clear with this example.
Mixing Java and C++ with JNI
The Java Native Interface, or JNI, is a part of the Java language that allows the programmer to access functionality that was not written in Java. Because Java is a cross-platform language, the original intent was to make it possible for Java programs to interact with the operating system. JNI also allows programmers to make use of libraries written in other languages, such as C++. Access to C++ libraries may be useful to a Java programmer who has a performance-critical piece of his application, or who needs to use legacy code.
JNI can also be used to execute Java code within a C++ program, but such a use is far less common. Because this is a C++ book, we do not include an introduction to the Java language. This section is targeted at readers who already know Java and wish to incorporate C++ code into their Java code.
To begin your Java cross-language adventure, start with the Java program. For this example, the simplest of Java programs will suffice:
public class HelloCpp {
public static void main(String[] args)
{
System.out.println("Hello from Java!");
}
}
Code snippet from JNI\HelloCpp.java
Next, you need to declare a Java method that will be written in another language. To do this, you use the native keyword and leave out the implementation:
public class HelloCpp {
// This will be implemented in C++.
public native void callCpp();
// Remainder omitted for brevity
}
Code snippet from JNI\HelloCpp.java
C++ code will eventually be compiled into a shared library that gets dynamically loaded into the Java program. You need to load this library inside a Java static block so that it is loaded when the Java program begins executing. The name of the library can be whatever you want, for example hellocpp.so on Unix systems, or hellocpp.dll on Windows systems.
public class HelloCpp {
static {
System.loadLibrary("hellocpp");
}
// Remainder omitted for brevity
}
Code snippet from JNI\HelloCpp.java
Finally, you need to actually call the C++ code from within the Java program. The callCpp() Java method serves as a placeholder for the not-yet-written C++ code. Because callCpp() is a method of the HelloCpp class, you need to create a new HelloCpp object and call the callCpp() method on it:
public class HelloCpp