【问题标题】:C++/CLI Dll with C# Connection problem带有 C# 连接问题的 C++/CLI Dll
【发布时间】:2011-06-27 17:05:41
【问题描述】:

我在一个解决方案中有 3 个项目。

我有:

  • 本机 C++ dll,
  • 一个 C# Winform,
  • 和一个没有纯模式的代理 C++/CLI 来建立其他 2 个项目之间的链接(并在 C# 的托管代码中使用本机函数)

所以当我启动应用程序时,一切正常。但是,当我在我的 winform 中按下按钮“Geneer”时,它执行 C++/CLI 的函数 NativeMethod::Test(),它崩溃了,我有这个弹出消息:

未处理的类型异常 'System.BadImageFormatException' 发生在 System.Windows.Forms.dll 中

附加信息:无法加载 文件或程序集 'EngineInterfaceWrapper.dll' 或其中之一 它的依赖关系。 n'est pasune 应用程序 Win32 验证。 (例外 来自 HRESULT:0x800700C1)

当我进入 Conf.属性 -> 链接器 -> 高级:目标机器,它为我的 C++ 本机和托管 DLL 设置了值“MachineX86”,我的 WinForm 也在 X86 中。我厌倦了许多配置,但它不起作用。

编辑:

问题可能是 C++/CLI 标头中的标头“TradeEngine.h”:EngineInterfaceWrapper.h。因为当我取消链接本机 C++ Dll(并删除 CLI 包装器中的所有代码)时,如果我构建解决方案它会起作用,但如果“#include "TradeEngine.h"" 总是在 CLI 标头中,我会遇到同样的错误。你有什么想法吗?

代码:

原生 C++

TradeEngine.h

#ifdef TRADEENGINE_EXPORTS
#define SYMBOL_DECLSPEC __declspec(dllexport)
#define SYMBOL_DEF
#else
#define SYMBOL_DECLSPEC __declspec(dllimport)
#define SYMBOL_DEF      __declspec(dllimport)
#endif

EXTERN_C SYMBOL_DECLSPEC void __stdcall Test(void);

TradeEngine.cpp

SYMBOL_DECLSPEC void __stdcall Test(void)
{
}

C++/CLI

EngineInterfaceWrapper.h

#pragma once

#include "TradeEngine.h"

using namespace System;
using namespace System::Runtime::InteropServices;

namespace EngineInterfaceWrapper {

    public ref class NativeMethod
    {
    public:
        static void AjoutColonneDifferenceCourtClotureOuvertureReelle(void);
        static void Test();
    };
}

EngineInterfaceWrapper.cpp

#pragma region Includes
#include "stdafx.h"
#include "EngineInterfaceWrapper.h"
using namespace EngineInterfaceWrapper;

#include <msclr/marshal.h>
using namespace msclr::interop;
#pragma endregion

void NativeMethod::Test()
{
    ::Test();
}

C# Winform

程序.cs

namespace TradeInterface
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Form1.cs

generer_Click() 是用户点击Geneer时按钮启动的事件。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Runtime.InteropServices;
using EngineInterfaceWrapper;

namespace TradeInterface
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void generer_Click(object sender, EventArgs e)
        {
            NativeMethod.Test();
        }
    }
}

我该如何解决这个问题?如果您需要更多信息,请告诉我。

【问题讨论】:

  • 执行客户端项目时,natice C++ Dll 是否可用?它应该在当前、exe、系统目录中或通过 PATH 可用。
  • 我启动 WinForm 时的所有工作。所有东西都在同一个调试文件夹中。当我按下启动 Test() 的按钮时它会崩溃。
  • NativeMethod::Test 不调用 ::Test 是否有效?
  • 另外,如果 EngineInterfaceWrapper.dll 未链接到本机 dll,它是否有效?尝试隔离问题。
  • 不,它会因相同的弹出消息而崩溃。问题是我不懂 C# 和 CLI,所以这对我来说真的很难。 :S

标签: c# c++ dll c++-cli


【解决方案1】:

我找到了解决办法:

http://marc.info/?l=boost-users&m=123425857320026

在配置属性 -> C/C++ -> 预处理器 -> 预处理器 定义添加 BOOST_ALL_DYN_LINK 以强制使用 DLL。此外,将必要的 DLL 复制到 可执行文件驻留。例如。将 boost_thread-vc90-mt-gd-1_XX.dll 复制到 MyApp/bin/Debug.

【讨论】:

    【解决方案2】:

    一个原因可能是您尝试在 64 位进程中加载​​本机 32 位 dll。确保在您的应用中将平台目标设置为 x86,以强制您的应用在 WoW64 中运行。

    【讨论】:

    • 我添加了更多信息,我在 x86 中设置了所有项目,但我总是遇到同样的问题。
    • @Nazka:C# 和 C++/CLI 项目都是用相同版本的 .NET 构建的吗?
    • @Ben Voigt 所有都使用 v4.0。我们能不能看到V4.0XXXX?
    猜你喜欢
    • 2013-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多