| 416 | } |
| 417 | |
| 418 | DWORD spawn(const SpawnParameters& sp, HANDLE& processHandle) |
| 419 | { |
| 420 | BOOL inheritHandles = FALSE; |
| 421 | |
| 422 | STARTUPINFO si = {}; |
| 423 | si.cb = sizeof(si); |
| 424 | |
| 425 | // inherit handles if we plan to use stdout or stderr reroute |
| 426 | if (sp.stdOut != INVALID_HANDLE_VALUE) { |
| 427 | si.hStdOutput = sp.stdOut; |
| 428 | inheritHandles = TRUE; |
| 429 | si.dwFlags |= STARTF_USESTDHANDLES; |
| 430 | } |
| 431 | |
| 432 | if (sp.stdErr != INVALID_HANDLE_VALUE) { |
| 433 | si.hStdError = sp.stdErr; |
| 434 | inheritHandles = TRUE; |
| 435 | si.dwFlags |= STARTF_USESTDHANDLES; |
| 436 | } |
| 437 | |
| 438 | const auto bin = QDir::toNativeSeparators(sp.binary.absoluteFilePath()); |
| 439 | const auto cwd = QDir::toNativeSeparators(sp.currentDirectory.absolutePath()); |
| 440 | |
| 441 | QString commandLine = "\"" + bin + "\""; |
| 442 | if (!sp.arguments.isEmpty()) { |
| 443 | commandLine += " " + sp.arguments; |
| 444 | } |
| 445 | |
| 446 | const QString moPath = QCoreApplication::applicationDirPath(); |
| 447 | const auto oldPath = env::appendToPath(QDir::toNativeSeparators(moPath)); |
| 448 | |
| 449 | PROCESS_INFORMATION pi = {}; |
| 450 | BOOL success = FALSE; |
| 451 | |
| 452 | logSpawning(sp, commandLine); |
| 453 | |
| 454 | const auto wcommandLine = commandLine.toStdWString(); |
| 455 | const auto wcwd = cwd.toStdWString(); |
| 456 | |
| 457 | const DWORD flags = CREATE_BREAKAWAY_FROM_JOB; |
| 458 | |
| 459 | if (sp.hooked) { |
| 460 | success = ::usvfsCreateProcessHooked( |
| 461 | nullptr, const_cast<wchar_t*>(wcommandLine.c_str()), nullptr, nullptr, |
| 462 | inheritHandles, flags, nullptr, wcwd.c_str(), &si, &pi); |
| 463 | } else { |
| 464 | success = ::CreateProcess(nullptr, const_cast<wchar_t*>(wcommandLine.c_str()), |
| 465 | nullptr, nullptr, inheritHandles, flags, nullptr, |
| 466 | wcwd.c_str(), &si, &pi); |
| 467 | } |
| 468 | |
| 469 | const auto e = GetLastError(); |
| 470 | env::setPath(oldPath); |
| 471 | |
| 472 | if (!success) { |
| 473 | return e; |
| 474 | } |
| 475 |
no test coverage detected