Home

GetStdHandle

GetStdHandle is a Windows API function that retrieves a handle to one of the process’s standard devices: input, output, or error. It is declared in Windows.h and implemented in kernel32.dll. The function is commonly used in low-level Windows I/O scenarios to obtain a handle that can be used with native I/O functions or console APIs.

The parameter nStdHandle selects which device to return. It accepts STD_INPUT_HANDLE (-10), STD_OUTPUT_HANDLE (-11), or STD_ERROR_HANDLE

Usage considerations include checking the return value before using the handle and recognizing that pseudo handles

(-12).
The
return
value
is
a
HANDLE.
On
success,
the
handle
may
be
a
real
system
handle
to
the
device
or
a
pseudo
handle
that
represents
the
standard
device.
If
the
process
has
no
associated
console,
GetStdHandle
can
return
a
pseudo
handle.
Pseudo
handles
are
valid
for
certain
console
I/O
operations
but
are
not
interchangeable
with
real
handles
in
all
API
calls.
If
the
function
fails,
the
return
value
is
INVALID_HANDLE_VALUE
and
GetLastError
provides
the
specific
error
code.
have
limitations.
The
obtained
handle
can
be
used
with
low-level
I/O
functions
such
as
ReadFile
and
WriteFile,
or
with
console
APIs
that
accept
a
HANDLE.
To
replace
or
redirect
standard
handles,
SetStdHandle
can
be
used,
or
a
console
can
be
allocated
with
AllocConsole
in
GUI
applications
that
require
a
console.
GetStdHandle
is
a
common
starting
point
when
a
program
needs
to
interact
with
the
process’s
conventional
input
or
output
streams
at
the
Windows
API
level.