lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> Hi Lua List,
> I've found Alien to be a very interesting FFI library for Lua, but did not
> find bindings using it (if any).
> In particular, I was searching some Win32 API bindings in Lua for Alien but
> to no avail... :-(
> What would be even more interesting would be IMHO to build a script to
> generate those bindings from windows header files...
> Do you guys have insights about this or already done similar things? Maybe
> links or directions for me?
> Thanks!
> Jack

Hi!
I've made a module which uses lpeg and mingw to automate this for me.
It's not in any way beautiful code, but it works and many small
scripts i've written depend on it, so i'm not touching it without
reason. It should definately be rewritten. Anyway, i use it like this:

glue = require "luben.parser.peg.glue"
win = glue.Glue("@#include <windows.h>", {"kernel32", "user32",
"gdi32"}, "-D WINVER=0x0500 -D_WIN32_WINDOWS=0x0500")
local ClickLeftMouse
do
	local INPUT = win.INPUT
	local Input = INPUT.new()
	local SendInput = win.SendInput
	ClickLeftMouse = function()
		Input.type = win.INPUT_MOUSE
		Input.mi.dx = 0
		Input.mi.dy = 0
		Input.mi.dwFlags = win.MOUSEEVENTF_LEFTDOWN
		Input.mi.time = 0
		Input.mi.dwExtraInfo = 0
		SendInput(1, Input(), INPUT.sizeof())
		Input.mi.dwFlags = win.MOUSEEVENTF_LEFTUP
		SendInput(1, Input(), INPUT.sizeof())
	end	
end
win.Sleep(2000)
while 0==win.GetAsyncKeyState(win.VK_SPACE) do
	win.Sleep(500)
	ClickLeftMouse()
end

The script passes the includes trough gcc's preprocessor, fixing
additional includes and defines. The functions, constants, structs are
parsed when used(and memoized). functions are loaded from the dll's
supplied. structs and unions work, as can be seen in the example above
:)
/Pierre