public static extern IntPtr XLLRT_RegisterGlobalObj(IntPtr env, XLLRTObject theObject);
public static void RegisterClass() { //得到Lua环境句柄 IntPtr pNULL = new IntPtr(0); IntPtr hEnv; hEnv = XLLRTWarper.XLLRT_GetEnv(pNULL); //注册MyClassFactory 工厂类,通常这个类只有一个方法就是创造MyClass 实例 IntPtr pMember = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(XLLRTGlobalAPI))*2); XLLRTGlobalAPI pAPI = new XLLRTGlobalAPI(); pAPI.funName = "CreateInstance"; pAPI.permission =0; pAPI.func = _CreateInstance;//注意delegate对象的生命周期管理 Marshal.StructureToPtr(pAPI, pMember, true); IntPtr pMember2 = new IntPtr( pMember.ToInt32() + Marshal.SizeOf(typeof(XLLRTGlobalAPI))); XLLRTGlobalAPI pAPI2 = new XLLRTGlobalAPI(); pAPI.func = null; pAPI.funName = null; pAPI.permission = 0; Marshal.StructureToPtr(pAPI2, pMember2, true); fnGetObject pFun = new fnGetObject(LuaMyClassFactoryInstance); XLLRTObject theObject = new XLLRTObject(); theObject.className = "HelloBolt.MyClass.Factory.Class"; theObject.objName = "HelloBolt.MyClass.Factory"; theObject.userData = pNULL; theObject.pfnGetObject = pFun; theObject.memberFunctions = pMember; //真正注册 XLLRTWarper.XLLRT_RegisterGlobalObj(hEnv, theObject); //注册MyClass的类型信息 IntPtr pMyClassMember = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(XLLRTGlobalAPI)) * 4); XLLRTGlobalAPI pMyClassAPI = new XLLRTGlobalAPI(); pMyClassAPI.permission = 0; pMyClassAPI.funName = "Add"; pMyClassAPI.func = _Add;//注意delegate对象的生命周期管理 Marshal.StructureToPtr(pMyClassAPI, pMyClassMember, false); IntPtr pMyClassMember2 = new IntPtr(pMyClassMember.ToInt32() + Marshal.SizeOf(typeof(XLLRTGlobalAPI))); pMyClassAPI.funName = "AttachResultListener"; pMyClassAPI.func = _AttachResultListener;//注意delegate对象的生命周期管理 Marshal.StructureToPtr(pMyClassAPI, pMyClassMember2, false); IntPtr pMyClassMember3 = new IntPtr(pMyClassMember2.ToInt32() + Marshal.SizeOf(typeof(XLLRTGlobalAPI))); pMyClassAPI.funName = "__gc"; pMyClassAPI.func = _DeleteSelf;//注意delegate对象的生命周期管理 Marshal.StructureToPtr(pMyClassAPI, pMyClassMember3, false); IntPtr pMyClassMember4 = new IntPtr(pMyClassMember3.ToInt32() + Marshal.SizeOf(typeof(XLLRTGlobalAPI))); pMyClassAPI.func = null; pMyClassAPI.funName = null; Marshal.StructureToPtr(pMyClassAPI, pMyClassMember4, false); //真正注册 XLLRTWarper.XLLRT_RegisterClass(hEnv,"HelloBolt.MyClass",pMyClassMember,null,0); //清理资源 Marshal.FreeHGlobal(pMember); Marshal.FreeHGlobal(pMyClassMember); }