Page 1 of 1
Lua using a PB dll?
Posted: Sun Apr 30, 2023 2:32 pm
by jassing
I tried to get Lua to call a PB dll, but I'm not getting anywhere. Has anyone done this?
I get weird results:
Lua results wrote:
Test2 result: [ function: 00000001800010a8 ]
Interestingly, the 1800010a8 is the function address w/in the DLL, but I can't seem to call it.
test.pb
Code: Select all
rocedureCDLL test2()
ProcedureReturn 2
EndProcedure
test.lua
Code: Select all
test2 = assert(package.loadlib("luatest.dll","test2"))
print("Test2 result: [", test2(),"]")
Re: Lua using a PB dll?
Posted: Sun Apr 30, 2023 3:18 pm
by Crusiatus Black
Hi Jassing,
What are you attempting to do? Do you want to create a Lua module, or do you merely want to invoke a DLL from Lua?
If you want to do the first, the functions invoked by loadlib should;
- Use the CDECL calling convention (i.e. ProcedureCDLL)
- Return an 32-bit int (PB's .l) describing the number of items on the Lua stack that should be returned
* and return said number of items on the Lua stack through the Lua stack manipulation functions, such as lua_pushinteger(L, 1)
- Accept a pointer to the lua_State struct (i.e. ProcedureCDLL.l luaopen_mylib(*L.lua_State))
Depending on which Lua version you're using, read the associated docs on loadlib:
-
https://www.lua.org/manual/5.1/manual.h ... ge.loadlib
-
https://www.lua.org/manual/5.4/manual.h ... ge.loadlib
For this to fully be done you might want a binding to Lua in PureBasic. If you merely want to call a function from a DLL
that you created in PureBasic, you might want to look into the FFI solutions that the Lua community has to offer, such as:
-
https://github.com/jmckaskill/luaffi
-
https://github.com/mascarenhas/alien
Re: Lua using a PB dll?
Posted: Sun Apr 30, 2023 3:42 pm
by jassing
I want to do the latter...
Ah, so there's no way to call a DLL function natively in Lua it requires an extension?
From Lua, I wanted to call a DLL's function expecting a numeric result, not dealing with the Lua stack directly.
thanks for the explanation;Apparently, I had (mis-)read the documentation & thought I could use loadlib & call the function.
Cheers,
-j
Re: Lua using a PB dll?
Posted: Mon May 01, 2023 1:20 pm
by Quin
It really depends actually. if you're using LuaJIT, there's functionality built in. I've called DLLs using it before.
http://luajit.org/ext_ffi.html
It's not a third-party module. The page says:
The FFI library is tightly integrated into LuaJIT (it's not available as a separate module).
If you're not using LuaJIT, though, it's a pain, and yeah, you need a third-party solution.
Re: Lua using a PB dll?
Posted: Mon May 01, 2023 1:26 pm
by jassing
Quin wrote: Mon May 01, 2023 1:20 pm
If you're not using LuaJIT, though, it's a pain, and yeah, you need a third-party solution.
Well, that is unfortunate...
I'll have a look at LuaJit then.. thanks.
Re: Lua using a PB dll?
Posted: Mon May 01, 2023 5:59 pm
by Crusiatus Black
Quin wrote: Mon May 01, 2023 1:20 pm
It really depends actually. if you're using LuaJIT, there's functionality built in. I've called DLLs using it before.
http://luajit.org/ext_ffi.html
It's not a third-party module. The page says:
The FFI library is tightly integrated into LuaJIT (it's not available as a separate module).
If you're not using LuaJIT, though, it's a pain, and yeah, you need a third-party solution.
I wouldn't say a pain, when you choose Lua you always have to align your requirements with the environment you're in.
So yes, a bit of effort depending on the situation

Re: Lua using a PB dll?
Posted: Mon May 01, 2023 6:20 pm
by jassing
Crusiatus Black wrote: Mon May 01, 2023 5:59 pm
I wouldn't say a pain, when you choose Lua you always have to align your requirements with the environment you're in.
So yes, a bit of effort depending on the situation
I expect effort; but it's how much I'm willing to put in to add something to a program -- My goal was to permit the user to specify a DLL & function to be called with varying parameters & I was hoping I could use Lua easily for that. I can do it with Kix, but with kix you have to write a file, then load the file (can't just load from memory like with Lua). I thought about AutoIt, but using com in PB is a pita. (Lua would have been faster since it wouldn't need disk i/o)
Lua is bare bones for sure... I have LuaJit on my todo list Gave it a quick try-to-compile, it failed, so I'm on to the next thing for now.
Cheers
-j
Re: Lua using a PB dll?
Posted: Tue May 02, 2023 10:25 am
by Crusiatus Black
jassing wrote: Mon May 01, 2023 6:20 pm
Crusiatus Black wrote: Mon May 01, 2023 5:59 pm
I wouldn't say a pain, when you choose Lua you always have to align your requirements with the environment you're in.
So yes, a bit of effort depending on the situation
I expect effort; but it's how much I'm willing to put in to add something to a program -- My goal was to permit the user to specify a DLL & function to be called with varying parameters & I was hoping I could use Lua easily for that. I can do it with Kix, but with kix you have to write a file, then load the file (can't just load from memory like with Lua). I thought about AutoIt, but using com in PB is a pita. (Lua would have been faster since it wouldn't need disk i/o)
Lua is bare bones for sure... I have LuaJit on my todo list Gave it a quick try-to-compile, it failed, so I'm on to the next thing for now.
Cheers
-j
What Lua version and what platform does your application require?
I have a small binding to libffi for Lua which I mostly use on Windows,
I might be able to provide you with a Lua module that you can use.
Re: Lua using a PB dll?
Posted: Tue May 02, 2023 5:12 pm
by benubi
I tried to write a Lua DLL in PB and to use it with the official Lua interpreter and ran into problems.
You have two choices: either all DLL's including your app/exe compile with the static lib (embedded mode), or all "plug-in" DLL's including the main app use the DLL (extension mode). Otherwise there will be problems when mixing static/dynamic modes.
I used the extension approach, because I couldn't manage to make or find a static Lua lib for the current version that should be 5.4, but the Lua interpreter has the lib statically linked. So I would have a few seemingly successful calls and then errors.
Your code will always produce an error when called from Lua.
Code: Select all
ProcedureCDLL test2()
ProcedureReturn 2
EndProcedure
Lua "C-functions" have to look like this:
Code: Select all
ProcedureCDLL tolua_myproc(*L)
Protected return_values
ProcedureReturn return_values
EndProcedure
You need to have the amount of return values on the Lua stack otherwise they will be replaced/fill with nil values.
No matter if you use a DLL, or embedded "C" functions (PureBasic main app), or Lua scripts to create modules, you should check the documentation about the new package-system, how the "require" command works, among other things.
Re: Lua using a PB dll?
Posted: Tue May 02, 2023 10:40 pm
by jassing
Not entirely true - you can use an ordinary dll in lua.
It's all solved; working perfectly...
Re: Lua using a PB dll?
Posted: Tue May 02, 2023 10:45 pm
by Crusiatus Black
jassing wrote: Tue May 02, 2023 10:40 pm
Not entirely true - you can use an ordinary dll in lua.
It's all solved; working perfectly...
Good to hear!
