StrokeSeg 1.0.0
Loading...
Searching...
No Matches
executors.cpp
1#include "executors.h"
2
3bool execWithConsole(const std::wstring& childExePath, const std::wstring& selfDirPath, int consoleStatus)
4{
5 bool bRes = true;
6
7 STARTUPINFOW si;
8 PROCESS_INFORMATION pi;
9 ZeroMemory(&si, sizeof(si));
10 si.cb = sizeof(si);
11 ZeroMemory(&pi, sizeof(pi));
12
13 // Get the standard input, output, and error handles of the parent process
14 HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
15 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
16 HANDLE hStdErr = GetStdHandle(STD_ERROR_HANDLE);
17
18 // Inherit the standard handles so that the child process can use them
19 SetHandleInformation(hStdIn, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT);
20 SetHandleInformation(hStdOut, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT);
21 SetHandleInformation(hStdErr, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT);
22
23 // Specify to STARTUPINFO to use these handles
24 si.dwFlags |= STARTF_USESTDHANDLES;
25 si.hStdInput = hStdIn;
26 si.hStdOutput = hStdOut;
27 si.hStdError = hStdErr;
28
29 // Command line to execute the child process. CreateProcessW requires a writable wide string (wchar_t*).
30 std::wstring commandLine = childExePath;
31
32 // Launch the child process using CreateProcessW for wide characters
33 std::wcout << L"Application starting.\n\rPlease wait...\n\r" << std::endl;
34 if (!CreateProcessW(
35 nullptr, // No module name (use command line)
36 &commandLine[0], // Command line
37 nullptr, // Process handle not inheritable
38 nullptr, // Thread handle not inheritable
39 TRUE, // Set handle inheritance to TRUE
40 0, // No creation flags (important: NOT CREATE_NEW_CONSOLE)
41 nullptr, // Use parent's environment block
42 &selfDirPath[0], // Use parent's starting directory
43 &si, // Pointer to STARTUPINFO structure
44 &pi // Pointer to PROCESS_INFORMATION structure
45 ))
46 {
47 std::wcerr << L"CreateProcessW failed (" << GetLastError() << L")." << std::endl;
48 bRes = false;
49 }
50
51 // Wait for the child process to finish
52 WaitForSingleObject(pi.hProcess, INFINITE);
53
54 // Close process and thread handles
55 CloseHandle(pi.hProcess);
56 CloseHandle(pi.hThread);
57
58 if (consoleStatus == 0) // If consoleStatus is 0, we assume a new console was created
59 {
60 std::wcout << L"Child process finished.\n\rPress any key to exit console..." << std::endl;
61 std::cin.ignore();
62 }
63 else if (consoleStatus == 1) // If consoleStatus is 1, we assume we are inheriting the console from the parent
64 {
65 std::wcout << L"Finish\r\n" << std::endl;
66 }
67 FreeConsole();
68
69 return bRes;
70}
71
72bool execWitoutConsole(const std::wstring& childExePath, const std::wstring& selfDirPath)
73{
74 bool bRes = true;
75
76 STARTUPINFO si;
77 PROCESS_INFORMATION pi;
78 ZeroMemory(&si, sizeof(si));
79 si.cb = sizeof(si);
80 ZeroMemory(&pi, sizeof(pi));
81
82 // Explicitly set dwFlags and wShowWindow for GUI applications
83 si.dwFlags = STARTF_USESHOWWINDOW;
84 si.wShowWindow = SW_HIDE; // This will prevent the console window from appearing
85
86 // Command line to execute the child process. CreateProcessW requires a writable wide string (wchar_t*).
87 std::wstring commandLine = childExePath;
88
89 // Launch the child process (python.exe) using CreateProcessW for wide characters
90 if (!CreateProcessW(
91 NULL, // No module name (use command line)
92 &commandLine[0], // Command line arguments (includes python.exe path)
93 NULL, // Process handle not inheritable
94 NULL, // Thread handle not inheritable
95 FALSE, // Set handle inheritance to FALSE
96 CREATE_NO_WINDOW, // IMPORTANT: This flag prevents a new console window
97 NULL, // Use parent's environment block
98 &selfDirPath[0], // Use current executable's directory as starting directory
99 &si, // Pointer to STARTUPINFO structure
100 &pi // Pointer to PROCESS_INFORMATION structure
101 ))
102 {
103 MessageBoxW(NULL, (L"Failed to launch Python script. Error code: " + std::to_wstring(GetLastError())).c_str(), L"Launcher Error", MB_OK | MB_ICONERROR);
104 bRes = false;
105 }
106
107 return bRes;
108}
Header file for the executors module. This file contains declarations for functions related to launch...