1996-09-04 07:17:43 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
|
|
|
/* Objective Caml */
|
|
|
|
/* */
|
|
|
|
/* Xavier Leroy and Pascal Cuoq, projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* */
|
|
|
|
/* Copyright 1996 Institut National de Recherche en Informatique et */
|
1999-11-17 10:59:06 -08:00
|
|
|
/* en Automatique. All rights reserved. This file is distributed */
|
2001-12-07 05:41:02 -08:00
|
|
|
/* under the terms of the GNU Library General Public License, with */
|
|
|
|
/* the special exception on linking described in file ../../LICENSE. */
|
1996-09-04 07:17:43 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <mlvalues.h>
|
2001-08-28 07:47:48 -07:00
|
|
|
#include <osdeps.h>
|
1996-09-04 07:17:43 -07:00
|
|
|
#include "unixsupport.h"
|
|
|
|
|
2000-02-13 09:27:46 -08:00
|
|
|
static int win_has_console(void);
|
|
|
|
|
1997-09-03 07:38:02 -07:00
|
|
|
value win_create_process_native(value cmd, value cmdline, value env,
|
|
|
|
value fd1, value fd2, value fd3)
|
1996-09-04 07:17:43 -07:00
|
|
|
{
|
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
STARTUPINFO si;
|
1996-09-17 07:43:05 -07:00
|
|
|
char * exefile, * envp;
|
2000-02-13 09:27:46 -08:00
|
|
|
int flags;
|
1996-09-04 07:17:43 -07:00
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
exefile = search_exe_in_path(String_val(cmd));
|
1996-09-04 07:17:43 -07:00
|
|
|
if (env != Val_int(0)) {
|
|
|
|
envp = String_val(Field(env, 0));
|
|
|
|
} else {
|
|
|
|
envp = NULL;
|
|
|
|
}
|
2000-02-13 09:27:46 -08:00
|
|
|
/* Prepare stdin/stdout/stderr redirection */
|
1996-09-04 07:17:43 -07:00
|
|
|
GetStartupInfo(&si);
|
|
|
|
si.dwFlags |= STARTF_USESTDHANDLES;
|
1997-09-03 07:38:02 -07:00
|
|
|
si.hStdInput = Handle_val(fd1);
|
|
|
|
si.hStdOutput = Handle_val(fd2);
|
|
|
|
si.hStdError = Handle_val(fd3);
|
2001-10-01 20:09:38 -07:00
|
|
|
/* If we do not have a console window, then we must create one
|
|
|
|
before running the process (keep it hidden for apparence).
|
2001-10-02 02:26:59 -07:00
|
|
|
Also one must suppress spurious flags in si.dwFlags.
|
|
|
|
Otherwise the redirections are ignored.
|
|
|
|
If we are starting a GUI application, the newly created
|
|
|
|
console should not matter. */
|
2000-02-13 09:27:46 -08:00
|
|
|
if (win_has_console())
|
|
|
|
flags = 0;
|
2001-10-01 20:09:38 -07:00
|
|
|
else {
|
|
|
|
flags = CREATE_NEW_CONSOLE;
|
2001-10-02 02:26:59 -07:00
|
|
|
si.dwFlags = (STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES);
|
2001-10-01 20:09:38 -07:00
|
|
|
si.wShowWindow = SW_HIDE;
|
|
|
|
}
|
2000-02-13 09:27:46 -08:00
|
|
|
/* Create the process */
|
1996-09-17 07:43:05 -07:00
|
|
|
if (! CreateProcess(exefile, String_val(cmdline), NULL, NULL,
|
2000-02-13 09:27:46 -08:00
|
|
|
TRUE, flags, envp, NULL, &si, &pi)) {
|
2001-08-28 07:47:48 -07:00
|
|
|
win32_maperr(GetLastError());
|
1997-09-02 09:01:39 -07:00
|
|
|
uerror("create_process", cmd);
|
1996-09-04 07:17:43 -07:00
|
|
|
}
|
2000-02-13 09:27:46 -08:00
|
|
|
CloseHandle(pi.hThread);
|
|
|
|
/* Return the process handle as pseudo-PID
|
|
|
|
(this is consistent with the wait() emulation in the MSVC C library */
|
1996-09-04 07:17:43 -07:00
|
|
|
return Val_int(pi.hProcess);
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value win_create_process(value * argv, int argn)
|
1996-09-04 07:17:43 -07:00
|
|
|
{
|
|
|
|
return win_create_process_native(argv[0], argv[1], argv[2],
|
|
|
|
argv[3], argv[4], argv[5]);
|
|
|
|
}
|
2000-02-13 09:27:46 -08:00
|
|
|
|
|
|
|
static int win_has_console(void)
|
|
|
|
{
|
2001-10-02 02:26:59 -07:00
|
|
|
HANDLE h, log;
|
|
|
|
int i;
|
2000-02-13 09:27:46 -08:00
|
|
|
|
|
|
|
h = CreateFile("CONOUT$", GENERIC_WRITE, FILE_SHARE_WRITE, NULL,
|
|
|
|
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
if (h == INVALID_HANDLE_VALUE) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
CloseHandle(h);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|