| 411 | |
| 412 | |
| 413 | DWORD StartProcessInBackground(LPCWSTR exePath, LPCWSTR commandLine) { |
| 414 | STARTUPINFO si = { 0 }; |
| 415 | si.cb = sizeof(STARTUPINFO); |
| 416 | si.dwFlags = STARTF_USESHOWWINDOW; |
| 417 | si.wShowWindow = SW_HIDE; // Start the process in the background |
| 418 | |
| 419 | PROCESS_INFORMATION pi = { 0 }; |
| 420 | |
| 421 | // Combine exePath and commandLine into a single buffer |
| 422 | std::wstring fullCommand = std::wstring(exePath) + L" " + std::wstring(commandLine); |
| 423 | std::vector<wchar_t> commandBuffer(fullCommand.begin(), fullCommand.end()); |
| 424 | commandBuffer.push_back(0); // Null-terminate the buffer |
| 425 | |
| 426 | // Create the process |
| 427 | if (CreateProcess( |
| 428 | nullptr, // Application name (null to use command line) |
| 429 | commandBuffer.data(), // Command line |
| 430 | nullptr, // Process security attributes |
| 431 | nullptr, // Thread security attributes |
| 432 | FALSE, // Inherit handles |
| 433 | CREATE_NO_WINDOW, // Creation flags |
| 434 | nullptr, // Use parent's environment block |
| 435 | nullptr, // Use parent's current directory |
| 436 | &si, // Pointer to STARTUPINFO |
| 437 | &pi // Pointer to PROCESS_INFORMATION |
| 438 | )) { |
| 439 | DWORD pid = pi.dwProcessId; // Retrieve the process ID |
| 440 | |
| 441 | // Close handles to avoid resource leaks |
| 442 | CloseHandle(pi.hProcess); |
| 443 | CloseHandle(pi.hThread); |
| 444 | |
| 445 | return pid; |
| 446 | } |
| 447 | else { |
| 448 | // Print error and return 0 if process creation failed |
| 449 | std::wcerr << L"Failed to start process. Error: " << GetLastError() << std::endl; |
| 450 | //LOG_W(LOG_ERROR, L"Failed to start process. Error: %d", GetLastError()); |
| 451 | return 0; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | |
| 456 | wchar_t* char2wcharAlloc(const char* charStr) { |