【发布时间】: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