2012-04-10 129 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