tags:

views:

92

answers:

2

I am able to load a DLL created from C source from within Lua. So what I want to be able to do is pass the current Lua state FROM Lua to the loaded DLL.

Basically I'm using a game engine that uses Lua. The scene editor of said game engine creates the Lua state and calls Lua scripts and I know for a fact that it uses 1 lua state for all the scripts that it calls. So, I would imagine that state is known from within these lua scripts themselves. From within these Lua scripts I want to load my own DLL and pass in this state to that DLL, so my C++ code can use that lua state to call lua functions from the same lua scripts and be in the same state. Does that make sense?

+3  A: 

I'm missing something obvious, I guess (which wouldn't surprise me - I'm far from a Lua expert).

But if you call package.loadlib, the function handle you get back IS going to be called with the state by Lua itself, isn't it? See the CFunction prototype

Mike G.
This seems correct to me - the C functions called by Lua are passed the Lua state. Just define your DLL functions like so: int myLuaFunction(lua_State *L) { //L contains the state }. Sorry about the formatting, comments can't seem to have newlines.
badgerr
+3  A: 

Write your DLL as a normal Lua module implemented in C. PiL has a description, but it adds up to naming a single exported function after the DLL's name so that the normal require function can load it. To get a library loaded by require "mylib", you create mylib.dll with the exported function luaopen_mylib() that creates a table containing all of the methods you want to be able to use and returns it. That function, as well as all the other methods it creates, is passed the current Lua state on each call.

If your engine doesn't allow require in scripts, then it isn't likely to have allowed package.loadlib either, and you are probably not going to be able to load your DLL at all.

RBerteig