Re: Using FFI as a plugin

  • From: Dimiter 'malkia' Stanev <malkia@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Thu, 17 May 2012 10:37:07 -0700

Yes, I had considered this, but that requires knowing exactly where
the host decided to install my plugin, which can and will vary from
system to system. If the plugin could somehow resolve its own path,
that would do the trick, but at present I don't think there's a way to
do that.

/s/ Adam


On Windows, there are three ways for your module to get it's own path:

Here are the first two, unfortunately I don't think they can work in luajit, not unless you link or know specific symbol that is part of your DLL - for example it might work if you link statically to luajit, but you are still DLL, then instead of using __ImageBase (this is MSVC specific, or
&get_module_filename, use it, for example lua_open, or something like it)

extern "C" IMAGE_DOS_HEADER __ImageBase;
static std::string get_module_filename()
{
        MEMORY_BASIC_INFORMATION mbi;
        VirtualQuery(&get_module_filename,&mbi, sizeof(mbi) );
        HMODULE mod1 = (HMODULE)mbi.AllocationBase;
        HMODULE mod2 = (HMODULE)&__ImageBase;
        assert( mod1 == mod2 );
        char buf[ _MAX_PATH ];
        GetModuleFileNameA( mod1, buf, sizeof( buf ) );
        return buf;
}


But there is a better way, and I think LuaJIT is already using it, it should work on anything XP+:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms683200(v=vs.85).aspx <http://msdn.microsoft.com/en-us/library/windows/desktop/ms683200%28v=vs.85%29.aspx>

static std::string get_module_filename()
{
    HMODULE hModule = 0;
    GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, 
get_module_filename,&hModule);
    char buf[ _MAX_PATH ];
    GetModuleFileNameA(hModule, buf, sizeof( buf ) );
    return buf;
}


Sorry for giving out C, instead of luajit/ffi examples, but these should be easy to translate (I just copied them from a forum, where I posted the original suggestion, and was given the
latter which is much better)

I don't think how this is done on UNIX* like systems, but there is probably a way...


Other related posts: