next up previous contents
Next: 2.3.2 Using Methods and Up: 2.3 ActiveX binding Previous: 2.3 ActiveX binding   Contents

2.3.1 Implementing dispinterfaces in Lua

The ActiveX binding has a C++ class that implements a generic IDispatch interface. The implementation of this class translates the method calls and property accesses done on the objects of this class to Lua calls and table accesses. So, one may implement an ActiveX interface entirely in Lua provided it has a type library describing it. This type library may be a stand-alone one (referenced by its location on the file system) or may be associated with some registered component. In this case, it may be referenced by the ProgID of the component. The C++ objects of this class can be used in any place where an IDispatch or IUnknown interface is expected. Follows a sample implementation of an ActiveX dispinterface in Lua.

-- Creates and fills the Lua table that will implement the
-- ActiveX interface

events_table = {}

function events_table:AfterUpdate()
  print("AfterUpdate called!")
end

-- Here we implement the interface DCalendarEvents, which is part
-- of the Microsoft(R) Calendar object, whose ProgID is MSCAL.Calendar

events_obj = luacom_ImplInterface(
  events_table, 
  "MSCAL.Calendar",
  "DCalendarEvents")

-- Checks for errors
--
if events_obj == nil then
  print("Implementation failed")
  exit(1)
end

-- Tests the interface: this must generate a call to the events:AfterUpdate
-- defined above
--
events_obj:AfterUpdate()
If the interface to be implemented is described in a stand-alone type library, the function luacom_ImplInterfaceFromTypelib must be used instead:

-- Creates and fills the Lua table that will implement the
-- ActiveX interface

hello_table = {}

function hello:Hello()
  print("Hello World!")
end

-- Here we implement the interface IHello
--
hello_obj = luacom_ImplInterfaceFromTypelib("hello.tlb","IHello")

-- Checks for errors
--
if hello_obj == nil then
  print("Implementation failed")
  exit(1)
end

-- Tests the interface
--
hello_obj:Hello()
Both functions return a LuaCOM object, whose corresponding ActiveX object is implemented by the supplied table. So, any Lua calls to this LuaCOM object will be translated to ActiveX calls which, in turn, will be translated back to Lua calls on the implementation table. This LuaCOM object can be passed as an argument to ActiveX methods who expect a dispinterface or to LuaCOM API functions (like luacom_addConnection). One can also use the luacom_NewObject function, which is best suited to the situation where one needs to create a complete ActiveX object in Lua and wants to export it, so that it can be accessed through COM by any running application.
next up previous contents
Next: 2.3.2 Using Methods and Up: 2.3 ActiveX binding Previous: 2.3 ActiveX binding   Contents
Vinicius da Silva Almendra 2003-06-04