Click to Rate and Give Feedback
MSDN
MSDN Library
System Services
Console Reference
Console Functions
 AttachConsole Function
AttachConsole Function

Attaches the calling process to the console of the specified process.

Syntax

C++
BOOL WINAPI AttachConsole(
  __in  DWORD dwProcessId
);

Parameters

dwProcessId [in]

The identifier of the process whose console is to be used. This parameter can be one of the following values.

ValueMeaning
pid

Use the console of the specified process.

ATTACH_PARENT_PROCESS
(DWORD)-1

Use the console of the parent of the current process.

 

Return Value

If the function succeeds, the return value is nonzero.

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

Remarks

A process can be attached to at most one console. If the calling process is already attached to a console, the error code returned is ERROR_ACCESS_DENIED (5). If the specified process does not have a console, the error code returned is ERROR_INVALID_HANDLE (6). If the specified process does not exist, the error code returned is ERROR_GEN_FAILURE (31).

A process can use the FreeConsole function to detach itself from its console. If other processes share the console, the console is not destroyed, but the process that called FreeConsole cannot refer to it. A console is closed when the last process attached to it terminates or calls FreeConsole. After a process calls FreeConsole, it can call the AllocConsole function to create a new console or AttachConsole to attach to another console.

To compile an application that uses this function, define _WIN32_WINNT as 0x0501 or later. For more information, see Using the Windows Headers.

Requirements

Minimum supported clientWindows XP
Minimum supported serverWindows Server 2003
HeaderWincon.h (include Windows.h)
LibraryKernel32.lib
DLLKernel32.dll

See Also

Console Functions
Consoles
AllocConsole
FreeConsole
GetConsoleProcessList

Send comments about this topic to Microsoft

Build date: 11/19/2009

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Beware using AttachConsole on Windows 2000      ScottJ1s   |   Edit   |   Show History
There is a bug in the header file wincon.h, on line 753, which defines AttachConsole conditionally with:

#if

(_WIN32_WINNT >= 0x0500)

Note that 0x0500 is what _WIN32_WINNT_WIN2K defines to, on line 24 of sdkddkver.h:

#define

_WIN32_WINNT_WIN2K 0x0500


We just shipped product that crashes on startup for our Windows 2000 clients, because of this.

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker