Re: FFI newby question: importing C functions

  • From: Dimiter 'malkia' Stanev <malkia@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Mon, 25 Jun 2012 22:35:33 -0700

On 6/25/2012 10:05 PM, QuentinC wrote:
> Sure, but instead of a classic Lua/C API wrapper, you could use a
direct wrapper. Then export the wrapper function and bind to it
via the FFI.

What do you mean by that ? Making a dummy DLL separated from my executable code containing what I need and bind to it, or something simpler ?

Because it appears that I can't bind to any arbitrary function present in my executable. If I do this, it doesn't work either :
ffi.cdef[[ int dummy (int) ; ]]
print(ffi.C.dummy(7))
for a function declared in C in that way :
int dummy (int n) { return n*3; }
IN fact it works only for functions I haven't defined myself, i.e. part of any external DLL, such as user32.dll for MessageBoxA. That would definitely explin why round doesn't work, if MinGW adds its definition on the fly.


Even if it's suboptimal in this particular case, the question is still interesting, because it's probably not the only function provided by MinGW in that fashion.


You can export functions from your main executable.
I don't use mingw, but Windows SDK/DDK and by just having a certain function marked __declspec(dllexport) would put as export in the executable.
Then you should be able to refer it from there.

Something like this:

in C
__declspec(dllexport) int myfun(void)
{
}

in Lua:
ffi.cdef[[ int myfun() ]]
myexe = ffi.load( "myexe.exe" )
myexe.myfun()


Other related posts: