lua-users home
lua-l archive

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


I'm not saying this is the most elegant method, but it worked for me, I
managed to write an extension DLL which allowed LUA to run as a NT service.
The basic idea is to save the lua interface function addresses into a single
control block structure, which is then passed to a function in the DLL which
registers all the newly available extensions. As DLL's are considered part
of the address space of the main exe this is simple. I have a slight worry
about the global control blocks in the DLL and main program, do they
reference the same memory, I don't think so, it would probably be better
just to use a ptr here.

Here is the code, I've just cut and pasted the relevant parts, with a little
editing...
Let me know if you spot any obvious typos/errors.

Lyndon

My main executable has some code which is like this

  ...
  add_luaextension("mmext.dll");
  add_luaextension("networkext.dll");
  ...

The function is defined something like;

  struct ecb ecbv;  // extension control block, global so its allways
around.

  int add_luaextension(char *libname) {
    int alext_rc = 0;
    HANDLE hextlib;   // windows dll handle
    luaext_regfuncp extreg;  // ptr to an extension register function

    ecbv.lua_register = &lua_register;			// Save LUA func
addresses
    ecbv.luaL_check_lstr = &luaL_check_lstr;
    ecbv.lua_pushstring = &lua_pushstring;
    ecbv.lua_pushnumber = &lua_pushnumber;
    ecbv.luaL_check_number = &luaL_check_number;
    ecbv.lua_dostring = &lua_dostring;

    hextlib = LoadLibrary(libname);		// What no error checking?
    extreg =
(luaext_regfuncp)GetProcAddress(hextlib,"register_newluafuncs");  // Again?
    luaext_regfuncp &register_newluafuncs = extreg; // Only works in C++ but
it's either this or macros to 
                                                    // make it more readable
    alext_rc = register_newluafuncs(&ecbv);                   // or
(*extreg)(&ecbv);

    return alext_rc;
  }

with some other required definitions;

  struct ecb {
    void (*lua_register)(char *,void (*) (void));
    char *(*luaL_check_lstr)(int,long *);
    void (*lua_pushstring)(char *);
    void (*lua_pushnumber)(double);
    double (*luaL_check_number)(int);
    int (*lua_dostring)(char *);
  };
  typedef int (*luaext_regfuncp)(struct ecb *); 

In your extension dll have something like this

  // Following is just to make things more readable, could use a C++ ref as
well.
  #define lua_register(a,b) (*(gecbv.lua_register))(a,b) 
  struct ecb gecbv;  // global extension control block, so everyone can do
LUA things

  _declspec(dllexport) int register_newluafuncs(struct ecb *ecbpparm) {
    gecbv = *ecbpparm; // Make available to all.

    lua_register("w32ext_messagebox",mymessageboxfunc); // As in regular LUA
	
    return 0; // All ok???
  }

> -----Original Message-----
> From:	Jeroen Janssen [SMTP:japj@oce.nl]
> Sent:	Tuesday, May 18, 1999 9:30 AM
> To:	Samson, Lyndon (GEIS)
> Subject:	dynamic loading of windows dll's in lua as plugins?
> 
> At 05:26 5/17/99 -0300, you wrote:
> >Hello Jeroen
> 
> Hello Samson,
> 
> >I have something like this, I can send you the code if you like, it's
> >probably about 80% done, it might point you in the right direction
> anyway!
> 
> Yes please, if you could send me the code (or perhaps provide a link so 
> other people also can have a look) that would help me very much!
> 
> regards,
> 
> Jeroen Janssen
>