Format String C++ in CreateProcess - c++

ello all I hope someone can help me with this issue I am using the following example from msdn for createprocess function.
#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 );
}
This is accessed via dos and works perfectly using this command in command prompt. When I type this
this.exe "my.exe test" > result.txt
The my.exe is another console application that takes the input test and the > result.txt is for my output log. I am trying to remove the need for the command line so I am trying to feed the path into the createprocess function call. This is where I get stuck here is what I am trying
if( !CreateProcess( NULL, // No module name (use command line)
"\"my.exe test \" > result.txt", // this.exe "my.exe test" > result.txt
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
still doesnt work I thought \" would give me the result I needed but it doesnt seem to work, will parse the my.exe and test part but not the > result.txt output. However it works from the command prompt fine if i pass it to argv[1].
Any help much appreciated.
So in summary
In console I can parse
this.exe "my.exe test" > result.txt // Works fine via cmd.exe
To app I tried
my.exe test > result.txt // Not work but missing ""
and
\"my.exe test \" > result.txt // work for first section

CreateProcess only does basic command line parsing by breaking up the words into individual arguments -- it doesn't understand file redirection or anything. If you pass it "> result.txt", it tries to parse that as two arguments named literally ">" and "result.txt".
If you want to redirect the output of the command, you have two options:
Do the redirection yourself. To do that, you'd first open the file with CreateFile (passing in security attributes which make the handle inheritable), and then assign the resulting handle to the hStdOut member of the STARTUPINFO structure you pass in. Then, remember to close the file after CreateProcess returns, since otherwise you'd leak a handle.
Use another program to do the redirection. When you type in commands on the command line, it's cmd.exe which parses your command line and does the file redirection. So instead of creating a my.exe process, you could instead create a cmd.exe process with a command line something like this:
cmd.exe "my.exe test > result.txt"

CreateProcess only expects an executable name and some arguments. The redirection is actually not a program argument. That is interpreted by the shell (cmd.exe).
What is happening when you invoke your own program is as follows...
cmd> this.exe "my.exe test" > result.txt
argv[0] = "this.exe"
argv[1] = "my.exe test"
Output is sent to result.txt by the shell
Now, your one that doesn't work:
cmd> this.exe my.exe test > result.txt
argv[0] = "this.exe"
argv[1] = "my.exe"
argv[2] = "test"
Output is sent to result.txt by the shell
You'll see that because you only send argv[1] to CreateProcess, the behaviour is not as you expected.
Now, as I mentioned, CreateProcess does not actually redirect output. To do that, you should use the system call which invokes cmd.exe or whatever command interpreter the system uses:
system( "my.exe test > result.txt" );
See: http://msdn.microsoft.com/en-us/library/277bwbdz(v=vs.80).aspx

Related

Creating "dir" command using CreateProcess function failed with error code 2

I was just playing with Win32-API and wanted to create a process using CreateProcess function. I used the following code from MSDN website:
#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 );
}
But surprisingly I can't create a dir process using this piece of code. Error code indicated that 'The system cannot find the file specified.'
I'm using Visual studio 2015 and Windows 7 64Bit. But when I run the same executable in Windows 10, everything is OK.
dir is not an external command that can be run. It's a command internal to the Windows Command Prompt. You'll need to call your program as myprogram "cmd /c dir" to do that.
Of course, there are better ways to iterate a directory than calling an external program, but that's a separate question.
After hours of going through 1500 lines of C code, it finally dawned on me what my problem was and why it worked on one Windows 10 system of mine but not on another. The system that it worked on, I really did have a DIR.EXE. But is was not the COMSPEC DIR that was running. I had DIR.EXEs in Git and MinGW folders.
Read this for how to correctly use CREATEPROCESS.
https://learn.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-createprocessa

why argv in createProcess() is different from normal c++ program

this is my code, and i found the first output is "thisProgram.exe"
and the second output is "a".
why?
i read the doc in msdn, however i don't quite clear why the argv[0] can be "a", is there something different in windows when using createProcess. Could someone please tell me the difference from lpApplicationName and lpCommandline? thanks
int main( int argc, char *argv[] ) {
cout << argv[0] << endl;
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
// Start the child process.
if (!CreateProcess("thisProgram.exe", // No module name (use command line)
"a b c", // 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 1;
}
// Wait until child process exits.
WaitForSingleObject(pi.hProcess, INFINITE);
// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return 0;
}
CreateProcess passes the second argument (the command line) to the new process as its command line. CreateProcess will not prepend the module name. If you want the application name to appear as argv[0] you must repeat the application name in the command line argument.
The documentation says it like this:
If both lpApplicationName and lpCommandLine are non-NULL, the null-terminated string pointed to by lpApplicationName specifies the module to execute, and the null-terminated string pointed to by lpCommandLine specifies the command line. The new process can use GetCommandLine to retrieve the entire command line. Console processes written in C can use the argc and argv arguments to parse the command line. Because argv[0] is the module name, C programmers generally repeat the module name as the first token in the command line.
It's generally simplest to pass NULL for the application name, and for the command line pass the application name and the arguments concatenated together.

Beginner C++ CreateProcess () Error 2

I am trying to create a process that send command to cmd.exe and receive Error 2,
Why? It's posible? How?
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
String pathexe = "C:\Windows\system32\cmd.exe";
String command= "notepad.exe";
if(!CreateProcess(
pathexe.c_str(), // lpApplicationName
command.c_str(), // lpCommandLine
NULL, // lpProcessAttributes
NULL, // lpThreadAttributes
FALSE, // bInheritHandles
0, // dwCreationFlags
NULL, // lpEnvironment
NULL, // lpCurrentDirectory
&si, // lpStartupInfo
&pi // lpProcessInformation
))
{
AnsiString error = GetLastError();
ShowMessage("Error: " + error);
}
WaitForSingleObject( pi.hProcess, INFINITE );
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
PD: 1) It is assumed that you can use CreateProcess () for this purpose, I should not do it with ShellExecute () or system().
2) I have read about it in the forum and can not find a solution to this error, there are many answers to similar questions but do not address the error, other functions are proposed, or mix with the route command.
3) I do not think that issue permits because I built while the manifest.
4) I currently use C ++ Builder, in win7, 32bits but not important.
5) I guess the issue will be voted as negative and duplicate (as usual) but the proposed testing examples also receive errors.
Thanks to all
FIRST CONCLUSIONS:
Error 2: The system cannot find the file specified.
Link funtion: https://msdn.microsoft.com/es-es/library/windows/desktop/ms679360(v=vs.85).aspx
Link error: https://msdn.microsoft.com/es-es/library/windows/desktop/ms681382(v=vs.85).aspx
With error 2: check syntax, file path and existence.
works:
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
String command = "notepad.exe";
if(!CreateProcess(
NULL, // lpApplicationName
commmand.c_str(), // lpCommandLine
NULL, // lpProcessAttributes
NULL, // lpThreadAttributes
FALSE, // bInheritHandles
0, // dwCreationFlags
NULL, // lpEnvironment
NULL, // lpCurrentDirectory
&si, // lpStartupInfo
&pi // lpProcessInformation
))
{
AnsiString error = GetLastError();
ShowMessage("Error: " + error);
}
WaitForSingleObject( pi.hProcess, INFINITE );
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
This example works also for exe
String command = "cd C:\\sample\\calc.exe";
But no with cmd´s general commands, there must be a way to send commands to cmd as:
notepad.exe && cd C:\sample\ && sample1.txt
THANKS TO ALL
You're trying to run this command:
cmd notepad
(You aren't doing that quite right, either; the lpCommandLine argument should include the entire string, not just notepad, and you haven't quoted the backslashes properly.)
But even once you fix those problems, it won't work, because you've got the syntax wrong. You'll find it won't work if typed on the command line either!
Instead, try:
String pathexe = "C:\\Windows\\system32\\cmd.exe";
String command= "cmd /c notepad.exe";
The /c option means "run this command". You can use /k instead if you want the command window to stay open after the command has finished, though it's unusual for a program to do so.
One final note: I'm assuming here than notepad is just a stand-in for a more complicated command. If you actually want to run notepad, or any other executable, you shouldn't be invoking cmd.exe at all:
String command= "notepad";
if(!CreateProcess(
NULL, // lpApplicationName
command.c_str(), // lpCommandLine
...
You only need to call on cmd.exe if you need to run a built-in command, or a composite command line.
(Actually, calling cmd.exe is considered poor practice even in those cases; in Windows, you are generally expected do that sort of thing for yourself via the API rather than farming out the job to the command interpreter. But there are edge cases, and your mileage may vary.)

why Commands are not executed with CreateProcess

i hev problem with CreatProcess function introduced in windows.h header for c++.
whenever i try to pass it a TCHAR variable containing a cmd command it returns error : CreateProcess failed (2) .
and for this am waiting for your explanations and solutions.
consider code below:
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
int _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 0;
}
// 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 0;
}
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}
notice:when i launch an application with specifying its path ..it works fine like=> "c:\code.exe";
If you want to run a command DOS, you have to run the shell cmd before.
CreateProcess doesn't do that for you.
The option /c of cmd permits to run a command in the shell and terminate. You just have to build a command line of the type cmd /c <your command here>.
I compiled your code on VS2012 and I tried : test.exe "cmd /c dir" and it works like a charm.
From the Microsoft documentation :
To run a batch file (or a batch command), you must start the command interpreter; set lpApplicationName to cmd.exe and set lpCommandLine to the following arguments: /c plus the name of the batch file.
Source : http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx
In order to execute a command implemented by the command shell aka cmd.exe, you need to actually run cmd.exe. CreateProcess doesn't automatically do that for you.
Build a command line of the form cmd.exe /c <your command here>. /c means "run one command, then terminate".

Redirecting stdout output in cpp

I've been searching for an answer regarding this issue for a few days now, I hope you guys will be able to assist me. (I've searched and found some solutions, but each has its own issue...).
Here is the thing:
I'm writing an automation at work, which is responsible for launching an external ".exe" file of a code written by my colleagues. As those programs they write go to customers, I'm not allowed to make any modification to their code. Those programs, once launched, are waiting for specific key strokes, and prints a message when a legal key stroke has been received.
My goal is this:
To write a program which will execute the external program, send it key strokes, and receive the output from their stdout.
So far, I have been able to run the program from my program (using ShellExecute), and simulate some sort of keyboard listener (using SendMessage) to the other program. I can see that it works - I can see the output in the tested program's console.
I'm trying to fetch the messages printed on the tested program's shell in real-time (and just get a bulk of data when the program terminates) so that I could analyse it when it occurs.
Those I've tried:
Writing an external batch file with inline output redirection to a text file.
Using freopen.
Redirecting the output while exectuing "ShellExecute".
You use handles for stdin, stdout, and stderr. Create process with CreateProcess function to get that handles.
Sample code - incomplete for your case, but good example of how to do it:
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
/*for test.exe
#include <iostream>
#include <string> */
void _tmain( int argc, TCHAR *argv[] )
{
/*for test.exe
std::cout << "test output" << std::endl;
for (;;)
{
std::string line;
std::getline(std::cin, line);
std::cout << "line: " << line << std::endl;
}
return;*/
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)
"test.exe", // 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;
}
/* HANDLE hStdInput;
HANDLE hStdOutput;
HANDLE hStdError;*/
HANDLE me_hStdInput = GetStdHandle(STD_INPUT_HANDLE);
HANDLE me_hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE proc_hStdInput = si.hStdInput;
HANDLE proc_hStdOutput = si.hStdOutput;
char buff[64];
DWORD chars;
while (!ReadConsole(me_hStdInput, buff, sizeof(buff), &chars, NULL))
{
for (DWORD written = 0, writtenThisTime; written < chars; written += writtenThisTime)
if (!WriteConsole(proc_hStdOutput, buff + written, chars - written, &writtenThisTime, NULL))
{
//handle error - TODO
}
}
//possibly handle error for ReadConsole - TODO
// Wait until child process exits.
//WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}