Old 08-13-2019, 02:20 PM   #1
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,629
Default Does this load external dlls in Lua in Reaper?

While looking for something completely different, I stumbled upon this function:

package.loadlib

from the reference-manual:

Quote:
package.loadlib (libname, funcname)
Dynamically links the host program with the C library libname. Inside this library, looks for a function funcname and returns this function as a C function. (So, funcname must follow the protocol (see lua_CFunction)).

This is a low-level function. It completely bypasses the package and module system. Unlike require, it does not perform any path searching and does not automatically adds extensions. libname must be the complete file name of the C library, including if necessary a path and extension. funcname must be the exact name exported by the C library (which may depend on the C compiler and linker used).

This function is not supported by ANSI C. As such, it is only available on some platforms (Windows, Linux, Mac OS X, Solaris, BSD, plus other Unix systems that support the dlfcn standard).
This function is accepted by Lua in Reaper and now I wonder: Can we use this, to load dlls/dylibs/so-files in Reaper?

Has someone of you tried to use this already?
__________________
Use you/she/her.Ultraschall-Api Lua Api4Reaper - Donate, if you wish

On vacation for the time being...
Meo-Ada Mespotine is offline   Reply With Quote
Old 08-13-2019, 03:40 PM   #2
Xenakios
Human being with feelings
 
Xenakios's Avatar
 
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 8,062
Default

I didn't yet try it but I think this isn't very promising :

Code:
funcname must follow the protocol (see lua_CFunction)
Obviously, generic C libraries are not going to use the Lua call protocol except by chance.

Even if some really basic stuff would work, there probably isn't any way to write and read into/from C structs, which would be a pretty crucial thing to do with most libraries.

Binary Lua modules would still be hopeless to use. They only export a single function, that requires the lua_State* to be given as a parameter, which Reaper doesn't give access to.

edit : Doesn't look good. This just crashed Reaper with an access violation.

Code:
path = "C:\\Program Files\\Mega-Nerd\\libsndfile\\bin\\libsndfile-1.dll"
local f = assert(package.loadlib(path, "sf_version_string"))
str = f()
That was just trying to call a simple C function with the following signature :

Code:
const char * sf_version_string (void) ;
__________________
I am no longer part of the REAPER community. Please don't contact me with any REAPER-related issues.

Last edited by Xenakios; 08-13-2019 at 04:47 PM.
Xenakios is offline   Reply With Quote
Old 08-15-2019, 01:32 PM   #3
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,629
Default

Ahhh...whatapity... :/

Thanks for trying it
__________________
Use you/she/her.Ultraschall-Api Lua Api4Reaper - Donate, if you wish

On vacation for the time being...
Meo-Ada Mespotine is offline   Reply With Quote
Old 08-16-2019, 03:33 PM   #4
snooks
Banned
 
Join Date: Sep 2015
Posts: 1,650
Default

Interesting, this does allow us to call C! But not any C, as mentioned in the docs this only works with lua C functions, eg...

Code:
// for Windows dlls
#define API_FUNC extern "C" __declspec( dllexport )

API_FUNC int test(lua_State* L)
{
    lua_pushstring(L, "Hello, C world");
    return 1;
}
You need to statically link Lua when compiling your C code - full Windows static .libs (and include files) are here...

https://sourceforge.net/projects/lua...raries/Static/

(It would be nice if the devs could build REAPER with a Lua as a separate shared lib to enable using ordinary Lua C libs)
snooks is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 10:15 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.