Next: luacom_IDispatch2LuaCOM
Up: luacom_detectAutomation
Previous: Description
Contents
Fabio Mascarenhas de Queiroz 2005-01-07
Sample
/* * com_object.cpp * * This sample C++ code initializes the libraries and * the COM engine to export a COM object implemented in Lua */ #include <ole2.h> // libraries extern "C" { #include <lua.h> #include <lualib.h> } #include <luacom.h> int main (int argc, char *argv[]) { int a = 0; CoInitialize(NULL); IupOpen(); lua_State *L = lua_open(0); lua_baselibopen (L); lua_strlibopen(L); lua_iolibopen(L); luacom_open(L); lua_dofile(L, "implementation.lua"); // Pushes the table containing the functions // responsible for the initialization of the // COM object lua_getglobal(L, "COM"); // detects whether the program was invoked for Automation, // registration or none of that int result = luacom_detectAutomation(L, argc, argv); switch(result) { case LUACOM_AUTOMATION: // runs the message loop, as all the needed initialization // has already been performed MessageLoop(); break; case LUACOM_NOAUTOMATION: // This only works as a COM server printf("Error. This is a COM server\n"); break; case LUACOM_REGISTER: // Notifies that the COM object has been // registered printf("COM object successfully registered."); break; case LUACOM_AUTOMATION_ERROR: // detectAutomation found /Automation or /Register but // the initialization Lua functions returned some error printf("Error starting Automation"); break; } luacom_close(L); lua_close(L); CoUninitialize(); return 0; } ------- -- implementation.lua -- -- This is a sample implementation of a COM server in Lua -- -- This is the implementation of the COM object TestObj = {} function TestObj:showWindow() dialog.show() end function TestObj:hideWindow() dialog.hide() end -- Here we create and populate the table to -- be used with detectAutomation COM = {} -- This functions creates the COM object to be -- exported and exposes it. function COM:StartAutomation() -- creates the object using its default interface COMAppObject, events, e = luacom.NewObject(TestObj, "TESTE.Teste") -- This error will be caught by detectAutomation if COMAppObject == nil then error("NewObject failed: "..e) end -- Exposes the object cookie = luacom.ExposeObject(COMAppObject) if cookie == nil then error("ExposeObject failed!") end end function COM:Register() -- fills table with registration information local reginfo = {} reginfo.VersionIndependentProgID = "TESTE.Teste" reginfo.ProgID = reginfo.VersionIndependentProgID..".1" reginfo.TypeLib = "teste.tlb" reginfo.CoClass = "Teste" reginfo.ComponentName = "Test Component" reginfo.Arguments = "/Automation" -- stores component information in the registry local res = luacom.RegisterObject(reginfo) if res == nil then error("RegisterObject failed!") end end function COM:UnRegister() -- fills table with registration information local reginfo = {} reginfo.VersionIndependentProgID = "TESTE.Teste" reginfo.ProgID = reginfo.VersionIndependentProgID..".1" reginfo.TypeLib = "teste.tlb" reginfo.CoClass = "Teste" -- removes component information from the registry local res = luacom.UnRegisterObject(reginfo) if res == nil then error("UnRegisterObject failed!") end end
Fabio Mascarenhas de Queiroz 2005-01-07