lua-users home
lua-l archive

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

> #define LUA_ROOT	  "C:\\Program Files\\Lua51"
> #define LUA_LDIR	  LUA_ROOT "\\lua"
> #define LUA_CDIR	  LUA_ROOT "\\dll"

I question the use of these strings.

"C:\\Program Files" opens one up to the famous security hole
of someone installing C:\program.exe. If you must use this 
convention then %ProgramFiles% is available in the environment.
It beats the hardcoded string.

For loading Windows DLLs I have used the following strategy:

Assume that we have an executable or DLL that contains the
entry point luaopen_loadlib(). e.g. lua.exe lua5lib.dll lua5.dll or

I added to the default LUA_CPATH the subsitution character '#'
(if you can suggest a better character please do). So we my
default LUA_CPATH looks like.

#define LUA_CPATH_DEFAULT "#l?;#?;.\\l?;.\\?;?" /* # is the module directory */

in luaopen_loadlib() I added the following code

  /* set field `cpath' */
  path = getenv(LUA_CPATH);
  if (path == NULL) path = LUA_CPATH_DEFAULT;

  if (GetModuleFileName(NULL, moddir, sizeof(moddir))) {
    char* p=strrchr(moddir, '\\');
    if (p) *p='\0';
    if (p && *p != '\\') {*p++='\\'; *p='\0';}
    path = luaL_gsub(L, path, "#", moddir);
  else {
    lua_pushstring(L, path);
  lua_setfield(L, -2, "cpath");

This means that we have a DLL search path that is logically relative
to the module
directory of loadlib, yet is expands to an absolute path.

It has advantages in that the deployment of a Lua based application is
and flexible.

e.g. one can set LUA_CPATH to things like

#? - same directory as the Lua DLL
#\\usr   |  a place for user DLLs 
#..\usr  |
? use the default Windows search strategy

By expanding the path we can save a lot of searching without harcoding a path.
This also has the advantage that an application installer does not have to
build a CPATH that is dependent on the users chosen install directory.

In the default path that is provided above we get application DLLs loaded
a) The same directory as the Lua DLL (trys the 'l' prefixed version first)
b) the current working direcrtory
c) The standard Windows search strategy

To use a package like Lua socket simply requires something like

#define LUA_CPATH_DEFAULT "#l?;#?;#\\luasocket" /* # is the module directory */