lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Good evening,

For those of you who have been following along you will remember I had issues getting luaInterface (version 2) to work with the luaForWindows version of luaSQL in Visual Studio .NET 2008 - for my particular usage this was critical.

I've hit upon a solution and thought I would pass along the technique in case anyone else is trying to utilize one or more of the modules with luaInterface 2:

The solution involves building the module into the luaDLL that gets built as part of the luaInterface build. Sounds complicated but it really wasn't. The hardest part was reworking the luaSQL code to handle the odbc32 SQLWCHAR type rather than the SQLCHAR type. Here is what I did:

In the file "luainterface2\lua-5.1.2\src\linit.c"

// Added the call to open the luaSQL module - this will cause the module to be loaded automatically

static const luaL_Reg lualibs[] = {
  {"", luaopen_base},
  {LUA_LOADLIBNAME, luaopen_package},
  {LUA_TABLIBNAME, luaopen_table},
  {LUA_IOLIBNAME, luaopen_io},
  {LUA_OSLIBNAME, luaopen_os},
  {LUA_STRLIBNAME, luaopen_string},
  {LUA_MATHLIBNAME, luaopen_math},
  {LUA_DBLIBNAME, luaopen_debug},
  {LUA_LUASQL, luaopen_luasql_odbc},
  {NULL, NULL}
};

// Added the prototype and supporting entries in "luainterface2\lua-5.1.2\src\lualib.h"

#ifndef LUASQL_API
    #define LUASQL_API
#endif
#define LUA_LUASQL "luasql"
LUASQL_API int luaopen_luasql_odbc (lua_State *L);

Created a directory called "Modules" in "luainterface2\lua-5.1.2\src"

Copied the relevant source code files to the luainterface2\lua-5.1.2\src\Modules directory:

luainterface2\lua-5.1.2\src\Modules
    ls_odbc.c
    luasql.c
    luasql.h

Added the following two lines to "luainterface2\lua-5.1.2\src\luaglue.cpp" - this causes the luaSQL code to be included and compiled

#include "Modules/luasql.c"
#include "Modules/ls_odbc.c"

Edited ls_odbc.c and modified the code to use SQLWCHAR in the right places and to convert from SQLWCHAR
to char where needed.

Added odbc32.lib to the list of libraries to be linked with.

Then I rebuilt the luaInteface project, creating the two DLLs in luainterface2\Built:

lua51.dll
LuaInterface.dll

I then added those DLLs as references in my C# Windows Application project. This allowed me to use Lua as so:

    private Lua pLuaVM = null;
    pLuaVM = new Lua();

Just as described in the LuaInterface documentation. In lua code I can access luaSQL like this - note that "require luasql" isnt needed - the module is built into the Lua51.dll and automatically loaded:

    sql_env, Msg = luasql.odbc()
    connection, Msg = sqlEnv:connect(odbcName,userName, password)

Feel free to ping me with any questions. The modified source to luaSQL is pretty specific to my needs and wouldn't likely do anyone else much good. On the other hand, the technique I used to get it working could be useful to anyone trying to integrate using luaInteface and .NET with various lua modules. Is this a clean and generic solution? No it is not. Does it work? Like a charm.

Have a great evening
Terry