我最终在评论中关注了@“Hovercraft Full Of Eels”链接:
Calling .Net Dlls from Java code without using regasm.exe
我使用 C++\CLI 在本机代码和托管代码之间架起了一座桥梁,它运行良好。主要问题是我的桥 DLL 在 JVM 下运行,而我尝试加载的 DLL 不在 JRE\bin 目录中。为了克服这个问题,我从 C++/CLI 代码中动态加载了 .Net 程序集(基于此):
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;
}