2012-04-10 126 views
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")); 
     } 
    } 

回答

2

你應該試試就知道哪個組件無法加載,因爲也許你試着組裝加載有無法找到引用。或引用存在但對於32位,沒有64位版本

嘗試使用Fusion log知道哪些組件丟失

+0

你的回答使我對蘇珊庫克的博客在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