[ZeroBrane Studio] Re: Call arbitrary Windows DLL Function

  • From: Rob Probin <rob.probin@xxxxxxxxx>
  • To: "zerobrane@xxxxxxxxxxxxx" <zerobrane@xxxxxxxxxxxxx>
  • Date: Thu, 2 Oct 2014 08:10:32 +0100

Not sure about the Lua built into ZBS, but standard Lua doesn't support such 
things without extension. But that's not difficult. 

For instance LuaforWindows https://code.google.com/p/luaforwindows/ has various 
extension libraries, for instance Alien - which allows access to unknown DLLs. 
There are other ones like LuaInterface that allows access to the CLR. 

You can use Alien even if you are not using LuaForWindows - it's available as a 
LuaRock, for instance, the Lua package manager. 

> On 1 Oct 2014, at 11:56, "Steve Russell" 
> <steve.russell@xxxxxxxxxxxxxxxxxxxxx> wrote:
> 
> Hi,
>  
> Does anybody know if it is possible to call a function in any system DLL in 
> Windows/Lua 5.2/ZBS?
>  
> I would like to call 3rd party and system functions. Ideally I’d call the DLL 
> functions from Lua directly, but I’m also considering writing a proxy DLL for 
> Lua to load and this would then load the 3rd party DLL and interact with the 
> functions contained therein.
>  
> I started by creating a Win32 DLL in VS2010 but Lua command line and ZBS 0.80 
> hang/crash during the ‘require(“LuaProxy”)’, here’s the C code:
>  
> // LuaProxy.cpp : Defines the exported functions for the DLL application.
> //
> #include "stdafx.h"
> #include <iostream> 
> // LuaProxy.cpp : Defines the exported functions for the DLL application.
> //
> #include <lua.hpp>
> extern "C"
> {
> static int funval = 10;
> static int function_1(lua_State* l)
> {
>     std::cout << "[DLL] Function 1 is very funny." << std::endl;
>     std::cout << "[DLL] It's fun value is: " << funval << std::endl;
>     lua_pushnumber(l, funval);
>     return 1;
> }
> static int function_2(lua_State* l)
> {
>     std::cout << "[DLL] Function 2 is twice as funny!" << std::endl;
>     std::cout << "[DLL] It's fun value is: " << 2*funval << std::endl;
>     lua_pushnumber(l, 2*funval);
>     return 1;
> }
> /*
>  * Registering functions
>  */
> static const struct luaL_Reg cfunctions[] = {
>     {"fun1", function_1},
>     {"fun2", function_2},
>     {NULL, NULL}
> };
> int __declspec(dllexport) luaopen_LuaProxy(lua_State* l)
> {
>  //   luaL_newlibtable(l, cfunctions);
> //    luaL_setfuncs(l, cfunctions, 0);
>          luaL_openlib(l, "lp", cfunctions, 0);
>          std::cout << "fun1(), fun2()" << std::endl;
>     return 1;
> }
> } // end extern "C"
>  
> Cheers,
> Steve
>  

Other related posts: