lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

mathijs wrote:


I'm having a problem using lua and .net. I'm using an unmanaged class with lua in it in a managed .net app. It compiles fine but when calling the first function that uses lua I get the following error:

An unhandled exception of type 'System.TypeLoadException' occurred in Medusa Editor.exe

Additional information: Could not load type lua_State from assembly Medusa Editor, Version=1.0.1728.36664, Culture=neutral, PublicKeyToken=null.

The same class works fine in a fully unmanaged class.

Anyone had this problem before and/or knows how to fix it?

Yes, I've encountered this and it has nothing to do with Lua specifically.

IMHO it's a bug in the compiler for Managed C++. It only happens (as you experienced) when compiling managed code. The compiler gets confused by forward declarations of unmanaged types (like lua_State, for example) and emits metadata that makes the runtime engine think it should find a managed class/struct by that name. When you first attempt to load the code containing this metadata, it doesn't find that type definition (surprise, surprise), and you get the TypeLoadException.

The workaround for it is pretty simple, but a little tedious. In *every* file that references lua_State (or any other forward declared, unmanaged class/struct), you have to provide a fake definition for the type. Adding the line:

   struct lua_State {};

in the affected .c/.cpp file(s) should fix your problem.

I credit for helping me through this problem a few months ago. Worth reading.