lua-users home
lua-l archive

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


On Friday, July 10, 2015 10:32:55 AM Björn Kalkbrenner wrote:
> Hi,
> 
> i have a question regarding the use of luaffi
> (https://github.com/jmckaskill/luaffi) and since i have read this
> mailinglist silently the last months the answers given to questions were
> really quite helpful, it's a great list - thanks to the contributors.
> 
> Maybe someone is here using luaffi or luajit/ffi (if the differences to
> luajit isn't that big) who can give me a direction. The OS is Windows.
> 
> I am creating a library which is dynamically loaded by another host
> process. I have no control over the host process and the API is fixed.
> 
> This host process calls functions in my library receiving a function
> pointer as argument (to return results async) and some struct memory
> adresses. My function adds these values to the lua stack and executes a
> lua script with lua_pcall. In other words, i am mapping calls to the
> library directly to lua-functions in my script.
> 
> Currently i am using luaffi in my script calling win32 API functions and
> working with the structs which is working great, the last thing which i
> have not yet resolved is calling the given callback which i have
> received as function pointer.
> 

Using FFI here seems like driving a finishing nail with a sledgehammer. Were 
you aware of the native winapi[1] bindings?

For the callback, the function pointer can be pushed to Lua as lightuserdata. 
Then define your own C function to be called from Lua

int mycallback(Lua_state *L) {
    RESULT_CB cb = lua_touserdata(L, 1);
    cb(lua_tointeger(L, 2), 0);
    return 0;
}

FFI isn't just an extra dependency for you to carry, it's also very, very slow 
unless you're using LuaJIT.

[1] https://stevedonovan.github.io/winapi/topics/readme.md.html

-- 
tom <telliamed@whoopdedo.org>