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, either the binary library of LuaCOM must be linked with the host program, just like the Lua library and other add-ons, or you should load a LuaCOMdynamic library through Lua 5's require/loadlib mechanism. To use dynamic loading in Lua 4 you should implement a similar mechanism. There are different versions of the LuaCOM binary for the different versions of the Lua library, so pay attention to link the right one.

If you are linking LuaCOMto your program, 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.1. 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.

Using Lua 5 to dynamically load LuaCOM is simpler. Just call require("luacom") in your Lua script, and make sure the file luacom.lua is in your LUA_PATH environment variable, and the Lua and LuaCOM DLLs (lua-5.0.dll, lualib-5.0.dll and luacom-lua5-1.2.dll, respectively) are in your PATH. Then run your script with the Lua standalone interpreter.


next up previous contents
Next: 2.2 Locating COM Objects Up: 2. Tutorial Previous: 2. Tutorial   Contents
Fabio Mascarenhas de Queiroz 2004-09-13