Naz*_*zka 5 c# dll interop c++-cli visual-c++
我在一个解决方案中有3个项目.
我有 :
所以,当我启动应用程序时,所有工作.但是当我在我的winform中按下"Generer"按钮时,执行C++/CLI 的函数NativeMethod :: Test()它崩溃了,我有这个弹出消息:
System.Windows.Forms.dll中发生未处理的"System.BadImageFormatException"类型异常
附加信息:无法加载文件或程序集"EngineInterfaceWrapper.dll"或其依赖项之一.n'est pas une application Win32 valide.(HRESULT异常:0x800700C1)
当我进入Conf的项目礼仪时.属性 - >链接器 - >高级:目标机器,它为我的C++本机和托管DLL设置了值"MachineX86",我的WinForm也在X86中.我厌倦了许多配置,但它不起作用.
编辑:
问题可能是C++/CLI标题中的标题"TradeEngine.h":EngineInterfaceWrapper.h.因为当我取消链接原生C++ Dll(并删除CLI包装器中的所有代码)时,如果我构建解决方案,它将起作用但如果"#include"TradeEngine.h""总是在CLI标题中,我会有同样的错误.你有什么想法吗?
编辑: - 已解决 -
我终于找到了问题.它是我的本机C++ .h中调用的boost库.由于包装器需要具有本机头,它还链接Boost库(即使在代码中使用我的pragma,这很奇怪)和Boost在托管代码中调用时会产生许多问题,所以我将头文件放在本机.h原生.cpp和所有作品.谢谢大家的帮助.
代码:
原生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
#pragma managed(push, off)
#include <curl/curl.h>
#include <boost\filesystem.hpp>
#include <boost\tokenizer.hpp>
#include <boost\lexical_cast.hpp>
#include <boost\thread.hpp>
#pragma managed(pop)
EXTERN_C SYMBOL_DECLSPEC void __stdcall Test(void);
Run Code Online (Sandbox Code Playgroud)
TradeEngine.cpp
SYMBOL_DECLSPEC void __stdcall Test(void)
{
}
Run Code Online (Sandbox Code Playgroud)
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();
};
}
Run Code Online (Sandbox Code Playgroud)
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();
}
Run Code Online (Sandbox Code Playgroud)
C#Winform
Program.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());
}
}
}
Run Code Online (Sandbox Code Playgroud)
Form1.cs的
generer_Click()是用户单击Generer时按钮启动的事件.
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我找到了解决方案:
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.