c# and lua dll
hi.
i''m using c# and is trying to use lua with it. the base is rather simple with some [DllImport("lua.dll")] UIntPtr lua_open(); etc. but i have run in to a problem. see, lua_register() is a define of 3 statements:
lua_pushstring() - pushes the function name into the stack
lua_pushcclosure() - pushes the function into the stacl
lua_settable() - "sets" the function
and lua_pushcclosure() needs a function pointer. but how do i get that function pointer in c# ?! i''ve tried using delegate but when and it runs fine, but it crashes just after i''ve run the function with a System.NullReferenceException:
System.NullReferenceException: Object reference not set to an instance of an object.
any idea why ?
A delegate is what you should marshall from C# into unmanaged code when a function pointer is expected. Maybe you could give us some more details?
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
oh, sorry. here's what i've done:
LUA.cs
MainForm.cs
test.lua
[edited by - arthc on July 21, 2003 4:55:36 AM]
LUA.cs
namespace Scripting{ class LUA { // The delegate for the Register() function public delegate int lua_CFunction(UIntPtr L); // Private LUA functions (liblualib.dll = lua.dll) [DllImport("liblualib.dll")] private static extern UIntPtr lua_open(); [DllImport("liblualib.dll")] private static extern void lua_close(UIntPtr L); [DllImport("liblualib.dll")] private static extern int lua_dofile(UIntPtr L, string filename); [DllImport("liblualib.dll")] private static extern void lua_pushstring(UIntPtr L, string s); [DllImport("liblualib.dll")] private static extern void lua_settable(UIntPtr L, int idx); [DllImport("liblualib.dll")] private static extern void lua_pushcclosure(UIntPtr L, lua_CFunction fn, int n);... // Private LUA constants private const int LUA_GLOBALSINDEX = -10001;... // Public functions public int DoFile(string filename) { return lua_dofile(L, filename); } public void Register(string n, lua_CFunction f) { lua_pushstring(L, n); lua_pushcclosure(L, f, 0); // lua_pushcfunction(L, f) lua_settable(L, LUA_GLOBALSINDEX); } . . . }}
MainForm.cs
namespace MyNamespace{ class MainForm { . . . private void Test_Click(object sender, EventArgs e) { LUA L = new LUA(); L.Register("Test", new LUA.lua_CFunction(l_Test)); if( L.DoFile("test.lua") != 0 ) MessageBox.Show(L.ToString(0)); // Debugging System.Windows.Forms.MessageBox.Show(L.ToString(L.GetGlobal("testvar"))); }... public static int l_Test(UIntPtr L) { LUA.PushString(L, "Testing"); return 1; } // <- crashes after here (i guess)... }}
test.lua
testvar = Test();
[edited by - arthc on July 21, 2003 4:55:36 AM]
Do you have symbols and source for the Lua stuff? If so, you can step into the Lua code in order to see where it crashes and/or get a better stack trace. Note that you have to enable debugging of native code in this case, and debugging will be veerrry slow.
BTW: Where does the L come from in the LUA class? Does it get initialized?
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
BTW: Where does the L come from in the LUA class? Does it get initialized?
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
hmm.. i tried stepping through the code but it doesn't get "inside" so i can't debug. dunno why.
L is an UIntPtr and gets initialized in the LUA() constructor simply with L = lua_open();
my guess is that the dll loses its instance when a new instance of the LUA class is created. i'll try making them static or something
EDIT: Doh. they are already static..
[edited by - arthc on July 21, 2003 7:16:10 PM]
L is an UIntPtr and gets initialized in the LUA() constructor simply with L = lua_open();
my guess is that the dll loses its instance when a new instance of the LUA class is created. i'll try making them static or something
EDIT: Doh. they are already static..
[edited by - arthc on July 21, 2003 7:16:10 PM]
quote: Original post by arthc
hmm.. i tried stepping through the code but it doesn''t get "inside" so i can''t debug. dunno why.
Well, as I mentioned, you need the debug symbols(*.pdb) for the Lua source, as well as the source itself.
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Did you enable native debugging? Properties->Configuration Properties->Debugging->Debuggers->Enable Unmanaged Debugging. But I warn you - its slooooowww...
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Also, the Modules(Debugger->Windows->Modules) window will tell you if the debugger was able to load symbols.
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement