how to save executed output cmd line in file in c++ - c++

I have this code for execute CMD line in c++
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
void _tmain( int argc, TCHAR *argv[] )
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
if( argc != 2 )
{
printf("Usage: %s [cmdline]\n", argv[0]);
return;
}
// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line)
argv[1], // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
)
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
return;
}
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}
I want save executed output in file.but how?

In CreateProcess you pass a STARTUPINFO-structure. You can set the STARTF_USESTDHANDLES in si.dwFlags and then fill out the hStdInput, hStdOutput, and hStdError-fields with valid file-descriptors, especially hStdOutput should be a handle to a previously opened file (returned by successful CreateFile) which will then receive the std-output of the started process.
Edit:
This was kind of mean answer, because it needs more work to make that thing work: You need to create that file with the right SECURITY_ATTRIBUTES and have to set the Set handle inheritance to TRUE in CreateProcess. So it's also kind of purists nightmare to do it that way.
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
void _tmain( int argc, TCHAR *argv[] )
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
si.dwFlags |=STARTF_USESTDHANDLES ;
si.hStdInput=GetStdHandle(STD_INPUT_HANDLE);
si.hStdError=GetStdHandle(STD_ERROR_HANDLE);
SECURITY_ATTRIBUTES sa;
ZeroMemory( &sa, sizeof(sa) );
sa.nLength=sizeof(sa);
sa.bInheritHandle=TRUE;
si.hStdOutput=CreateFile ("log.txt", GENERIC_READ|GENERIC_WRITE, 0, &sa, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
ZeroMemory( &pi, sizeof(pi) );
if( argc != 2 )
{
printf("Usage: %s [cmdline]\n", argv[0]);
return;
}
// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line)
argv[1], // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
TRUE, // Set handle inheritance to TRUE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
)
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
return;
}
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
CloseHandle (si.hStdOutput);
}

Either redirect stdout to a file with
freopen("file.txt", "w", stdout);
Or, pipe the output to a file with windows
cmd> prg.exe > file.txt

Related

How to execute CMD command with CreateProcess

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.
I see tag Qt and parameter's type QString in your question. So, you can use QProcess and QFile, here is example:
#include <QProcess>
#include <QDebug>
#include <QFile>
int main(int /*argc*/, char* /*argv*/[])
{
QProcess process;
process.start("wmic logicaldisk get name");
process.waitForFinished();
auto output = process.readAll();
QFile outputFile("file.log");
outputFile.open(QIODevice::WriteOnly);
outputFile.write(output);
outputFile.close();
return 0;
}
If you want to use CreateProcess, you can do it like this (based on your source):
#include <QString>
#include <Windows.h>
bool ExecuteConsoleCommand(QString arg_console_command)
{
arg_console_command.prepend("/C ");
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
// Start the child process.
if(!CreateProcessW(L"C:\\Windows\\System32\\cmd.exe",
(WCHAR*)arg_console_command.toStdWString().c_str(),
NULL, NULL, FALSE,
NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW,
NULL, NULL, &si, &pi))
{
return false;
}
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
return true;
}
int main(int /*argc*/, char* /*argv*/[])
{
ExecuteConsoleCommand("wmic logicaldisk get name > file.log");
return 0;
}

Process of c++ executable doesn't close correctly

in my program I start other c++ application in the same window closing the previous. Sometimes the process that I see in the Task Manager doesn't close. So I'll have many process whit the same name. How can I avoid this?
startup("../folder/c++_executable.exe");
exit(0);
void startup(LPCTSTR lpApplicationName)
{
// additional information
STARTUPINFO si;
PROCESS_INFORMATION pi;
// set the size of the structures
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
// start the program up
CreateProcess( lpApplicationName, // the path
"", // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
;
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}
I think you will need to check why it's not closing - get VisualStudio/WinDBG and attach to process that is not closing and check where it hang.

How to execute external programm in the parent C++ programm in Windows?

int system(const char *str);
declared in stdlib.h
As I know, this code will spawn a new process not within parent process and in Task Manager will be two processes - parent and child.
How to spawn child process in the parent process? ie in Task Manager must be only one - parent - process.
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
void _tmain( int argc, TCHAR *argv[] )
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
if( argc != 2 )
{
printf("Usage: %s [cmdline]\n", argv[0]);
return;
}
// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line)
argv[1], // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
)
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
return;
}
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}
Source: MSDN

CreateProcess not in primary thread

I use CreateProcess() to start a other programm from my C++ code.
The helppage says that it
Creates a new process and its primary thread. The new process runs in the security context of the calling process.
I would like to have it in his own thread, so the application would keep on going. Is there any simple way to do so? I am not familar with boost and Co.
My code:
bool startup(LPWSTR lpApplicationName)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line)
lpApplicationName, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
)
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
return false;
}
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
return true;
}
and how to call the function:
int _tmain(int argc, _TCHAR* argv[])
{
wchar_t path[] = L"C:\\path\\to\\paridise.exe";
startup(path);
}
Transforming the comment by ElderBug in a reply
The problem in your code is the presence of an infinite wait after the CreateProcess call:
WaitForSingleObject( pi.hProcess, INFINITE );
Just comment it and the program execution will not stop.
I also did this same error and I stumbled in this SO question; the fact is that many of us get the code from the MSDN example here (without much attention), which has in fact this infinite wait.

CreateProcess() compiling error?

i'm trying to figure out how to use the CreateProcess() function, and i'm not too proficient in C++. i've tried a few things to try making the error go away, but then it appears that the application doesn't do what i expect it to do thereafter.
what I want to do is pass "cmd.exe /c ipconfig > C:\test.txt" to it and have it execute that as expected.
the errors that i'm getting (in dev C++) are:
6 C:\Dev-Cpp\project\test\Untitled1.cpp main' must returnint'
C:\Dev-Cpp\project\test\Untitled1.cpp In function `int main(...)':
28 C:\Dev-Cpp\project\test\Untitled1.cpp return-statement with no value, in function returning 'int'
any help would be greatly appreciated.
here's the code i'm using (taken from microsoft's example):
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
void _tmain( int argc, TCHAR *argv[] )
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line)
argv[1], // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
)
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
return;
}
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}
Change
void _tmain( int argc, TCHAR *argv[] )
to
int _tmain( int argc, TCHAR *argv[] )
and return an exit code in your program, that is, change
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
return;
}
to
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
return 1;
}