MCPcopy Create free account
hub / github.com/Illation/ETEngine / execConsoleCommand

Function execConsoleCommand

Engine/source/EtCore/Util/Command.cpp:9–102  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7
8
9std::string execConsoleCommand(std::string const& cmd)
10{
11 //modified version of https://stackoverflow.com/a/35935443/4414168
12#ifdef PLATFORM_Win
13
14 // Allocate 1Mo to store the output (final buffer will be sized to actual output)
15 // If output exceeds that size, it will be truncated
16 const SIZE_T RESULT_SIZE = sizeof(char) * 1024 * 1024;
17 //char* output = (char*)LocalAlloc(LPTR, RESULT_SIZE);
18 std::string output;
19
20 HANDLE readPipe, writePipe;
21 SECURITY_ATTRIBUTES security;
22 STARTUPINFOA start;
23 PROCESS_INFORMATION processInfo;
24
25 security.nLength = sizeof(SECURITY_ATTRIBUTES);
26 security.bInheritHandle = true;
27 security.lpSecurityDescriptor = NULL;
28
29 if (CreatePipe( &readPipe, &writePipe, &security, 0))
30 {
31 GetStartupInfoA(&start);
32 start.hStdOutput = writePipe;
33 start.hStdError = writePipe;
34 start.hStdInput = readPipe;
35 start.dwFlags = STARTF_USESTDHANDLES + STARTF_USESHOWWINDOW;
36 start.wShowWindow = SW_HIDE;
37
38 std::string fullCommand = "cmd.exe /C " + cmd;
39 LPSTR command = const_cast<char *>(fullCommand.c_str());
40
41 // We have to start the DOS app the same way cmd.exe does (using the current Win32 ANSI code-page).
42 // So, we use the "ANSI" version of createProcess, to be able to pass a LPSTR (single/multi-byte character string)
43 // instead of a LPWSTR (wide-character string) and we use the UNICODEtoANSI function to convert the given command
44 if (CreateProcessA(NULL, // pointer to name of executable module
45 command, // pointer to command line string
46 &security, // pointer to process security attributes
47 &security, // pointer to thread security attributes
48 TRUE, // handle inheritance flag
49 NORMAL_PRIORITY_CLASS, // creation flags
50 NULL, // pointer to new environment block
51 NULL, // pointer to current directory name
52 &start, // pointer to STARTUPINFO
53 &processInfo // pointer to PROCESS_INFORMATION
54 ))
55 {
56 //// wait for the child process to start
57 //for (UINT state = WAIT_TIMEOUT; state == WAIT_TIMEOUT; state = WaitForSingleObject(processInfo.hProcess, 100));
58
59 auto WaitResult = WaitForSingleObject(processInfo.hProcess, INFINITE);
60 if (WAIT_FAILED == WaitResult)
61 throw std::runtime_error(("Waiting for batch process failed. Error code: " + std::to_string(GetLastError())).c_str());
62 //else if (WAIT_TIMEOUT == WaitResult)
63
64 DWORD bytesRead = 0, count = 0;
65 const int BUFF_SIZE = 1024;
66 char* buffer = (char*)malloc(sizeof(char)*BUFF_SIZE + 1);

Callers 1

DebugCopyResourceFilesFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected