在Windows 7 64位上使用Assembly.LoadFile遇到FileNotFoundException异常

3
我正在尝试加载一个混合的托管/本地DLL,我已经为32位和64位编译了它。如果我在Windows 7 32位上运行程序,我可以无问题地加载32位DLL。如果我加载64位DLL,则会出现BadImageFormatException错误,这是我预料到的。
我的问题是当我在Windows 7 64位下进行同样的测试时:
如果我加载32位DLL,我会得到BadImageFormatException错误,这个结果是正确的。
但是如果我加载64位DLL,则会出现FileNotFoundException错误。这个信息是不正确的,因为我事先检查了该DLL的存在性!
有人能告诉我,为什么我不能在Windows 7 64位下加载我的64位DLL吗?
这是我的示例代码:
    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"));
        }
    }
1个回答

2

您应该尝试了解哪个程序集无法加载,因为您尝试加载的程序集可能有无法找到的引用。或者引用存在,但是只有32位版本,而不是64位版本。

尝试使用Fusion日志来确定确切缺少哪个程序集。


1
你的回答引导我进入Suzanne Cook的博客,网址为http://blogs.msdn.com/b/suzcook/archive/2003/05/29/57120.aspx, 这个博客帮助我找到了解决方案。本地dll的引用msvcr100.dll,在系统上没有这个文件。Fusion日志对此并没有帮助,但是Dependency Walker起了作用。 - MTR

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接