[ZeroBrane Studio] Call arbitrary Windows DLL Function

  • From: "Steve Russell" <steve.russell@xxxxxxxxxxxxxxxxxxxxx>
  • To: <zerobrane@xxxxxxxxxxxxx>
  • Date: Wed, 1 Oct 2014 11:56:41 +0100

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: