uiautomationcore: Implement IUIAutomationElement::get_CachedName.
[wine.git] / dlls / kernelbase / console.c
blob831e358c6922e6d33af2768f8169575e2c24858f
1 /*
2 * Win32 console functions
4 * Copyright 1995 Martin von Loewis and Cameron Heide
5 * Copyright 1997 Karl Garrison
6 * Copyright 1998 John Richardson
7 * Copyright 1998 Marcus Meissner
8 * Copyright 2001,2002,2004,2005,2010 Eric Pouech
9 * Copyright 2001 Alexandre Julliard
10 * Copyright 2020 Jacek Caban for CodeWeavers
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <limits.h>
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winnls.h"
37 #include "winerror.h"
38 #include "wincon.h"
39 #include "winternl.h"
40 #include "wine/condrv.h"
41 #include "wine/exception.h"
42 #include "wine/debug.h"
43 #include "kernelbase.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(console);
48 static CRITICAL_SECTION console_section;
49 static CRITICAL_SECTION_DEBUG critsect_debug =
51 0, 0, &console_section,
52 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
53 0, 0, { (DWORD_PTR)(__FILE__ ": console_section") }
55 static CRITICAL_SECTION console_section = { &critsect_debug, -1, 0, 0, 0, 0 };
57 static HANDLE console_connection;
58 static unsigned int console_flags;
60 #define CONSOLE_INPUT_HANDLE 0x01
61 #define CONSOLE_OUTPUT_HANDLE 0x02
62 #define CONSOLE_ERROR_HANDLE 0x04
64 static WCHAR input_exe[MAX_PATH + 1];
66 struct ctrl_handler
68 PHANDLER_ROUTINE func;
69 struct ctrl_handler *next;
72 static BOOL WINAPI default_ctrl_handler( DWORD type )
74 FIXME( "Terminating process %lx on event %lx\n", GetCurrentProcessId(), type );
75 RtlExitUserProcess( 0 );
76 return TRUE;
79 static struct ctrl_handler default_handler = { default_ctrl_handler, NULL };
80 static struct ctrl_handler *ctrl_handlers = &default_handler;
82 static BOOL console_ioctl( HANDLE handle, DWORD code, void *in_buff, DWORD in_count,
83 void *out_buff, DWORD out_count, DWORD *read )
85 IO_STATUS_BLOCK io;
86 NTSTATUS status;
88 if (handle == CONSOLE_HANDLE_SHELL_NO_WINDOW)
90 WARN("Incorrect access to Shell-no-window console (ioctl=%lx)\n", code);
91 SetLastError( ERROR_INVALID_ACCESS );
92 return FALSE;
94 status = NtDeviceIoControlFile( handle, NULL, NULL, NULL, &io, code, in_buff, in_count,
95 out_buff, out_count );
96 switch( status )
98 case STATUS_SUCCESS:
99 if (read) *read = io.Information;
100 return TRUE;
101 case STATUS_INVALID_PARAMETER:
102 break;
103 default:
104 status = STATUS_INVALID_HANDLE;
105 break;
107 if (read) *read = 0;
108 return set_ntstatus( status );
111 /* map input records to ASCII */
112 static void input_records_WtoA( INPUT_RECORD *buffer, int count )
114 UINT cp = GetConsoleCP();
115 int i;
116 char ch;
118 for (i = 0; i < count; i++)
120 if (buffer[i].EventType != KEY_EVENT) continue;
121 WideCharToMultiByte( cp, 0, &buffer[i].Event.KeyEvent.uChar.UnicodeChar, 1, &ch, 1, NULL, NULL );
122 buffer[i].Event.KeyEvent.uChar.AsciiChar = ch;
126 /* map input records to Unicode */
127 static void input_records_AtoW( INPUT_RECORD *buffer, int count )
129 UINT cp = GetConsoleCP();
130 int i;
131 WCHAR ch;
133 for (i = 0; i < count; i++)
135 if (buffer[i].EventType != KEY_EVENT) continue;
136 MultiByteToWideChar( cp, 0, &buffer[i].Event.KeyEvent.uChar.AsciiChar, 1, &ch, 1 );
137 buffer[i].Event.KeyEvent.uChar.UnicodeChar = ch;
141 /* map char infos to ASCII */
142 static void char_info_WtoA( UINT cp, CHAR_INFO *buffer, int count )
144 char ch;
146 while (count-- > 0)
148 WideCharToMultiByte( cp, 0, &buffer->Char.UnicodeChar, 1, &ch, 1, NULL, NULL );
149 buffer->Char.AsciiChar = ch;
150 buffer++;
154 /* map char infos to Unicode */
155 static void char_info_AtoW( CHAR_INFO *buffer, int count )
157 UINT cp = GetConsoleOutputCP();
158 WCHAR ch;
160 while (count-- > 0)
162 MultiByteToWideChar( cp, 0, &buffer->Char.AsciiChar, 1, &ch, 1 );
163 buffer->Char.UnicodeChar = ch;
164 buffer++;
168 /* helper function for GetLargestConsoleWindowSize */
169 static COORD get_largest_console_window_size( HANDLE handle )
171 struct condrv_output_info info;
172 COORD c = { 0, 0 };
174 if (!console_ioctl( handle, IOCTL_CONDRV_GET_OUTPUT_INFO, NULL, 0, &info, sizeof(info), NULL ))
175 return c;
177 c.X = info.max_width;
178 c.Y = info.max_height;
179 TRACE( "(%p), returning %dx%d\n", handle, c.X, c.Y );
180 return c;
183 /* helper function for GetConsoleFontSize */
184 static COORD get_console_font_size( HANDLE handle, DWORD index )
186 struct condrv_output_info info;
187 COORD c = {0,0};
189 if (index >= 1 /* number of console fonts */)
191 SetLastError(ERROR_INVALID_PARAMETER);
192 return c;
195 if (DeviceIoControl( handle, IOCTL_CONDRV_GET_OUTPUT_INFO, NULL, 0, &info, sizeof(info), NULL, NULL ))
197 c.X = info.font_width;
198 c.Y = info.font_height;
200 else SetLastError( ERROR_INVALID_HANDLE );
201 return c;
204 /* helper function for GetConsoleTitle and GetConsoleOriginalTitle */
205 static DWORD get_console_title( WCHAR *title, DWORD size, BOOL current_title )
207 struct condrv_title_params *params;
208 size_t max_size = sizeof(*params) + (size - 1) * sizeof(WCHAR);
210 if (!title || !size) return 0;
212 if (!(params = HeapAlloc( GetProcessHeap(), 0, max_size )))
213 return 0;
215 if (console_ioctl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle, IOCTL_CONDRV_GET_TITLE,
216 &current_title, sizeof(current_title), params, max_size, &size ) &&
217 size >= sizeof(*params))
219 size -= sizeof(*params);
220 memcpy( title, params->buffer, size );
221 title[ size / sizeof(WCHAR) ] = 0;
222 size = params->title_len;
224 else size = 0;
226 HeapFree( GetProcessHeap(), 0, params );
227 return size;
230 static HANDLE create_console_server( void )
232 OBJECT_ATTRIBUTES attr = {sizeof(attr)};
233 UNICODE_STRING string = RTL_CONSTANT_STRING( L"\\Device\\ConDrv\\Server" );
234 IO_STATUS_BLOCK iosb;
235 HANDLE handle;
236 NTSTATUS status;
238 attr.ObjectName = &string;
239 attr.Attributes = OBJ_INHERIT;
240 status = NtCreateFile( &handle, FILE_WRITE_PROPERTIES | FILE_READ_PROPERTIES | SYNCHRONIZE,
241 &attr, &iosb, NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_OPEN,
242 FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 );
243 return set_ntstatus( status ) ? handle : NULL;
246 static HANDLE create_console_reference( HANDLE root )
248 OBJECT_ATTRIBUTES attr = {sizeof(attr)};
249 UNICODE_STRING string = RTL_CONSTANT_STRING( L"Reference" );
250 IO_STATUS_BLOCK iosb;
251 HANDLE handle;
252 NTSTATUS status;
254 attr.RootDirectory = root;
255 attr.ObjectName = &string;
256 status = NtCreateFile( &handle, FILE_READ_DATA | FILE_WRITE_DATA | FILE_WRITE_PROPERTIES |
257 FILE_READ_PROPERTIES | SYNCHRONIZE, &attr, &iosb, NULL, FILE_ATTRIBUTE_NORMAL,
258 0, FILE_OPEN, FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 );
259 return set_ntstatus( status ) ? handle : NULL;
262 static BOOL create_console_connection( HANDLE root )
264 OBJECT_ATTRIBUTES attr = {sizeof(attr)};
265 UNICODE_STRING string;
266 IO_STATUS_BLOCK iosb;
267 NTSTATUS status;
269 RtlInitUnicodeString( &string, root ? L"Connection" : L"\\Device\\ConDrv\\Connection" );
270 attr.RootDirectory = root;
271 attr.ObjectName = &string;
272 status = NtCreateFile( &console_connection, FILE_WRITE_PROPERTIES | FILE_READ_PROPERTIES | SYNCHRONIZE, &attr,
273 &iosb, NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_OPEN, FILE_NON_DIRECTORY_FILE, NULL, 0 );
274 return set_ntstatus( status );
277 static BOOL init_console_std_handles( BOOL override_all )
279 HANDLE std_out = NULL, std_err = NULL, handle;
280 OBJECT_ATTRIBUTES attr = {sizeof(attr)};
281 IO_STATUS_BLOCK iosb;
282 UNICODE_STRING name;
283 NTSTATUS status;
285 attr.ObjectName = &name;
286 attr.Attributes = OBJ_INHERIT;
288 if (override_all || !GetStdHandle( STD_INPUT_HANDLE ))
290 RtlInitUnicodeString( &name, L"\\Device\\ConDrv\\Input" );
291 status = NtCreateFile( &handle, FILE_READ_DATA | FILE_WRITE_DATA | SYNCHRONIZE | FILE_READ_ATTRIBUTES |
292 FILE_WRITE_ATTRIBUTES, &attr, &iosb, NULL, FILE_ATTRIBUTE_NORMAL,
293 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_CREATE,
294 FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 );
295 if (!set_ntstatus( status )) return FALSE;
296 console_flags |= CONSOLE_INPUT_HANDLE;
297 SetStdHandle( STD_INPUT_HANDLE, handle );
300 if (!override_all)
302 std_out = GetStdHandle( STD_OUTPUT_HANDLE );
303 std_err = GetStdHandle( STD_ERROR_HANDLE );
304 if (std_out && std_err) return TRUE;
307 RtlInitUnicodeString( &name, L"\\Device\\ConDrv\\Output" );
308 status = NtCreateFile( &handle, FILE_READ_DATA | FILE_WRITE_DATA | SYNCHRONIZE | FILE_READ_ATTRIBUTES |
309 FILE_WRITE_ATTRIBUTES, &attr, &iosb, NULL, FILE_ATTRIBUTE_NORMAL,
310 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_CREATE,
311 FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 );
312 if (!set_ntstatus( status )) return FALSE;
313 if (!std_out)
315 console_flags |= CONSOLE_OUTPUT_HANDLE;
316 SetStdHandle( STD_OUTPUT_HANDLE, handle );
319 if (!std_err)
321 if (!std_out && !DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(),
322 &handle, 0, TRUE, DUPLICATE_SAME_ACCESS ))
323 return FALSE;
324 console_flags |= CONSOLE_ERROR_HANDLE;
325 SetStdHandle( STD_ERROR_HANDLE, handle );
328 return TRUE;
332 /******************************************************************
333 * AddConsoleAliasA (kernelbase.@)
335 BOOL WINAPI AddConsoleAliasA( LPSTR source, LPSTR target, LPSTR exename )
337 FIXME( ": (%s, %s, %s) stub!\n", debugstr_a(source), debugstr_a(target), debugstr_a(exename) );
338 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
339 return FALSE;
343 /******************************************************************
344 * AddConsoleAliasW (kernelbase.@)
346 BOOL WINAPI AddConsoleAliasW( LPWSTR source, LPWSTR target, LPWSTR exename )
348 FIXME( ": (%s, %s, %s) stub!\n", debugstr_w(source), debugstr_w(target), debugstr_w(exename) );
349 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
350 return FALSE;
354 /******************************************************************
355 * AttachConsole (kernelbase.@)
357 BOOL WINAPI DECLSPEC_HOTPATCH AttachConsole( DWORD pid )
359 BOOL ret;
361 TRACE( "(%lx)\n", pid );
363 RtlEnterCriticalSection( &console_section );
365 if (RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle)
367 RtlLeaveCriticalSection( &console_section );
368 WARN( "console already attached\n" );
369 SetLastError( ERROR_ACCESS_DENIED );
370 return FALSE;
373 ret = create_console_connection( NULL ) &&
374 console_ioctl( console_connection, IOCTL_CONDRV_BIND_PID, &pid, sizeof(pid), NULL, 0, NULL );
375 if (ret)
377 RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle = create_console_reference( console_connection );
378 if (RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle)
380 STARTUPINFOW si;
381 GetStartupInfoW( &si );
382 init_console_std_handles( !(si.dwFlags & STARTF_USESTDHANDLES) );
384 else ret = FALSE;
387 if (!ret) FreeConsole();
388 RtlLeaveCriticalSection( &console_section );
389 return ret;
393 static BOOL alloc_console( BOOL headless )
395 SECURITY_ATTRIBUTES inheritable_attr = { sizeof(inheritable_attr), NULL, TRUE };
396 STARTUPINFOW app_si, console_si;
397 HANDLE server, console = NULL;
398 WCHAR buffer[1024], cmd[256], conhost_path[MAX_PATH];
399 PROCESS_INFORMATION pi;
400 void *redir;
401 BOOL ret;
403 TRACE("()\n");
405 RtlEnterCriticalSection( &console_section );
407 if (RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle)
409 /* we already have a console opened on this process, don't create a new one */
410 RtlLeaveCriticalSection( &console_section );
411 SetLastError( ERROR_ACCESS_DENIED );
412 return FALSE;
415 if (!(server = create_console_server()) || !(console = create_console_reference( server ))) goto error;
417 GetStartupInfoW(&app_si);
419 memset(&console_si, 0, sizeof(console_si));
420 console_si.cb = sizeof(console_si);
421 /* setup a view arguments for conhost (it'll use them as default values) */
422 if (app_si.dwFlags & STARTF_USECOUNTCHARS)
424 console_si.dwFlags |= STARTF_USECOUNTCHARS;
425 console_si.dwXCountChars = app_si.dwXCountChars;
426 console_si.dwYCountChars = app_si.dwYCountChars;
428 if (app_si.dwFlags & STARTF_USEFILLATTRIBUTE)
430 console_si.dwFlags |= STARTF_USEFILLATTRIBUTE;
431 console_si.dwFillAttribute = app_si.dwFillAttribute;
433 if (app_si.dwFlags & STARTF_USESHOWWINDOW)
435 console_si.dwFlags |= STARTF_USESHOWWINDOW;
436 console_si.wShowWindow = app_si.wShowWindow;
438 if (app_si.lpTitle)
439 console_si.lpTitle = app_si.lpTitle;
440 else if (GetModuleFileNameW(0, buffer, ARRAY_SIZE(buffer)))
442 buffer[ARRAY_SIZE(buffer) - 1] = 0;
443 console_si.lpTitle = buffer;
446 swprintf( conhost_path, ARRAY_SIZE(conhost_path), L"%s\\conhost.exe", system_dir );
447 swprintf( cmd, ARRAY_SIZE(cmd), L"\"%s\" --server 0x%x", conhost_path, condrv_handle( server ));
448 if (headless) wcscat( cmd, L" --headless" );
449 Wow64DisableWow64FsRedirection( &redir );
450 ret = CreateProcessW( conhost_path, cmd, NULL, NULL, TRUE, DETACHED_PROCESS, NULL, NULL, &console_si, &pi );
451 Wow64RevertWow64FsRedirection( redir );
453 if (!ret || !create_console_connection( console)) goto error;
454 if (!init_console_std_handles( !(app_si.dwFlags & STARTF_USESTDHANDLES) )) goto error;
456 RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle = console;
457 TRACE( "Started conhost pid=%08lx tid=%08lx\n", pi.dwProcessId, pi.dwThreadId );
459 CloseHandle( server );
460 RtlLeaveCriticalSection( &console_section );
461 SetLastError( ERROR_SUCCESS );
462 return TRUE;
464 error:
465 ERR("Can't allocate console\n");
466 NtClose( console );
467 NtClose( server );
468 FreeConsole();
469 RtlLeaveCriticalSection( &console_section );
470 return FALSE;
474 /******************************************************************
475 * AllocConsole (kernelbase.@)
477 BOOL WINAPI AllocConsole(void)
479 return alloc_console( FALSE );
483 /******************************************************************************
484 * CreateConsoleScreenBuffer (kernelbase.@)
486 HANDLE WINAPI DECLSPEC_HOTPATCH CreateConsoleScreenBuffer( DWORD access, DWORD share,
487 SECURITY_ATTRIBUTES *sa, DWORD flags,
488 void *data )
490 OBJECT_ATTRIBUTES attr = {sizeof(attr)};
491 IO_STATUS_BLOCK iosb;
492 UNICODE_STRING name = RTL_CONSTANT_STRING( L"\\Device\\ConDrv\\ScreenBuffer" );
493 HANDLE handle;
494 NTSTATUS status;
496 TRACE( "(%lx,%lx,%p,%lx,%p)\n", access, share, sa, flags, data );
498 if (flags != CONSOLE_TEXTMODE_BUFFER || data)
500 SetLastError( ERROR_INVALID_PARAMETER );
501 return INVALID_HANDLE_VALUE;
504 attr.ObjectName = &name;
505 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
506 if (sa && sa->bInheritHandle) attr.Attributes |= OBJ_INHERIT;
507 status = NtCreateFile( &handle, access, &attr, &iosb, NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_OPEN,
508 FILE_NON_DIRECTORY_FILE, NULL, 0 );
509 return set_ntstatus( status ) ? handle : INVALID_HANDLE_VALUE;
513 /******************************************************************************
514 * CtrlRoutine (kernelbase.@)
516 DWORD WINAPI CtrlRoutine( void *arg )
518 DWORD_PTR event = (DWORD_PTR)arg;
519 struct ctrl_handler *handler;
521 if (event == CTRL_C_EVENT)
523 BOOL caught_by_dbg = TRUE;
524 /* First, try to pass the ctrl-C event to the debugger (if any)
525 * If it continues, there's nothing more to do
526 * Otherwise, we need to send the ctrl-C event to the handlers
528 __TRY
530 RaiseException( DBG_CONTROL_C, 0, 0, NULL );
532 __EXCEPT_ALL
534 caught_by_dbg = FALSE;
536 __ENDTRY
537 if (caught_by_dbg) return 0;
540 if (NtCurrentTeb()->Peb->ProcessParameters->ConsoleFlags & 1) return 0;
542 RtlEnterCriticalSection( &console_section );
543 for (handler = ctrl_handlers; handler; handler = handler->next)
545 if (handler->func( event )) break;
547 RtlLeaveCriticalSection( &console_section );
548 return 1;
552 /******************************************************************
553 * ExpungeConsoleCommandHistoryA (kernelbase.@)
555 void WINAPI ExpungeConsoleCommandHistoryA( LPCSTR exename )
557 FIXME( ": (%s) stub!\n", debugstr_a(exename) );
558 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
562 /******************************************************************
563 * ExpungeConsoleCommandHistoryW (kernelbase.@)
565 void WINAPI ExpungeConsoleCommandHistoryW( LPCWSTR exename )
567 FIXME( ": (%s) stub!\n", debugstr_w(exename) );
568 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
572 /******************************************************************************
573 * FillConsoleOutputAttribute (kernelbase.@)
575 BOOL WINAPI DECLSPEC_HOTPATCH FillConsoleOutputAttribute( HANDLE handle, WORD attr, DWORD length,
576 COORD coord, DWORD *written )
578 struct condrv_fill_output_params params;
580 TRACE( "(%p,%d,%ld,(%dx%d),%p)\n", handle, attr, length, coord.X, coord.Y, written );
582 if (!written)
584 SetLastError( ERROR_INVALID_ACCESS );
585 return FALSE;
588 *written = 0;
590 params.mode = CHAR_INFO_MODE_ATTR;
591 params.x = coord.X;
592 params.y = coord.Y;
593 params.count = length;
594 params.wrap = TRUE;
595 params.ch = 0;
596 params.attr = attr;
597 return console_ioctl( handle, IOCTL_CONDRV_FILL_OUTPUT, &params, sizeof(params),
598 written, sizeof(*written), NULL );
602 /******************************************************************************
603 * FillConsoleOutputCharacterA (kernelbase.@)
605 BOOL WINAPI DECLSPEC_HOTPATCH FillConsoleOutputCharacterA( HANDLE handle, CHAR ch, DWORD length,
606 COORD coord, DWORD *written )
608 WCHAR wch;
610 MultiByteToWideChar( GetConsoleOutputCP(), 0, &ch, 1, &wch, 1 );
611 return FillConsoleOutputCharacterW( handle, wch, length, coord, written );
615 /******************************************************************************
616 * FillConsoleOutputCharacterW (kernelbase.@)
618 BOOL WINAPI DECLSPEC_HOTPATCH FillConsoleOutputCharacterW( HANDLE handle, WCHAR ch, DWORD length,
619 COORD coord, DWORD *written )
621 struct condrv_fill_output_params params;
623 TRACE( "(%p,%s,%ld,(%dx%d),%p)\n", handle, debugstr_wn(&ch, 1), length, coord.X, coord.Y, written );
625 if (!written)
627 SetLastError( ERROR_INVALID_ACCESS );
628 return FALSE;
631 *written = 0;
633 params.mode = CHAR_INFO_MODE_TEXT;
634 params.x = coord.X;
635 params.y = coord.Y;
636 params.count = length;
637 params.wrap = TRUE;
638 params.ch = ch;
639 params.attr = 0;
640 return console_ioctl( handle, IOCTL_CONDRV_FILL_OUTPUT, &params, sizeof(params),
641 written, sizeof(*written), NULL );
645 /***********************************************************************
646 * FreeConsole (kernelbase.@)
648 BOOL WINAPI DECLSPEC_HOTPATCH FreeConsole(void)
650 RtlEnterCriticalSection( &console_section );
652 if (RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle != CONSOLE_HANDLE_SHELL_NO_WINDOW)
654 NtClose( console_connection );
655 console_connection = NULL;
657 NtClose( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle );
659 RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle = NULL;
661 if (console_flags & CONSOLE_INPUT_HANDLE) NtClose( GetStdHandle( STD_INPUT_HANDLE ));
662 if (console_flags & CONSOLE_OUTPUT_HANDLE) NtClose( GetStdHandle( STD_OUTPUT_HANDLE ));
663 if (console_flags & CONSOLE_ERROR_HANDLE) NtClose( GetStdHandle( STD_ERROR_HANDLE ));
664 console_flags = 0;
666 RtlLeaveCriticalSection( &console_section );
667 return TRUE;
671 /******************************************************************************
672 * GenerateConsoleCtrlEvent (kernelbase.@)
674 BOOL WINAPI DECLSPEC_HOTPATCH GenerateConsoleCtrlEvent( DWORD event, DWORD group )
676 struct condrv_ctrl_event ctrl_event;
678 TRACE( "(%ld, %lx)\n", event, group );
680 if (event != CTRL_C_EVENT && event != CTRL_BREAK_EVENT)
682 ERR( "Invalid event %ld for PGID %lx\n", event, group );
683 return FALSE;
686 ctrl_event.event = event;
687 ctrl_event.group_id = group;
688 return console_ioctl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle,
689 IOCTL_CONDRV_CTRL_EVENT, &ctrl_event, sizeof(ctrl_event), NULL, 0, NULL );
693 /******************************************************************
694 * GetConsoleAliasA (kernelbase.@)
696 DWORD WINAPI GetConsoleAliasA( LPSTR source, LPSTR buffer, DWORD len, LPSTR exename )
698 FIXME( "(%s,%p,%ld,%s): stub\n", debugstr_a(source), buffer, len, debugstr_a(exename) );
699 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
700 return 0;
704 /******************************************************************
705 * GetConsoleAliasW (kernelbase.@)
707 DWORD WINAPI GetConsoleAliasW( LPWSTR source, LPWSTR buffer, DWORD len, LPWSTR exename )
709 FIXME( "(%s,%p,%ld,%s): stub\n", debugstr_w(source), buffer, len, debugstr_w(exename) );
710 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
711 return 0;
715 /******************************************************************
716 * GetConsoleAliasExesLengthA (kernelbase.@)
718 DWORD WINAPI GetConsoleAliasExesLengthA(void)
720 FIXME( ": stub\n" );
721 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
722 return 0;
726 /******************************************************************
727 * GetConsoleAliasExesLengthW (kernelbase.@)
729 DWORD WINAPI GetConsoleAliasExesLengthW(void)
731 FIXME( ": stub\n" );
732 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
733 return 0;
737 /******************************************************************
738 * GetConsoleAliasesLengthA (kernelbase.@)
740 DWORD WINAPI GetConsoleAliasesLengthA( LPSTR unknown )
742 FIXME( ": (%s) stub!\n", debugstr_a(unknown) );
743 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
744 return 0;
748 /******************************************************************
749 * GetConsoleAliasesLengthW (kernelbase.@)
751 DWORD WINAPI GetConsoleAliasesLengthW( LPWSTR unknown )
753 FIXME( ": (%s) stub!\n", debugstr_w(unknown) );
754 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
755 return 0;
759 /******************************************************************
760 * GetConsoleCommandHistoryA (kernelbase.@)
762 DWORD WINAPI GetConsoleCommandHistoryA( LPSTR buffer, DWORD len, LPCSTR exename )
764 FIXME( ": (%p, 0x%lx, %s) stub\n", buffer, len, debugstr_a(exename) );
765 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
766 return 0;
770 /******************************************************************
771 * GetConsoleCommandHistoryW (kernelbase.@)
773 DWORD WINAPI GetConsoleCommandHistoryW( LPWSTR buffer, DWORD len, LPCWSTR exename )
775 FIXME( ": (%p, 0x%lx, %s) stub\n", buffer, len, debugstr_w(exename) );
776 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
777 return 0;
781 /******************************************************************
782 * GetConsoleCommandHistoryLengthA (kernelbase.@)
784 DWORD WINAPI GetConsoleCommandHistoryLengthA( LPCSTR exename )
786 FIXME( ": (%s) stub!\n", debugstr_a(exename) );
787 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
788 return 0;
792 /******************************************************************
793 * GetConsoleCommandHistoryLengthW (kernelbase.@)
795 DWORD WINAPI GetConsoleCommandHistoryLengthW( LPCWSTR exename )
797 FIXME( ": (%s) stub!\n", debugstr_w(exename) );
798 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
799 return 0;
803 /******************************************************************************
804 * GetConsoleCP (kernelbase.@)
806 UINT WINAPI DECLSPEC_HOTPATCH GetConsoleCP(void)
808 struct condrv_input_info info;
810 if (!console_ioctl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle,
811 IOCTL_CONDRV_GET_INPUT_INFO, NULL, 0, &info, sizeof(info), NULL ))
812 return 0;
813 return info.input_cp;
817 /******************************************************************************
818 * GetConsoleCursorInfo (kernelbase.@)
820 BOOL WINAPI DECLSPEC_HOTPATCH GetConsoleCursorInfo( HANDLE handle, CONSOLE_CURSOR_INFO *info )
822 struct condrv_output_info condrv_info;
824 if (!console_ioctl( handle, IOCTL_CONDRV_GET_OUTPUT_INFO, NULL, 0, &condrv_info, sizeof(condrv_info), NULL ))
825 return FALSE;
827 if (!info)
829 SetLastError( ERROR_INVALID_ACCESS );
830 return FALSE;
833 info->dwSize = condrv_info.cursor_size;
834 info->bVisible = condrv_info.cursor_visible;
835 TRACE("(%p) returning (%ld,%d)\n", handle, info->dwSize, info->bVisible);
836 return TRUE;
840 /***********************************************************************
841 * GetConsoleDisplayMode (kernelbase.@)
843 BOOL WINAPI GetConsoleDisplayMode( LPDWORD flags )
845 TRACE( "semi-stub: %p\n", flags );
846 /* It is safe to successfully report windowed mode */
847 *flags = 0;
848 return TRUE;
852 /***********************************************************************
853 * GetConsoleFontSize (kernelbase.@)
855 #if defined(__i386__) && !defined(__MINGW32__) && !defined(_MSC_VER)
856 #undef GetConsoleFontSize
857 DWORD WINAPI GetConsoleFontSize( HANDLE handle, DWORD index )
859 union {
860 COORD c;
861 DWORD w;
862 } x;
864 x.c = get_console_font_size( handle, index );
865 return x.w;
867 #else
868 COORD WINAPI GetConsoleFontSize( HANDLE handle, DWORD index )
870 return get_console_font_size( handle, index );
872 #endif /* !defined(__i386__) */
875 /***********************************************************************
876 * GetConsoleInputExeNameA (kernelbase.@)
878 BOOL WINAPI DECLSPEC_HOTPATCH GetConsoleInputExeNameA( DWORD len, LPSTR buffer )
880 RtlEnterCriticalSection( &console_section );
881 if (WideCharToMultiByte( CP_ACP, 0, input_exe, -1, NULL, 0, NULL, NULL ) <= len)
882 WideCharToMultiByte( CP_ACP, 0, input_exe, -1, buffer, len, NULL, NULL );
883 else SetLastError(ERROR_BUFFER_OVERFLOW);
884 RtlLeaveCriticalSection( &console_section );
885 return TRUE;
889 /***********************************************************************
890 * GetConsoleInputExeNameW (kernelbase.@)
892 BOOL WINAPI DECLSPEC_HOTPATCH GetConsoleInputExeNameW( DWORD len, LPWSTR buffer )
894 RtlEnterCriticalSection( &console_section );
895 if (len > lstrlenW(input_exe)) lstrcpyW( buffer, input_exe );
896 else SetLastError( ERROR_BUFFER_OVERFLOW );
897 RtlLeaveCriticalSection( &console_section );
898 return TRUE;
902 /***********************************************************************
903 * GetConsoleMode (kernelbase.@)
905 BOOL WINAPI DECLSPEC_HOTPATCH GetConsoleMode( HANDLE handle, DWORD *mode )
907 return console_ioctl( handle, IOCTL_CONDRV_GET_MODE, NULL, 0, mode, sizeof(*mode), NULL );
911 /***********************************************************************
912 * GetConsoleOriginalTitleA (kernelbase.@)
914 DWORD WINAPI DECLSPEC_HOTPATCH GetConsoleOriginalTitleA( LPSTR title, DWORD size )
916 WCHAR *ptr = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) );
917 DWORD ret;
919 if (!ptr) return 0;
921 ret = GetConsoleOriginalTitleW( ptr, size );
922 if (ret)
923 WideCharToMultiByte( GetConsoleOutputCP(), 0, ptr, -1, title, size, NULL, NULL);
925 HeapFree( GetProcessHeap(), 0, ptr );
926 return ret;
930 /***********************************************************************
931 * GetConsoleOriginalTitleW (kernelbase.@)
933 DWORD WINAPI DECLSPEC_HOTPATCH GetConsoleOriginalTitleW( LPWSTR title, DWORD size )
935 return get_console_title( title, size, FALSE );
939 /***********************************************************************
940 * GetConsoleOutputCP (kernelbase.@)
942 UINT WINAPI DECLSPEC_HOTPATCH GetConsoleOutputCP(void)
944 struct condrv_input_info info;
946 if (!console_ioctl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle,
947 IOCTL_CONDRV_GET_INPUT_INFO, NULL, 0, &info, sizeof(info), NULL ))
948 return 0;
949 return info.output_cp;
953 /***********************************************************************
954 * GetConsoleProcessList (kernelbase.@)
956 DWORD WINAPI GetConsoleProcessList( DWORD *list, DWORD count )
958 DWORD saved;
959 NTSTATUS status;
960 IO_STATUS_BLOCK io;
962 TRACE( "(%p,%ld)\n", list, count);
964 if (!list || count < 1)
966 SetLastError(ERROR_INVALID_PARAMETER);
967 return 0;
970 saved = *list;
971 status = NtDeviceIoControlFile( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle,
972 NULL, NULL, NULL, &io, IOCTL_CONDRV_GET_PROCESS_LIST,
973 NULL, 0, list, count * sizeof(DWORD) );
975 if (!status) return io.Information / sizeof(DWORD);
976 if (status == STATUS_BUFFER_TOO_SMALL)
978 DWORD ret = *list;
979 *list = saved;
980 return ret;
983 *list = saved;
984 set_ntstatus( status );
985 return 0;
989 /***********************************************************************
990 * GetConsoleScreenBufferInfo (kernelbase.@)
992 BOOL WINAPI DECLSPEC_HOTPATCH GetConsoleScreenBufferInfo( HANDLE handle, CONSOLE_SCREEN_BUFFER_INFO *info )
994 struct condrv_output_info condrv_info;
996 if (!console_ioctl( handle , IOCTL_CONDRV_GET_OUTPUT_INFO, NULL, 0,
997 &condrv_info, sizeof(condrv_info), NULL ))
998 return FALSE;
1000 info->dwSize.X = condrv_info.width;
1001 info->dwSize.Y = condrv_info.height;
1002 info->dwCursorPosition.X = condrv_info.cursor_x;
1003 info->dwCursorPosition.Y = condrv_info.cursor_y;
1004 info->wAttributes = condrv_info.attr;
1005 info->srWindow.Left = condrv_info.win_left;
1006 info->srWindow.Right = condrv_info.win_right;
1007 info->srWindow.Top = condrv_info.win_top;
1008 info->srWindow.Bottom = condrv_info.win_bottom;
1009 info->dwMaximumWindowSize.X = min(condrv_info.width, condrv_info.max_width);
1010 info->dwMaximumWindowSize.Y = min(condrv_info.height, condrv_info.max_height);
1012 TRACE( "(%p,(%d,%d) (%d,%d) %d (%d,%d-%d,%d) (%d,%d)\n", handle,
1013 info->dwSize.X, info->dwSize.Y, info->dwCursorPosition.X, info->dwCursorPosition.Y,
1014 info->wAttributes, info->srWindow.Left, info->srWindow.Top, info->srWindow.Right,
1015 info->srWindow.Bottom, info->dwMaximumWindowSize.X, info->dwMaximumWindowSize.Y );
1016 return TRUE;
1020 /***********************************************************************
1021 * GetConsoleScreenBufferInfoEx (kernelbase.@)
1023 BOOL WINAPI DECLSPEC_HOTPATCH GetConsoleScreenBufferInfoEx( HANDLE handle,
1024 CONSOLE_SCREEN_BUFFER_INFOEX *info )
1026 struct condrv_output_info condrv_info;
1028 if (info->cbSize != sizeof(CONSOLE_SCREEN_BUFFER_INFOEX))
1030 SetLastError( ERROR_INVALID_PARAMETER );
1031 return FALSE;
1034 if (!console_ioctl( handle, IOCTL_CONDRV_GET_OUTPUT_INFO, NULL, 0, &condrv_info,
1035 sizeof(condrv_info), NULL ))
1036 return FALSE;
1038 info->dwSize.X = condrv_info.width;
1039 info->dwSize.Y = condrv_info.height;
1040 info->dwCursorPosition.X = condrv_info.cursor_x;
1041 info->dwCursorPosition.Y = condrv_info.cursor_y;
1042 info->wAttributes = condrv_info.attr;
1043 info->srWindow.Left = condrv_info.win_left;
1044 info->srWindow.Top = condrv_info.win_top;
1045 info->srWindow.Right = condrv_info.win_right;
1046 info->srWindow.Bottom = condrv_info.win_bottom;
1047 info->dwMaximumWindowSize.X = min( condrv_info.width, condrv_info.max_width );
1048 info->dwMaximumWindowSize.Y = min( condrv_info.height, condrv_info.max_height );
1049 info->wPopupAttributes = condrv_info.popup_attr;
1050 info->bFullscreenSupported = FALSE;
1051 memcpy( info->ColorTable, condrv_info.color_map, sizeof(info->ColorTable) );
1052 return TRUE;
1056 /******************************************************************************
1057 * GetConsoleTitleA (kernelbase.@)
1059 DWORD WINAPI DECLSPEC_HOTPATCH GetConsoleTitleA( LPSTR title, DWORD size )
1061 WCHAR *ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR) * size );
1062 DWORD ret;
1064 if (!ptr) return 0;
1066 ret = GetConsoleTitleW( ptr, size );
1067 if (ret)
1068 WideCharToMultiByte( GetConsoleOutputCP(), 0, ptr, -1, title, size, NULL, NULL);
1070 HeapFree( GetProcessHeap(), 0, ptr );
1071 return ret;
1075 /******************************************************************************
1076 * GetConsoleTitleW (kernelbase.@)
1078 DWORD WINAPI DECLSPEC_HOTPATCH GetConsoleTitleW( LPWSTR title, DWORD size )
1080 return get_console_title( title, size, TRUE );
1084 /******************************************************************************
1085 * GetConsoleWindow (kernelbase.@)
1087 HWND WINAPI GetConsoleWindow(void)
1089 condrv_handle_t win;
1090 BOOL ret;
1092 ret = DeviceIoControl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle,
1093 IOCTL_CONDRV_GET_WINDOW, NULL, 0, &win, sizeof(win), NULL, NULL );
1094 return ret ? LongToHandle( win ) : NULL;
1098 /***********************************************************************
1099 * GetCurrentConsoleFontEx (kernelbase.@)
1101 BOOL WINAPI GetCurrentConsoleFontEx( HANDLE handle, BOOL maxwindow, CONSOLE_FONT_INFOEX *info )
1103 DWORD size;
1104 struct
1106 struct condrv_output_info info;
1107 WCHAR face_name[LF_FACESIZE - 1];
1108 } data;
1110 if (info->cbSize != sizeof(CONSOLE_FONT_INFOEX))
1112 SetLastError(ERROR_INVALID_PARAMETER);
1113 return FALSE;
1116 if (!DeviceIoControl( handle, IOCTL_CONDRV_GET_OUTPUT_INFO, NULL, 0,
1117 &data, sizeof(data), &size, NULL ))
1119 SetLastError( ERROR_INVALID_HANDLE );
1120 return FALSE;
1123 info->nFont = 0;
1124 if (maxwindow)
1126 info->dwFontSize.X = min( data.info.width, data.info.max_width );
1127 info->dwFontSize.Y = min( data.info.height, data.info.max_height );
1129 else
1131 info->dwFontSize.X = data.info.font_width;
1132 info->dwFontSize.Y = data.info.font_height;
1134 size -= sizeof(data.info);
1135 if (size) memcpy( info->FaceName, data.face_name, size );
1136 info->FaceName[size / sizeof(WCHAR)] = 0;
1137 info->FontFamily = data.info.font_pitch_family;
1138 info->FontWeight = data.info.font_weight;
1139 return TRUE;
1143 /***********************************************************************
1144 * GetCurrentConsoleFont (kernelbase.@)
1146 BOOL WINAPI GetCurrentConsoleFont( HANDLE handle, BOOL maxwindow, CONSOLE_FONT_INFO *info )
1148 BOOL ret;
1149 CONSOLE_FONT_INFOEX res;
1151 res.cbSize = sizeof(CONSOLE_FONT_INFOEX);
1153 ret = GetCurrentConsoleFontEx( handle, maxwindow, &res );
1154 if (ret)
1156 info->nFont = res.nFont;
1157 info->dwFontSize.X = res.dwFontSize.X;
1158 info->dwFontSize.Y = res.dwFontSize.Y;
1160 return ret;
1164 /***********************************************************************
1165 * GetLargestConsoleWindowSize (kernelbase.@)
1167 #if defined(__i386__) && !defined(__MINGW32__) && !defined(_MSC_VER)
1168 #undef GetLargestConsoleWindowSize
1169 DWORD WINAPI DECLSPEC_HOTPATCH GetLargestConsoleWindowSize( HANDLE handle )
1171 union {
1172 COORD c;
1173 DWORD w;
1174 } x;
1175 x.c = get_largest_console_window_size( handle );
1176 return x.w;
1179 #else
1181 COORD WINAPI DECLSPEC_HOTPATCH GetLargestConsoleWindowSize( HANDLE handle )
1183 return get_largest_console_window_size( handle );
1186 #endif /* !defined(__i386__) */
1189 /***********************************************************************
1190 * GetNumberOfConsoleInputEvents (kernelbase.@)
1192 BOOL WINAPI DECLSPEC_HOTPATCH GetNumberOfConsoleInputEvents( HANDLE handle, DWORD *count )
1194 struct condrv_input_info info;
1195 if (!console_ioctl( handle, IOCTL_CONDRV_GET_INPUT_INFO, NULL, 0, &info, sizeof(info), NULL ))
1196 return FALSE;
1197 *count = info.input_count;
1198 return TRUE;
1202 /***********************************************************************
1203 * GetNumberOfConsoleMouseButtons (kernelbase.@)
1205 BOOL WINAPI GetNumberOfConsoleMouseButtons( DWORD *count )
1207 FIXME( "(%p): stub\n", count );
1208 *count = 2;
1209 return TRUE;
1213 /***********************************************************************
1214 * PeekConsoleInputA (kernelbase.@)
1216 BOOL WINAPI DECLSPEC_HOTPATCH PeekConsoleInputA( HANDLE handle, INPUT_RECORD *buffer,
1217 DWORD length, DWORD *count )
1219 DWORD read;
1221 if (!PeekConsoleInputW( handle, buffer, length, &read )) return FALSE;
1222 input_records_WtoA( buffer, read );
1223 if (count) *count = read;
1224 return TRUE;
1228 /***********************************************************************
1229 * PeekConsoleInputW (kernelbase.@)
1231 BOOL WINAPI DECLSPEC_HOTPATCH PeekConsoleInputW( HANDLE handle, INPUT_RECORD *buffer,
1232 DWORD length, DWORD *count )
1234 DWORD read;
1235 if (!console_ioctl( handle, IOCTL_CONDRV_PEEK, NULL, 0, buffer, length * sizeof(*buffer), &read ))
1236 return FALSE;
1237 if (count) *count = read / sizeof(*buffer);
1238 return TRUE;
1242 /******************************************************************************
1243 * ReadConsoleOutputAttribute (kernelbase.@)
1245 BOOL WINAPI DECLSPEC_HOTPATCH ReadConsoleOutputAttribute( HANDLE handle, WORD *attr, DWORD length,
1246 COORD coord, DWORD *count )
1248 struct condrv_output_params params;
1249 BOOL ret;
1251 TRACE( "(%p,%p,%ld,%dx%d,%p)\n", handle, attr, length, coord.X, coord.Y, count );
1253 if (!count)
1255 SetLastError( ERROR_INVALID_ACCESS );
1256 return FALSE;
1259 params.mode = CHAR_INFO_MODE_ATTR;
1260 params.x = coord.X;
1261 params.y = coord.Y;
1262 params.width = 0;
1263 ret = console_ioctl( handle, IOCTL_CONDRV_READ_OUTPUT, &params, sizeof(params),
1264 attr, length * sizeof(*attr), count );
1265 *count /= sizeof(*attr);
1266 return ret;
1270 /******************************************************************************
1271 * ReadConsoleOutputCharacterA (kernelbase.@)
1273 BOOL WINAPI DECLSPEC_HOTPATCH ReadConsoleOutputCharacterA( HANDLE handle, LPSTR buffer, DWORD length,
1274 COORD coord, DWORD *count )
1276 DWORD read;
1277 BOOL ret;
1278 LPWSTR wptr;
1280 if (!count)
1282 SetLastError( ERROR_INVALID_ACCESS );
1283 return FALSE;
1286 *count = 0;
1287 if (!(wptr = HeapAlloc( GetProcessHeap(), 0, length * sizeof(WCHAR) )))
1289 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1290 return FALSE;
1292 if ((ret = ReadConsoleOutputCharacterW( handle, wptr, length, coord, &read )))
1294 read = WideCharToMultiByte( GetConsoleOutputCP(), 0, wptr, read, buffer, length, NULL, NULL);
1295 *count = read;
1297 HeapFree( GetProcessHeap(), 0, wptr );
1298 return ret;
1302 /******************************************************************************
1303 * ReadConsoleOutputCharacterW (kernelbase.@)
1305 BOOL WINAPI DECLSPEC_HOTPATCH ReadConsoleOutputCharacterW( HANDLE handle, LPWSTR buffer, DWORD length,
1306 COORD coord, DWORD *count )
1308 struct condrv_output_params params;
1309 BOOL ret;
1311 TRACE( "(%p,%p,%ld,%dx%d,%p)\n", handle, buffer, length, coord.X, coord.Y, count );
1313 if (!count)
1315 SetLastError( ERROR_INVALID_ACCESS );
1316 return FALSE;
1319 params.mode = CHAR_INFO_MODE_TEXT;
1320 params.x = coord.X;
1321 params.y = coord.Y;
1322 params.width = 0;
1323 ret = console_ioctl( handle, IOCTL_CONDRV_READ_OUTPUT, &params, sizeof(params), buffer,
1324 length * sizeof(*buffer), count );
1325 *count /= sizeof(*buffer);
1326 return ret;
1330 /******************************************************************************
1331 * ReadConsoleOutputA (kernelbase.@)
1333 BOOL WINAPI DECLSPEC_HOTPATCH ReadConsoleOutputA( HANDLE handle, CHAR_INFO *buffer, COORD size,
1334 COORD coord, SMALL_RECT *region )
1336 BOOL ret;
1337 int y;
1339 ret = ReadConsoleOutputW( handle, buffer, size, coord, region );
1340 if (ret && region->Right >= region->Left)
1342 UINT cp = GetConsoleOutputCP();
1343 for (y = 0; y <= region->Bottom - region->Top; y++)
1344 char_info_WtoA( cp, &buffer[(coord.Y + y) * size.X + coord.X], region->Right - region->Left + 1 );
1346 return ret;
1350 /******************************************************************************
1351 * ReadConsoleOutputW (kernelbase.@)
1353 BOOL WINAPI DECLSPEC_HOTPATCH ReadConsoleOutputW( HANDLE handle, CHAR_INFO *buffer, COORD size,
1354 COORD coord, SMALL_RECT *region )
1356 struct condrv_output_params params;
1357 unsigned int width, height, y;
1358 SMALL_RECT *result;
1359 DWORD count;
1360 BOOL ret;
1362 if (region->Left > region->Right || region->Top > region->Bottom)
1364 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1365 return FALSE;
1367 if (size.X <= coord.X || size.Y <= coord.Y)
1369 region->Right = region->Left - 1;
1370 region->Bottom = region->Top - 1;
1371 SetLastError( ERROR_INVALID_FUNCTION );
1372 return FALSE;
1374 width = min( region->Right - region->Left + 1, size.X - coord.X );
1375 height = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
1376 region->Right = region->Left + width - 1;
1377 region->Bottom = region->Top + height - 1;
1379 count = sizeof(*result) + width * height * sizeof(*buffer);
1380 if (!(result = HeapAlloc( GetProcessHeap(), 0, count )))
1382 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1383 return FALSE;
1386 params.mode = CHAR_INFO_MODE_TEXTATTR;
1387 params.x = region->Left;
1388 params.y = region->Top;
1389 params.width = width;
1390 if ((ret = console_ioctl( handle, IOCTL_CONDRV_READ_OUTPUT, &params, sizeof(params), result, count, &count )) && count)
1392 CHAR_INFO *char_info = (CHAR_INFO *)(result + 1);
1393 *region = *result;
1394 width = region->Right - region->Left + 1;
1395 height = region->Bottom - region->Top + 1;
1396 for (y = 0; y < height; y++)
1397 memcpy( &buffer[(y + coord.Y) * size.X + coord.X], &char_info[y * width], width * sizeof(*buffer) );
1399 HeapFree( GetProcessHeap(), 0, result );
1400 return ret;
1404 /******************************************************************************
1405 * ScrollConsoleScreenBufferA (kernelbase.@)
1407 BOOL WINAPI DECLSPEC_HOTPATCH ScrollConsoleScreenBufferA( HANDLE handle, const SMALL_RECT *scroll,
1408 const SMALL_RECT *clip, COORD origin, const CHAR_INFO *fill )
1410 CHAR_INFO ciW;
1412 ciW.Attributes = fill->Attributes;
1413 MultiByteToWideChar( GetConsoleOutputCP(), 0, &fill->Char.AsciiChar, 1, &ciW.Char.UnicodeChar, 1 );
1415 return ScrollConsoleScreenBufferW( handle, scroll, clip, origin, &ciW );
1419 /******************************************************************************
1420 * ScrollConsoleScreenBufferW (kernelbase.@)
1422 BOOL WINAPI DECLSPEC_HOTPATCH ScrollConsoleScreenBufferW( HANDLE handle, const SMALL_RECT *scroll,
1423 const SMALL_RECT *clip_rect, COORD origin,
1424 const CHAR_INFO *fill )
1426 struct condrv_scroll_params params;
1428 if (clip_rect)
1429 TRACE( "(%p,(%d,%d-%d,%d),(%d,%d-%d,%d),%d-%d,%p)\n", handle,
1430 scroll->Left, scroll->Top, scroll->Right, scroll->Bottom,
1431 clip_rect->Left, clip_rect->Top, clip_rect->Right, clip_rect->Bottom,
1432 origin.X, origin.Y, fill );
1433 else
1434 TRACE("(%p,(%d,%d-%d,%d),(nil),%d-%d,%p)\n", handle,
1435 scroll->Left, scroll->Top, scroll->Right, scroll->Bottom,
1436 origin.X, origin.Y, fill );
1438 params.scroll = *scroll;
1439 params.origin = origin;
1440 params.fill.ch = fill->Char.UnicodeChar;
1441 params.fill.attr = fill->Attributes;
1442 if (!clip_rect)
1444 params.clip.Left = params.clip.Top = 0;
1445 params.clip.Right = params.clip.Bottom = SHRT_MAX;
1447 else params.clip = *clip_rect;
1448 return console_ioctl( handle, IOCTL_CONDRV_SCROLL, (void *)&params, sizeof(params), NULL, 0, NULL );
1452 /******************************************************************************
1453 * SetConsoleActiveScreenBuffer (kernelbase.@)
1455 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleActiveScreenBuffer( HANDLE handle )
1457 TRACE( "(%p)\n", handle );
1458 return console_ioctl( handle, IOCTL_CONDRV_ACTIVATE, NULL, 0, NULL, 0, NULL );
1462 /******************************************************************************
1463 * SetConsoleCP (kernelbase.@)
1465 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleCP( UINT cp )
1467 struct condrv_input_info_params params = { SET_CONSOLE_INPUT_INFO_INPUT_CODEPAGE };
1469 params.info.input_cp = cp;
1470 return console_ioctl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle,
1471 IOCTL_CONDRV_SET_INPUT_INFO, &params, sizeof(params), NULL, 0, NULL );
1475 /******************************************************************************
1476 * SetConsoleCtrlHandler (kernelbase.@)
1478 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleCtrlHandler( PHANDLER_ROUTINE func, BOOL add )
1480 struct ctrl_handler *handler;
1481 BOOL ret = FALSE;
1483 TRACE( "(%p,%d)\n", func, add );
1485 RtlEnterCriticalSection( &console_section );
1487 if (!func)
1489 if (add) NtCurrentTeb()->Peb->ProcessParameters->ConsoleFlags |= 1;
1490 else NtCurrentTeb()->Peb->ProcessParameters->ConsoleFlags &= ~1;
1491 ret = TRUE;
1493 else if (add)
1495 if ((handler = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*handler) )))
1497 handler->func = func;
1498 handler->next = ctrl_handlers;
1499 ctrl_handlers = handler;
1500 ret = TRUE;
1503 else
1505 struct ctrl_handler **p_handler;
1507 for (p_handler = &ctrl_handlers; *p_handler; p_handler = &(*p_handler)->next)
1509 if ((*p_handler)->func == func) break;
1511 if (*p_handler && *p_handler != &default_handler)
1513 handler = *p_handler;
1514 *p_handler = handler->next;
1515 RtlFreeHeap( GetProcessHeap(), 0, handler );
1516 ret = TRUE;
1518 else SetLastError( ERROR_INVALID_PARAMETER );
1521 RtlLeaveCriticalSection( &console_section );
1522 return ret;
1526 /******************************************************************************
1527 * SetConsoleCursorInfo (kernelbase.@)
1529 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleCursorInfo( HANDLE handle, CONSOLE_CURSOR_INFO *info )
1531 struct condrv_output_info_params params = { SET_CONSOLE_OUTPUT_INFO_CURSOR_GEOM };
1533 TRACE( "(%p,%ld,%d)\n", handle, info->dwSize, info->bVisible);
1535 params.info.cursor_size = info->dwSize;
1536 params.info.cursor_visible = info->bVisible;
1537 return console_ioctl( handle, IOCTL_CONDRV_SET_OUTPUT_INFO, &params, sizeof(params),
1538 NULL, 0, NULL );
1542 /******************************************************************************
1543 * SetConsoleCursorPosition (kernelbase.@)
1545 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleCursorPosition( HANDLE handle, COORD pos )
1547 struct condrv_output_info_params params = { SET_CONSOLE_OUTPUT_INFO_CURSOR_POS };
1549 TRACE( "%p %d %d\n", handle, pos.X, pos.Y );
1551 params.info.cursor_x = pos.X;
1552 params.info.cursor_y = pos.Y;
1553 return console_ioctl( handle, IOCTL_CONDRV_SET_OUTPUT_INFO, &params, sizeof(params), NULL, 0, NULL );
1557 /***********************************************************************
1558 * SetConsoleDisplayMode (kernelbase.@)
1560 BOOL WINAPI SetConsoleDisplayMode( HANDLE handle, DWORD flags, COORD *size )
1562 TRACE( "(%p, %lx, (%d, %d))\n", handle, flags, size->X, size->Y );
1563 if (flags == 1)
1565 /* We cannot switch to fullscreen */
1566 return FALSE;
1568 return TRUE;
1572 /******************************************************************************
1573 * SetConsoleInputExeNameA (kernelbase.@)
1575 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleInputExeNameA( LPCSTR name )
1577 if (!name || !name[0])
1579 SetLastError( ERROR_INVALID_PARAMETER );
1580 return FALSE;
1582 RtlEnterCriticalSection( &console_section );
1583 MultiByteToWideChar( CP_ACP, 0, name, -1, input_exe, ARRAY_SIZE(input_exe) );
1584 RtlLeaveCriticalSection( &console_section );
1585 return TRUE;
1589 /******************************************************************************
1590 * SetConsoleInputExeNameW (kernelbase.@)
1592 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleInputExeNameW( LPCWSTR name )
1594 if (!name || !name[0])
1596 SetLastError( ERROR_INVALID_PARAMETER );
1597 return FALSE;
1599 RtlEnterCriticalSection( &console_section );
1600 lstrcpynW( input_exe, name, ARRAY_SIZE(input_exe) );
1601 RtlLeaveCriticalSection( &console_section );
1602 return TRUE;
1606 /******************************************************************************
1607 * SetConsoleMode (kernelbase.@)
1609 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleMode( HANDLE handle, DWORD mode )
1611 TRACE( "(%p,%lx)\n", handle, mode );
1612 return console_ioctl( handle, IOCTL_CONDRV_SET_MODE, &mode, sizeof(mode), NULL, 0, NULL );
1616 /******************************************************************************
1617 * SetConsoleOutputCP (kernelbase.@)
1619 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleOutputCP( UINT cp )
1621 struct condrv_input_info_params params = { SET_CONSOLE_INPUT_INFO_OUTPUT_CODEPAGE };
1623 params.info.output_cp = cp;
1624 return console_ioctl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle,
1625 IOCTL_CONDRV_SET_INPUT_INFO, &params, sizeof(params), NULL, 0, NULL );
1629 /******************************************************************************
1630 * SetConsoleScreenBufferInfoEx (kernelbase.@)
1632 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleScreenBufferInfoEx( HANDLE handle,
1633 CONSOLE_SCREEN_BUFFER_INFOEX *info )
1635 struct condrv_output_info_params params =
1636 { SET_CONSOLE_OUTPUT_INFO_CURSOR_POS | SET_CONSOLE_OUTPUT_INFO_SIZE |
1637 SET_CONSOLE_OUTPUT_INFO_ATTR | SET_CONSOLE_OUTPUT_INFO_POPUP_ATTR |
1638 SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW | SET_CONSOLE_OUTPUT_INFO_MAX_SIZE };
1640 TRACE("(%p, %p)\n", handle, info);
1642 if (info->cbSize != sizeof(CONSOLE_SCREEN_BUFFER_INFOEX))
1644 SetLastError(ERROR_INVALID_PARAMETER);
1645 return FALSE;
1648 params.info.width = info->dwSize.X;
1649 params.info.height = info->dwSize.Y;
1650 params.info.cursor_x = info->dwCursorPosition.X;
1651 params.info.cursor_y = info->dwCursorPosition.Y;
1652 params.info.attr = info->wAttributes;
1653 params.info.win_left = info->srWindow.Left;
1654 params.info.win_top = info->srWindow.Top;
1655 params.info.win_right = info->srWindow.Right;
1656 params.info.win_bottom = info->srWindow.Bottom;
1657 params.info.popup_attr = info->wPopupAttributes;
1658 params.info.max_width = min( info->dwMaximumWindowSize.X, info->dwSize.X );
1659 params.info.max_height = min( info->dwMaximumWindowSize.Y, info->dwSize.Y );
1660 return console_ioctl( handle, IOCTL_CONDRV_SET_OUTPUT_INFO, &params, sizeof(params), NULL, 0, NULL );
1664 /******************************************************************************
1665 * SetConsoleScreenBufferSize (kernelbase.@)
1667 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleScreenBufferSize( HANDLE handle, COORD size )
1669 struct condrv_output_info_params params = { SET_CONSOLE_OUTPUT_INFO_SIZE };
1671 TRACE( "(%p,(%d,%d))\n", handle, size.X, size.Y );
1673 params.info.width = size.X;
1674 params.info.height = size.Y;
1675 return console_ioctl( handle, IOCTL_CONDRV_SET_OUTPUT_INFO, &params, sizeof(params), NULL, 0, NULL );
1679 /******************************************************************************
1680 * SetConsoleTextAttribute (kernelbase.@)
1682 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleTextAttribute( HANDLE handle, WORD attr )
1684 struct condrv_output_info_params params = { SET_CONSOLE_OUTPUT_INFO_ATTR };
1686 TRACE( "(%p,%d)\n", handle, attr );
1688 params.info.attr = attr;
1689 return console_ioctl( handle, IOCTL_CONDRV_SET_OUTPUT_INFO, &params, sizeof(params), NULL, 0, NULL );
1693 /******************************************************************************
1694 * SetConsoleTitleA (kernelbase.@)
1696 BOOL WINAPI SetConsoleTitleA( LPCSTR title )
1698 LPWSTR titleW;
1699 BOOL ret;
1700 DWORD len = MultiByteToWideChar( GetConsoleOutputCP(), 0, title, -1, NULL, 0 );
1701 if (!(titleW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return FALSE;
1702 MultiByteToWideChar( GetConsoleOutputCP(), 0, title, -1, titleW, len );
1703 ret = SetConsoleTitleW(titleW);
1704 HeapFree( GetProcessHeap(), 0, titleW );
1705 return ret;
1709 /******************************************************************************
1710 * SetConsoleTitleW (kernelbase.@)
1712 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleTitleW( LPCWSTR title )
1714 TRACE( "%s\n", debugstr_w( title ));
1716 return console_ioctl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle, IOCTL_CONDRV_SET_TITLE,
1717 (void *)title, lstrlenW(title) * sizeof(WCHAR), NULL, 0, NULL );
1721 /******************************************************************************
1722 * SetConsoleWindowInfo (kernelbase.@)
1724 BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleWindowInfo( HANDLE handle, BOOL absolute, SMALL_RECT *window )
1726 struct condrv_output_info_params params = { SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW };
1727 SMALL_RECT rect = *window;
1729 TRACE( "(%p,%d,(%d,%d-%d,%d))\n", handle, absolute, rect.Left, rect.Top, rect.Right, rect.Bottom );
1731 if (!absolute)
1733 CONSOLE_SCREEN_BUFFER_INFO info;
1735 if (!GetConsoleScreenBufferInfo( handle, &info )) return FALSE;
1736 rect.Left += info.srWindow.Left;
1737 rect.Top += info.srWindow.Top;
1738 rect.Right += info.srWindow.Right;
1739 rect.Bottom += info.srWindow.Bottom;
1742 params.info.win_left = rect.Left;
1743 params.info.win_top = rect.Top;
1744 params.info.win_right = rect.Right;
1745 params.info.win_bottom = rect.Bottom;
1746 return console_ioctl( handle, IOCTL_CONDRV_SET_OUTPUT_INFO, &params, sizeof(params), NULL, 0, NULL );
1750 /******************************************************************************
1751 * SetCurrentConsoleFontEx (kernelbase.@)
1753 BOOL WINAPI SetCurrentConsoleFontEx( HANDLE handle, BOOL maxwindow, CONSOLE_FONT_INFOEX *info )
1755 struct
1757 struct condrv_output_info_params params;
1758 WCHAR face_name[LF_FACESIZE];
1759 } data;
1761 size_t size;
1763 TRACE( "(%p %d %p)\n", handle, maxwindow, info );
1765 if (info->cbSize != sizeof(*info))
1767 SetLastError(ERROR_INVALID_PARAMETER);
1768 return FALSE;
1771 data.params.mask = SET_CONSOLE_OUTPUT_INFO_FONT;
1773 data.params.info.font_width = info->dwFontSize.X;
1774 data.params.info.font_height = info->dwFontSize.Y;
1775 data.params.info.font_pitch_family = info->FontFamily;
1776 data.params.info.font_weight = info->FontWeight;
1778 size = wcsnlen( info->FaceName, LF_FACESIZE - 1 ) * sizeof(WCHAR);
1779 memcpy( data.face_name, info->FaceName, size );
1781 size += sizeof(struct condrv_output_info_params);
1782 return console_ioctl( handle, IOCTL_CONDRV_SET_OUTPUT_INFO, &data, size, NULL, 0, NULL );
1786 /***********************************************************************
1787 * ReadConsoleInputA (kernelbase.@)
1789 BOOL WINAPI ReadConsoleInputA( HANDLE handle, INPUT_RECORD *buffer, DWORD length, DWORD *count )
1791 DWORD read;
1793 if (!ReadConsoleInputW( handle, buffer, length, &read )) return FALSE;
1794 input_records_WtoA( buffer, read );
1795 if (count) *count = read;
1796 return TRUE;
1800 /***********************************************************************
1801 * ReadConsoleInputW (kernelbase.@)
1803 BOOL WINAPI ReadConsoleInputW( HANDLE handle, INPUT_RECORD *buffer, DWORD length, DWORD *count )
1805 if (!console_ioctl( handle, IOCTL_CONDRV_READ_INPUT, NULL, 0,
1806 buffer, length * sizeof(*buffer), count ))
1807 return FALSE;
1808 *count /= sizeof(*buffer);
1809 return TRUE;
1813 /******************************************************************************
1814 * WriteConsoleInputA (kernelbase.@)
1816 BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleInputA( HANDLE handle, const INPUT_RECORD *buffer,
1817 DWORD count, DWORD *written )
1819 INPUT_RECORD *recW = NULL;
1820 BOOL ret;
1822 if (count > 0)
1824 if (!buffer)
1826 SetLastError( ERROR_INVALID_ACCESS );
1827 return FALSE;
1829 if (!(recW = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*recW) )))
1831 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1832 return FALSE;
1834 memcpy( recW, buffer, count * sizeof(*recW) );
1835 input_records_AtoW( recW, count );
1837 ret = WriteConsoleInputW( handle, recW, count, written );
1838 HeapFree( GetProcessHeap(), 0, recW );
1839 return ret;
1843 /******************************************************************************
1844 * WriteConsoleInputW (kernelbase.@)
1846 BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleInputW( HANDLE handle, const INPUT_RECORD *buffer,
1847 DWORD count, DWORD *written )
1849 TRACE( "(%p,%p,%ld,%p)\n", handle, buffer, count, written );
1851 if (count > 0 && !buffer)
1853 SetLastError( ERROR_INVALID_ACCESS );
1854 return FALSE;
1857 if (!DeviceIoControl( handle, IOCTL_CONDRV_WRITE_INPUT, (void *)buffer, count * sizeof(*buffer), NULL, 0, NULL, NULL ))
1858 return FALSE;
1860 if (!written)
1862 SetLastError( ERROR_INVALID_ACCESS );
1863 return FALSE;
1865 *written = count;
1866 return TRUE;
1870 /***********************************************************************
1871 * WriteConsoleOutputA (kernelbase.@)
1873 BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleOutputA( HANDLE handle, const CHAR_INFO *buffer,
1874 COORD size, COORD coord, SMALL_RECT *region )
1876 int y;
1877 BOOL ret;
1878 COORD new_size, new_coord;
1879 CHAR_INFO *ciW;
1881 new_size.X = min( region->Right - region->Left + 1, size.X - coord.X );
1882 new_size.Y = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
1884 if (new_size.X <= 0 || new_size.Y <= 0)
1886 region->Bottom = region->Top + new_size.Y - 1;
1887 region->Right = region->Left + new_size.X - 1;
1888 return TRUE;
1891 /* only copy the useful rectangle */
1892 if (!(ciW = HeapAlloc( GetProcessHeap(), 0, sizeof(CHAR_INFO) * new_size.X * new_size.Y )))
1893 return FALSE;
1894 for (y = 0; y < new_size.Y; y++)
1895 memcpy( &ciW[y * new_size.X], &buffer[(y + coord.Y) * size.X + coord.X],
1896 new_size.X * sizeof(CHAR_INFO) );
1897 char_info_AtoW( ciW, new_size.X * new_size.Y );
1898 new_coord.X = new_coord.Y = 0;
1899 ret = WriteConsoleOutputW( handle, ciW, new_size, new_coord, region );
1900 HeapFree( GetProcessHeap(), 0, ciW );
1901 return ret;
1905 /***********************************************************************
1906 * WriteConsoleOutputW (kernelbase.@)
1908 BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleOutputW( HANDLE handle, const CHAR_INFO *buffer,
1909 COORD size, COORD coord, SMALL_RECT *region )
1911 struct condrv_output_params *params;
1912 unsigned int width, height, y;
1913 size_t params_size;
1914 BOOL ret;
1916 TRACE( "(%p,%p,(%d,%d),(%d,%d),(%d,%dx%d,%d)\n",
1917 handle, buffer, size.X, size.Y, coord.X, coord.Y,
1918 region->Left, region->Top, region->Right, region->Bottom );
1920 if (region->Left > region->Right || region->Top > region->Bottom || size.X <= coord.X || size.Y <= coord.Y)
1922 SetLastError( ERROR_INVALID_PARAMETER );
1923 return FALSE;
1926 width = min( region->Right - region->Left + 1, size.X - coord.X );
1927 height = min( region->Bottom - region->Top + 1, size.Y - coord.Y );
1928 region->Right = region->Left + width - 1;
1929 region->Bottom = region->Top + height - 1;
1931 params_size = sizeof(*params) + width * height * sizeof(*buffer);
1932 if (!(params = HeapAlloc( GetProcessHeap(), 0, params_size )))
1934 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1935 return FALSE;
1938 params->mode = CHAR_INFO_MODE_TEXTATTR;
1939 params->x = region->Left;
1940 params->y = region->Top;
1941 params->width = width;
1943 for (y = 0; y < height; y++)
1944 memcpy( &((CHAR_INFO *)(params + 1))[y * width], &buffer[(y + coord.Y) * size.X + coord.X], width * sizeof(CHAR_INFO) );
1946 ret = console_ioctl( handle, IOCTL_CONDRV_WRITE_OUTPUT, params, params_size, region, sizeof(*region), NULL );
1947 HeapFree( GetProcessHeap(), 0, params );
1948 return ret;
1952 /******************************************************************************
1953 * WriteConsoleOutputAttribute (kernelbase.@)
1955 BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleOutputAttribute( HANDLE handle, const WORD *attr, DWORD length,
1956 COORD coord, DWORD *written )
1958 struct condrv_output_params *params;
1959 size_t size;
1960 BOOL ret;
1962 TRACE( "(%p,%p,%ld,%dx%d,%p)\n", handle, attr, length, coord.X, coord.Y, written );
1964 if ((length > 0 && !attr) || !written)
1966 SetLastError( ERROR_INVALID_ACCESS );
1967 return FALSE;
1970 *written = 0;
1971 size = sizeof(*params) + length * sizeof(WORD);
1972 if (!(params = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
1973 params->mode = CHAR_INFO_MODE_ATTR;
1974 params->x = coord.X;
1975 params->y = coord.Y;
1976 params->width = 0;
1977 memcpy( params + 1, attr, length * sizeof(*attr) );
1978 ret = console_ioctl( handle, IOCTL_CONDRV_WRITE_OUTPUT, params, size, written, sizeof(*written), NULL );
1979 HeapFree( GetProcessHeap(), 0, params );
1980 return ret;
1984 /******************************************************************************
1985 * WriteConsoleOutputCharacterA (kernelbase.@)
1987 BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleOutputCharacterA( HANDLE handle, LPCSTR str, DWORD length,
1988 COORD coord, DWORD *written )
1990 BOOL ret;
1991 LPWSTR strW = NULL;
1992 DWORD lenW = 0;
1994 TRACE( "(%p,%s,%ld,%dx%d,%p)\n", handle, debugstr_an(str, length), length, coord.X, coord.Y, written );
1996 if (length > 0)
1998 UINT cp = GetConsoleOutputCP();
1999 if (!str)
2001 SetLastError( ERROR_INVALID_ACCESS );
2002 return FALSE;
2004 lenW = MultiByteToWideChar( cp, 0, str, length, NULL, 0 );
2006 if (!(strW = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) )))
2008 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
2009 return FALSE;
2011 MultiByteToWideChar( cp, 0, str, length, strW, lenW );
2013 ret = WriteConsoleOutputCharacterW( handle, strW, lenW, coord, written );
2014 HeapFree( GetProcessHeap(), 0, strW );
2015 return ret;
2019 /******************************************************************************
2020 * WriteConsoleOutputCharacterW (kernelbase.@)
2022 BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleOutputCharacterW( HANDLE handle, LPCWSTR str, DWORD length,
2023 COORD coord, DWORD *written )
2025 struct condrv_output_params *params;
2026 size_t size;
2027 BOOL ret;
2029 TRACE( "(%p,%s,%ld,%dx%d,%p)\n", handle, debugstr_wn(str, length), length, coord.X, coord.Y, written );
2031 if ((length > 0 && !str) || !written)
2033 SetLastError( ERROR_INVALID_ACCESS );
2034 return FALSE;
2037 *written = 0;
2038 size = sizeof(*params) + length * sizeof(WCHAR);
2039 if (!(params = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
2040 params->mode = CHAR_INFO_MODE_TEXT;
2041 params->x = coord.X;
2042 params->y = coord.Y;
2043 params->width = 0;
2044 memcpy( params + 1, str, length * sizeof(*str) );
2045 ret = console_ioctl( handle, IOCTL_CONDRV_WRITE_OUTPUT, params, size, written, sizeof(*written), NULL );
2046 HeapFree( GetProcessHeap(), 0, params );
2047 return ret;
2051 /***********************************************************************
2052 * ReadConsoleA (kernelbase.@)
2054 BOOL WINAPI ReadConsoleA( HANDLE handle, void *buffer, DWORD length, DWORD *count, void *reserved )
2056 if (length > INT_MAX)
2058 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
2059 return FALSE;
2062 return console_ioctl( handle, IOCTL_CONDRV_READ_FILE, NULL, 0, buffer, length, count );
2066 /***********************************************************************
2067 * ReadConsoleW (kernelbase.@)
2069 BOOL WINAPI ReadConsoleW( HANDLE handle, void *buffer, DWORD length, DWORD *count, void *reserved )
2071 BOOL ret;
2073 TRACE( "(%p,%p,%ld,%p,%p)\n", handle, buffer, length, count, reserved );
2075 if (length > INT_MAX)
2077 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2078 return FALSE;
2081 if (reserved)
2083 CONSOLE_READCONSOLE_CONTROL* crc = reserved;
2084 char *tmp;
2086 if (crc->nLength != sizeof(*crc) || crc->nInitialChars >= length)
2088 SetLastError( ERROR_INVALID_PARAMETER );
2089 return FALSE;
2091 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, sizeof(DWORD) + length * sizeof(WCHAR) )))
2093 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
2094 return FALSE;
2097 memcpy( tmp, &crc->dwCtrlWakeupMask, sizeof(DWORD) );
2098 memcpy( tmp + sizeof(DWORD), buffer, crc->nInitialChars * sizeof(WCHAR) );
2099 ret = console_ioctl( handle, IOCTL_CONDRV_READ_CONSOLE_CONTROL,
2100 tmp, sizeof(DWORD) + crc->nInitialChars * sizeof(WCHAR),
2101 tmp, sizeof(DWORD) + length * sizeof(WCHAR), count );
2102 if (ret)
2104 memcpy( &crc->dwConsoleKeyState, tmp, sizeof(DWORD) );
2105 *count -= sizeof(DWORD);
2106 memcpy( buffer, tmp + sizeof(DWORD), *count );
2108 HeapFree( GetProcessHeap(), 0, tmp );
2110 else
2112 ret = console_ioctl( handle, IOCTL_CONDRV_READ_CONSOLE, NULL, 0, buffer,
2113 length * sizeof(WCHAR), count );
2115 if (ret) *count /= sizeof(WCHAR);
2116 return ret;
2120 /***********************************************************************
2121 * WriteConsoleA (kernelbase.@)
2123 BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleA( HANDLE handle, const void *buffer, DWORD length,
2124 DWORD *written, void *reserved )
2126 BOOL ret;
2128 TRACE( "(%p,%s,%ld,%p,%p)\n", handle, debugstr_an(buffer, length), length, written, reserved );
2130 ret = console_ioctl( handle, IOCTL_CONDRV_WRITE_FILE, (void *)buffer, length, NULL, 0, NULL );
2131 if (written) *written = ret ? length : 0;
2132 return ret;
2136 /***********************************************************************
2137 * WriteConsoleW (kernelbase.@)
2139 BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleW( HANDLE handle, const void *buffer, DWORD length,
2140 DWORD *written, void *reserved )
2142 BOOL ret;
2144 TRACE( "(%p,%s,%ld,%p,%p)\n", handle, debugstr_wn(buffer, length), length, written, reserved );
2146 ret = console_ioctl( handle, IOCTL_CONDRV_WRITE_CONSOLE, (void *)buffer,
2147 length * sizeof(WCHAR), NULL, 0, NULL );
2148 if (written) *written = ret ? length : 0;
2149 return ret;
2153 /***********************************************************************
2154 * FlushConsoleInputBuffer (kernelbase.@)
2156 BOOL WINAPI FlushConsoleInputBuffer( HANDLE handle )
2158 return console_ioctl( handle, IOCTL_CONDRV_FLUSH, NULL, 0, NULL, 0, NULL );
2162 /***********************************************************************
2163 * Beep (kernelbase.@)
2165 BOOL WINAPI Beep( DWORD frequency, DWORD duration )
2167 /* FIXME: we should not require a console to be attached */
2168 console_ioctl( RtlGetCurrentPeb()->ProcessParameters->ConsoleHandle,
2169 IOCTL_CONDRV_BEEP, NULL, 0, NULL, 0, NULL );
2170 return TRUE;
2174 static HANDLE create_pseudo_console( COORD size, HANDLE input, HANDLE output, HANDLE signal,
2175 DWORD flags, HANDLE *process )
2177 WCHAR cmd[MAX_PATH], conhost_path[MAX_PATH];
2178 PROCESS_INFORMATION pi;
2179 HANDLE server, console;
2180 STARTUPINFOEXW si;
2181 void *redir;
2182 BOOL res;
2184 if (!(server = create_console_server())) return NULL;
2186 console = create_console_reference( server );
2187 if (!console)
2189 NtClose( server );
2190 return NULL;
2193 memset( &si, 0, sizeof(si) );
2194 si.StartupInfo.cb = sizeof(STARTUPINFOEXW);
2195 si.StartupInfo.hStdInput = input;
2196 si.StartupInfo.hStdOutput = output;
2197 si.StartupInfo.hStdError = output;
2198 si.StartupInfo.dwFlags = STARTF_USESTDHANDLES;
2199 swprintf( conhost_path, ARRAY_SIZE(conhost_path), L"%s\\conhost.exe", system_dir );
2200 if (signal)
2202 swprintf( cmd, ARRAY_SIZE(cmd),
2203 L"\"%s\" --headless %s--width %u --height %u --signal 0x%x --server 0x%x",
2204 conhost_path, (flags & PSEUDOCONSOLE_INHERIT_CURSOR) ? L"--inheritcursor " : L"",
2205 size.X, size.Y, signal, server );
2207 else
2209 swprintf( cmd, ARRAY_SIZE(cmd), L"\"%s\" --unix --width %u --height %u --server 0x%x",
2210 conhost_path, size.X, size.Y, server );
2212 Wow64DisableWow64FsRedirection( &redir );
2213 res = CreateProcessW( conhost_path, cmd, NULL, NULL, TRUE, DETACHED_PROCESS, NULL, NULL,
2214 &si.StartupInfo, &pi );
2215 Wow64RevertWow64FsRedirection( redir );
2216 NtClose( server );
2217 if (!res)
2219 NtClose( console );
2220 return NULL;
2223 NtClose( pi.hThread );
2224 *process = pi.hProcess;
2225 return console;
2228 /******************************************************************************
2229 * CreatePseudoConsole (kernelbase.@)
2231 HRESULT WINAPI CreatePseudoConsole( COORD size, HANDLE input, HANDLE output, DWORD flags, HPCON *ret )
2233 SECURITY_ATTRIBUTES inherit_attr = { sizeof(inherit_attr), NULL, TRUE };
2234 struct pseudo_console *pseudo_console;
2235 HANDLE tty_input = NULL, tty_output;
2236 HANDLE signal = NULL;
2237 WCHAR pipe_name[64];
2239 TRACE( "(%u,%u) %p %p %lx %p\n", size.X, size.Y, input, output, flags, ret );
2241 if (!size.X || !size.Y || !ret) return E_INVALIDARG;
2243 if (!(pseudo_console = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pseudo_console) ))) return E_OUTOFMEMORY;
2245 swprintf( pipe_name, ARRAY_SIZE(pipe_name), L"\\\\.\\pipe\\wine_pty_signal_pipe%x",
2246 GetCurrentThreadId() );
2247 signal = CreateNamedPipeW( pipe_name, PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE,
2248 PIPE_UNLIMITED_INSTANCES, 4096, 4096, NMPWAIT_USE_DEFAULT_WAIT, &inherit_attr );
2249 if (signal == INVALID_HANDLE_VALUE)
2251 HeapFree( GetProcessHeap(), 0, pseudo_console );
2252 return HRESULT_FROM_WIN32( GetLastError() );
2254 pseudo_console->signal = CreateFileW( pipe_name, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL );
2255 if (pseudo_console->signal != INVALID_HANDLE_VALUE &&
2256 DuplicateHandle( GetCurrentProcess(), input, GetCurrentProcess(), &tty_input, 0, TRUE, DUPLICATE_SAME_ACCESS) &&
2257 DuplicateHandle( GetCurrentProcess(), output, GetCurrentProcess(), &tty_output, 0, TRUE, DUPLICATE_SAME_ACCESS))
2259 pseudo_console->reference = create_pseudo_console( size, tty_input, tty_output, signal, flags,
2260 &pseudo_console->process );
2261 NtClose( tty_output );
2263 NtClose( tty_input );
2264 NtClose( signal );
2265 if (!pseudo_console->reference)
2267 ClosePseudoConsole( pseudo_console );
2268 return HRESULT_FROM_WIN32( GetLastError() );
2271 *ret = pseudo_console;
2272 return S_OK;
2275 /******************************************************************************
2276 * ClosePseudoConsole (kernelbase.@)
2278 void WINAPI ClosePseudoConsole( HPCON handle )
2280 struct pseudo_console *pseudo_console = handle;
2282 TRACE( "%p\n", handle );
2284 if (!pseudo_console) return;
2285 if (pseudo_console->signal) CloseHandle( pseudo_console->signal );
2286 if (pseudo_console->process)
2288 WaitForSingleObject( pseudo_console->process, INFINITE );
2289 CloseHandle( pseudo_console->process );
2291 if (pseudo_console->reference) CloseHandle( pseudo_console->reference );
2294 /******************************************************************************
2295 * ResizePseudoConsole (kernelbase.@)
2297 HRESULT WINAPI ResizePseudoConsole( HPCON handle, COORD size )
2299 FIXME( "%p (%u,%u)\n", handle, size.X, size.Y );
2300 return E_NOTIMPL;
2303 static BOOL is_tty_handle( HANDLE handle )
2305 return ((UINT_PTR)handle & 3) == 1;
2308 void init_console( void )
2310 RTL_USER_PROCESS_PARAMETERS *params = RtlGetCurrentPeb()->ProcessParameters;
2312 if (params->ConsoleHandle == CONSOLE_HANDLE_SHELL)
2314 HANDLE tty_in = NULL, tty_out = NULL, process = NULL;
2315 COORD size;
2317 if (is_tty_handle( params->hStdInput ))
2319 tty_in = params->hStdInput;
2320 params->hStdInput = NULL;
2322 if (is_tty_handle( params->hStdOutput ))
2324 tty_out = params->hStdOutput;
2325 params->hStdOutput = NULL;
2327 if (is_tty_handle( params->hStdError ))
2329 if (tty_out) CloseHandle( params->hStdError );
2330 else tty_out = params->hStdError;
2331 params->hStdError = NULL;
2334 size.X = params->dwXCountChars;
2335 size.Y = params->dwYCountChars;
2336 TRACE( "creating unix console (size %u %u)\n", size.X, size.Y );
2337 params->ConsoleHandle = create_pseudo_console( size, tty_in, tty_out, NULL, 0, &process );
2338 CloseHandle( process );
2339 CloseHandle( tty_in );
2340 CloseHandle( tty_out );
2342 if (params->ConsoleHandle && create_console_connection( params->ConsoleHandle ))
2344 init_console_std_handles( FALSE );
2347 else if (params->ConsoleHandle == CONSOLE_HANDLE_ALLOC ||
2348 params->ConsoleHandle == CONSOLE_HANDLE_ALLOC_NO_WINDOW)
2350 BOOL no_window = params->ConsoleHandle == CONSOLE_HANDLE_ALLOC_NO_WINDOW;
2351 HMODULE mod = GetModuleHandleW( NULL );
2352 params->ConsoleHandle = NULL;
2353 if (RtlImageNtHeader( mod )->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
2354 alloc_console( no_window );
2356 else if (params->ConsoleHandle && params->ConsoleHandle != CONSOLE_HANDLE_SHELL_NO_WINDOW)
2357 create_console_connection( params->ConsoleHandle );