🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

c# and lua dll

Started by
20 comments, last by arthc 20 years, 11 months ago
finally i got inside the code. it was a library that hadn''t created a .pdb. now the debugger tells me:
The value ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.

any idea what this means ?
Advertisement
That means you gotta check your LUA function headers in your code and compare them with the function implementation in the LUA code. I''ve not used C# personally, but in C++ this would be like an ''extern "C"'' missing or __stdcall, etc.

Chris Pergrossi
< ctoan >
My Realm
Chris PergrossiMy Realm | "Good Morning, Dave"
quote: Original post by c t o a n
That means you gotta check your LUA function headers in your code and compare them with the function implementation in the LUA code. I''ve not used C# personally, but in C++ this would be like an ''extern "C"'' missing or __stdcall, etc.


hmm.. now i am not following you. check my lua function headers and compare with the function implementation in the lua code ?
i''m a little confused
All functions have a calling scheme, which can vary depending on preference or need. You are familiar with these, ya? Things such as __stdcall, etc. are called calling schemes, and they have to do with who''s responsibility it is to clean up the stack and how parameters are passed, etc. Therefore, if function A is calling function B, but B is in a DLL file, you need a declaration of B in a header file so you can use it (otherwise you''ve no clue it exists). Well, if the declaration of B (in the header file) says A (the calling func) will clean up the stack, but the actual implementation of B in the DLL file cleans up the stack itself (contrary to what A expects), then you will get an error similar to the one you described. Hopefully you can follow this, as it''s seems quite screwball to me! Anyway, this is the way it would work in VC++ 6.0, but .NET is unknown to me, so I''m merely offering my $0.02.

Chris Pergrossi
< ctoan >
My Realm
Chris PergrossiMy Realm | "Good Morning, Dave"
okay.. this is what i have so far:

Scripting.cs
// created on 2003-07-19 at 12:56using System;using System.Runtime.InteropServices;namespace Scripting{	class LUA	{		// The delegate for the Register() function		public delegate int lua_CFunction(IntPtr L);				// Private LUA functions		[DllImport("lua")] private static extern IntPtr lua_open();		[DllImport("lua")] private static extern void lua_close(IntPtr L);		[DllImport("lua")] private static extern void lua_pushstring(IntPtr L, string s);				[DllImport("lua")] private static extern void lua_gettable(IntPtr L, int idx);		[DllImport("lua")] private static extern void lua_settable(IntPtr L, int idx);		[DllImport("lua")] private static extern int lua_gettop(IntPtr L);		[DllImport("lua")] private static extern string lua_tostring(IntPtr L, int idx);		[DllImport("lua")] private static extern void lua_settop(IntPtr L, int idx);				[DllImport("lua")] private static extern void lua_pushcclosure(IntPtr L, lua_CFunction fn, int n);		[DllImport("lua")] private static extern int lua_dofile(IntPtr L, string filename);		[DllImport("lua")] private static extern int lua_dostring(IntPtr L, string str);				// Private LUA constants		private const int LUA_GLOBALSINDEX = -10001;				// Private members		private IntPtr L = IntPtr.Zero;				// Constructor		public LUA()		{			L = lua_open();		}				// Destructor		~LUA()		{			if( L != IntPtr.Zero )			{				lua_settop(L, 0);				lua_close(L);			}		}				// Public functions		public int DoFile(string filename)		{			return lua_dofile(L, filename);		}				public int DoString(string str)		{			return lua_dostring(L, str);		}				public int GetGlobal(string s)		{			// The two lines below are the same as lua_getglobal() in lua.h			lua_pushstring(L, s);			lua_gettable(L, LUA_GLOBALSINDEX);						// Return the index of the value			return lua_gettop(L);		}				public string ToString(int idx)		{			return lua_tostring(L, idx);		}				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);		}				// Static members		public static void PushString(IntPtr _L, string s)		{			lua_pushstring(_L, s);		}	}}


MainForm.cs
...private void Test_Click(object sender, EventArgs e)		{			LUA L = new LUA();			LUA.lua_CFunction test = new LUA.lua_CFunction(l_Test); // Need to keep an instance of this			L.Register("Test", test);			L.DoFile("test.lua");						MessageBox.Show(L.ToString(L.GetGlobal("testvar")));		}				public static int l_Test(IntPtr L)		{			MessageBox.Show("Test()");			LUA.PushString(L, "Pushed string");			return 1; // <- Crashes after the return		}...


and in the test.lua file there is just:
testvar = Test();

[edited by - arthc on July 22, 2003 3:10:40 AM]
Hmm... Stack Pointer indeed...

I don''t know how to do extern "C" in C#... I guess I would make a small managed C++ class library to call the LUA stuff and act as a go-between.
did you tried to declare your class unsafe?Then the use of Pointers is allowed.
The DllImport attribute allows you to specify the calling convention:

[DllImport( "lua", CallingConvention = CallingConvention.Cdecl )private static extern somefunc( UIntPtr L ); 


Unfortunately, C# doesn''t allow you to set the calling convention on a callback(however, MC++ and MSIL does). See the following thread: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&threadm=ew6mLkV6BHA.1932%40tkmsftngp03&rnum=1&prev=/groups%3Fq%3Dcalling%2Bconvention%2Bdelegate%2Bgroup%253Amicrosoft.public.dotnet.*%26btnG%3DGoogle%2BSearch%26hl%3Den%26lr%3D%26ie%3DISO-8859-1


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]
quote: Original post by Nypyren
Hmm... Stack Pointer indeed...

I don''t know how to do extern "C" in C#... I guess I would make a small managed C++ class library to call the LUA stuff and act as a go-between.


i tested this. now the problem is how i declare function pointers here so that c# understands them

You''d still need a delegate.
--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