I wanted to execute some cmd command and then save their result for further work. I used system() for my purposes but it doesn't work silently. When system() executes my commands, a console window will appear. I didn't want console windows to appear to the user. How can I overwrite the following function with qprocess to execute my cmd command silently?
bool MainWindow::CreateTemporaryFiles()
{
QString command_description = "wmic logicaldisk get description > C:\\Users\\Test\\AppData\\Local\\Temp\\description.log";
system(command_description.toStdString().c_str());
QString command_device_name = "wmic logicaldisk get DeviceID > C:\\Users\\Test\\AppData\\Local\\Temp\\names.log";
system(command_device_name.toStdString().c_str());
QString command_free_space = "wmic logicaldisk get FreeSpace > C:\\Users\\Test\\AppData\\Local\\Temp\\space.log";
system(command_free_space.toStdString().c_str());
QString command_size = "wmic logicaldisk get size > C:\\Users\\Test\\AppData\\Local\\Temp\\size.log";
system(command_size.toStdString().c_str());
QString command_file_system = "wmic logicaldisk get FileSystem > C:\\Users\\Test\\AppData\\Local\\Temp\\file.log";
system(command_file_system.toStdString().c_str());
QString command_sysname = "wmic logicaldisk get SystemName > C:\\Users\\Test\\AppData\\Local\\Temp\\sysname.log";
system(command_sysname.toStdString().c_str());
return true;
}
You can use popen to run a command and get the output.
You can find an example here .
QProcess
allows you to do that also. QProcess docs explain how to do it. Several examples here .
(Edited.)
If you need direct Windows, you have a complete example in the docs .
This was shown as example.
#include <stdio.h>
int main(void)
{
FILE *pipein_fp, *pipeout_fp;
char readbuf[80];
/* Create one way pipe line with call to popen() */
if (( pipein_fp = popen("ls", "r")) == NULL)
{
perror("popen");
exit(1);
}
/* Create one way pipe line with call to popen() */
if (( pipeout_fp = popen("sort", "w")) == NULL)
{
perror("popen");
exit(1);
}
/* Processing loop */
while(fgets(readbuf, 80, pipein_fp))
fputs(readbuf, pipeout_fp);
/* Close the pipes */
pclose(pipein_fp);
pclose(pipeout_fp);
return(0);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.