Trouble loading Unmanaged C++ DLL from WPF applica

2019-08-04 17:09发布

First of all I would like to thank anyone reading this for their time! I'm a pretty well-informed C# programmer with WinForms and I'm giving WPF a shot. I've been having trouble calling functions from my WPF application so I decided to create a very easy example project to illustrate my problem. In this application I'm creating a C++ Dll (using Visual Studio 2012 -> Visual C++ -> Win32 Console Application -> .DLL Project). In this DLL, I'm simply creating a function to return a DWORD. My DLL is shown below:

// 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;
}

Now I also have a WPF application (using VS 2012) in which I'm simply trying to call and print the results of the function from the aforementioned DLL. My WPF code creates a button within the form. Upon clicking the button, I call my DLL function and open a MessageBox to return the value from the function, simple as that:

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());
        }
    }
}

In my project settings, I'm compiling under "unsafe", and also for x86 Platform targets. I'm able to compile both projects and I set the DLL project's build output to the respective Release/Debug directories of my WPF project. I can hit the "Start" button at the top in either Debug/Release mode and the WPF form opens and displays the button. I can click the button and I see the correct return code from my DLL's function, everything works PERFECTLY. However, when I copy the release folder onto any other machine to run it there, my WPF form opens only upon clicking the button (and calling my DLL's function), my program crashes with:

"Unhandled exception at 0x7c812fd3 in testdll.exe: 0xE0434352: 0xe0434352."

I can create a similar GUI in C# using Visual Studio 2005. I'm making the same DLL call and utilizing the same C# code and settings. I have no problems opening this application on other computers and the DLL function is called appropriately with no errors. This seems to be an issue I'm having only with VS2012. Just to note, I have .NET Framework 4 and C++ 2012 Redistributable package installed on the computer's that are failing. After searching the internet for similar results, and mucking with many many project settings, I'm still stumped. Any help would be GREATLY appreciated.

0条回答
登录 后发表回答