[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: os.execute and the dreaded Windows console
- From: "Fabio Mascarenhas" <mascarenhas@...>
- Date: Mon, 8 Sep 2008 18:03:13 -0500
This is not really the same as os.execute, because CreateProcess is
asynchronous. You also need to release the process and thread handles
that Windows creates for the new process. Check
http://msdn.microsoft.com/en-us/library/ms682512(VS.85).aspx.
For the function below, put this before "return 0":
WaitForSingleObject( info.hProcess, INFINITE );
CloseHandle( info.hProcess );
CloseHandle( info.hThread );
This will make the parent process wait for the child to finish (thus
making it synchronous, like os.execute) and release resources
appropriately.
A fancier API that disposes of the handles via __gc and is
asynchronous but returns a handle that you can block on is left as an
exercise for the reader :-), but it's pretty straightforward C code.
"...something like io.popen or whatever with some work" is a little
bit of an understatement :-), see
http://msdn.microsoft.com/en-us/library/ms682499(VS.85).aspx on how to
do it.
--
Fabio Mascarenhas
On Mon, Sep 8, 2008 at 5:34 PM, Chris <coderight@gmail.com> wrote:
> 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
>