Browse - Programming Tips - Old - How can I get a Windows HANDLE for stdin or stdout?

Date: 2010may5
OS: Windows
Language: C/C++

Q.  How can I get a Windows HANDLE for stdin or stdout?

A.  Here's how:

#include <stdio.h> #include <io.h> HANDLE hStdIn, hStdOut; hStdIn = GetStdHandle(STD_INPUT_HANDLE); hstdOut = GetStdHandle(STD_OUTPUT_HANDLE);
or:
hStdIn = (HANDLE) _get_osfhandle(fileno(stdin)); hStdOut = (HANDLE) _get_osfhandle(fileno(stdout)); // Now you can use hStdIn and hStdOut in regular win32 function like: GetFileInformationByHandle(hStdIn, &info); GetFileSizeEx(hStdOut, &li); type = GetFileType(hStdIn); ...