-->

麻烦从WPF应用程序加载非托管的C ++ DLL(Trouble loading Unmanaged

2019-10-17 17:27发布

首先,我要感谢的人读这所付出的时间! 我是一个相当灵通的C#程序员与WinForms和我给WPF一个镜头。 我一直有从我的WPF应用程序的麻烦通话功能,所以我决定创建一个非常简单的例子项目来说明我的问题。 在这种应用中,我创建一个C ++ DLL(使用Visual Studio 2012 - > VISUAL C ++ - > Win32控制台应用程序 - > .DLL项目)。 在这个DLL,我只是创建一个函数,返回一个DWORD。 我的DLL如下图所示:

// mydll.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
extern "C" __declspec(dllexport) DWORD __cdecl init();

DWORD __cdecl init()
{
    return 0x1234;
}

现在,我也有一个WPF应用程序(使用VS 2012)中,我只是试图调用,从上述DLL打印功能的结果。 我的WPF代码创建表单中的一个按钮。 当点击该按钮时,我把我的DLL函数,并打开一个消息,从功能,就这么简单返回值:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
namespace testdll
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        [DllImport("mydll.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern uint init();

        public unsafe MainWindow()
        {
            InitializeComponent();

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            uint ret = init();
            MessageBox.Show("Return = " + ret.ToString());
        }
    }
}

在我的项目设置,我在“不安全”的编制,也可用于x86平台的目标。 我能够编译两个项目,我的DLL项目的构建输出设置为我的WPF项目的各自发布/调试目录。 我可以在任何调试/发布模式上点击“开始”按钮,WPF形式打开并显示该按钮。 我可以单击该按钮,我看到从我的DLL函数正确的返回代码,一切都完美。 然而,当我复制发布文件夹复制到其他机器上运行它,我的WPF的形​​式只有在点击按钮(并调用我的DLL函数),我的程序崩溃,并打开:

“:0xE0434352:在testdll.exe在0x7c812fd3未处理的异常0xe0434352。”

我可以创建使用Visual Studio 2005我正在做同样的DLL调用和使用相同的C#代码,并设置在C#中类似的GUI。 我没有问题打开其他计算机上的这个应用程序和DLL函数没有错误适当地调用。 这似乎是我在只与VS2012的问题。 只是要注意,我已经安装在电脑上的那些失败的.NET Framework 4和C ++ 2012可再发行组件包。 搜索互联网为类似的结果,并与很多很多的项目设置碴后,我仍然遭到拒绝。 任何帮助将不胜感激。

文章来源: Trouble loading Unmanaged C++ DLL from WPF application