Attach to a console
A process can be attached to a console using the attachconsole function, and only one console can be attached to a process. A console can have many processes attached to it. You can call the getconsoleprocesslist function to obtain the list attached to a console.
Close a console
A process can use the freeconsole function to remove itself from a console. If other processes share this console, the console will not be destroyed, but the process that calls the freeconsole function cannot use it. After the process calls freeconsole, it can use allocconsole to create a new console or attachconsole to attach to another console.
The control is disabled when the last attached process is disabled or when the freeconsole is called.
Console handle
The console process uses a handle to access the console's input and screen buffers. A process can use the getstdhandle, createfile, or createconsolescreenbuffer functions to open these handles.
GetstdhandleThe function provides a process-related standard input (STDIN), standard output (STDOUT), and standard error (STDERR) handle. The system creates these handles during console creation. The initial STDIN is a handle pointing to the console input buffer, and STDOUT and STDERR are handles of the console activity screen buffer. However, the SetStdHandle function can change the handles associated with STDIN, STDOUT, and STDERR to redirect standard handles. Because the standard handle of the parent process can be inherited by the quilt process, the redirection handle is returned when the GetStdHandle function is called later. The handle returned by GetStdHandle can point to another handle, not necessarily console I/O. For example, before a child process is created, the parent process uses SetStdHandle to set STDIN as a pipe handle. This feature is inherited by the process. The pipeline handle is returned when the sub-process calls GetStdHandle. This means that the parent process can control the standard handle of the child process. The handle returned by GetStdHandle has the permission to access GENERIC_READ | GENERIC_WRITE. Of course, if you set less access permissions when SetStdHandle is used.
GetstdhandleThe returned handle values are not 0, 1, and 2. Therefore, the predefined stream constants in Stdio. h (STDIN, STDOUT, and STDERR) cannot be used in functions that require a console handle.
Createfile FunctionEven if STDIN and STDOUT are not redirected, a process can get the handle of the input buffer and the Active Screen buffer. To open a console input buffer handle, you need to specify the CONIN $ value when calling CreateFile, and specify the CONOUT $ value to open a handle to the console activity screen buffer. The CreateFile function allows you to read and write operations through the handle returned by it.
Function createconsolescreenbufferCreate a new screen buffer and return this handle, which can be used in all functions that accept a control output as a parameter. The new screen buffer is not active. It is set to active only when the SetConsoleActiveScreenBuffer function is called. Changes to the Active Screen buffer do not change the value of GetStdHandle. Similarly, using SetStdHandle to change the STDOUT handle does not affect the Active Screen buffer.
The handle returned by the createfile and createconsolescreenbuffer functions can be used in any function that requires the console to input a buffer or console screen buffer as a parameter, the handle returned by getstdhandle can be used in the console function as long as it is not redirected to another handle. If the standard handle is redirected to a file or pipeline, these handles can only be used by the readfile and writefile functions.
A process can use the duplicatehandle function to create a copy of the console handle. Different access and inheritance permissions can be specified. Note that a process can create a console handle copy for its own use. This is different from other handle types (such as files, pipelines, and semaphore objects). These types can be used to create a valid handle through duplicatehandle, and other processes can also use it.
Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 697517