Calling a 3rd party .NET DLL using JNI
.net, dll, java, java-native-interface
Solution
I finnaly followed @"Hovercraft Full Of Eels" link in the comments: Calling .Net Dlls from Java code without using regasm.exe
I used C++\CLI to bridge between the native and managed code and it worked beautifully. The main issue was that my bridge DLL runs under JVM, and the DLL I tried to load wasn't in JRE\bin directory. To overcome this problem I loaded the .Net assemblies dynamically from C++/CLI code (Based on this):
static Assembly^ MyResolveEventHandler( Object^ sender, ResolveEventArgs^ args )
{
//Retrieve the list of referenced assemblies in an array of AssemblyName.
Assembly^ MyAssembly;
Assembly^ objExecutingAssemblies;
String^ strTempAssmbPath = "";
objExecutingAssemblies = Assembly::GetExecutingAssembly();
array<AssemblyName ^>^ arrReferencedAssmbNames = objExecutingAssemblies->GetReferencedAssemblies();
//Loop through the array of referenced assembly names.
for each (AssemblyName^ strAssmbName in arrReferencedAssmbNames)
{
//Check for the assembly names that have raised the "AssemblyResolve" event.
if (strAssmbName->FullName->Substring(0, strAssmbName->FullName->IndexOf(",")) == args->Name->Substring(0, args->Name->IndexOf(",")))
{
//Build the path of the assembly from where it has to be loaded.
strTempAssmbPath = pathBase + args->Name->Substring(0, args->Name->IndexOf(",")) + ".dll";
break;
}
}
//Load the assembly from the specified path.
MyAssembly = Assembly::LoadFrom(strTempAssmbPath);
//Return the loaded assembly.
return MyAssembly;
}
Problem
I'm trying to call a 3rd party .NET DLL (Taken from here) from within a JAVA program. After looking here and here I managed to get the whole thing to compile and run. But I get an exception when running the .NET code: fatal error has been detected by the Java Runtime Environment This only happens when I try to access another .net object and method from within the .NET DLL: ``` JNIEXPORT void JNICALL Java_test_broadcast (JNIEnv *, jobject) { // Instantiate the MC++ class. IManagedWrapper* t = IManagedWrapper::CreateInstance(); // The actual call is made. t->Broadcast(); } void ManagedWrapper::Broadcast(std::string message) { //Uncommenting the following line will raise the error //IXDBroadcast^ broadcast = XDBroadcast::CreateBroadcast(XDTransportMode::WindowsMessaging); } ``` I managed to create a .NET DLL that links to the above code and works as desired. How can I call the .NET objects and method from the Java code?