lua-users home
lua-l archive

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


On Mon, Sep 8, 2008 at 3:58 PM, Taylor Venable <taylor@metasyntax.net> wrote:
> Hi all,
>
> Is there any good way to prevent a Windows console from appearing
> every time I use os.execute from a non-console app?
>
> Thanks for any ideas.

I don't know if you're going to be able to do it with any of the
built-in Lua commands.  I just created a quick little function that
works though.  Assuming you add it as "os.CreateProcess" then usage is
something like:

os.CreateProcess('cmd.exe /c path_to_my_script.bat')

It creates the process with no window.  Modify as needed.  I believe
this could also be expanded to do something like io.popen or whatever
with some work.

Windows-only of course.

#ifdef _WIN32
static int CreateProcess_(lua_State* L)
{
   size_t len;
   const char* cmd0 = luaL_checklstring(L, 1, &len);
   char* cmd = lua_newuserdata(L, len + 1);
   STARTUPINFO start;
   PROCESS_INFORMATION info;

   strcpy(cmd, cmd0);
   memset(&start, 0x00, sizeof(start));
   start.cb = sizeof(start);

   if (!CreateProcess(NULL, cmd, NULL, NULL, FALSE, CREATE_NO_WINDOW,
NULL, NULL, &start, &info))
      luaL_error(L, "CreateProcess failed");

   return 0;
}
#endif /* _WIN32 */

CR