LuaCOM User Manual
(Version 1.4)
Vinicius Almendra Renato Cerqueira Fabio Mascarenhas
May 16, 2008
Contents
1
Chapter 1
Introduction
LuaCOM is an add-on library to the Lua language that allows Lua programs to use and implement ob-
jects that follow Microsoft’s Component Object Model (COM) specification and use the Automation
technology for property access and method calls.
1.1 Features
Currently, the LuaCOM library supports the following features:
• dynamic instantiation of COM objects registered in the system registry, via the CreateObject
method;
• dynamic access to running COM objects via GetObject;
• COM method calls as normal Lua function calls and property accesses as normal table field
accesses;
• ability to read type libraries and to generate HTML documentation on-the-fly for COM objects;
• use of COM objects without type information;
• type conversion between OLE Automation types and Lua types;
• object disposal using Lua garbage collection mechanism;
• implementation of COM interfaces and objects using Lua tables;
• implementation of OLE controls using Lua tables (needs a Lua GUI toolkit that can create
in-place windows, like IUP);
• use of COM connection point mechanism for bidirectional communication and event handling;
• fully compatible with Lua 5.1;
• log mechanism to ease the debugging of applications.
2
1.2 Who Should Read What (or About the Manual)
This manual is mostly a reference manual. Here we document the behavior of LuaCOM in a variety
of situations, some implementation decisions that affect the end-user of the library and its limitations.
When facing some strange behavior in an application built using LuaCOM, the first step is to read
all the chapter ??, where the majority of possible problems are documented. There can be found
references to other sections of the manual, where more detailed information is provided.
Newbies For those who are newcomers, we provide a tutorial section (chapter ??) with a step-by-
step guide to start using LuaCOM. More help and samples can be found in LuaCOM’s home page
and in the demos folder. Notice that VBScript code can be easily converted to Lua with LuaCOM.
This manual does not provide information for developers who need deeper technical information
about LuaCOM or who are willing to modify it for some reason. For this kind of information, please
contact the authors.
Knowledge required This manual presumes some knowledge of COM and Automation. We don’t
intend to explain in detail how these technologies work or how they can be used to solve particular
problems. This information can be found easily in the web or in good books.
Some information about samples
The sample codes shown in this documentation are all for Lua 5.1. Samples for previous Lua versions
can be found in the documentation for the previous versions of LuaCOM.
3
Chapter 2
Tutorial
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
LuaCOM dynamic library through Lua 5’s module require 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 LuaCOM to 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 initialized after both COM and Lua; LuaCOM must be terminated
before Lua, and Lua must be terminated before COM
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 = luaL_newstate();
luacom_open(L);
if(luaL_dofile("luacom_sample.lua") != 0) {
puts("Error running sample!");
exit(1);
}
luacom_close(L);
1
Notice that COM must be initialized in each thread that will use it. To use LuaCOM in this situation, it’s not safe
to share a single Lua state among several threads; one should create a new Lua state for each thread and then initialize
LuaCOM with this state.
4
评论4
最新资源