Question

I'm trying to make a UI with C# using WPF that calls into an unmanaged C++ .dll. I've tried using the Platform Invoke method, and also just by compiling the C++ code with the /clr option, which is nicer. Both of these methods work fine, until the C++ .dll uses any code that includes the standard Lua header files. To be sure, the C++ code works fine when compiled as an .exe, and also when the .dll is called by another C++ project.

Just for testing, the C# side is something like the first code block below when using PInvoke :

namespace ThermoSIM_UI
{
    public class CppInterface
    {
        [DllImport("ThermoSIMDLL_1.2.dll")]
        public static extern int TestPassString();

        public CppInterface()
        {
            TestPassString();
        }
    }
}    

And the C++ .dll has a header like this :

#ifdef THERMOSIMDLL_EXPORTS
#define THERMOSIMDLL_API __declspec(dllexport) 
#else
#define THERMOSIMDLL_API __declspec(dllimport) 
#endif

extern "C"
{
    THERMOSIMDLL_API int TestPassString();
}

As I say, this works until I have a file in the C++ .dll that uses the Lua includes :

extern "C" {
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#include "luajit.h"
}

Ah, and the C# error is this :

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.

Additionally in the Output window there is this :

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 seems to have to do with things like the .dll having a different build configuration, like mixing x64 with x86 or something. Anyways I haven't figured it out yet. http://msdn.microsoft.com/en-us/library/system.badimageformatexception(v=vs.110).aspx

Has anyone encountered this, or have an idea why this .dll becomes incompatible with C#?

Was it helpful?

Solution

Following @Schollii's advice did the trick: If I build Lua myself with Visual Studio, it works. This SO post helped, with a small modification: Lua in visual studio 2012?.

  1. download Lua files lua.org/download.html
  2. made a new Visual Studio project that creates a library. I made a static library.
  3. remove the interpreter file lua.c because it conflicts with luac.c
  4. compile with same settings as my c++ dll, which is x64, Release.

Now the C# project can call into the C++ dll.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top