Permalink
Fetching contributors…
Cannot retrieve contributors at this time
151 lines (119 sloc) 3.75 KB
title description author ms.author ms.topic ms.prod keywords MS-HAID MSHAttr ms.assetid topic_type api_name api_location api_type
AttachConsole function
Attaches the calling process to the console of the specified process.
bitcrazed
richturn
article
console
console, character mode applications, command line applications, terminal applications, console api
\_win32\_attachconsole
base.attachconsole
consoles.attachconsole
PreferredSiteName:MSDN
PreferredLib:/library/windows/desktop
c00691c3-5878-4a06-9bf3-6073326f36c4
apiref
AttachConsole
Kernel32.dll
API-MS-Win-Core-Console-l2-1-0.dll
KernelBase.dll
API-MS-Win-DownLevel-Kernel32-l1-1-0.dll
DllExport

AttachConsole function

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

Syntax

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.

Value Meaning
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_INVALID_PARAMETER (87).

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 client

Windows XP [desktop apps only]

Minimum supported server

Windows Server 2003 [desktop apps only]

Header

Wincon.h (include Windows.h)

Library

Kernel32.lib

DLL

Kernel32.dll

See also

Console Functions

Consoles

AllocConsole

FreeConsole

GetConsoleProcessList