Lua Proxy Dll Three

lua-users home
wiki

Below are two Lua script to create a proxy dll for Lua 5.1. They is similar to what you can find on LuaProxyDllTwo (and to some extent on LuaProxyDll). However they have several advantages:

The first one, mkforwardlib.lua, requires Visual C++. It has been tested with Visual C++ 2005 Express, and with the free Visual C++ for AMD64 and IA64 platforms available with the Microsoft Platform SDK. If you use another version of Visual C++ you will have to put the path to link.exe in the VCPATH environment variable.

The second one, mkforwardlib-gcc.lua, requires GCC. It has been tested with MinGW 5.0.2 for x86.

mkforwardlib.lua:

local reallib
local fakelib
local machine
local extrasymbols = {}

local args = {...}
local errmsg
if args[1] then reallib = args[1] end
if args[2] then fakelib = args[2] end
if args[3] then machine = args[3] end
for i=4,#args do
	table.insert(extrasymbols, args[i])
end

if #args<3 then
	errmsg = "not enough parameters"
elseif not ({X86=true, AMD64=true, IA64=true})[machine] then
	errmsg = "'"..machine.."' is not a recognized machine architecture"
end
if errmsg then
	if errmsg then io.stderr:write("Error: "..errmsg.."\n") end
	io.stderr:write[[
Usage: mkforwardlib <reallib> <fakelib> <machine> [<extrasymbols>...]

    reallib         The name of the existing dll that you want to use.
    fakelib         The name of the fake dll that you want to create and that
                    will call the reallib instead.
    machine         The hardware architecture of your windows version among:
                    X86, AMD64, IA64
    extrasymbols    Additionnal symbols to export beyond standard Lua symbols
                    (if you have a very custom Lua dll).

Example: mkforwardlib lua51.dll lua5.1.dll X86
]]
	-- :TODO: add support for AM33, ARM, CEE, EBC, M32R, MIPS, MIPS16, MIPSFPU, MIPSFPU16, MIPSR41XX, SH4, SH5, THUMB
	os.exit(0)
end

local VisualC = os.getenv('VCPATH')
if not VisualC then
	if machine=='AMD64' then
		local PlatformSDK = assert(os.getenv"MSSDK", "MSSDK environment variable is not set, please set it to the path to your Platform SDK")
		VisualC = PlatformSDK.."\\Bin\\win64\\x86\\AMD64"
	elseif machine=='IA64' then
		local PlatformSDK = assert(os.getenv"MSSDK", "MSSDK environment variable is not set, please set it to the path to your Platform SDK")
		VisualC = PlatformSDK.."\\Bin\\win64"
	else
	--	local VisualCRoot = (os.getenv"PROGRAMFILES(X86)" or os.getenv"PROGRAMFILES").."\\Microsoft Visual Studio .NET 2003\\Vc7"
		local VisualCRoot = (os.getenv"PROGRAMFILES(X86)" or os.getenv"PROGRAMFILES").."\\Microsoft Visual Studio 8\\VC"
		VisualC = VisualCRoot.."\\bin"
	end
end

local symbols = {
	"luaL_addlstring",
	"luaL_addstring",
	"luaL_addvalue",
	"luaL_argerror",
	"luaL_buffinit",
	"luaL_callmeta",
	"luaL_checkany",
	"luaL_checkinteger",
	"luaL_checklstring",
	"luaL_checknumber",
	"luaL_checkoption",
	"luaL_checkstack",
	"luaL_checktype",
	"luaL_checkudata",
	"luaL_error",
	"luaL_findtable",
	"luaL_getmetafield",
	"luaL_gsub",
	"luaL_loadbuffer",
	"luaL_loadfile",
	"luaL_loadstring",
	"luaL_newmetatable",
	"luaL_newstate",
	"luaL_openlib",
	"luaL_openlibs",
	"luaL_optinteger",
	"luaL_optlstring",
	"luaL_optnumber",
	"luaL_prepbuffer",
	"luaL_pushresult",
	"luaL_ref",
	"luaL_register",
	"luaL_typerror",
	"luaL_unref",
	"luaL_where",
	"lua_atpanic",
	"lua_call",
	"lua_checkstack",
	"lua_close",
	"lua_concat",
	"lua_cpcall",
	"lua_createtable",
	"lua_dump",
	"lua_equal",
	"lua_error",
	"lua_gc",
	"lua_getallocf",
	"lua_getfenv",
	"lua_getfield",
	"lua_gethook",
	"lua_gethookcount",
	"lua_gethookmask",
	"lua_getinfo",
	"lua_getlocal",
	"lua_getmetatable",
	"lua_getstack",
	"lua_gettable",
	"lua_gettop",
	"lua_getupvalue",
	"lua_insert",
	"lua_iscfunction",
	"lua_isnumber",
	"lua_isstring",
	"lua_isuserdata",
	"lua_lessthan",
	"lua_load",
	"lua_newstate",
	"lua_newthread",
	"lua_newuserdata",
	"lua_next",
	"lua_objlen",
	"lua_pcall",
	"lua_pushboolean",
	"lua_pushcclosure",
	"lua_pushfstring",
	"lua_pushinteger",
	"lua_pushlightuserdata",
	"lua_pushlstring",
	"lua_pushnil",
	"lua_pushnumber",
	"lua_pushstring",
	"lua_pushthread",
	"lua_pushvalue",
	"lua_pushvfstring",
	"lua_rawequal",
	"lua_rawget",
	"lua_rawgeti",
	"lua_rawset",
	"lua_rawseti",
	"lua_remove",
	"lua_replace",
	"lua_resume",
	"lua_setallocf",
	"lua_setfenv",
	"lua_setfield",
	"lua_sethook",
	"lua_setlocal",
	"lua_setmetatable",
	"lua_settable",
	"lua_settop",
	"lua_setupvalue",
	"lua_status",
	"lua_toboolean",
	"lua_tocfunction",
	"lua_tointeger",
	"lua_tolstring",
	"lua_tonumber",
	"lua_topointer",
	"lua_tothread",
	"lua_touserdata",
	"lua_type",
	"lua_typename",
	"lua_xmove",
	"lua_yield",
--	"luaopen_base",
--	"luaopen_debug",
--	"luaopen_io",
--	"luaopen_math",
--	"luaopen_os",
--	"luaopen_package",
--	"luaopen_string",
--	"luaopen_table",
}

local command = {
	'"'..VisualC..'\\link.exe"',
	" -dll",
	" -nologo",
	" -noentry",
	" -machine:"..machine,
	" -incremental:no",
	" -nodefaultlib",
	" -out:"..fakelib..".dll",
	" -implib:"..fakelib..".lib",
}

for _,symbol in ipairs(symbols) do
	table.insert(command," /export:"..symbol.."="..reallib.."."..symbol)
end
for _,symbol in ipairs(extrasymbols) do
	table.insert(command," /export:"..symbol.."="..reallib.."."..symbol)
end

os.execute('"'..table.concat(command)..'"')

mkforwardlib-gcc.lua:

local reallib
local fakelib
local machine
local extrasymbols = {}

local args = {...}
local errmsg
if args[1] then reallib = args[1] end
if args[2] then fakelib = args[2] end
if args[3] then machine = args[3] end
for i=4,#args do
	table.insert(extrasymbols, args[i])
end

if #args<3 then errmsg = "not enough parameters" end
if not ({X86=true})[machine] then errmsg = "'"..machine.."' is not a recognized machine architecture" end
if errmsg then
	if errmsg then io.stderr:write("Error: "..errmsg.."\n") end
	io.stderr:write[[
Usage: mkforwardlib-gcc <reallib> <fakelib> <machine> [<extrasymbols>...]

    reallib         The name of the existing dll that you want to use.
    fakelib         The name of the fake dll that you want to create and that
                    will call the reallib instead.
    machine         The hardware architecture of your windows version among:
                    X86
    extrasymbols    Additionnal symbols to export beyond standard Lua symbols
                    (if you have a very custom Lua dll).

Example: mkforwardlib lua51.dll lua5.1.dll X86
]]
	-- :TODO: add support for AMD64, IA64, AM33, ARM, CEE, EBC, M32R, MIPS, MIPS16, MIPSFPU, MIPSFPU16, MIPSR41XX, SH4, SH5, THUMB
	os.exit(0)
end

local symbols = {
	"luaL_addlstring",
	"luaL_addstring",
	"luaL_addvalue",
	"luaL_argerror",
	"luaL_buffinit",
	"luaL_callmeta",
	"luaL_checkany",
	"luaL_checkinteger",
	"luaL_checklstring",
	"luaL_checknumber",
	"luaL_checkoption",
	"luaL_checkstack",
	"luaL_checktype",
	"luaL_checkudata",
	"luaL_error",
	"luaL_findtable",
	"luaL_getmetafield",
	"luaL_gsub",
	"luaL_loadbuffer",
	"luaL_loadfile",
	"luaL_loadstring",
	"luaL_newmetatable",
	"luaL_newstate",
	"luaL_openlib",
	"luaL_openlibs",
	"luaL_optinteger",
	"luaL_optlstring",
	"luaL_optnumber",
	"luaL_prepbuffer",
	"luaL_pushresult",
	"luaL_ref",
	"luaL_register",
	"luaL_typerror",
	"luaL_unref",
	"luaL_where",
	"lua_atpanic",
	"lua_call",
	"lua_checkstack",
	"lua_close",
	"lua_concat",
	"lua_cpcall",
	"lua_createtable",
	"lua_dump",
	"lua_equal",
	"lua_error",
	"lua_gc",
	"lua_getallocf",
	"lua_getfenv",
	"lua_getfield",
	"lua_gethook",
	"lua_gethookcount",
	"lua_gethookmask",
	"lua_getinfo",
	"lua_getlocal",
	"lua_getmetatable",
	"lua_getstack",
	"lua_gettable",
	"lua_gettop",
	"lua_getupvalue",
	"lua_insert",
	"lua_iscfunction",
	"lua_isnumber",
	"lua_isstring",
	"lua_isuserdata",
	"lua_lessthan",
	"lua_load",
	"lua_newstate",
	"lua_newthread",
	"lua_newuserdata",
	"lua_next",
	"lua_objlen",
	"lua_pcall",
	"lua_pushboolean",
	"lua_pushcclosure",
	"lua_pushfstring",
	"lua_pushinteger",
	"lua_pushlightuserdata",
	"lua_pushlstring",
	"lua_pushnil",
	"lua_pushnumber",
	"lua_pushstring",
	"lua_pushthread",
	"lua_pushvalue",
	"lua_pushvfstring",
	"lua_rawequal",
	"lua_rawget",
	"lua_rawgeti",
	"lua_rawset",
	"lua_rawseti",
	"lua_remove",
	"lua_replace",
	"lua_resume",
	"lua_setallocf",
	"lua_setfenv",
	"lua_setfield",
	"lua_sethook",
	"lua_setlocal",
	"lua_setmetatable",
	"lua_settable",
	"lua_settop",
	"lua_setupvalue",
	"lua_status",
	"lua_toboolean",
	"lua_tocfunction",
	"lua_tointeger",
	"lua_tolstring",
	"lua_tonumber",
	"lua_topointer",
	"lua_tothread",
	"lua_touserdata",
	"lua_type",
	"lua_typename",
	"lua_xmove",
	"lua_yield",
--	"luaopen_base",
--	"luaopen_debug",
--	"luaopen_io",
--	"luaopen_math",
--	"luaopen_os",
--	"luaopen_package",
--	"luaopen_string",
--	"luaopen_table",
}

local def = io.open("lib"..fakelib..".def", "wb")
def:write("EXPORTS\n")
if reallib:find("%.") then
	reallib = '"'..reallib..'"'
end
for _,symbol in ipairs(symbols) do
	def:write(""..symbol.."="..reallib.."."..symbol.."\n")
end
for _,symbol in ipairs(extrasymbols) do
	def:write(""..symbol.."="..reallib.."."..symbol.."\n")
end
def:close()

os.execute('"'..table.concat({
	'gcc',
--	" -shared", -- this option seem to be incompatible with -nostartfiles
	" -o "..fakelib..".dll",
	" -nostartfiles",
	" lib"..fakelib..".def",
})..'"')

os.execute('"'..table.concat({
	'strip',
	" --strip-unneeded",
	" "..fakelib..".dll",
})..'"')
-- JeromeVuarand

This is the same script as above with few modifications to make it work with Lua 5.2:

mkforwardlib-gcc-52.lua:

local reallib
local fakelib
local machine
local extrasymbols = {}

local args = {...}
local errmsg
if args[1] then reallib = args[1] end
if args[2] then fakelib = args[2] end
if args[3] then machine = args[3] end
for i=4,#args do
	table.insert(extrasymbols, args[i])
end

if #args<3 then errmsg = "not enough parameters" end
if not ({X86=true})[machine] then errmsg = "'"..machine.."' is not a recognized machine architecture" end
if errmsg then
	if errmsg then io.stderr:write("Error: "..errmsg.."\n") end
	io.stderr:write[[
Usage: mkforwardlib-gcc <reallib> <fakelib> <machine> [<extrasymbols>...]

    reallib         The name of the existing dll that you want to use.
    fakelib         The name of the fake dll that you want to create and that
                    will call the reallib instead.
    machine         The hardware architecture of your windows version among:
                    X86
    extrasymbols    Additionnal symbols to export beyond standard Lua symbols
                    (if you have a very custom Lua dll).

Example: mkforwardlib lua52 lua5.2 X86
]]
	-- :TODO: add support for AMD64, IA64, AM33, ARM, CEE, EBC, M32R, MIPS, MIPS16, MIPSFPU, MIPSFPU16, MIPSR41XX, SH4, SH5, THUMB
	os.exit(0)
end

local symbols = {
        'luaL_addlstring',
        'luaL_addstring',
        'luaL_addvalue',
        'luaL_argerror',
        'luaL_buffinit',
        'luaL_buffinitsize',
        'luaL_callmeta',
        'luaL_checkany',
        'luaL_checkinteger',
        'luaL_checklstring',
        'luaL_checknumber',
        'luaL_checkoption',
        'luaL_checkstack',
        'luaL_checktype',
        'luaL_checkudata',
        'luaL_checkunsigned',
        'luaL_checkversion_',
        'luaL_error',
        'luaL_execresult',
        'luaL_fileresult',
        'luaL_getmetafield',
        'luaL_getsubtable',
        'luaL_gsub',
        'luaL_len',
        'luaL_loadbufferx',
        'luaL_loadfilex',
        'luaL_loadstring',
        'luaL_newmetatable',
        'luaL_newstate',
        'luaL_openlib',
        'luaL_openlibs',
        'luaL_optinteger',
        'luaL_optlstring',
        'luaL_optnumber',
        'luaL_optunsigned',
        'luaL_prepbuffsize',
        'luaL_pushmodule',
        'luaL_pushresult',
        'luaL_pushresultsize',
        'luaL_ref',
        'luaL_requiref',
        'luaL_setfuncs',
        'luaL_setmetatable',
        'luaL_testudata',
        'luaL_tolstring',
        'luaL_traceback',
        'luaL_unref',
        'luaL_where',
        'lua_absindex',
        'lua_arith',
        'lua_atpanic',
        'lua_callk',
        'lua_checkstack',
        'lua_close',
        'lua_compare',
        'lua_concat',
        'lua_copy',
        'lua_createtable',
        'lua_dump',
        'lua_error',
        'lua_gc',
        'lua_getallocf',
        'lua_getctx',
        'lua_getfield',
        'lua_getglobal',
        'lua_gethook',
        'lua_gethookcount',
        'lua_gethookmask',
        'lua_getinfo',
        'lua_getlocal',
        'lua_getmetatable',
        'lua_getstack',
        'lua_gettable',
        'lua_gettop',
        'lua_getupvalue',
        'lua_getuservalue',
        'lua_insert',
        'lua_iscfunction',
        'lua_isnumber',
        'lua_isstring',
        'lua_isuserdata',
        'lua_len',
        'lua_load',
        'lua_newstate',
        'lua_newthread',
        'lua_newuserdata',
        'lua_next',
        'lua_pcallk',
        'lua_pushboolean',
        'lua_pushcclosure',
        'lua_pushfstring',
        'lua_pushinteger',
        'lua_pushlightuserdata',
        'lua_pushlstring',
        'lua_pushnil',
        'lua_pushnumber',
        'lua_pushstring',
        'lua_pushthread',
        'lua_pushunsigned',
        'lua_pushvalue',
        'lua_pushvfstring',
        'lua_rawequal',
        'lua_rawget',
        'lua_rawgeti',
        'lua_rawgetp',
        'lua_rawlen',
        'lua_rawset',
        'lua_rawseti',
        'lua_rawsetp',
        'lua_remove',
        'lua_replace',
        'lua_resume',
        'lua_setallocf',
        'lua_setfield',
        'lua_setglobal',
        'lua_sethook',
        'lua_setlocal',
        'lua_setmetatable',
        'lua_settable',
        'lua_settop',
        'lua_setupvalue',
        'lua_setuservalue',
        'lua_status',
        'lua_toboolean',
        'lua_tocfunction',
        'lua_tointegerx',
        'lua_tolstring',
        'lua_tonumberx',
        'lua_topointer',
        'lua_tothread',
        'lua_tounsignedx',
        'lua_touserdata',
        'lua_type',
        'lua_typename',
        'lua_upvalueid',
        'lua_upvaluejoin',
        'lua_version',
        'lua_xmove',
        'lua_yieldk',
--      'luaopen_base',
--      'luaopen_bit32',
--      'luaopen_coroutine',
--      'luaopen_debug',
--      'luaopen_io',
--      'luaopen_math',
--      'luaopen_os',
--      'luaopen_package',
--      'luaopen_string',
--      'luaopen_table',
}

local def = io.open(fakelib..".def", "wb")
def:write("EXPORTS\n")
if reallib:find("%.") then
	reallib = '"'..reallib..'"'
end
for _,symbol in ipairs(symbols) do
--	def:write(""..reallib.."."..symbol.."="..reallib.."."..symbol.."\n")
	def:write(""..symbol.."="..reallib.."."..symbol.."\n")
end
for _,symbol in ipairs(extrasymbols) do
--	def:write(""..reallib.."."..symbol.."="..reallib.."."..symbol.."\n")
	def:write(""..symbol.."="..reallib.."."..symbol.."\n")
end
def:close()

os.execute('"'..table.concat({
	'gcc',
--	" -shared", -- this option seem to be incompatible with -nostartfiles
	" -o "..fakelib..".dll",
	" -nostartfiles",
	" "..fakelib..".def",
})..'"')

os.execute('"'..table.concat({
	'strip',
	" --strip-unneeded",
	" "..fakelib..".dll",
})..'"')
-- PaulKulchenko?

See Also


RecentChanges · preferences
edit · history
Last edited October 31, 2013 7:44 pm GMT (diff)