Win32:Console
Console Functions
- [https://msdn.microsoft.com/ko-kr/library/windows/desktop/ms682073(v=vs.85.aspx MDSN: Console Functions]
The following functions are used to access a console.
-
AddConsoleAlias
: Defines a console alias for the specified executable. -
AllocConsole
: Allocates a new console for the calling process. -
AttachConsole
: Attaches the calling process to the console of the specified process. -
CreateConsoleScreenBuffer
: Creates a console screen buffer. -
FillConsoleOutputAttribute
: Sets the text and background color attributes for a specified number of character cells. -
FillConsoleOutputCharacter
: Writes a character to the console screen buffer a specified number of times. -
FlushConsoleInputBuffer
: Flushes the console input buffer. -
FreeConsole
: Detaches the calling process from its console. -
GenerateConsoleCtrlEvent
: Sends a specified signal to a console process group that shares the console associated with the calling process. -
GetConsoleAlias
: Retrieves the specified alias for the specified executable. -
GetConsoleAliases
: Retrieves all defined console aliases for the specified executable. -
GetConsoleAliasesLength
: Returns the size, in bytes, of the buffer needed to store all of the console aliases for the specified executable. -
GetConsoleAliasExes
: Retrieves the names of all executables with console aliases defined. -
GetConsoleAliasExesLength
: Returns the size, in bytes, of the buffer needed to store the names of all executables that have console aliases defined. -
GetConsoleCP
: Retrieves the input code page used by the console associated with the calling process. -
GetConsoleCursorInfo
: Retrieves information about the size and visibility of the cursor for the specified console screen buffer. -
GetConsoleDisplayMode
: Retrieves the display mode of the current console. -
GetConsoleFontSize
: Retrieves the size of the font used by the specified console screen buffer. -
GetConsoleHistoryInfo
: Retrieves the history settings for the calling process's console. -
GetConsoleMode
: Retrieves the current input mode of a console's input buffer or the current output mode of a console screen buffer. -
GetConsoleOriginalTitle
: Retrieves the original title for the current console window. -
GetConsoleOutputCP
: Retrieves the output code page used by the console associated with the calling process. -
GetConsoleProcessList
: Retrieves a list of the processes attached to the current console. -
GetConsoleScreenBufferInfo
: Retrieves information about the specified console screen buffer. -
GetConsoleScreenBufferInfoEx
: Retrieves extended information about the specified console screen buffer. -
GetConsoleSelectionInfo
: Retrieves information about the current console selection. -
GetConsoleTitle
: Retrieves the title for the current console window. -
GetConsoleWindow
: Retrieves the window handle used by the console associated with the calling process. -
GetCurrentConsoleFont
: Retrieves information about the current console font. -
GetCurrentConsoleFontEx
: Retrieves extended information about the current console font. -
GetLargestConsoleWindowSize
: Retrieves the size of the largest possible console window. -
GetNumberOfConsoleInputEvents
: Retrieves the number of unread input records in the console's input buffer. -
GetNumberOfConsoleMouseButtons
: Retrieves the number of buttons on the mouse used by the current console. -
GetStdHandle
: Retrieves a handle for the standard input, standard output, or standard error device. -
HandlerRoutine
: An application-defined function used with the SetConsoleCtrlHandler function. -
PeekConsoleInput
: Reads data from the specified console input buffer without removing it from the buffer. -
ReadConsole
: Reads character input from the console input buffer and removes it from the buffer. -
ReadConsoleInput
: Reads data from a console input buffer and removes it from the buffer. -
ReadConsoleOutput
: Reads character and color attribute data from a rectangular block of character cells in a console screen buffer. -
ReadConsoleOutputAttribute
: Copies a specified number of foreground and background color attributes from consecutive cells of a console screen buffer. -
ReadConsoleOutputCharacter
: Copies a number of characters from consecutive cells of a console screen buffer. -
ScrollConsoleScreenBuffer
: Moves a block of data in a screen buffer. -
SetConsoleActiveScreenBuffer
: Sets the specified screen buffer to be the currently displayed console screen buffer. -
SetConsoleCP
: Sets the input code page used by the console associated with the calling process. -
SetConsoleCtrlHandler
: Adds or removes an application-defined HandlerRoutine from the list of handler functions for the calling process. -
SetConsoleCursorInfo
: Sets the size and visibility of the cursor for the specified console screen buffer. -
SetConsoleCursorPosition
: Sets the cursor position in the specified console screen buffer. -
SetConsoleDisplayMode
: Sets the display mode of the specified console screen buffer. -
SetConsoleHistoryInfo
: Sets the history settings for the calling process's console. -
SetConsoleMode
: Sets the input mode of a console's input buffer or the output mode of a console screen buffer. -
SetConsoleOutputCP
: Sets the output code page used by the console associated with the calling process. -
SetConsoleScreenBufferInfoEx
: Sets extended information about the specified console screen buffer. -
SetConsoleScreenBufferSize
: Changes the size of the specified console screen buffer. -
SetConsoleTextAttribute
: Sets the foreground (text) and background color attributes of characters written to the console screen buffer. -
SetConsoleTitle
: Sets the title for the current console window. -
SetConsoleWindowInfo
: Sets the current size and position of a console screen buffer's window. -
SetCurrentConsoleFontEx
: Sets extended information about the current console font. -
SetStdHandle
: Sets the handle for the standard input, standard output, or standard error device. -
WriteConsole
: Writes a character string to a console screen buffer beginning at the current cursor location. -
WriteConsoleInput
: Writes data directly to the console input buffer. -
WriteConsoleOutput
: Writes character and color attribute data to a specified rectangular block of character cells in a console screen buffer. -
WriteConsoleOutputAttribute
: Copies a number of foreground and background color attributes to consecutive cells of a console screen buffer. -
WriteConsoleOutputCharacter
: Copies a number of characters to consecutive cells of a console screen buffer.
Console Cursor Position
#include <windows.h>
// ...
void GotoXY(int x, int y)
{
COORD pos = { static_cast< short >( x ), static_cast< short >( y ) };
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), pos );
}
Console text color
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); // Get handle to standard output
SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE);
Console IO Redirection
- 윈도우 콘솔(console) 입출력 리다이렉션(Input/Out Redirection)
- C++: Redirecting STDOUT
- Stdout Visual C/c + + 프로그램에서 파일로 리디렉션하는 방법
- 콘솔 프로그램의 출력 결과물을 갈무리하는 방법 (Console Redirection)
- 콘솔 실행정보를 리다이렉션(Redirection) 하기
- [추천] 콘솔 프로그램의 출력 결과물을 갈무리하는 방법 (Console Redirection)
표준 입력 핸들에서 입력을 가져오거나 표준 출력 핸들로 출력을 보내는 자식 프로세스의 입출력을 리다이렉트(redirect)하는 방법을 기술한다.
참고로, 이와 같은 방법을 종종 Console redirection 또는 Screen Output Capture 이라고 한다.
#include <windows.h>
#include <stdio.h>
#define APP "C:\\Windows\\system32\\ipconfig.exe"
int main(int argc, char **argv)
{
char szBuff[256];
DWORD dwRead = 0, dwOut = 0, dwErr = 0;
HANDLE hStdOutWrite = NULL, hStdOutRead = NULL;
HANDLE hStdErrWrite = NULL, hStdErrRead = NULL;
STARTUPINFO si;
SECURITY_ATTRIBUTES sa;
PROCESS_INFORMATION pi;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
CreatePipe(&hStdOutRead, &hStdOutWrite, &sa, 0); /* 실행될 콘솔 프로그램에 넘길 stdout */
CreatePipe(&hStdErrRead, &hStdErrWrite, &sa, 0); /* 실행될 콘솔 프로그램에 넘길 stderr */
ZeroMemory(&si,sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
si.hStdOutput = hStdOutWrite;
si.hStdInput = NULL;
si.hStdError = hStdErrWrite;
si.wShowWindow = SW_HIDE; /* 눈에 보이지 않는 상태로 프로세스 시작 */
CreateProcess(NULL,APP, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
CloseHandle(pi.hThread);
while ( PeekNamedPipe(hStdOutRead, NULL, 0, NULL, &dwOut, NULL) ||
PeekNamedPipe(hStdErrRead, NULL, 0, NULL, &dwErr, NULL) ) /* 읽어들일 데이터가 있는가? */
{
if ( dwOut <= 0 && dwErr <= 0 && WaitForSingleObject(pi.hProcess, 0) != WAIT_TIMEOUT )
break; /* 실행되어진 콘솔 응용프로그램이 종료된 경우 */
while ( PeekNamedPipe(hStdOutRead, NULL, 0, NULL, &dwOut, NULL) && dwOut > 0 )
{
ReadFile(hStdOutRead, szBuff, sizeof(szBuff), &dwRead,NULL);
szBuff[dwRead] = 0;
printf("%s", szBuff);
}
while ( PeekNamedPipe(hStdErrRead, NULL, 0, NULL, &dwErr, NULL) && dwErr > 0 )
{
ReadFile(hStdErrRead, szBuff, sizeof(szBuff), &dwRead,NULL);
szBuff[dwRead] = 0;
printf("%s", szBuff);
}
}
CloseHandle(pi.hProcess);
CloseHandle(hStdOutRead);
CloseHandle(hStdOutWrite);
CloseHandle(hStdErrRead);
CloseHandle(hStdErrWrite);
return 0;
}