mirror of
https://github.com/loot/libloot.git
synced 2026-07-27 14:16:01 -07:00
Update helpers.cpp
Code cleanup. Removed unused RunCommand function.
This commit is contained in:
@@ -274,90 +274,6 @@ namespace boss {
|
||||
return g_lang_any;
|
||||
}
|
||||
|
||||
//Runs a command using the Win32 API.
|
||||
bool RunCommand(const std::string& command, std::string& output) {
|
||||
HANDLE consoleWrite = NULL;
|
||||
HANDLE consoleRead = NULL;
|
||||
|
||||
SECURITY_ATTRIBUTES saAttr;
|
||||
|
||||
PROCESS_INFORMATION piProcInfo;
|
||||
STARTUPINFO siStartInfo;
|
||||
|
||||
CHAR chBuf[BUFSIZE];
|
||||
DWORD dwRead;
|
||||
|
||||
DWORD exitCode;
|
||||
|
||||
//Init attributes.
|
||||
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
|
||||
saAttr.bInheritHandle = TRUE;
|
||||
saAttr.lpSecurityDescriptor = NULL;
|
||||
|
||||
BOOST_LOG_TRIVIAL(trace) << "Creating a pipe for the process.";
|
||||
|
||||
//Create I/O pipes.
|
||||
if (!CreatePipe(&consoleRead, &consoleWrite, &saAttr, 0)) {
|
||||
BOOST_LOG_TRIVIAL(error) << "Could not create pipe for process.";
|
||||
throw error(error::windows_error, lc::translate("Could not create pipe for process."));
|
||||
}
|
||||
|
||||
//Create a child process.
|
||||
BOOST_LOG_TRIVIAL(trace) << "Creating a child process.";
|
||||
ZeroMemory(&piProcInfo, sizeof(PROCESS_INFORMATION));
|
||||
|
||||
ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
|
||||
siStartInfo.cb = sizeof(STARTUPINFO);
|
||||
siStartInfo.hStdError = consoleWrite;
|
||||
siStartInfo.hStdOutput = consoleWrite;
|
||||
siStartInfo.dwFlags |= STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
|
||||
siStartInfo.wShowWindow = SW_HIDE;
|
||||
|
||||
const int utf16Len = MultiByteToWideChar(CP_UTF8, 0, command.c_str(), -1, NULL, 0);
|
||||
wchar_t * cmdLine = new wchar_t[utf16Len];
|
||||
MultiByteToWideChar(CP_UTF8, 0, command.c_str(), -1, cmdLine, utf16Len);
|
||||
|
||||
bool result = CreateProcess(NULL,
|
||||
cmdLine, // command line
|
||||
NULL, // process security attributes
|
||||
NULL, // primary thread security attributes
|
||||
TRUE, // handles are inherited
|
||||
CREATE_NO_WINDOW, // creation flags
|
||||
NULL, // use parent's environment
|
||||
NULL, // use parent's current directory
|
||||
&siStartInfo, // STARTUPINFO pointer
|
||||
&piProcInfo); // receives PROCESS_INFORMATION
|
||||
|
||||
delete [] cmdLine;
|
||||
|
||||
if (!result) {
|
||||
BOOST_LOG_TRIVIAL(error) << "Could not create process.";
|
||||
throw error(error::windows_error, lc::translate("Could not create process."));
|
||||
}
|
||||
|
||||
BOOST_LOG_TRIVIAL(trace) << "Waiting for process to complete.";
|
||||
|
||||
WaitForSingleObject(piProcInfo.hProcess, INFINITE);
|
||||
|
||||
BOOST_LOG_TRIVIAL(trace) << "Getting the process exit code.";
|
||||
|
||||
if (!GetExitCodeProcess(piProcInfo.hProcess, &exitCode)) {
|
||||
BOOST_LOG_TRIVIAL(error) << "Could not get process exit code.";
|
||||
throw error(error::windows_error, lc::translate("Could not get process exit code."));
|
||||
}
|
||||
|
||||
BOOST_LOG_TRIVIAL(trace) << "Getting the process output.";
|
||||
|
||||
if (!ReadFile(consoleRead, chBuf, BUFSIZE, &dwRead, NULL)) {
|
||||
BOOST_LOG_TRIVIAL(error) << "Could not read process output.";
|
||||
throw error(error::windows_error, lc::translate("Could not read process output."));
|
||||
}
|
||||
|
||||
output = string(chBuf, dwRead);
|
||||
|
||||
return exitCode == 0;
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////
|
||||
// Version Class Functions
|
||||
|
||||
Reference in New Issue
Block a user