【问题标题】:Open File Dialog causes a dll issue in WPF application打开文件对话框导致 WPF 应用程序中的 dll 问题
【发布时间】:2014-04-03 09:00:12
【问题描述】:

我在 VS2012 中有一个使用 .Net 框架 4.0 的 WPF 应用程序项目。
我的 WPF 应用程序有一些对托管 C++ dll 的引用。

我希望用户能够选择要打开的文件,但是当我在托管 C++ dll 中的某些函数之前调用 OpenFileDialog 时,我得到一个 System.BadImageFormatException,但如果我发送硬编码路径而不是让用户使用 OpenFileDialog 进行选择,那么应用程序可以正常工作。
当我查看 System.BadImageFormatException 时,我发现这与格式问题有关,但我在 x64 位机器上运行并将包括 c++ dll 在内的所有内容编译为 x64 位格式,但我仍然不断收到这个例外。

OpenFileDialog 是否存在一些我不知道的已知问题?

这是我的代码示例:

private void WorkingFunction()
{
    String filePath = @"C:\MyFile.txt";
    // Calling the managed c++ functions...
}

private void NotWorkingFunction()
{
    Microsoft.Win32.OpenFileDialog fileDialog = new Microsoft.Win32.OpenFileDialog();
    Nullable<bool> result = fileDialog.ShowDialog(); 
    if(result == true)
    {
        String filePath = fileDialog.FileName;
        // Calling the managed c++ functions...
    }
}

【问题讨论】:

    标签: c# c++ wpf dll openfiledialog


    【解决方案1】:

    您可以尝试在 x86 中显式编译代码,这意味着您将以 32 位模式运行应用程序。

    否则,您可以使用包装在 Windows 窗体中的相同类。

    System.Windows.Forms.OpenFileDialog 
    

    【讨论】:

    • 我无法将我的应用程序编译为 x86,当我尝试使用 System.Windows.Forms.OpenFileDialog 时遇到了同样的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2018-05-03
    • 1970-01-01
    • 2016-07-25
    • 2012-11-16
    相关资源
    最近更新 更多