在c++ DLL中包含Lua使其与c#不兼容
本文关键字:不兼容 Lua c++ DLL 包含 | 更新日期: 2023-09-27 17:49:14
我试图用c#使用WPF调用一个非托管的c++ .dll的UI。我试过使用平台调用方法,也只是通过编译c++代码与/clr选项,这是更好的。这两种方法都工作得很好,直到c++ .dll使用任何包含标准Lua头文件的代码。可以肯定的是,c++代码在编译成。exe文件时运行良好,在另一个c++项目调用。dll文件时也运行良好。
只是为了测试,c#端在使用PInvoke时类似于下面的第一个代码块:
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#不兼容?
遵循@Schollii的建议:如果我自己用Visual Studio构建Lua,它就能工作。这篇文章有所帮助,只是做了一个小小的修改:visual studio 2012中的Lua。
- 下载Lua文件lua.org/download.html
- 创建了一个新的Visual Studio项目来创建一个库。我创建了一个静态库。
- 删除解释器文件lua.c,因为它与luac.c冲突
- 编译与我的c++ dll相同的设置,它是x64,发布。
现在c#项目可以调用到c++ dll中