next up previous contents
Next: 2.2 Locating COM Objects Up: 2. Tutorial Previous: 2. Tutorial   Contents

2.1 Using The LuaCOM library

LuaCOM is an add-on to the Lua language. To be used, the binary library of LuaCOM must be linked with the host program, just like the Lua library and other add-ons2.1. There are different version of the LuaCOM binary for the different version of the Lua library, so pay attention to link the right one. The next step is to modify the source code of the host program to call LuaCOM's and COM initialization and termination functions, which are part of the C/C++ API. To do so, include the LuaCOM's header -- luacom.h -- and call these functions in the proper order: LuaCOM must be initialize after COM and after Lua; it must be terminated before Lua; COM must be terminated AFTER Lua2.2. Here is an example of a simple C host program program using LuaCOM.
   /*
    * Sample C program using luacom
    */
   #include <stdio.h>
   #include <ole2.h> // needed for CoInitialize and CoUninitialize
   #include <lua.h>

   #include "luacom.h"

   int main (int argc, char *argv[]) {

     /* COM initialization */
     CoInitialize(NULL);

     /* library initialization */

     lua_State *L = lua_open();

     luacom_open(L);

     if(lua_dofile("luacom_sample.lua") != 0) {
       puts("Error running sample!");
       exit(1);
     }
     luacom_close(L);
     lua_close(L);

     CoUninitialize(NULL);
     return 0;
   }
Notice that it's necessary to initialize COM before lua_open and to terminate it only after the last lua_close, otherwise fatal errors may occur.
next up previous contents
Next: 2.2 Locating COM Objects Up: 2. Tutorial Previous: 2. Tutorial   Contents
Vinicius da Silva Almendra 2004-02-19