dynamic loading of windows dll's in lua as plugins?

classic Classic list List threaded Threaded
16 messages Options
Reply | Threaded
Open this post in threaded view
|

dynamic loading of windows dll's in lua as plugins?

Jeroen Janssen
Hello,

I'm working on using lua for a demosystem and I was thinking about a
pluginsystem using dll's (windows platform) to add lua functions.

Basicly what I am thinking about is to have LoadLibrary (windows function
to runtime load a dll) accessable from lua and have a DllMain in such a dll
that makes new C functions available in lua. Thus you can write the
following script:

--Start
LoadLibrary("plugin1.dll")
LoadLibrary("plugin2.dll")

functionFromPlugin1("Hello");
functionFromPlugin2("This is a plugin example!");
--End

Is there anybody that has already written something like this for windows?
is it available from anywhere?

Thanks in advance,

Jeroen Janssen

Reply | Threaded
Open this post in threaded view
|

RE: dynamic loading of windows dll's in lua as plugins?

Samson
Hello Jeroen

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!

regards
Lyndon


> -----Original Message-----
> From:	Jeroen Janssen [SMTP:[hidden email]]
> Sent:	Monday, May 17, 1999 8:12 AM
> To:	Multiple recipients of list
> Subject:	dynamic loading of windows dll's in lua as plugins?
> 
> Hello,
> 
> I'm working on using lua for a demosystem and I was thinking about a
> pluginsystem using dll's (windows platform) to add lua functions.
> 
> Basicly what I am thinking about is to have LoadLibrary (windows function
> to runtime load a dll) accessable from lua and have a DllMain in such a
> dll
> that makes new C functions available in lua. Thus you can write the
> following script:
> 
> --Start
> LoadLibrary("plugin1.dll")
> LoadLibrary("plugin2.dll")
> 
> functionFromPlugin1("Hello");
> functionFromPlugin2("This is a plugin example!");
> --End
> 
> Is there anybody that has already written something like this for windows?
> is it available from anywhere?
> 
> Thanks in advance,
> 
> Jeroen Janssen

Reply | Threaded
Open this post in threaded view
|

RE: dynamic loading of windows dll's in lua as plugins?

Jeroen Janssen
In reply to this post by Jeroen Janssen
At 05:26 5/17/99 -0300, you wrote:
>Hello Jeroen
>
>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

Reply | Threaded
Open this post in threaded view
|

RE: dynamic loading of windows dll's in lua as plugins?

Samson
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:[hidden email]]
> 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
> 

Reply | Threaded
Open this post in threaded view
|

RE: dynamic loading of windows dll's in lua as plugins?

Jeroen Janssen
In reply to this post by Jeroen Janssen
At 06:27 5/18/99 -0300, you wrote:
>
>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.

is there a special reason you chose for this solution?

>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.

Don't know if this will work but exactly as I think but:

If I create a LUA.DLL that exports all lua functions I can let my main
application load the LUA.DLL (runtime or loadtime).
Extension DLL's can refer to functions from the LUA.DLL (using the import
.LIB from the DLL).
When I use LoadLibrary to load an extension DLL (from my main application
or from a LUA script) the extension dll will have access to the LUA DLL
functions (because the .LIB imports all functions of LUA.DLL and the
LUA.DLL is in memory). Also since every DLL has a DllMain function that
gets executed by LoadLibrary I can register luafunctions from the DllMain
of the extension DLL.

Thus in short extension DLLs will make C/C++ functions available to LUA
from their DllMain function and because the extension DLLs get linked with
the import LIB from LUA.DLL they have access to the LUA functions without
needing to have a control structure with all available LUA functions,etc
(and not having to call a "register" function yourself)

I haven't got a chance to try this explicitly because I'm currently busy
with some other things (altough I have got a LUA.DLL at the moment and I
did some test regarding DLLs and LoadLibrary), but I think it should
work... any suggestions or remarks if this idea would (not) work? 

Jeroen

Reply | Threaded
Open this post in threaded view
|

RE: dynamic loading of windows dll's in lua as plugins?

Ashley Fryer-2
>
> Thus in short extension DLLs will make C/C++ functions available to LUA
> from their DllMain function and because the extension DLLs get linked with
> the import LIB from LUA.DLL they have access to the LUA functions without
> needing to have a control structure with all available LUA functions,etc
> (and not having to call a "register" function yourself)
>
> I haven't got a chance to try this explicitly because I'm currently busy
> with some other things (altough I have got a LUA.DLL at the moment and I
> did some test regarding DLLs and LoadLibrary), but I think it should
> work... any suggestions or remarks if this idea would (not) work?
>

I think your plan will work.

Don't forget to un-register your lua functions from your dll's clean-up
code.  Otherwise Lua will end up with invalid function pointers.

There isn't a lua_unregister macro, but this should do the trick:

#define lua_unregister(n)	(lua_pushnil(), lua_setglobal(n))

Regards,
ashley

Reply | Threaded
Open this post in threaded view
|

Re: dynamic loading of windows dll's in lua as plugins?

Eckhart Köppen
In reply to this post by Samson
I'll just want to add that it might be useful to have some sort of
compatibility of the dynamich loading mechanisms under Unix (Linux) and
Windows. There is the Lua-Linux package by Chris Gonnerman (at
http://members.linuxstart.com/~solomoriah/lua-linux.html) that uses two
functions for dynamic loading: loadpackage and require. Maybe it's
worthwhile to agree on common names across platforms?

Regards,

Eckhart

Reply | Threaded
Open this post in threaded view
|

RE: dynamic loading of windows dll's in lua as plugins?

Samson
In reply to this post by Samson
This is a good clean solution, you may end up having to recompile all your
extensions if you change your lua.dll though. I think implicit links are
done through ordinal numbers.

> -----Original Message-----
> From:	Ashley Fryer [SMTP:[hidden email]]
> Sent:	Tuesday, May 18, 1999 12:38 PM
> To:	Multiple recipients of list
> Subject:	RE: dynamic loading of windows dll's in lua as plugins?
> 
> >
> > Thus in short extension DLLs will make C/C++ functions available to LUA
> > from their DllMain function and because the extension DLLs get linked
> with
> > the import LIB from LUA.DLL they have access to the LUA functions
> without
> > needing to have a control structure with all available LUA functions,etc
> > (and not having to call a "register" function yourself)
> >
> > I haven't got a chance to try this explicitly because I'm currently busy
> > with some other things (altough I have got a LUA.DLL at the moment and I
> > did some test regarding DLLs and LoadLibrary), but I think it should
> > work... any suggestions or remarks if this idea would (not) work?
> >
> 
> I think your plan will work.
> 
> Don't forget to un-register your lua functions from your dll's clean-up
> code.  Otherwise Lua will end up with invalid function pointers.
> 
> There isn't a lua_unregister macro, but this should do the trick:
> 
> #define lua_unregister(n)	(lua_pushnil(), lua_setglobal(n))
> 
> Regards,
> ashley

Reply | Threaded
Open this post in threaded view
|

RE: dynamic loading of windows dll's in lua as plugins?

Jeroen Janssen
In reply to this post by Ashley Fryer-2
At 08:38 5/18/99 -0300, you wrote:
>I think your plan will work.
>
>Don't forget to un-register your lua functions from your dll's clean-up
>code.  Otherwise Lua will end up with invalid function pointers.

well, I wasn't planning to unload the DLLs until the end of the application
(so no LUA script will be run anymore), but it'll be better to unregister
the functions if you provide runtime unloading as well. The reason I don't
want to unload DLLs during runtime is that you'll also want to check if
there aren't any variables present in the LUA environment that need the DLL
to be present. 

i.e.

if I have defined a new lua "class" type with an extension dll I don't want
the DLL to be unloaded until all variables of that specific class type get
destroyed... don't know what would happen if you did

anyone other ideas about loading/unloading LUA extension and making sure
code only gets unloaded when possible?

>There isn't a lua_unregister macro, but this should do the trick:
>
>#define lua_unregister(n)	(lua_pushnil(), lua_setglobal(n))

ok, thanks

Jeroen

Reply | Threaded
Open this post in threaded view
|

Re: dynamic loading of windows dll's in lua as plugins?

Roberto Ierusalimschy
> There is the Lua-Linux package by Chris Gonnerman (at
> http://members.linuxstart.com/~solomoriah/lua-linux.html) that uses two
> functions for dynamic loading: loadpackage and require. Maybe it's
> worthwhile to agree on common names across platforms?

There is a guy here (Renato Borges) that already implemented a cross 
platform load package facility (for Linux, Solaris, Windows and others). It 
is called "loadlib: Dynamic library loading facilities for use with the Lua 
language". I am not sure whether he is shy ("travando"?) or he is not 
reading the list, but for sure you should see his implementation before 
defining a more general solution. If he does not show up in the list, he 
can be contacted directly at <[hidden email]>. 

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: dynamic loading of windows dll's in lua as plugins?

Jeroen Janssen
In reply to this post by Eckhart Köppen
At 09:12 5/18/99 -0300, you wrote:
>I'll just want to add that it might be useful to have some sort of
>compatibility of the dynamich loading mechanisms under Unix (Linux) and
>Windows. There is the Lua-Linux package by Chris Gonnerman (at
>http://members.linuxstart.com/~solomoriah/lua-linux.html) that uses two
>functions for dynamic loading: loadpackage and require. Maybe it's
>worthwhile to agree on common names across platforms?

yes, I agree.. I'll have a look at Lua-Linux and the dynamic loading part
of that distribution.

Jeroen

Reply | Threaded
Open this post in threaded view
|

Re: dynamic loading of windows dll's in lua as plugins?

Renato Borges
In reply to this post by Roberto Ierusalimschy
>There is a guy here (Renato Borges) that already implemented a cross 
>platform load package facility (for Linux, Solaris, Windows and others). It 
>is called "loadlib: Dynamic library loading facilities for use with the Lua 
>language".

Ok, here I am. 
I put loadlib at http://www.tecgraf.puc-rio.br/~rborges/loadlib/.
As Roberto already said, it's a library for dynamic library loading
in Lua (CGILua currently uses loadlib).
It supports dll (ms-windows) and dlfcn (most unix flavors) APIs.
It also has the implementation for shl (hp-ux) and rld (NeXT) APIs, 
although not tested. 

--Renato



Reply | Threaded
Open this post in threaded view
|

Re: dynamic loading of windows dll's in lua as plugins?

Luiz Henrique de Figueiredo
In reply to this post by Jeroen Janssen
>From [hidden email] Tue May 18 15:37:39 1999

>>There is a guy here (Renato Borges) that already implemented a cross 
>>platform load package facility (for Linux, Solaris, Windows and others). It 
>>is called "loadlib: Dynamic library loading facilities for use with the Lua 
>>language".
>
>Ok, here I am. 
>I put loadlib at http://www.tecgraf.puc-rio.br/~rborges/loadlib/.
>As Roberto already said, it's a library for dynamic library loading
>in Lua (CGILua currently uses loadlib).
>It supports dll (ms-windows) and dlfcn (most unix flavors) APIs.
>It also has the implementation for shl (hp-ux) and rld (NeXT) APIs, 
>although not tested. 

I've addded a link to loadlib in the addons page.
--lhf

Reply | Threaded
Open this post in threaded view
|

Re: dynamic loading of windows dll's in lua as plugins?

Jeroen Janssen
At 17:57 5/18/99 -0300, you wrote:
>>From [hidden email] Tue May 18 15:37:39 1999
>>Ok, here I am. 
>>I put loadlib at http://www.tecgraf.puc-rio.br/~rborges/loadlib/.
>>As Roberto already said, it's a library for dynamic library loading
>>in Lua (CGILua currently uses loadlib).
>>It supports dll (ms-windows) and dlfcn (most unix flavors) APIs.
>>It also has the implementation for shl (hp-ux) and rld (NeXT) APIs, 
>>although not tested. 
>
>I've addded a link to loadlib in the addons page.
>--lhf

I downloaded it, it and looks like that's exactly what I need (also
regarding the fact that this library will append ".so" & ".dll" when
loading libraries, thus scripts that run on different lua platforms
(linux/windows) won't need to be rewritten if you need to have
crossplatform luascripts). 

Thanks for all your replies,

Now I can start working on the actual thing :)

Jeroen Janssen



Reply | Threaded
Open this post in threaded view
|

Re: dynamic loading of windows dll's in lua as plugins?

Jeroen Janssen
At 09:18 5/19/99 -0300, you wrote:
>At 17:57 5/18/99 -0300, you wrote:
>>>From [hidden email] Tue May 18 15:37:39 1999
>>>Ok, here I am. 
>>>I put loadlib at http://www.tecgraf.puc-rio.br/~rborges/loadlib/.
>
>I downloaded it, it and looks like that's exactly what I need (also

>Now I can start working on the actual thing :)

Yesterday I managed to create the following on the win32 platform :
lua.dll, tolua.dll, lualib.dll, loadlib.dll and lmidas.dll and a test
executable based upon lua.c (the console lua app).

My test executable is dynamicly linked with lua.dll, tolua.dll, lualib.dll
and loadlib.dll and I can use the lua loadlib function to load the
lmidas.dll from lua. This lmidas.dll contains the lua "tolua" code for
midas11.dll (a soundsystem) publicly available from www.s2.org/midas. The
DllMain function of lmidas.dll calls the tolua_lmidas_open() function. 

As a result of all this (and as I needed to able to do) I now can load a
dll from within lua, that dll will provide the lua "glue" code for another
dll (in this case midas11.dll) and I can call midas functions from within lua.

However, after thinking about some remarks made about dynamicly
loading/unloading libraries I send an email to the tolua author asking to
add support for a tolua_package_close() function that will remove the
package from lua. Currently only a tolua_package_open() function gets
generated and for the unloading proces of libraries it's usefull if the
code to remove the package from lua also gets generated by tolua.

I also am wondering if there is some kind of mechanism to allow an
(auto-generated)check? be performed on wether there are lua variables that
contain a type provided by the to-be-unloaded package. (this to garantee
that other (lua) code will not crash when trying to access an unloaded
function or type). any ideas?

The main reason (at the moment) for this consideration is to have a console
app available that can be used to dynamicly load, test & unload dynamic
libraries (and start the load,test,unload cycle again) instead of providing
only the loading of dynamic libraries wich implicit requires to exit the
running program and start a new program.

regards,

Jeroen Janssen

Reply | Threaded
Open this post in threaded view
|

[ANNOUNCE] CGILua 3.2

Anna M. Hester
In reply to this post by Luiz Henrique de Figueiredo
CGILua version 3.2 is now available!


* What is CGILua?
-----------------

CGILua is a tool for developing dynamic HTML pages and manipulating input data from forms. 
It uses the interpreted language Lua for codifying its scripts.
CGILua has been on the road for 4 years now, being used in many industrial 
web sites and academic projects. Its main features are:

+ extensibility
  through Lua libraries and dynamic loading of C/C++ libraries (uses internally loadlib)
+ multi-paradigmatic scripting
  scripts can be written as Lua programs or HTML templates (HTML with Lua code embedded)
+ security
  environment can be configured providing customized security schemes
+ portability
  source code is POSIX compliant. Binaries were built for Linux, Solaris, SunOS, IRIX,
  Windows 9x/NT, and soon for AIX and FreeBSD. Scripts are platform-independent.
+ flexibility
  it is a Lua application! :-)

* Availability
---------------

CGILua is freely available for both academic and commercial 
purposes and can be downloaded from the site below:

http://www.tecgraf.puc-rio.br/cgilua

* New in version 3.2
--------------------

+ Uses the new version of the Lua interpreter (3.2)
+ New documentation, both in english and in portuguese
+ Improved error messages
+ File upload handling
+ New template directive IF
+ Enhanced flexibility for customizing errors signaling (error information 
  can be easily sent for the webmaster by email, for instance)
+ New libraries providing mail capability, criptography functions and cookies facilities
+ New core functions for HTTP headers handling
+ New state managing mechanism provided by the function cgilua.savestate()
  
Soon there will be available beta versions of ISAPI and Apache API bindings.

Please send your comments to us at [hidden email]. 
Thanks.