2
我試圖加載一個混合託管/本機DLL,我編譯爲32位和64位。 如果我在Windows 7 32位上運行我的程序,我可以加載32位DLL沒有任何問題。如果我加載64位DLL,我得到BadImageFormatException,這是我的預期。FileNotFoundException與Windows 7上的Assembly.LoadFile 64位
我的問題是,當我做同樣的測試在Windows 7下64位:
如果我加載32位的DLL,我得到BadImageFormatException。目前爲止還沒有問題。
但是,如果我加載64位的DLL,我得到FileNotFoundException。而這個信息是不正確的,因爲我事先檢查了這個DLL的存在!
有人可以告訴我,爲什麼我不能加載我的64位DLL在Windows 7 64位下?
這裏是我的示例代碼:
private void Button32_Click(object sender, RoutedEventArgs e)
{
string path = System.IO.Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "x86", "Native.dll");
LoadAssembly(path);
}
private void Button64_Click(object sender, RoutedEventArgs e)
{
string path = System.IO.Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "amd64", "Native.dll");
LoadAssembly(path);
}
void LoadAssembly(string path)
{
if(File.Exists(path))
{
MessageBox.Show("Loading " + path);
Assembly asm = null;
try
{
asm = Assembly.LoadFile(path);
}
catch(Exception ex)
{
MessageBox.Show("Exception!!!\n" + ex);
}
MessageBox.Show("Success " + (asm == null ? "no" : "yes"));
}
}
你的回答使我對蘇珊庫克的博客在http://blogs.msdn.com /b/suzcook/archive/2003/05/29/57120.aspx幫助我找到解決方案。本機dll引用了msvcr100.dll,它在系統上缺失。 Fusion日誌在這裏沒有幫助,但Dependency Walker做到了。 – MTR 2012-04-10 10:28:54