+
+ private fun basic_exec_execute(prog, args: CString, argc: Int, pipeflag: Int): NativeProcess `{
+#ifdef _WIN32
+ SECURITY_ATTRIBUTES sec_attr;
+ sec_attr.nLength = sizeof(SECURITY_ATTRIBUTES);
+ sec_attr.bInheritHandle = TRUE;
+ sec_attr.lpSecurityDescriptor = NULL;
+
+ STARTUPINFO start_info;
+ ZeroMemory(&start_info, sizeof(STARTUPINFO));
+ start_info.cb = sizeof(STARTUPINFO);
+ start_info.dwFlags = STARTF_USESTDHANDLES;
+
+ HANDLE in_fd[2];
+ HANDLE out_fd[2];
+ HANDLE err_fd[2];
+
+ se_exec_data_t *result = (se_exec_data_t*)malloc(sizeof(se_exec_data_t));
+
+ // Redirect stdin?
+ if (pipeflag & 1) {
+ if (!CreatePipe(&in_fd[0], &in_fd[1], &sec_attr, 0)) {
+ return NULL;
+ }
+ start_info.hStdInput = in_fd[0];
+ result->in_fd = _open_osfhandle((intptr_t)in_fd[1], _O_WRONLY);
+ if ( !SetHandleInformation(in_fd[1], HANDLE_FLAG_INHERIT, 0) )
+ return NULL;
+ } else {
+ start_info.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
+ result->in_fd = -1;
+ }
+
+ // Redirect stdout?
+ if (pipeflag & 2) {
+ if (!CreatePipe(&out_fd[0], &out_fd[1], &sec_attr, 0)) {
+ return NULL;
+ }
+ start_info.hStdOutput = out_fd[1];
+ result->out_fd = _open_osfhandle((intptr_t)out_fd[0], _O_RDONLY);
+ if ( !SetHandleInformation(out_fd[0], HANDLE_FLAG_INHERIT, 0) )
+ return NULL;
+ } else {
+ start_info.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
+ result->out_fd = -1;
+ }
+
+ // Redirect stderr?
+ if (pipeflag & 4) {
+ if (!CreatePipe(&err_fd[0], &err_fd[1], &sec_attr, 0)) {
+ return NULL;
+ }
+ start_info.hStdError = err_fd[1];
+ result->err_fd = _open_osfhandle((intptr_t)err_fd[0], _O_RDONLY);
+ if ( !SetHandleInformation(err_fd[0], HANDLE_FLAG_INHERIT, 0) )
+ return NULL;
+ } else {
+ start_info.hStdError = GetStdHandle(STD_ERROR_HANDLE);
+ result->err_fd = -1;
+ }
+
+ PROCESS_INFORMATION proc_info;
+ ZeroMemory(&proc_info, sizeof(PROCESS_INFORMATION));
+
+ BOOL created = CreateProcess(NULL,
+ args, // command line
+ NULL, // process security attributes
+ NULL, // primary thread security attributes
+ TRUE, // inherit handles
+ 0, // creation flags
+ NULL, // use parent's environment
+ NULL, // use parent's current directory
+ &start_info,
+ &proc_info);
+
+ if (pipeflag & 1) CloseHandle(in_fd[0]);
+ if (pipeflag & 2) CloseHandle(out_fd[1]);
+ if (pipeflag & 3) CloseHandle(err_fd[1]);
+
+ // Error?
+ if (!created) {
+ result->running = 0;
+ result->status = 127;
+
+ // Close subprocess pipes
+ if (pipeflag & 1) CloseHandle(in_fd[1]);
+ if (pipeflag & 2) CloseHandle(out_fd[0]);
+ if (pipeflag & 3) CloseHandle(err_fd[0]);
+ } else {
+ result->h_process = proc_info.hProcess;
+ result->h_thread = proc_info.hThread;
+ result->id = GetProcessId(proc_info.hProcess);
+ result->running = 1;
+ }
+
+ return result;
+#else