Lua Proxy Dll Two

lua-users home
wiki

The page LuaProxyDll describes how to make a proxy DLL that redirects application function calls to an EXE which has Lua core built-in. The utility presented below is similar in the sense that it helps to build a proxy DLL but its goal is slightly different: to allow combined use of applications or libraries linked with either of lua5.1.dll and lua51.dll. The application code doesn't have to be changed (existing binaries that require lua51.dll should be able to work with lua5.1.dll via that proxy immediately).

The utility requires using of MinGW development system.

Prerequisites:

1. mkproxy.lua (below on this page)
2. liblua5.1.def (definition file, generated when lua5.1.dll is built)
3. lua5.1.dll

mkproxy.lua:

-- Goal:         Create a proxy dll that redirects all calls to the "real" dll
-- Written by:   Shmuel Zeigerman

-- CONFIGURATION
local DEFFILE   = "liblua5.1.def" -- name of definition file
local REALPATH  = "c:\\exe"       -- path of forwarded DLL
local REALNAME  = "lua5.1"        -- name of forwarded DLL (without extension)
local PROXYNAME = "lua51"         -- name of forwarder (proxy) DLL
-- END OF CONFIGURATION

local cfile = assert(io.open("temp.c", "w"))
cfile:write[[
  #include <windows.h>
  BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD Reason, LPVOID Reserved)
  { return TRUE; }
]]
cfile:close()

local cmdline = table.concat ({
	"gcc --add-stdcall-alias -shared -s -nostdlib temp.o",
	DEFFILE,
	"-L" .. REALPATH,
	"-l" .. REALNAME,
	"-o" .. PROXYNAME .. ".dll",
}, " ")

os.execute("gcc -c temp.c -o temp.o")
os.execute(cmdline)
----------------------------------------------------------------------

-- ShmuelZeigerman

Comments

--I've wondered why LuaBinaries renames lua51.dll as lua5.1.dll, or, if this is a better approach, why Lua itself doesn't follow. This all just makes things needlessly more complicated for new users and for module authors not familiar with the rules. It's not a Good Thing(TM), for we come to the above. I see LuaBinaries is now distributed with such a wrapper too. --DavidManura

This was already discussed on the mailing list: [1] --ShmuelZeigerman

The following build is for MSVC2005. It uses the "nodefaultlib" linker option. The final proxy DLL was about 9 KB.

local DEFFILE  = "lua5.1.def" -- .def file for lua5.1.dll
local LIBFILE  = "lua5.1.lib" -- .lib file for lua5.1.dll
local CFILE   = "luaproxy.c"-- output file
local MAKFILE = "luaproxy.mak"  -- output file
----------------------------------------------------------------------
local cfile = assert(io.open(CFILE, "w"))
cfile:write [=[
#include <windows.h>
    BOOL APIENTRY
DllMain(HANDLE module, DWORD reason, LPVOID reserved)
    { return TRUE; }
]=]
cfile:close()
----------------------------------------------------------------------
local makfile = assert(io.open(MAKFILE, "w"))
makfile:write(string.format([=[
lua51.dll : luaproxy.c
	cl -I../lua/src /O2 /LD /GS- luaproxy.c /link /def:%s %s \
	   /out:lua51.dll /nodefaultlib /entry:DllMain
]=], DEFFILE, LIBFILE))
makfile:close()
----------------------------------------------------------------------

--DavidManura

It seems that what you are doing is building an empty C file which links to the stubs in the import library, and reexposing those stubs as a dll interface. This would mean that the DLL you generate:

1. does contain code (DllMain?, and the import library)
2. add a function call overhead to each API call
This is not necessary. There is a mechanism in Windows DLL format that allow to make the DLL loader create aliases on the fly when the DLL is loaded. When you open the DLL in depends.exe you can clearly see these entry points marked as forwards/aliases.

Tell me if I misunderstood your code.

-- JeromeVuarand

See Also


FindPage · RecentChanges · preferences
edit · history
Last edited December 8, 2007 10:07 pm GMT (diff)