Ritchie
2003-08-05 12:12:58 UTC
Hi all,
I've been experimenting with reading/writing to StdIn/Stdout using
ReadFile and WriteFile and noticed that GetConsoleMode() always fails
if anything is piped into the application, GetLastError returns 6,
which means "The handle in invalid". Surely, if the handle was invalid,
then GetStdHandle() would have failed. Any ideas why this happens?
================= screen dump ==================
D:\data\msdev\test\Debug> dir | test
Error 6: GetConsoleMode failed
D:\data\msdev\test\Debug> test < myfile.txt
Error 6: GetConsoleMode failed
D:\data\msdev\test\Debug> test
Error 0: No errors
================= screen dump ==================
And here's a trimmed version of the code:-
#include <windows.h>
#include <stdio.h>
int main(void)
{
HANDLE hStdIn;
DWORD dwStdInMode;
hStdIn = GetStdHandle(STD_INPUT_HANDLE);
if(hStdIn == INVALID_HANDLE_VALUE)
return printf("Error %d: GetStdHandle failed\n", GetLastError());
if(!GetConsoleMode(hStdIn, &dwStdInMode))
return printf("Error %d: GetConsoleMode failed\n", GetLastError());
return printf("Error %d: No errors\n", GetLastError());
}
--
Ritchie
I've been experimenting with reading/writing to StdIn/Stdout using
ReadFile and WriteFile and noticed that GetConsoleMode() always fails
if anything is piped into the application, GetLastError returns 6,
which means "The handle in invalid". Surely, if the handle was invalid,
then GetStdHandle() would have failed. Any ideas why this happens?
================= screen dump ==================
D:\data\msdev\test\Debug> dir | test
Error 6: GetConsoleMode failed
D:\data\msdev\test\Debug> test < myfile.txt
Error 6: GetConsoleMode failed
D:\data\msdev\test\Debug> test
Error 0: No errors
================= screen dump ==================
And here's a trimmed version of the code:-
#include <windows.h>
#include <stdio.h>
int main(void)
{
HANDLE hStdIn;
DWORD dwStdInMode;
hStdIn = GetStdHandle(STD_INPUT_HANDLE);
if(hStdIn == INVALID_HANDLE_VALUE)
return printf("Error %d: GetStdHandle failed\n", GetLastError());
if(!GetConsoleMode(hStdIn, &dwStdInMode))
return printf("Error %d: GetConsoleMode failed\n", GetLastError());
return printf("Error %d: No errors\n", GetLastError());
}
--
Ritchie