【发布时间】:2014-04-13 22:59:00
【问题描述】:
我正在尝试使用调用非托管 C++ .dll 的 WPF 使用 C# 制作 UI。我尝试过使用 Platform Invoke 方法,也尝试使用更好的 /clr 选项编译 C++ 代码。这两种方法都可以正常工作,直到 C++ .dll 使用包含标准 Lua 头文件的任何代码。可以肯定的是,C++ 代码在编译为 .exe 以及 .dll 被另一个 C++ 项目调用时也可以正常工作。
仅用于测试,使用 PInvoke 时,C# 端类似于下面的第一个代码块:
namespace ThermoSIM_UI
{
public class CppInterface
{
[DllImport("ThermoSIMDLL_1.2.dll")]
public static extern int TestPassString();
public CppInterface()
{
TestPassString();
}
}
}
而 C++ .dll 有一个像这样的标题:
#ifdef THERMOSIMDLL_EXPORTS
#define THERMOSIMDLL_API __declspec(dllexport)
#else
#define THERMOSIMDLL_API __declspec(dllimport)
#endif
extern "C"
{
THERMOSIMDLL_API int TestPassString();
}
正如我所说,这一直有效,直到我在 C++ .dll 中有一个使用 Lua 包含的文件:
extern "C" {
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#include "luajit.h"
}
啊,C# 错误是这样的:
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Additional information: The invocation of the constructor on type 'ThermoSIM_UI.MainWindow' that matches the specified binding constraints threw an exception.
在输出窗口中还有这个:
A first chance exception of type 'System.BadImageFormatException' occurred in ThermoSIM_UI.exe
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
A first chance exception of type 'System.Xaml.XamlObjectWriterException' occurred in System.Xaml.dll
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Additional information: The invocation of the constructor on type 'ThermoSIM_UI.MainWindow' that matches the specified binding constraints threw an exception.
The program '[8048] ThermoSIM_UI.vshost.exe: Managed (v4.0.30319)' has exited with code -1 (0xffffffff).
BadImageFormatException 似乎与具有不同构建配置的 .dll 之类的事情有关,例如将 x64 与 x86 混合等。反正我还没想通。 http://msdn.microsoft.com/en-us/library/system.badimageformatexception(v=vs.110).aspx
有没有人遇到过这种情况,或者知道为什么这个 .dll 与 C# 不兼容?
【问题讨论】:
-
你是在“Any CPU”中编译吗?
-
不,我正在为 C# 和 C++ 编译 x64
-
您刚刚添加了 .h 并停止工作,或者您使用了 lua 中的任何方法?
-
您是否正在使用与您的 C++ DLL 相同的配置设置构建 Lua?
-
只需将头文件添加到 .cpp 文件中,它就会停止工作。也许Schollii的事情是看哪里。我为 x64 构建了 LuaJIT,但它不像其他项目那样通过 VS。 luajit.org/install.html。我尝试使用这里 luabinaries.sourceforge.net/download.html 的预构建 x64 二进制文件,但运气不好。我不确定如何从 VS 构建 lua 库。