From Sandcat Browser

Lua: DLLs

Lua allows you to combine compiled code and Lua scripts (see below a C++ example and a Delphi/Free Pascal example of a Lua DLL).

Wrappers for the Lua library are available for almost every compiled language. Alternatively, you can use C/Invoke to connect to C libraries at runtime.

Creating a Lua DLL (C++ Example)


//lua_net.cpp

#include <windows.h>
#include "..\lua\include\lua.hpp"

//open
static int net_Open(lua_State* L)
{
   lua_pushstring(L,"net open");
   return 1;
}

//close
static int net_Close(lua_State* L)
{
   lua_pushstring(L,"net close");
   return 1;
}


static const luaL_Reg syslib[] = {
   {"open",      net_Open},
   {"close",      net_Close},
   {NULL, NULL}
};


extern "C"{

__declspec(dllexport)

int luaopen_lua_net(lua_State *L)
{
  luaL_register(L, "net", syslib);
  return 1;
}

}

Loading the DLL


require "lua_net"
app.showmessage(net.open())
app.showmessage(net.close())

Creating a Lua DLL (Delphi/Free Pascal Example)


library Test;
{$MODE DELPHI}

uses Lua;

function test_Open(L: plua_State):integer; cdecl;
begin
 lua_pushstring(L,pchar('test open'));
 Result:=1;
end;

function luaopen_Test(L: plua_State):integer; cdecl;
begin
 lua_register(L,'test_open',@test_Open);
 Result := 1;
end;

Exports
 luaopen_Test;

begin
end.

Loading the DLL


require "Test"
app.showmessage(test_open())
Retrieved from http://www.syhunt.com/sandcat/index.php?n=Lua.CLibs
Page last modified on May 17, 2013, at 05:32 PM