lua-users home
lua-l archive

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


i have a question regarding the use of 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.

Can i cast a function pointer to something luaffi interprets as callable function?

I have tried this (snipped out of the code):

    typedef void (__stdcall *RESULT_CB)(DWORD ResultID,LONG Result);

function libraryFunctionXYZ(data)
  local _cb = =ffi.cast("RESULT_CB*",data.cbAddr)


That doesn't work, i am getting the error:

"only function callbacks are callable"

Is it even possible to cast a function pointer memory address to something luaffi can understand/call?