Lua using a PB dll?

Just starting out? Need help? Post your questions and find answers here.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Lua using a PB dll?

Post 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(),"]")
User avatar
Crusiatus Black
Enthusiast
Enthusiast
Posts: 389
Joined: Mon May 12, 2008 1:25 pm
Location: The Netherlands
Contact:

Re: Lua using a PB dll?

Post 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
Image
Bas Groothedde,
Imagine Programming

I live in a philosophical paradoxal randome filled with enigma's!
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Lua using a PB dll?

Post 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
Quin
Addict
Addict
Posts: 1125
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Lua using a PB dll?

Post 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.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Lua using a PB dll?

Post 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.
User avatar
Crusiatus Black
Enthusiast
Enthusiast
Posts: 389
Joined: Mon May 12, 2008 1:25 pm
Location: The Netherlands
Contact:

Re: Lua using a PB dll?

Post 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 :D
Image
Bas Groothedde,
Imagine Programming

I live in a philosophical paradoxal randome filled with enigma's!
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Lua using a PB dll?

Post 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 :D
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
User avatar
Crusiatus Black
Enthusiast
Enthusiast
Posts: 389
Joined: Mon May 12, 2008 1:25 pm
Location: The Netherlands
Contact:

Re: Lua using a PB dll?

Post 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 :D
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.
Image
Bas Groothedde,
Imagine Programming

I live in a philosophical paradoxal randome filled with enigma's!
benubi
Enthusiast
Enthusiast
Posts: 215
Joined: Tue Mar 29, 2005 4:01 pm

Re: Lua using a PB dll?

Post 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.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Lua using a PB dll?

Post by jassing »

Not entirely true - you can use an ordinary dll in lua.
It's all solved; working perfectly...
User avatar
Crusiatus Black
Enthusiast
Enthusiast
Posts: 389
Joined: Mon May 12, 2008 1:25 pm
Location: The Netherlands
Contact:

Re: Lua using a PB dll?

Post 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! :)
Image
Bas Groothedde,
Imagine Programming

I live in a philosophical paradoxal randome filled with enigma's!
Post Reply