// C# 调用dll 传递c回调函数模板格式
// LuaInterface.LuaDLL;
using LuaInterface;
public partial class xxx { //... ... //C#默认情况下委托都是stdcall的调用方式,但可以通过UnmanagedFunctionPointer特性来修改 [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate int _c_Function( IntPtr luaStata); [DllImport("lua51.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void lua_pushcclosure (IntPtr L , [ MarshalAs(UnmanagedType.FunctionPtr)] _c_Function fn, int n=0); public _c_Function fucx; public int CallBack(IntPtr L) { LuaDLL.lua_settop(Lua,0); LuaDLL.lua_getglobal( Lua, "__debug_string"); textEditorControl2.Text= LuaDLL.lua_tostring(Lua, -1); LuaDLL.lua_pop(Lua 1); return 0; } // ... ... public void do() { //此处必用如下new形式生成堆对象,否则运行报错 fucx=new _c_Function( CallBack) ; lua_pushcclosure( Lua, fucx ); LuaDLL.lua_setglobal( Lua , "CallBack"); } }
标签:Function,luainterface,LuaDLL,Lua,C#,lua,luanet,public From: https://www.cnblogs.com/chenyalin/p/16937156.html