Console handle

xiaoxiao2021-03-06  22

The console program uses the handle to access the input buffer and the screen buffer of the console. The program can pass GetStdHandle,

Createfile

Or the CreateConsolescreenBuffer function opens the desired handle.

The GetStdHandle function provides a mechanism for obtaining the standard input (stdin), standard output (stdout), and standard error (stderr) associated with the program. When the console is created, the system creates these handles. Start, stdin is the console input buffer handle, stdout and stderr are the current screen buffer of the console. However, these standard handles can be reset by the setstdhandle function. Since the standard handle of the parent program is inherited by any subroutine, the reset handle can be returned after calling getStdHandle. Therefore, the handle returned by GetStdHandle may not be a console I / O. For example, before creating a subroutine, the parent program can set the pipe handle for the subroutine, and the subroutine will be obtained by setting the pipe handle, and the subroutine will be obtained by setstdhandle. This means that the parent program can control the standard handle of the subroutine. If setStdHandle does not set less access to the standard handle, the handle returned by getStdHHHHHHHHHHHHHANDLE should contain generic_read | generic_write privileges.

The handle value returned by GetStdHandle is not 0, 1 and 2, so standard predefined constants in stdio.h (stdin, stdout, and stderr) cannot be used in a function that requires a handle handle.

The CREATEFILE function can get an input buffer of a program console and the current screen buffer, even if stdin and stdout are relocated. To open the handle of the console input buffer, simply specify the CREATEFILE to specify the CONIN $ value, and specify the handle of the Conout $ to open the current screen buffer. CREATEFILE allows you to specify the read / write permission to return the handle.

The CreateConsolescreenBuffer function creates a new screen buffer and returns its handle. The handle can be used in a function that needs to be entered anywhere. The new screen buffer will not be activated unless the setConsoleAventScreenBuffer function is called. Note: Change the current screen buffer does not affect the return value of getStdHandle;

The return value of CREATEFILE and CREATECONSOLESCREENBUFFER can be used anywhere to access functions of the input buffer and the screen buffer. GetStdHandle's return value is only valid when there is a non-reprinted I / O. If the standard handle is relocated to a file or pipe, this handle can only be used through the ReadFile and WriteFile functions.

Program can pass

DuplicateHandle

The function creates a copy of the console handle for different access or inheriting the original handle. Note: Despite this, the program can only create a copy of the console handle for its convenience. This is different from other handle types (such as files, pipelines or mutual exclusive classes), which can be used by DuplicateHandle by different programs.

To turn off a console handle, you can call

CloseHandle

function.

转载请注明原文地址:https://www.9cbs.com/read-56831.html

New Post(0)