Click to See Complete Forum and Search --> : [RESOLVED] SetStdHandle() not working for me
philkr
May 27th, 2006, 06:31 AM
Please take a look at this sample code:
#include <windows.h>
#include <iostream>
int main()
{
HANDLE hStdOutRd = INVALID_HANDLE_VALUE;
HANDLE hStdOutWr = INVALID_HANDLE_VALUE;
int r1 = CreatePipe(&hStdOutRd, &hStdOutWr, NULL, 8192);
int r2 = SetStdHandle(STD_OUTPUT_HANDLE, hStdOutWr);
int r3 = SetStdHandle(STD_ERROR_HANDLE, hStdOutWr);
std::cout << "Result: " << r1 << r2 << r3;
return 0;
}
The three functions all return a nonzero value for success, but the output has not been redirected to the pipe, as you can still read the result on the screen. What is wrong and what do I have to do to redirect the output?
golanshahar
May 27th, 2006, 07:05 AM
This article might help you: Redirect Output of CMD.EXE to a Pipe (http://www.codeguru.com/cpp/misc/misc/article.php/c277/)
Cheers
philkr
May 27th, 2006, 07:19 AM
Thank you for the article, but it is not quite what I want to achieve.
The article describes how to redirect the output of a child process.
I want to redirect the output of the current process. I hope it is possible.
philkr
May 27th, 2006, 09:52 AM
Well, perhaps it can be done without windows specific code. I just don't know enough about streams. What I want is basically that I can call system() function, but I want the result not (only) on the screen, but also in a char buffer so that I can work with it further. I know that I can associate another stream object with cout using rdbuf(), but how can I use this to save the output to my char buffer. It can't be that difficult, so please help!
philkr
May 27th, 2006, 12:18 PM
Currently I am using a workaround, though I don't like it because it is complicated.
Nevertheless I have another problem. When I send a command to cmd.exe through the pipe the first time I can read the right answer (Cool!). The second time I read "More? " as an answer (***?). What does this mean? What does cmd.exe want to tell me? The third time I send a command I don't get an answer at all (hm, strange).
philkr
June 1st, 2006, 12:04 PM
*up*
HoM
June 7th, 2006, 05:47 AM
What does this mean? What does cmd.exe want to tell me?
I would guess, that cmd wants to tell you, that you did not send a CR/LF at the end of the input, so it expects more input ...
regards
HoM
philkr
June 7th, 2006, 06:10 AM
Thanks! That was it (together with another error). I've waited almost a week for an answer to this. I wonder what these Microsoft guys thought when they made this error message "More? ". This is just ridiculous. If they were to lazy to write a proper message they could have at least written "CRLF? "with the same letter count.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.