Dave's Brain

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> 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); ...

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.