【问题标题】:Including Lua in C++ DLL makes it incompatible with C#?在 C++ DLL 中包含 Lua 使其与 C# 不兼容?
【发布时间】: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 库。

标签: c# c++ dll lua pinvoke


【解决方案1】:

遵循@Schollii 的建议就成功了:如果我自己使用 Visual Studio 构建 Lua,它就可以工作。这篇 SO 帖子有所帮助,稍作修改:Lua in visual studio 2012?

  1. 下载 Lua 文件 lua.org/download.html
  2. 创建了一个新的 Visual Studio 项目来创建一个库。我做了一个静态库。
  3. 删除解释器文件 lua.c,因为它与 luac.c 冲突
  4. 使用与我的 c++ dll 相同的设置进行编译,即 x64,Release。

现在 C# 项目可以调用 C++ dll。

【讨论】:

  • 您应该接受您自己的答案,以便将其关闭
  • 啊,谢谢,这不会让我再等 10 个小时。感谢您的帮助,我以为只是因为我有一个 x64 版本的 Lua 就可以了。这是一个棘手的错误,因为它不是那么冗长。
猜你喜欢
  • 2023-03-06
  • 1970-01-01
  • 2012-11-09
  • 1970-01-01
  • 1970-01-01
  • 2018-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多