| 66 | } |
| 67 | |
| 68 | s32 Process::spawn(const char * const *argv, u32 flags) |
| 69 | { |
| 70 | CE_ENSURE(process_internal::is_open(_priv) == false); |
| 71 | |
| 72 | #if CROWN_PLATFORM_WINDOWS |
| 73 | TempAllocator512 ta; |
| 74 | StringStream path(ta); |
| 75 | |
| 76 | for (s32 i = 0; argv[i] != NULL; ++i) { |
| 77 | const char *arg = argv[i]; |
| 78 | for (; *arg; ++arg) { |
| 79 | if (*arg == ' ') |
| 80 | break; |
| 81 | } |
| 82 | |
| 83 | if (*arg) |
| 84 | path << '"'; |
| 85 | |
| 86 | path << argv[i]; |
| 87 | |
| 88 | if (*arg) |
| 89 | path << '"'; |
| 90 | |
| 91 | path << ' '; |
| 92 | } |
| 93 | |
| 94 | // https://docs.microsoft.com/it-it/windows/win32/procthread/creating-a-child-process-with-redirected-input-and-output |
| 95 | SECURITY_ATTRIBUTES sattr; |
| 96 | sattr.nLength = sizeof(sattr); |
| 97 | sattr.bInheritHandle = TRUE; |
| 98 | sattr.lpSecurityDescriptor = NULL; |
| 99 | |
| 100 | if (flags & CROWN_PROCESS_STDOUT_PIPE) { |
| 101 | // Pipe for STDOUT of child process |
| 102 | BOOL ret; |
| 103 | ret = CreatePipe(&_priv->stdout_rd, &_priv->stdout_wr, &sattr, 0); |
| 104 | if (ret == 0) |
| 105 | return -1; |
| 106 | |
| 107 | // Do not inherit read handle of STDOUT pipe |
| 108 | ret = SetHandleInformation(_priv->stdout_rd, HANDLE_FLAG_INHERIT, 0); |
| 109 | if (ret == 0) |
| 110 | return -1; |
| 111 | } |
| 112 | |
| 113 | STARTUPINFO info; |
| 114 | memset(&info, 0, sizeof(info)); |
| 115 | info.cb = sizeof(info); |
| 116 | info.hStdOutput = (flags & CROWN_PROCESS_STDOUT_PIPE) ? _priv->stdout_wr : 0; |
| 117 | info.hStdError = (info.hStdOutput != 0) && (flags & CROWN_PROCESS_STDERR_MERGE) ? _priv->stdout_wr : 0; |
| 118 | info.dwFlags |= (info.hStdOutput != 0 || info.hStdError != 0) ? STARTF_USESTDHANDLES : 0; |
| 119 | |
| 120 | BOOL err = CreateProcess(argv[0] |
| 121 | , (LPSTR)string_stream::c_str(path) |
| 122 | , NULL |
| 123 | , NULL |
| 124 | , TRUE // Handles are inherited |
| 125 | , CREATE_NO_WINDOW |