Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
加载非托管C++;来自WPF应用程序的DLL_Wpf_Dllimport_Unmanaged_Dllexport - Fatal编程技术网

加载非托管C++;来自WPF应用程序的DLL

加载非托管C++;来自WPF应用程序的DLL,wpf,dllimport,unmanaged,dllexport,Wpf,Dllimport,Unmanaged,Dllexport,首先,我要感谢所有阅读本文的人!我是一个非常熟悉WinForms的C#程序员,我正在给WPF一个机会。我在从WPF应用程序调用函数时遇到了问题,所以我决定创建一个非常简单的示例项目来说明我的问题。在这个应用中,我正在创建一个C++ DLL(使用Visual Studio 2012—> Visual C++ + > Win32控制台应用程序> .dll项目)。在这个DLL中,我只是创建一个函数来返回DWORD。我的DLL如下所示: // mydll.cpp : Defines the export

首先,我要感谢所有阅读本文的人!我是一个非常熟悉WinForms的C#程序员,我正在给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函数并打开一个MessageBox以从函数返回值,简单如下:

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());
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用System.Runtime.InteropServices;
命名空间testdll
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
[DllImport(“mydll.dll”,CallingConvention=CallingConvention.Cdecl)]
私有静态外部单元初始化();
公共主窗口()
{
初始化组件();
}
私有无效按钮\u单击\u 1(对象发送者,路由目标)
{
uint ret=init();
MessageBox.Show(“Return=“+ret.ToString());
}
}
}
在我的项目设置中,我在“不安全”下编译,也针对x86平台目标进行编译。我能够编译这两个项目,并将DLL项目的构建输出设置为WPF项目的相应发布/调试目录。我可以在调试/发布模式下点击顶部的“开始”按钮,WPF表单打开并显示该按钮。我可以点击按钮,我看到了正确的返回代码从我的DLL的功能,一切都很完美。但是,当我将release文件夹复制到任何其他计算机上运行它时,我的WPF表单仅在单击按钮(并调用我的DLL函数)时才会打开,我的程序会崩溃:

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

我可以使用VisualStudio2005在C#中创建类似的GUI。我正在进行相同的DLL调用,并使用相同的C代码和设置。我在其他计算机上打开此应用程序时没有问题,并且正确调用DLL函数时没有错误。这似乎是我在VS2012中遇到的一个问题。只需注意,我在计算机上安装了.NETFramework 4和C++ 2012可再分发软件包,这些软件包都是失败的。在互联网上搜索类似的结果,并处理了许多项目设置后,我仍然感到困惑。任何帮助都将不胜感激