GetStdHandle Function

Retrieves a handle to the specified standard device (standard input, standard output, or standard error).

Syntax

C++
HANDLE WINAPI GetStdHandle(
  __in  DWORD nStdHandle
);

Parameters

nStdHandle [in]

The standard device. This parameter can be one of the following values.

ValueMeaning
STD_INPUT_HANDLE
(DWORD)-10

The standard input device. Initially, this is the console input buffer, CONIN$.

STD_OUTPUT_HANDLE
(DWORD)-11

The standard output device. Initially, this is the active console screen buffer, CONOUT$.

STD_ERROR_HANDLE
(DWORD)-12

The standard error device. Initially, this is the active console screen buffer, CONOUT$.

 

Return Value

If the function succeeds, the return value is a handle to the specified device, or a redirected handle set by a previous call to SetStdHandle. The handle has GENERIC_READ and GENERIC_WRITE access rights, unless the application has used SetStdHandle to set a standard handle with lesser access.

If the function fails, the return value is INVALID_HANDLE_VALUE. To get extended error information, call GetLastError.

If an application does not have associated standard handles, such as a service running on an interactive desktop, and has not redirected them, the return value is NULL.

Remarks

Handles returned by GetStdHandle can be used by applications that need to read from or write to the console. When a console is created, the standard input handle is a handle to the console's input buffer, and the standard output and standard error handles are handles of the console's active screen buffer. These handles can be used by the ReadFile and WriteFile functions, or by any of the console functions that access the console input buffer or a screen buffer (for example, the ReadConsoleInput, WriteConsole, or GetConsoleScreenBufferInfo functions).

The standard handles of a process may be redirected by a call to SetStdHandle, in which case GetStdHandle returns the redirected handle. If the standard handles have been redirected, you can specify the CONIN$ value in a call to the CreateFile function to get a handle to a console's input buffer. Similarly, you can specify the CONOUT$ value to get a handle to a console's active screen buffer.

Examples

For an example, see Reading Input Buffer Events.

Requirements

Minimum supported clientWindows 2000 Professional
Minimum supported serverWindows 2000 Server
HeaderWinbase.h (include Windows.h)
LibraryKernel32.lib
DLLKernel32.dll

See Also

Console Functions
Console Handles
CreateFile
GetConsoleScreenBufferInfo
ReadConsoleInput
ReadFile
SetStdHandle
WriteConsole
WriteFile

Send comments about this topic to Microsoft

Build date: 10/8/2009

Tags :


Community Content

BlueViper02
Typically, do not close the returned handle

It is not a duplicate handle, it is the real thing, so think twice before closing it :)

Tags : contentbug

dmex
vb.net syntax
<DllImport("kernel32.dll", SetLastError:=True)> _
Public Shared Function GetStdHandle(ByVal nStdHandle As Integer) As IntPtr
End Function
Tags : vb.net syntax

dmex
C# syntax
[DllImport("kernel32.dll", SetLastError=true)]
internal static extern IntPtr GetStdHandle(int nStdHandle);
Tags : c# syntax

Page view tracker