Next: 3.2.1 Object Disposal
Up: 3. LuaCOM Elements
Previous: 3.1 LuaCOM API
Contents
Subsections
Next: 3.2.1 Object Disposal Up: 3. LuaCOM Elements Previous: 3.1 LuaCOM API Contents Fabio Mascarenhas de Queiroz 2005-01-07
3.2 LuaCOM objects
LuaCOM deals with LuaCOM objects, which are no more than a Lua table with the LuaCOM metatable and a reference to the LuaCOM C++ object; this one is, in turn, a proxy for the COM object: it holds an IDispatch pointer to the object and translates Lua accesses to Automation calls and property accesses. Here is a sample where a LuaCOM object is used:
-- Instantiate a Microsoft(R) Calendar Object calendar = luacom.CreateObject("MSCAL.Calendar") -- Error check if calendar == nil then print("Error creating object") exit(1) end -- Method call calendar:AboutBox() -- Property Get current_day = calendar.Day -- Property Put calendar.Month = calendar.Month + 1 print(current_day) print(calendar.Month)
Every time LuaCOM needs to convert an IDispatch pointer to Lua it creates a LuaCOM object. There are two situations where this happens:
- when calling LuaCOM API functions that return COM objects (CreateObject, GetObject, NewObject, Connect etc.) and
- when receiving return values from COM, where some of the values are IDispatch pointers.
Follows a sample of these situations:
-- First, we get a luacom object using LuaCOM API excel = luacom.CreateObject("Excel.Application") assert(luacomE.GetType(excel) == "LuaCOM") -- now we get one from a method call sheets = excel.Sheets assert(luacomE.GetType(sheets) == "LuaCOM")
A LuaCOM object may be passed as a parameter to method calls on other LuaCOM objects, if these methods expect an argument of type dispinterface. Here is a sample to illustrate this situation:
-- Gets a running instance of Excel excel = luacom.GetObject("Excel.Application") -- Gets the set of worksheets sheets = excel.Worksheets -- gets the first two sheets sheet1 = sheets:Item(1) sheet2 = sheets:Item(2) -- Exchange them (here we pass the second sheet as a parameter -- to a method) sheet1:Move(nil, sheet2)
There are two kinds of LuaCOM objects: typed and generic ones. The typed ones are those whose COM object has type information. The generic ones are those whose COM object does not supply any type information. This distinction is important in some situations.
Subsections
Next: 3.2.1 Object Disposal Up: 3. LuaCOM Elements Previous: 3.1 LuaCOM API Contents Fabio Mascarenhas de Queiroz 2005-01-07