I wanted to execute cmd command like "wmic logicaldisk get name > file.log". I have written the following function but it doesn't work and my program will crash.
bool Information::ExecuteConsoleCommand(QString arg_console_command)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
// Start the child process.
if(!CreateProcess(NULL,
(WCHAR*)arg_console_command.toStdWString().c_str(),
NULL, NULL, FALSE,
NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW,
NULL, NULL, &si, &pi))
{
return false;
}
else
{
return true;
}
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}
How should I fix this function in the order it can execute cmd command? Also, I didn't want to use system() to run my commands because it show console window.