This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Problem with pex-win32.c
Here is a sample program which does the right thing (no spurious console
windows, all output visible) when run either from a console or from a
console-free environment, such as a Cygwin xterm. This is the code
we'll be working into libiberty -- unless someone has a better solution!
--
Mark Mitchell
CodeSourcery
mark@codesourcery.com
(650) 331-3385 x713
#include <windows.h>
#include <stdio.h>
int main() {
HANDLE stdin_handle;
HANDLE stdout_handle;
HANDLE stderr_handle;
DWORD dwCreationFlags;
OSVERSIONINFO version_info;
STARTUPINFO si;
PROCESS_INFORMATION pi;
/* Replace these with handles for pipes, etc. */
stdin_handle = GetStdHandle (STD_INPUT_HANDLE);
stdout_handle = GetStdHandle (STD_OUTPUT_HANDLE);
stderr_handle = GetStdHandle (STD_ERROR_HANDLE);
version_info.dwOSVersionInfoSize = sizeof (version_info);
GetVersionEx (&version_info);
if (version_info.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
/* On Windows 95/98/ME the CREATE_NO_WINDOW flag is not
supported, so we cannot avoid creating a console window. */
dwCreationFlags = 0;
else
{
HANDLE conout_handle;
/* Determine whether or not we have an associated console. */
conout_handle = CreateFile("CONOUT$",
GENERIC_WRITE,
FILE_SHARE_WRITE,
/*lpSecurityAttributes=*/NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
/*hTemplateFile=*/NULL);
if (conout_handle == INVALID_HANDLE_VALUE)
/* There is no console associated with this process. Since
the child is a console process, the OS would normally
create a new console Window for the child. Since we'll be
redirecting the child's standard streams, we do not need
the console window. */
dwCreationFlags = CREATE_NO_WINDOW;
else
{
/* There is a console associated with the process, so the OS
will not create a new console. And, if we use
CREATE_NO_WINDOW in this situation, the child will have
no associated console. Therefore, if the child's
standard streams are connected to the console, the output
will be discarded. */
CloseHandle(conout_handle);
dwCreationFlags = 0;
}
}
/* Since the child will be a console process, it will, by default,
connect standard input/output to its console. However, we want
the child to use the handles specifically designated above. In
addition, if there is no console (such as when we are running in
a Cygwin X window), then we must redirect the child's
input/output, as there is no console for the child to use. */
memset (&si, 0, sizeof (si));
si.cb = sizeof (si);
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdInput = stdin_handle;
si.hStdOutput = stdout_handle;
si.hStdError = stderr_handle;
fprintf (stderr, "About to invoke child.\n");
fflush (stderr);
/* Start the child. */
CreateProcess ("child.exe",
"child.exe",
NULL,
NULL,
/*bInheritHandles=*/TRUE,
dwCreationFlags,
/*lpEnvironment=*/NULL,
/*lpCurrentDirectory=*/NULL,
&si,
&pi);
WaitForSingleObject (pi.hProcess, INFINITE);
CloseHandle (pi.hProcess);
CloseHandle (pi.hThread);
fprintf (stderr, "Child done.\n");
fflush (stderr);
}