Welcome to MSDN Blogs Sign in | Join | Help

How do I know if console handle is redirected?

In a console application, how does it know if the console standard handle has been redirected (to a pipe or file) or not?

You can use GetStdHandle to get the standard handle, then use GetFileType to determine the type of the handle.

If the file type is not FILE_TYPE_CHAR, you know the console handle is redirected.

Published Thursday, July 07, 2005 10:57 PM by junfeng
Filed under:

Comments

# re: How do I know if console handle is redirected?

Friday, July 08, 2005 9:39 AM by oldnewthing
Note that you can be redirected to a character device, in which case you will still get FILE_TYPE_CHAR back.

# re: How do I know if console handle is redirected?

Friday, July 08, 2005 12:29 PM by junfeng
From MSDN, it seems the only other non-console character device is printer. Not sure how many people still use that nowadays.

Do you have better ways to distinguish this Raymond?

# re: How do I know if console handle is redirected?

Thursday, August 04, 2005 4:35 PM by Marc Brooks
How about detecting the underlying device name? Specifically, is it NUL:

# re: How do I know if console handle is redirected?

Wednesday, September 28, 2005 2:59 PM by Andrej
There is this undocumented Kernel32 function BOOL WINAPI VerifyConsoleIoHandle(HANDLE) that one can use instead or additionally to GetFileType().

There are at least two types of non-console character devices; that's lpt and com.
New Comments to this post are disabled
 
Page view tracker