我認爲你要找的是一個自定義程序集解析器。我不得不用一個來做我認爲你正在嘗試做的事情 - 我想找到一些不在最初的非託管DLL(最終加載託管代碼)的樹中的文件夾中找到一些DLL。
第1步是使一個函數可以調用來設置解析器:
void PrepareManagedCode()
{
// Set up our resolver for assembly loading
AppDomain^ currentDomain = AppDomain::CurrentDomain;
currentDomain->AssemblyResolve += gcnew ResolveEventHandler(currentDomain_AssemblyResolve);
} // PrepareManagedCode()
則解決。這個例子有一個全球性的ourFinalPath這將在你的情況是你使用額外的文件夾:
/// <summary>
/// This handler is called only when the CLR tries to bind to the assembly and fails
/// </summary>
/// <param name="sender">Event originator</param>
/// <param name="args">Event data</param>
/// <returns>The loaded assembly</returns>
Assembly^ currentDomain_AssemblyResolve(Object^ sender, ResolveEventArgs^ args)
{
sender;
// If this is an mscorlib, do a bare load
if (args->Name->Length >= 8 && args->Name->Substring(0, 8) == L"mscorlib")
{
return Assembly::Load(args->Name->Substring(0, args->Name->IndexOf(L",")) + L".dll");
}
// Load the assembly from the specified path
String^ finalPath = nullptr;
try
{
finalPath = gcnew String(ourAssemblyPath) + args->Name->Substring(0, args->Name->IndexOf(",")) + ".dll";
Assembly^ retval = Assembly::LoadFrom(finalPath);
return retval;
}
catch (...)
{
}
return nullptr;
}
哇,漢斯,工作!由於MyCoolApp.exe只是一個普通的舊的Win32應用程序(不是.NET),所以我真的很懷疑,所以我想爲它添加一個應用程序配置文件是無濟於事的。謝謝!你是否想把它寫成答案而不是評論,我會將其標記爲已接受? – Jordan0Day