2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: dll/win32/kernel32/client/console/console.c
5 * PURPOSE: Win32 server console functions
6 * PROGRAMMERS: James Tabor
7 * <jimtabor@adsl-64-217-116-74.dsl.hstntx.swbell.net>
10 /* INCLUDES *******************************************************************/
18 /* GLOBALS ********************************************************************/
20 extern RTL_CRITICAL_SECTION ConsoleLock
;
21 extern BOOL ConsoleInitialized
;
22 extern BOOL WINAPI
IsDebuggerPresent(VOID
);
24 /* Console reserved "file" names */
25 static LPCWSTR BaseConFileName
= CONSOLE_FILE_NAME
;
26 static LPCWSTR BaseConInputFileName
= CONSOLE_INPUT_FILE_NAME
;
27 static LPCWSTR BaseConOutputFileName
= CONSOLE_OUTPUT_FILE_NAME
;
29 PHANDLER_ROUTINE InitialHandler
[1];
30 PHANDLER_ROUTINE
* CtrlHandlers
;
32 ULONG NrAllocatedHandlers
;
33 BOOL LastCloseNotify
= FALSE
;
35 HANDLE InputWaitHandle
= INVALID_HANDLE_VALUE
;
37 #define INPUTEXENAME_BUFLEN 256
38 static WCHAR InputExeName
[INPUTEXENAME_BUFLEN
];
41 /* Default Console Control Handler ********************************************/
45 DefaultConsoleCtrlHandler(DWORD Event
)
47 DPRINT("Default handler called: %lx\n", Event
);
51 DPRINT("Ctrl-C Event\n");
54 case CTRL_BREAK_EVENT
:
55 DPRINT("Ctrl-Break Event\n");
58 case CTRL_CLOSE_EVENT
:
59 DPRINT("Ctrl Close Event\n");
62 case CTRL_LAST_CLOSE_EVENT
:
63 DPRINT("Ctrl Last Close Event\n");
66 case CTRL_LOGOFF_EVENT
:
67 DPRINT("Ctrl Logoff Event\n");
70 case CTRL_SHUTDOWN_EVENT
:
71 DPRINT("Ctrl Shutdown Event\n");
75 ExitProcess(CONTROL_C_EXIT
);
81 ConsoleControlDispatcher(IN LPVOID lpThreadParameter
)
84 DWORD CodeAndFlag
= PtrToUlong(lpThreadParameter
);
85 DWORD nCode
= CodeAndFlag
& MAXLONG
;
87 EXCEPTION_RECORD erException
;
89 DPRINT1("Console Dispatcher Active: %lx %lx\n", CodeAndFlag
, nCode
);
90 SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST
);
95 case CTRL_BREAK_EVENT
:
97 if (IsDebuggerPresent())
99 erException
.ExceptionCode
= (nCode
== CTRL_C_EVENT
?
100 DBG_CONTROL_C
: DBG_CONTROL_BREAK
);
101 erException
.ExceptionFlags
= 0;
102 erException
.ExceptionRecord
= NULL
;
103 erException
.ExceptionAddress
= DefaultConsoleCtrlHandler
;
104 erException
.NumberParameters
= 0;
108 RtlRaiseException(&erException
);
110 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
112 RtlEnterCriticalSection(&ConsoleLock
);
114 if ((nCode
!= CTRL_C_EVENT
) ||
115 (NtCurrentPeb()->ProcessParameters
->ConsoleFlags
!= 1))
117 for (i
= NrCtrlHandlers
; i
> 0; i
--)
119 if (CtrlHandlers
[i
- 1](nCode
)) break;
123 RtlLeaveCriticalSection(&ConsoleLock
);
132 case CTRL_CLOSE_EVENT
:
133 case CTRL_LOGOFF_EVENT
:
134 case CTRL_SHUTDOWN_EVENT
:
137 case CTRL_LAST_CLOSE_EVENT
:
139 * In case the console app hasn't register for last close notification,
140 * just kill this console handler thread. We don't want that such apps
141 * get killed for unexpected reasons. On the contrary apps that registered
142 * can be killed because they expect to be.
144 if (!LastCloseNotify
) ExitThread(0);
148 ExitProcess(CONTROL_C_EXIT
);
156 ASSERT(ConsoleInitialized
);
158 RtlEnterCriticalSection(&ConsoleLock
);
161 if ((nCode
!= CTRL_C_EVENT
) || (NtCurrentPeb()->ProcessParameters
->ConsoleFlags
!= 1))
163 for (i
= NrCtrlHandlers
; i
> 0; i
--)
166 (CodeAndFlag
& MINLONG
) &&
167 ((nCode
== CTRL_LOGOFF_EVENT
) || (nCode
== CTRL_SHUTDOWN_EVENT
)))
169 DPRINT("Skipping system/service apps\n");
173 if (CtrlHandlers
[i
- 1](nCode
))
177 case CTRL_CLOSE_EVENT
:
178 case CTRL_LAST_CLOSE_EVENT
:
179 case CTRL_LOGOFF_EVENT
:
180 case CTRL_SHUTDOWN_EVENT
:
181 nExitCode
= CodeAndFlag
;
189 RtlLeaveCriticalSection(&ConsoleLock
);
191 ExitThread(nExitCode
);
192 return STATUS_SUCCESS
;
197 InitConsoleCtrlHandling(VOID
)
199 /* Initialize Console Ctrl Handler */
200 NrAllocatedHandlers
= NrCtrlHandlers
= 1;
201 CtrlHandlers
= InitialHandler
;
202 CtrlHandlers
[0] = DefaultConsoleCtrlHandler
;
206 /* FUNCTIONS ******************************************************************/
209 IntCheckForConsoleFileName(IN LPCWSTR pszName
,
210 IN DWORD dwDesiredAccess
)
212 LPCWSTR ConsoleName
= pszName
;
213 ULONG DeviceNameInfo
;
216 * Check whether we deal with a DOS device, and if so,
217 * strip the path till the file name.
218 * Therefore, things like \\.\CON or C:\some_path\CONIN$
219 * are transformed into CON or CONIN$, for example.
221 DeviceNameInfo
= RtlIsDosDeviceName_U(pszName
);
222 if (DeviceNameInfo
!= 0)
224 ConsoleName
= (LPCWSTR
)((ULONG_PTR
)ConsoleName
+ ((DeviceNameInfo
>> 16) & 0xFFFF));
227 /* Return a standard console "file" name according to what we passed in parameters */
228 if (_wcsicmp(ConsoleName
, BaseConInputFileName
) == 0)
230 return BaseConInputFileName
;
232 else if (_wcsicmp(ConsoleName
, BaseConOutputFileName
) == 0)
234 return BaseConOutputFileName
;
236 else if (_wcsicmp(ConsoleName
, BaseConFileName
) == 0)
238 if ((dwDesiredAccess
& (GENERIC_READ
| GENERIC_WRITE
)) == GENERIC_READ
)
240 return BaseConInputFileName
;
242 else if ((dwDesiredAccess
& (GENERIC_READ
| GENERIC_WRITE
)) == GENERIC_WRITE
)
244 return BaseConOutputFileName
;
248 /* If we are there, that means that either the file name or the desired access are wrong */
254 * @implemented (Undocumented)
255 * @note See http://undoc.airesoft.co.uk/kernel32.dll/ConsoleMenuControl.php
259 ConsoleMenuControl(HANDLE hConsoleOutput
,
263 CONSOLE_API_MESSAGE ApiMessage
;
264 PCONSOLE_MENUCONTROL MenuControlRequest
= &ApiMessage
.Data
.MenuControlRequest
;
266 MenuControlRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
267 MenuControlRequest
->OutputHandle
= hConsoleOutput
;
268 MenuControlRequest
->CmdIdLow
= dwCmdIdLow
;
269 MenuControlRequest
->CmdIdHigh
= dwCmdIdHigh
;
270 MenuControlRequest
->MenuHandle
= NULL
;
272 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
274 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepMenuControl
),
275 sizeof(*MenuControlRequest
));
277 return MenuControlRequest
->MenuHandle
;
286 DuplicateConsoleHandle(HANDLE hConsole
,
287 DWORD dwDesiredAccess
,
291 CONSOLE_API_MESSAGE ApiMessage
;
292 PCONSOLE_DUPLICATEHANDLE DuplicateHandleRequest
= &ApiMessage
.Data
.DuplicateHandleRequest
;
294 if ( (dwOptions
& ~(DUPLICATE_CLOSE_SOURCE
| DUPLICATE_SAME_ACCESS
)) ||
295 (!(dwOptions
& DUPLICATE_SAME_ACCESS
) &&
296 (dwDesiredAccess
& ~(GENERIC_READ
| GENERIC_WRITE
))) )
298 SetLastError(ERROR_INVALID_PARAMETER
);
299 return INVALID_HANDLE_VALUE
;
302 DuplicateHandleRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
303 DuplicateHandleRequest
->SourceHandle
= hConsole
;
304 DuplicateHandleRequest
->DesiredAccess
= dwDesiredAccess
;
305 DuplicateHandleRequest
->InheritHandle
= bInheritHandle
;
306 DuplicateHandleRequest
->Options
= dwOptions
;
308 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
310 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepDuplicateHandle
),
311 sizeof(*DuplicateHandleRequest
));
312 if (!NT_SUCCESS(ApiMessage
.Status
))
314 BaseSetLastNTError(ApiMessage
.Status
);
315 return INVALID_HANDLE_VALUE
;
318 return DuplicateHandleRequest
->TargetHandle
;
327 GetConsoleHandleInformation(IN HANDLE hHandle
,
328 OUT LPDWORD lpdwFlags
)
330 CONSOLE_API_MESSAGE ApiMessage
;
331 PCONSOLE_GETHANDLEINFO GetHandleInfoRequest
= &ApiMessage
.Data
.GetHandleInfoRequest
;
333 GetHandleInfoRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
334 GetHandleInfoRequest
->Handle
= hHandle
;
336 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
338 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepGetHandleInformation
),
339 sizeof(*GetHandleInfoRequest
));
340 if (!NT_SUCCESS(ApiMessage
.Status
))
342 BaseSetLastNTError(ApiMessage
.Status
);
346 *lpdwFlags
= GetHandleInfoRequest
->Flags
;
357 SetConsoleHandleInformation(IN HANDLE hHandle
,
361 CONSOLE_API_MESSAGE ApiMessage
;
362 PCONSOLE_SETHANDLEINFO SetHandleInfoRequest
= &ApiMessage
.Data
.SetHandleInfoRequest
;
364 SetHandleInfoRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
365 SetHandleInfoRequest
->Handle
= hHandle
;
366 SetHandleInfoRequest
->Mask
= dwMask
;
367 SetHandleInfoRequest
->Flags
= dwFlags
;
369 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
371 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepSetHandleInformation
),
372 sizeof(*SetHandleInfoRequest
));
373 if (!NT_SUCCESS(ApiMessage
.Status
))
375 BaseSetLastNTError(ApiMessage
.Status
);
388 GetConsoleDisplayMode(LPDWORD lpModeFlags
)
390 CONSOLE_API_MESSAGE ApiMessage
;
391 PCONSOLE_GETDISPLAYMODE GetDisplayModeRequest
= &ApiMessage
.Data
.GetDisplayModeRequest
;
393 if (lpModeFlags
== NULL
)
395 SetLastError(ERROR_INVALID_PARAMETER
);
399 GetDisplayModeRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
401 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
403 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepGetDisplayMode
),
404 sizeof(*GetDisplayModeRequest
));
405 if (!NT_SUCCESS(ApiMessage
.Status
))
407 BaseSetLastNTError(ApiMessage
.Status
);
411 *lpModeFlags
= GetDisplayModeRequest
->DisplayMode
; // ModeFlags
418 * @unimplemented (Undocumented)
422 GetConsoleFontInfo(HANDLE hConsoleOutput
,
425 PCONSOLE_FONT_INFO lpConsoleFontInfo
)
427 DPRINT1("GetConsoleFontInfo(0x%p, %d, %lu, 0x%p) UNIMPLEMENTED!\n", hConsoleOutput
, bMaximumWindow
, nFontCount
, lpConsoleFontInfo
);
428 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
438 GetConsoleFontSize(HANDLE hConsoleOutput
,
441 COORD Empty
= {0, 0};
442 DPRINT1("GetConsoleFontSize(0x%p, 0x%x) UNIMPLEMENTED!\n", hConsoleOutput
, nFont
);
443 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
449 * @implemented (Undocumented)
453 GetConsoleHardwareState(HANDLE hConsoleOutput
,
457 CONSOLE_API_MESSAGE ApiMessage
;
458 PCONSOLE_GETSETHWSTATE HardwareStateRequest
= &ApiMessage
.Data
.HardwareStateRequest
;
460 DPRINT1("GetConsoleHardwareState(%lu, 0x%p) UNIMPLEMENTED!\n", Flags
, State
);
462 if (Flags
== NULL
|| State
== NULL
)
464 SetLastError(ERROR_INVALID_PARAMETER
);
468 HardwareStateRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
469 HardwareStateRequest
->OutputHandle
= hConsoleOutput
;
471 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
473 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepGetHardwareState
),
474 sizeof(*HardwareStateRequest
));
475 if (!NT_SUCCESS(ApiMessage
.Status
))
477 BaseSetLastNTError(ApiMessage
.Status
);
481 *Flags
= HardwareStateRequest
->Flags
;
482 *State
= HardwareStateRequest
->State
;
489 * @implemented (Undocumented)
493 GetConsoleInputWaitHandle(VOID
)
495 return InputWaitHandle
;
504 GetCurrentConsoleFont(HANDLE hConsoleOutput
,
506 PCONSOLE_FONT_INFO lpConsoleCurrentFont
)
508 DPRINT1("GetCurrentConsoleFont(0x%p, 0x%x, 0x%p) UNIMPLEMENTED!\n", hConsoleOutput
, bMaximumWindow
, lpConsoleCurrentFont
);
509 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
515 * @unimplemented (Undocumented)
519 GetNumberOfConsoleFonts(VOID
)
521 DPRINT1("GetNumberOfConsoleFonts() UNIMPLEMENTED!\n");
522 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
528 * @implemented (Undocumented)
529 * @note See http://blog.airesoft.co.uk/2012/10/things-ms-can-do-that-they-dont-tell-you-about-console-graphics/
533 InvalidateConsoleDIBits(IN HANDLE hConsoleOutput
,
534 IN PSMALL_RECT lpRect
)
536 CONSOLE_API_MESSAGE ApiMessage
;
537 PCONSOLE_INVALIDATEDIBITS InvalidateDIBitsRequest
= &ApiMessage
.Data
.InvalidateDIBitsRequest
;
541 SetLastError(ERROR_INVALID_PARAMETER
);
545 InvalidateDIBitsRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
546 InvalidateDIBitsRequest
->OutputHandle
= hConsoleOutput
;
547 InvalidateDIBitsRequest
->Region
= *lpRect
;
549 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
551 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepInvalidateBitMapRect
),
552 sizeof(*InvalidateDIBitsRequest
));
553 if (!NT_SUCCESS(ApiMessage
.Status
))
555 BaseSetLastNTError(ApiMessage
.Status
);
564 * @implemented (Undocumented)
568 OpenConsoleW(LPCWSTR wsName
,
569 DWORD dwDesiredAccess
,
573 CONSOLE_API_MESSAGE ApiMessage
;
574 PCONSOLE_OPENCONSOLE OpenConsoleRequest
= &ApiMessage
.Data
.OpenConsoleRequest
;
575 CONSOLE_HANDLE_TYPE HandleType
;
577 if (wsName
&& (_wcsicmp(wsName
, BaseConInputFileName
) == 0))
579 HandleType
= HANDLE_INPUT
;
581 else if (wsName
&& (_wcsicmp(wsName
, BaseConOutputFileName
) == 0))
583 HandleType
= HANDLE_OUTPUT
;
587 SetLastError(ERROR_INVALID_PARAMETER
);
588 return INVALID_HANDLE_VALUE
;
591 if ( (dwDesiredAccess
& ~(GENERIC_READ
| GENERIC_WRITE
)) ||
592 (dwShareMode
& ~(FILE_SHARE_READ
| FILE_SHARE_WRITE
)) )
594 SetLastError(ERROR_INVALID_PARAMETER
);
595 return INVALID_HANDLE_VALUE
;
598 OpenConsoleRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
599 OpenConsoleRequest
->HandleType
= HandleType
;
600 OpenConsoleRequest
->DesiredAccess
= dwDesiredAccess
;
601 OpenConsoleRequest
->InheritHandle
= bInheritHandle
;
602 OpenConsoleRequest
->ShareMode
= dwShareMode
;
604 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
606 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepOpenConsole
),
607 sizeof(*OpenConsoleRequest
));
608 if (!NT_SUCCESS(ApiMessage
.Status
))
610 BaseSetLastNTError(ApiMessage
.Status
);
611 return INVALID_HANDLE_VALUE
;
614 return OpenConsoleRequest
->Handle
;
619 * @implemented (Undocumented)
620 * @note See http://undoc.airesoft.co.uk/kernel32.dll/SetConsoleCursor.php
624 SetConsoleCursor(HANDLE hConsoleOutput
,
627 CONSOLE_API_MESSAGE ApiMessage
;
628 PCONSOLE_SETCURSOR SetCursorRequest
= &ApiMessage
.Data
.SetCursorRequest
;
630 SetCursorRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
631 SetCursorRequest
->OutputHandle
= hConsoleOutput
;
632 SetCursorRequest
->CursorHandle
= hCursor
;
634 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
636 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepSetCursor
),
637 sizeof(*SetCursorRequest
));
638 if (!NT_SUCCESS(ApiMessage
.Status
))
640 BaseSetLastNTError(ApiMessage
.Status
);
653 SetConsoleDisplayMode(HANDLE hConsoleOutput
,
654 DWORD dwFlags
, // dwModeFlags
655 PCOORD lpNewScreenBufferDimensions
)
657 CONSOLE_API_MESSAGE ApiMessage
;
658 PCONSOLE_SETDISPLAYMODE SetDisplayModeRequest
= &ApiMessage
.Data
.SetDisplayModeRequest
;
660 SetDisplayModeRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
661 SetDisplayModeRequest
->OutputHandle
= hConsoleOutput
;
662 SetDisplayModeRequest
->DisplayMode
= dwFlags
; // ModeFlags ; dwModeFlags
663 SetDisplayModeRequest
->NewSBDim
.X
= 0;
664 SetDisplayModeRequest
->NewSBDim
.Y
= 0;
665 /* SetDisplayModeRequest->EventHandle; */
667 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
669 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepSetDisplayMode
),
670 sizeof(*SetDisplayModeRequest
));
671 if (!NT_SUCCESS(ApiMessage
.Status
))
673 BaseSetLastNTError(ApiMessage
.Status
);
677 if (lpNewScreenBufferDimensions
)
678 *lpNewScreenBufferDimensions
= SetDisplayModeRequest
->NewSBDim
;
685 * @unimplemented (Undocumented)
689 SetConsoleFont(HANDLE hConsoleOutput
,
692 DPRINT1("SetConsoleFont(0x%p, %lu) UNIMPLEMENTED!\n", hConsoleOutput
, nFont
);
693 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
699 * @implemented (Undocumented)
703 SetConsoleHardwareState(HANDLE hConsoleOutput
,
707 CONSOLE_API_MESSAGE ApiMessage
;
708 PCONSOLE_GETSETHWSTATE HardwareStateRequest
= &ApiMessage
.Data
.HardwareStateRequest
;
710 DPRINT1("SetConsoleHardwareState(%lu, %lu) UNIMPLEMENTED!\n", Flags
, State
);
712 HardwareStateRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
713 HardwareStateRequest
->OutputHandle
= hConsoleOutput
;
714 HardwareStateRequest
->Flags
= Flags
;
715 HardwareStateRequest
->State
= State
;
717 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
719 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepSetHardwareState
),
720 sizeof(*HardwareStateRequest
));
721 if (!NT_SUCCESS(ApiMessage
.Status
))
723 BaseSetLastNTError(ApiMessage
.Status
);
732 * @unimplemented (Undocumented)
736 SetConsoleKeyShortcuts(DWORD Unknown0
,
741 DPRINT1("SetConsoleKeyShortcuts(0x%x, 0x%x, 0x%x, 0x%x) UNIMPLEMENTED!\n", Unknown0
, Unknown1
, Unknown2
, Unknown3
);
742 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
748 * @implemented (Undocumented)
749 * @note See http://undoc.airesoft.co.uk/kernel32.dll/SetConsoleMaximumWindowSize.php
750 * Does nothing, returns TRUE only. Checked on Windows Server 2003.
754 SetConsoleMaximumWindowSize(HANDLE hConsoleOutput
,
757 DPRINT1("SetConsoleMaximumWindowSize(0x%p, {%d, %d}) does nothing\n",
758 hConsoleOutput
, dwMaximumSize
.X
, dwMaximumSize
.Y
);
764 * @implemented (Undocumented)
768 SetConsoleMenuClose(BOOL bEnable
)
770 CONSOLE_API_MESSAGE ApiMessage
;
771 PCONSOLE_SETMENUCLOSE SetMenuCloseRequest
= &ApiMessage
.Data
.SetMenuCloseRequest
;
773 SetMenuCloseRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
774 SetMenuCloseRequest
->Enable
= bEnable
;
776 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
778 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepSetMenuClose
),
779 sizeof(*SetMenuCloseRequest
));
780 if (!NT_SUCCESS(ApiMessage
.Status
))
782 BaseSetLastNTError(ApiMessage
.Status
);
791 * @implemented (Undocumented)
792 * @note See http://comments.gmane.org/gmane.comp.lang.harbour.devel/27844
793 * Usage example: https://github.com/harbour/core/commit/d79a1b7b812cbde6ddf718ebfd6939a24f633e52
797 SetConsolePalette(HANDLE hConsoleOutput
,
801 CONSOLE_API_MESSAGE ApiMessage
;
802 PCONSOLE_SETPALETTE SetPaletteRequest
= &ApiMessage
.Data
.SetPaletteRequest
;
804 SetPaletteRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
805 SetPaletteRequest
->OutputHandle
= hConsoleOutput
;
806 SetPaletteRequest
->PaletteHandle
= hPalette
;
807 SetPaletteRequest
->Usage
= dwUsage
;
809 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
811 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepSetPalette
),
812 sizeof(*SetPaletteRequest
));
813 if (!NT_SUCCESS(ApiMessage
.Status
))
815 BaseSetLastNTError(ApiMessage
.Status
);
823 * @implemented (Undocumented)
824 * @note See http://undoc.airesoft.co.uk/kernel32.dll/ShowConsoleCursor.php
828 ShowConsoleCursor(HANDLE hConsoleOutput
,
831 CONSOLE_API_MESSAGE ApiMessage
;
832 PCONSOLE_SHOWCURSOR ShowCursorRequest
= &ApiMessage
.Data
.ShowCursorRequest
;
834 ShowCursorRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
835 ShowCursorRequest
->OutputHandle
= hConsoleOutput
;
836 ShowCursorRequest
->Show
= bShow
;
837 ShowCursorRequest
->RefCount
= 0;
839 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
841 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepShowCursor
),
842 sizeof(*ShowCursorRequest
));
844 return ShowCursorRequest
->RefCount
;
849 * FUNCTION: Checks whether the given handle is a valid console handle.
852 * hIoHandle - Handle to be checked.
855 * TRUE : Handle is a valid console handle.
856 * FALSE: Handle is not a valid console handle.
858 * STATUS: Officially undocumented
864 VerifyConsoleIoHandle(HANDLE hIoHandle
)
866 CONSOLE_API_MESSAGE ApiMessage
;
867 PCONSOLE_VERIFYHANDLE VerifyHandleRequest
= &ApiMessage
.Data
.VerifyHandleRequest
;
869 VerifyHandleRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
870 VerifyHandleRequest
->Handle
= hIoHandle
;
871 VerifyHandleRequest
->IsValid
= FALSE
;
873 /* If the process is not attached to a console, return invalid handle */
874 if (VerifyHandleRequest
->ConsoleHandle
== NULL
) return FALSE
;
876 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
878 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepVerifyIoHandle
),
879 sizeof(*VerifyHandleRequest
));
880 if (!NT_SUCCESS(ApiMessage
.Status
))
882 BaseSetLastNTError(ApiMessage
.Status
);
886 return VerifyHandleRequest
->IsValid
;
891 * @implemented (Undocumented)
895 CloseConsoleHandle(HANDLE hHandle
)
897 CONSOLE_API_MESSAGE ApiMessage
;
898 PCONSOLE_CLOSEHANDLE CloseHandleRequest
= &ApiMessage
.Data
.CloseHandleRequest
;
900 CloseHandleRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
901 CloseHandleRequest
->Handle
= hHandle
;
903 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
905 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepCloseHandle
),
906 sizeof(*CloseHandleRequest
));
907 if (!NT_SUCCESS(ApiMessage
.Status
))
909 BaseSetLastNTError(ApiMessage
.Status
);
922 GetStdHandle(DWORD nStdHandle
)
924 * FUNCTION: Get a handle for the standard input, standard output
925 * and a standard error device.
928 * nStdHandle - Specifies the device for which to return the handle.
930 * RETURNS: If the function succeeds, the return value is the handle
931 * of the specified device. Otherwise the value is INVALID_HANDLE_VALUE.
934 PRTL_USER_PROCESS_PARAMETERS Ppb
= NtCurrentPeb()->ProcessParameters
;
935 HANDLE Handle
= INVALID_HANDLE_VALUE
;
939 case STD_INPUT_HANDLE
:
940 Handle
= Ppb
->StandardInput
;
943 case STD_OUTPUT_HANDLE
:
944 Handle
= Ppb
->StandardOutput
;
947 case STD_ERROR_HANDLE
:
948 Handle
= Ppb
->StandardError
;
952 /* If the returned handle is invalid, set last error */
953 if (Handle
== INVALID_HANDLE_VALUE
) SetLastError(ERROR_INVALID_HANDLE
);
964 SetStdHandle(DWORD nStdHandle
,
967 * FUNCTION: Set the handle for the standard input, standard output or
968 * the standard error device.
971 * nStdHandle - Specifies the handle to be set.
972 * hHandle - The handle to set.
974 * RETURNS: TRUE if the function succeeds, FALSE otherwise.
977 PRTL_USER_PROCESS_PARAMETERS Ppb
= NtCurrentPeb()->ProcessParameters
;
979 /* No need to check if hHandle == INVALID_HANDLE_VALUE */
983 case STD_INPUT_HANDLE
:
984 Ppb
->StandardInput
= hHandle
;
987 case STD_OUTPUT_HANDLE
:
988 Ppb
->StandardOutput
= hHandle
;
991 case STD_ERROR_HANDLE
:
992 Ppb
->StandardError
= hHandle
;
996 /* nStdHandle was invalid, bail out */
997 SetLastError(ERROR_INVALID_HANDLE
);
1002 /*--------------------------------------------------------------
1012 PRTL_USER_PROCESS_PARAMETERS Parameters
= NtCurrentPeb()->ProcessParameters
;
1013 CONSOLE_API_MESSAGE ApiMessage
;
1014 PCONSOLE_ALLOCCONSOLE AllocConsoleRequest
= &ApiMessage
.Data
.AllocConsoleRequest
;
1015 PCSR_CAPTURE_BUFFER CaptureBuffer
;
1017 if (Parameters
->ConsoleHandle
)
1019 DPRINT1("AllocConsole: Allocating a console to a process already having one\n");
1020 SetLastError(ERROR_ACCESS_DENIED
);
1024 CaptureBuffer
= CsrAllocateCaptureBuffer(1, sizeof(CONSOLE_START_INFO
));
1025 if (CaptureBuffer
== NULL
)
1027 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
1028 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1032 CsrAllocateMessagePointer(CaptureBuffer
,
1033 sizeof(CONSOLE_START_INFO
),
1034 (PVOID
*)&AllocConsoleRequest
->ConsoleStartInfo
);
1036 InitConsoleInfo(AllocConsoleRequest
->ConsoleStartInfo
,
1037 &Parameters
->ImagePathName
);
1039 AllocConsoleRequest
->ConsoleHandle
= NULL
;
1040 AllocConsoleRequest
->CtrlDispatcher
= ConsoleControlDispatcher
;
1041 AllocConsoleRequest
->PropDispatcher
= PropDialogHandler
;
1043 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1045 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepAlloc
),
1046 sizeof(CONSOLE_ALLOCCONSOLE
));
1048 CsrFreeCaptureBuffer(CaptureBuffer
);
1050 if (!NT_SUCCESS(Status
))
1052 BaseSetLastNTError(Status
);
1056 Parameters
->ConsoleHandle
= AllocConsoleRequest
->ConsoleHandle
;
1057 SetStdHandle(STD_INPUT_HANDLE
, AllocConsoleRequest
->InputHandle
);
1058 SetStdHandle(STD_OUTPUT_HANDLE
, AllocConsoleRequest
->OutputHandle
);
1059 SetStdHandle(STD_ERROR_HANDLE
, AllocConsoleRequest
->ErrorHandle
);
1061 /* Initialize Console Ctrl Handler */
1062 InitConsoleCtrlHandling();
1064 InputWaitHandle
= AllocConsoleRequest
->InputWaitHandle
;
1070 /*--------------------------------------------------------------
1079 CONSOLE_API_MESSAGE ApiMessage
;
1080 PCONSOLE_FREECONSOLE FreeConsoleRequest
= &ApiMessage
.Data
.FreeConsoleRequest
;
1081 HANDLE ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
1083 /* We must have a non-trivial handle to close */
1084 if (ConsoleHandle
== NULL
) // IsConsoleHandle(ConsoleHandle)
1086 SetLastError(ERROR_INVALID_PARAMETER
);
1090 /* Set up the data to send to the Console Server */
1091 FreeConsoleRequest
->ConsoleHandle
= ConsoleHandle
;
1093 /* Call the server */
1094 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1096 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepFree
),
1097 sizeof(*FreeConsoleRequest
));
1099 /* Check for success */
1100 if (!NT_SUCCESS(ApiMessage
.Status
))
1102 BaseSetLastNTError(ApiMessage
.Status
);
1106 /* Reset the console handle */
1107 NtCurrentPeb()->ProcessParameters
->ConsoleHandle
= NULL
;
1109 /* Close the associated input handle */
1110 CloseHandle(InputWaitHandle
);
1111 InputWaitHandle
= INVALID_HANDLE_VALUE
;
1117 /*--------------------------------------------------------------
1118 * GetConsoleScreenBufferInfo
1124 GetConsoleScreenBufferInfo(HANDLE hConsoleOutput
,
1125 PCONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo
)
1127 CONSOLE_API_MESSAGE ApiMessage
;
1128 PCONSOLE_GETSCREENBUFFERINFO ScreenBufferInfoRequest
= &ApiMessage
.Data
.ScreenBufferInfoRequest
;
1130 if (lpConsoleScreenBufferInfo
== NULL
)
1132 SetLastError(ERROR_INVALID_PARAMETER
);
1136 ScreenBufferInfoRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
1137 ScreenBufferInfoRequest
->OutputHandle
= hConsoleOutput
;
1139 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1141 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepGetScreenBufferInfo
),
1142 sizeof(*ScreenBufferInfoRequest
));
1143 if (!NT_SUCCESS(ApiMessage
.Status
))
1145 BaseSetLastNTError(ApiMessage
.Status
);
1149 lpConsoleScreenBufferInfo
->dwSize
= ScreenBufferInfoRequest
->ScreenBufferSize
;
1150 lpConsoleScreenBufferInfo
->dwCursorPosition
= ScreenBufferInfoRequest
->CursorPosition
;
1151 lpConsoleScreenBufferInfo
->wAttributes
= ScreenBufferInfoRequest
->Attributes
;
1152 lpConsoleScreenBufferInfo
->srWindow
.Left
= ScreenBufferInfoRequest
->ViewOrigin
.X
;
1153 lpConsoleScreenBufferInfo
->srWindow
.Top
= ScreenBufferInfoRequest
->ViewOrigin
.Y
;
1154 lpConsoleScreenBufferInfo
->srWindow
.Right
= ScreenBufferInfoRequest
->ViewOrigin
.X
+ ScreenBufferInfoRequest
->ViewSize
.X
- 1;
1155 lpConsoleScreenBufferInfo
->srWindow
.Bottom
= ScreenBufferInfoRequest
->ViewOrigin
.Y
+ ScreenBufferInfoRequest
->ViewSize
.Y
- 1;
1156 lpConsoleScreenBufferInfo
->dwMaximumWindowSize
= ScreenBufferInfoRequest
->MaximumViewSize
;
1162 /*--------------------------------------------------------------
1163 * SetConsoleCursorPosition
1169 SetConsoleCursorPosition(HANDLE hConsoleOutput
,
1170 COORD dwCursorPosition
)
1172 CONSOLE_API_MESSAGE ApiMessage
;
1173 PCONSOLE_SETCURSORPOSITION SetCursorPositionRequest
= &ApiMessage
.Data
.SetCursorPositionRequest
;
1175 SetCursorPositionRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
1176 SetCursorPositionRequest
->OutputHandle
= hConsoleOutput
;
1177 SetCursorPositionRequest
->Position
= dwCursorPosition
;
1179 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1181 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepSetCursorPosition
),
1182 sizeof(*SetCursorPositionRequest
));
1183 if (!NT_SUCCESS(ApiMessage
.Status
))
1185 BaseSetLastNTError(ApiMessage
.Status
);
1193 /*--------------------------------------------------------------
1200 GetConsoleMode(HANDLE hConsoleHandle
,
1203 CONSOLE_API_MESSAGE ApiMessage
;
1204 PCONSOLE_GETSETCONSOLEMODE ConsoleModeRequest
= &ApiMessage
.Data
.ConsoleModeRequest
;
1208 SetLastError(ERROR_INVALID_PARAMETER
);
1212 ConsoleModeRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
1213 ConsoleModeRequest
->Handle
= hConsoleHandle
;
1215 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1217 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepGetMode
),
1218 sizeof(*ConsoleModeRequest
));
1219 if (!NT_SUCCESS(ApiMessage
.Status
))
1221 BaseSetLastNTError(ApiMessage
.Status
);
1225 *lpMode
= ConsoleModeRequest
->Mode
;
1231 /*--------------------------------------------------------------
1238 SetConsoleMode(HANDLE hConsoleHandle
,
1241 CONSOLE_API_MESSAGE ApiMessage
;
1242 PCONSOLE_GETSETCONSOLEMODE ConsoleModeRequest
= &ApiMessage
.Data
.ConsoleModeRequest
;
1244 ConsoleModeRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
1245 ConsoleModeRequest
->Handle
= hConsoleHandle
;
1246 ConsoleModeRequest
->Mode
= dwMode
;
1248 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1250 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepSetMode
),
1251 sizeof(*ConsoleModeRequest
));
1252 if (!NT_SUCCESS(ApiMessage
.Status
))
1254 BaseSetLastNTError(ApiMessage
.Status
);
1262 /*--------------------------------------------------------------
1263 * GetNumberOfConsoleInputEvents
1269 GetNumberOfConsoleInputEvents(HANDLE hConsoleInput
,
1270 LPDWORD lpNumberOfEvents
)
1272 CONSOLE_API_MESSAGE ApiMessage
;
1273 PCONSOLE_GETNUMINPUTEVENTS GetNumInputEventsRequest
= &ApiMessage
.Data
.GetNumInputEventsRequest
;
1275 GetNumInputEventsRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
1276 GetNumInputEventsRequest
->InputHandle
= hConsoleInput
;
1277 GetNumInputEventsRequest
->NumberOfEvents
= 0;
1279 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1281 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepGetNumberOfInputEvents
),
1282 sizeof(*GetNumInputEventsRequest
));
1283 if (!NT_SUCCESS(ApiMessage
.Status
))
1285 BaseSetLastNTError(ApiMessage
.Status
);
1289 if (lpNumberOfEvents
== NULL
)
1291 SetLastError(ERROR_INVALID_ACCESS
);
1295 *lpNumberOfEvents
= GetNumInputEventsRequest
->NumberOfEvents
;
1301 /*--------------------------------------------------------------
1302 * GetLargestConsoleWindowSize
1308 GetLargestConsoleWindowSize(HANDLE hConsoleOutput
)
1310 CONSOLE_API_MESSAGE ApiMessage
;
1311 PCONSOLE_GETLARGESTWINDOWSIZE GetLargestWindowSizeRequest
= &ApiMessage
.Data
.GetLargestWindowSizeRequest
;
1313 GetLargestWindowSizeRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
1314 GetLargestWindowSizeRequest
->OutputHandle
= hConsoleOutput
;
1315 GetLargestWindowSizeRequest
->Size
.X
= 0;
1316 GetLargestWindowSizeRequest
->Size
.Y
= 0;
1318 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1320 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepGetLargestWindowSize
),
1321 sizeof(*GetLargestWindowSizeRequest
));
1322 if (!NT_SUCCESS(ApiMessage
.Status
))
1324 BaseSetLastNTError(ApiMessage
.Status
);
1327 DPRINT("GetLargestConsoleWindowSize, X = %d, Y = %d\n", GetLargestWindowSizeRequest
->Size
.X
, GetLargestWindowSizeRequest
->Size
.Y
);
1328 return GetLargestWindowSizeRequest
->Size
;
1332 /*--------------------------------------------------------------
1333 * GetConsoleCursorInfo
1339 GetConsoleCursorInfo(HANDLE hConsoleOutput
,
1340 PCONSOLE_CURSOR_INFO lpConsoleCursorInfo
)
1342 CONSOLE_API_MESSAGE ApiMessage
;
1343 PCONSOLE_GETSETCURSORINFO CursorInfoRequest
= &ApiMessage
.Data
.CursorInfoRequest
;
1345 if (!lpConsoleCursorInfo
)
1347 if (!hConsoleOutput
)
1348 SetLastError(ERROR_INVALID_HANDLE
);
1350 SetLastError(ERROR_INVALID_ACCESS
);
1355 CursorInfoRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
1356 CursorInfoRequest
->OutputHandle
= hConsoleOutput
;
1358 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1360 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepGetCursorInfo
),
1361 sizeof(*CursorInfoRequest
));
1362 if (!NT_SUCCESS(ApiMessage
.Status
))
1364 BaseSetLastNTError(ApiMessage
.Status
);
1368 *lpConsoleCursorInfo
= CursorInfoRequest
->Info
;
1374 /*--------------------------------------------------------------
1375 * SetConsoleCursorInfo
1381 SetConsoleCursorInfo(HANDLE hConsoleOutput
,
1382 CONST CONSOLE_CURSOR_INFO
*lpConsoleCursorInfo
)
1384 CONSOLE_API_MESSAGE ApiMessage
;
1385 PCONSOLE_GETSETCURSORINFO CursorInfoRequest
= &ApiMessage
.Data
.CursorInfoRequest
;
1387 CursorInfoRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
1388 CursorInfoRequest
->OutputHandle
= hConsoleOutput
;
1389 CursorInfoRequest
->Info
= *lpConsoleCursorInfo
;
1391 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1393 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepSetCursorInfo
),
1394 sizeof(*CursorInfoRequest
));
1395 if (!NT_SUCCESS(ApiMessage
.Status
))
1397 BaseSetLastNTError(ApiMessage
.Status
);
1405 /*--------------------------------------------------------------
1406 * GetNumberOfConsoleMouseButtons
1412 GetNumberOfConsoleMouseButtons(LPDWORD lpNumberOfMouseButtons
)
1414 CONSOLE_API_MESSAGE ApiMessage
;
1415 PCONSOLE_GETMOUSEINFO GetMouseInfoRequest
= &ApiMessage
.Data
.GetMouseInfoRequest
;
1417 GetMouseInfoRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
1419 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1421 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepGetMouseInfo
),
1422 sizeof(*GetMouseInfoRequest
));
1423 if (!NT_SUCCESS(ApiMessage
.Status
))
1425 BaseSetLastNTError(ApiMessage
.Status
);
1429 *lpNumberOfMouseButtons
= GetMouseInfoRequest
->NumButtons
;
1434 /*--------------------------------------------------------------
1435 * SetConsoleActiveScreenBuffer
1441 SetConsoleActiveScreenBuffer(HANDLE hConsoleOutput
)
1443 CONSOLE_API_MESSAGE ApiMessage
;
1444 PCONSOLE_SETACTIVESCREENBUFFER SetScreenBufferRequest
= &ApiMessage
.Data
.SetScreenBufferRequest
;
1446 SetScreenBufferRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
1447 SetScreenBufferRequest
->OutputHandle
= hConsoleOutput
;
1449 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1451 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepSetActiveScreenBuffer
),
1452 sizeof(*SetScreenBufferRequest
));
1453 if (!NT_SUCCESS(ApiMessage
.Status
))
1455 BaseSetLastNTError(ApiMessage
.Status
);
1463 /*--------------------------------------------------------------
1464 * FlushConsoleInputBuffer
1470 FlushConsoleInputBuffer(HANDLE hConsoleInput
)
1472 CONSOLE_API_MESSAGE ApiMessage
;
1473 PCONSOLE_FLUSHINPUTBUFFER FlushInputBufferRequest
= &ApiMessage
.Data
.FlushInputBufferRequest
;
1475 FlushInputBufferRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
1476 FlushInputBufferRequest
->InputHandle
= hConsoleInput
;
1478 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1480 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepFlushInputBuffer
),
1481 sizeof(*FlushInputBufferRequest
));
1482 if (!NT_SUCCESS(ApiMessage
.Status
))
1484 BaseSetLastNTError(ApiMessage
.Status
);
1492 /*--------------------------------------------------------------
1493 * SetConsoleScreenBufferSize
1499 SetConsoleScreenBufferSize(HANDLE hConsoleOutput
,
1502 CONSOLE_API_MESSAGE ApiMessage
;
1503 PCONSOLE_SETSCREENBUFFERSIZE SetScreenBufferSizeRequest
= &ApiMessage
.Data
.SetScreenBufferSizeRequest
;
1505 SetScreenBufferSizeRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
1506 SetScreenBufferSizeRequest
->OutputHandle
= hConsoleOutput
;
1507 SetScreenBufferSizeRequest
->Size
= dwSize
;
1509 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1511 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepSetScreenBufferSize
),
1512 sizeof(*SetScreenBufferSizeRequest
));
1513 if (!NT_SUCCESS(ApiMessage
.Status
))
1515 BaseSetLastNTError(ApiMessage
.Status
);
1525 IntScrollConsoleScreenBuffer(HANDLE hConsoleOutput
,
1526 CONST SMALL_RECT
* lpScrollRectangle
,
1527 CONST SMALL_RECT
* lpClipRectangle
,
1528 COORD dwDestinationOrigin
,
1529 CONST CHAR_INFO
* lpFill
,
1532 CONSOLE_API_MESSAGE ApiMessage
;
1533 PCONSOLE_SCROLLSCREENBUFFER ScrollScreenBufferRequest
= &ApiMessage
.Data
.ScrollScreenBufferRequest
;
1535 ScrollScreenBufferRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
1536 ScrollScreenBufferRequest
->OutputHandle
= hConsoleOutput
;
1537 ScrollScreenBufferRequest
->ScrollRectangle
= *lpScrollRectangle
;
1539 if (lpClipRectangle
!= NULL
)
1541 ScrollScreenBufferRequest
->UseClipRectangle
= TRUE
;
1542 ScrollScreenBufferRequest
->ClipRectangle
= *lpClipRectangle
;
1546 ScrollScreenBufferRequest
->UseClipRectangle
= FALSE
;
1549 ScrollScreenBufferRequest
->DestinationOrigin
= dwDestinationOrigin
;
1550 ScrollScreenBufferRequest
->Fill
= *lpFill
;
1551 ScrollScreenBufferRequest
->Unicode
= bUnicode
;
1553 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1555 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepScrollScreenBuffer
),
1556 sizeof(*ScrollScreenBufferRequest
));
1557 if (!NT_SUCCESS(ApiMessage
.Status
))
1559 BaseSetLastNTError(ApiMessage
.Status
);
1567 /*--------------------------------------------------------------
1568 * ScrollConsoleScreenBufferA
1574 ScrollConsoleScreenBufferA(HANDLE hConsoleOutput
,
1575 CONST SMALL_RECT
* lpScrollRectangle
,
1576 CONST SMALL_RECT
* lpClipRectangle
,
1577 COORD dwDestinationOrigin
,
1578 CONST CHAR_INFO
* lpFill
)
1580 return IntScrollConsoleScreenBuffer(hConsoleOutput
,
1583 dwDestinationOrigin
,
1589 /*--------------------------------------------------------------
1590 * ScrollConsoleScreenBufferW
1596 ScrollConsoleScreenBufferW(HANDLE hConsoleOutput
,
1597 CONST SMALL_RECT
*lpScrollRectangle
,
1598 CONST SMALL_RECT
*lpClipRectangle
,
1599 COORD dwDestinationOrigin
,
1600 CONST CHAR_INFO
*lpFill
)
1602 return IntScrollConsoleScreenBuffer(hConsoleOutput
,
1605 dwDestinationOrigin
,
1611 /*--------------------------------------------------------------
1612 * SetConsoleWindowInfo
1618 SetConsoleWindowInfo(HANDLE hConsoleOutput
,
1620 CONST SMALL_RECT
*lpConsoleWindow
)
1622 CONSOLE_API_MESSAGE ApiMessage
;
1623 PCONSOLE_SETWINDOWINFO SetWindowInfoRequest
= &ApiMessage
.Data
.SetWindowInfoRequest
;
1625 if (lpConsoleWindow
== NULL
)
1627 SetLastError(ERROR_INVALID_PARAMETER
);
1631 SetWindowInfoRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
1632 SetWindowInfoRequest
->OutputHandle
= hConsoleOutput
;
1633 SetWindowInfoRequest
->Absolute
= bAbsolute
;
1634 SetWindowInfoRequest
->WindowRect
= *lpConsoleWindow
;
1636 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1638 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepSetWindowInfo
),
1639 sizeof(*SetWindowInfoRequest
));
1640 if (!NT_SUCCESS(ApiMessage
.Status
))
1642 BaseSetLastNTError(ApiMessage
.Status
);
1650 /*--------------------------------------------------------------
1651 * SetConsoleTextAttribute
1657 SetConsoleTextAttribute(HANDLE hConsoleOutput
,
1660 CONSOLE_API_MESSAGE ApiMessage
;
1661 PCONSOLE_SETTEXTATTRIB SetTextAttribRequest
= &ApiMessage
.Data
.SetTextAttribRequest
;
1663 SetTextAttribRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
1664 SetTextAttribRequest
->OutputHandle
= hConsoleOutput
;
1665 SetTextAttribRequest
->Attributes
= wAttributes
;
1667 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1669 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepSetTextAttribute
),
1670 sizeof(*SetTextAttribRequest
));
1671 if (!NT_SUCCESS(ApiMessage
.Status
))
1673 BaseSetLastNTError(ApiMessage
.Status
);
1683 AddConsoleCtrlHandler(PHANDLER_ROUTINE HandlerRoutine
)
1685 PHANDLER_ROUTINE
* NewCtrlHandlers
= NULL
;
1687 if (HandlerRoutine
== NULL
)
1689 NtCurrentPeb()->ProcessParameters
->ConsoleFlags
= TRUE
;
1693 if (NrCtrlHandlers
== NrAllocatedHandlers
)
1695 NewCtrlHandlers
= RtlAllocateHeap(RtlGetProcessHeap(),
1697 (NrCtrlHandlers
+ 4) * sizeof(PHANDLER_ROUTINE
));
1698 if (NewCtrlHandlers
== NULL
)
1700 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1704 memmove(NewCtrlHandlers
, CtrlHandlers
, sizeof(PHANDLER_ROUTINE
) * NrCtrlHandlers
);
1706 if (NrAllocatedHandlers
> 1) RtlFreeHeap(RtlGetProcessHeap(), 0, CtrlHandlers
);
1708 CtrlHandlers
= NewCtrlHandlers
;
1709 NrAllocatedHandlers
+= 4;
1712 ASSERT(NrCtrlHandlers
< NrAllocatedHandlers
);
1714 CtrlHandlers
[NrCtrlHandlers
++] = HandlerRoutine
;
1721 RemoveConsoleCtrlHandler(PHANDLER_ROUTINE HandlerRoutine
)
1725 if (HandlerRoutine
== NULL
)
1727 NtCurrentPeb()->ProcessParameters
->ConsoleFlags
= FALSE
;
1731 for (i
= 0; i
< NrCtrlHandlers
; i
++)
1733 if (CtrlHandlers
[i
] == HandlerRoutine
)
1735 if (i
< (NrCtrlHandlers
- 1))
1737 memmove(&CtrlHandlers
[i
],
1739 (NrCtrlHandlers
- i
+ 1) * sizeof(PHANDLER_ROUTINE
));
1747 SetLastError(ERROR_INVALID_PARAMETER
);
1757 SetConsoleCtrlHandler(PHANDLER_ROUTINE HandlerRoutine
,
1762 RtlEnterCriticalSection(&BaseDllDirectoryLock
);
1765 Ret
= AddConsoleCtrlHandler(HandlerRoutine
);
1769 Ret
= RemoveConsoleCtrlHandler(HandlerRoutine
);
1772 RtlLeaveCriticalSection(&BaseDllDirectoryLock
);
1777 /*--------------------------------------------------------------
1778 * GenerateConsoleCtrlEvent
1784 GenerateConsoleCtrlEvent(DWORD dwCtrlEvent
,
1785 DWORD dwProcessGroupId
)
1787 CONSOLE_API_MESSAGE ApiMessage
;
1788 PCONSOLE_GENERATECTRLEVENT GenerateCtrlEventRequest
= &ApiMessage
.Data
.GenerateCtrlEventRequest
;
1790 if (dwCtrlEvent
!= CTRL_C_EVENT
&& dwCtrlEvent
!= CTRL_BREAK_EVENT
)
1792 SetLastError(ERROR_INVALID_PARAMETER
);
1796 GenerateCtrlEventRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
1797 GenerateCtrlEventRequest
->CtrlEvent
= dwCtrlEvent
;
1798 GenerateCtrlEventRequest
->ProcessGroupId
= dwProcessGroupId
;
1800 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1802 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepGenerateCtrlEvent
),
1803 sizeof(*GenerateCtrlEventRequest
));
1804 if (!NT_SUCCESS(ApiMessage
.Status
))
1806 BaseSetLastNTError(ApiMessage
.Status
);
1815 IntGetConsoleTitle(LPVOID lpConsoleTitle
, DWORD dwNumChars
, BOOLEAN bUnicode
)
1817 CONSOLE_API_MESSAGE ApiMessage
;
1818 PCONSOLE_GETSETCONSOLETITLE TitleRequest
= &ApiMessage
.Data
.TitleRequest
;
1819 PCSR_CAPTURE_BUFFER CaptureBuffer
;
1821 if (dwNumChars
== 0) return 0;
1823 TitleRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
1824 TitleRequest
->Length
= dwNumChars
* (bUnicode
? sizeof(WCHAR
) : sizeof(CHAR
));
1825 TitleRequest
->Unicode
= bUnicode
;
1827 CaptureBuffer
= CsrAllocateCaptureBuffer(1, TitleRequest
->Length
);
1828 if (CaptureBuffer
== NULL
)
1830 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
1831 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1835 CsrAllocateMessagePointer(CaptureBuffer
,
1836 TitleRequest
->Length
,
1837 (PVOID
*)&TitleRequest
->Title
);
1839 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1841 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepGetTitle
),
1842 sizeof(*TitleRequest
));
1843 if (!NT_SUCCESS(ApiMessage
.Status
))
1845 CsrFreeCaptureBuffer(CaptureBuffer
);
1846 BaseSetLastNTError(ApiMessage
.Status
);
1850 dwNumChars
= TitleRequest
->Length
/ (bUnicode
? sizeof(WCHAR
) : sizeof(CHAR
));
1854 memcpy(lpConsoleTitle
, TitleRequest
->Title
, TitleRequest
->Length
);
1857 ((LPWSTR
)lpConsoleTitle
)[dwNumChars
] = L
'\0';
1859 ((LPSTR
)lpConsoleTitle
)[dwNumChars
] = '\0';
1862 CsrFreeCaptureBuffer(CaptureBuffer
);
1868 /*--------------------------------------------------------------
1875 GetConsoleTitleW(LPWSTR lpConsoleTitle
,
1878 return IntGetConsoleTitle(lpConsoleTitle
, nSize
, TRUE
);
1882 /*--------------------------------------------------------------
1889 GetConsoleTitleA(LPSTR lpConsoleTitle
,
1892 return IntGetConsoleTitle(lpConsoleTitle
, nSize
, FALSE
);
1897 IntSetConsoleTitle(CONST VOID
*lpConsoleTitle
, BOOLEAN bUnicode
)
1899 CONSOLE_API_MESSAGE ApiMessage
;
1900 PCONSOLE_GETSETCONSOLETITLE TitleRequest
= &ApiMessage
.Data
.TitleRequest
;
1901 PCSR_CAPTURE_BUFFER CaptureBuffer
;
1903 ULONG NumChars
= (ULONG
)(lpConsoleTitle
? (bUnicode
? wcslen(lpConsoleTitle
) : strlen(lpConsoleTitle
)) : 0);
1905 TitleRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
1906 TitleRequest
->Length
= NumChars
* (bUnicode
? sizeof(WCHAR
) : sizeof(CHAR
));
1907 TitleRequest
->Unicode
= bUnicode
;
1909 CaptureBuffer
= CsrAllocateCaptureBuffer(1, TitleRequest
->Length
);
1910 if (CaptureBuffer
== NULL
)
1912 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
1913 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1917 CsrCaptureMessageBuffer(CaptureBuffer
,
1918 (PVOID
)lpConsoleTitle
,
1919 TitleRequest
->Length
,
1920 (PVOID
*)&TitleRequest
->Title
);
1922 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1924 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepSetTitle
),
1925 sizeof(*TitleRequest
));
1927 CsrFreeCaptureBuffer(CaptureBuffer
);
1929 if (!NT_SUCCESS(ApiMessage
.Status
))
1931 BaseSetLastNTError(ApiMessage
.Status
);
1938 /*--------------------------------------------------------------
1945 SetConsoleTitleW(LPCWSTR lpConsoleTitle
)
1947 return IntSetConsoleTitle(lpConsoleTitle
, TRUE
);
1951 /*--------------------------------------------------------------
1958 SetConsoleTitleA(LPCSTR lpConsoleTitle
)
1960 return IntSetConsoleTitle(lpConsoleTitle
, FALSE
);
1964 /*--------------------------------------------------------------
1965 * CreateConsoleScreenBuffer
1971 CreateConsoleScreenBuffer(DWORD dwDesiredAccess
,
1973 CONST SECURITY_ATTRIBUTES
*lpSecurityAttributes
,
1975 LPVOID lpScreenBufferData
)
1977 CONSOLE_API_MESSAGE ApiMessage
;
1978 PCONSOLE_CREATESCREENBUFFER CreateScreenBufferRequest
= &ApiMessage
.Data
.CreateScreenBufferRequest
;
1979 PCSR_CAPTURE_BUFFER CaptureBuffer
= NULL
;
1980 PCONSOLE_GRAPHICS_BUFFER_INFO GraphicsBufferInfo
= lpScreenBufferData
;
1982 if ( (dwDesiredAccess
& ~(GENERIC_READ
| GENERIC_WRITE
)) ||
1983 (dwShareMode
& ~(FILE_SHARE_READ
| FILE_SHARE_WRITE
)) ||
1984 (dwFlags
!= CONSOLE_TEXTMODE_BUFFER
&& dwFlags
!= CONSOLE_GRAPHICS_BUFFER
) )
1986 SetLastError(ERROR_INVALID_PARAMETER
);
1987 return INVALID_HANDLE_VALUE
;
1990 CreateScreenBufferRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
1991 CreateScreenBufferRequest
->DesiredAccess
= dwDesiredAccess
;
1992 CreateScreenBufferRequest
->InheritHandle
=
1993 (lpSecurityAttributes
? lpSecurityAttributes
->bInheritHandle
: FALSE
);
1994 CreateScreenBufferRequest
->ShareMode
= dwShareMode
;
1995 CreateScreenBufferRequest
->ScreenBufferType
= dwFlags
;
1997 if (dwFlags
== CONSOLE_GRAPHICS_BUFFER
)
1999 if (CreateScreenBufferRequest
->InheritHandle
|| GraphicsBufferInfo
== NULL
)
2001 SetLastError(ERROR_INVALID_PARAMETER
);
2002 return INVALID_HANDLE_VALUE
;
2005 CreateScreenBufferRequest
->GraphicsBufferInfo
= *GraphicsBufferInfo
;
2007 CaptureBuffer
= CsrAllocateCaptureBuffer(1, GraphicsBufferInfo
->dwBitMapInfoLength
);
2008 if (CaptureBuffer
== NULL
)
2010 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
2011 return INVALID_HANDLE_VALUE
;
2014 CsrCaptureMessageBuffer(CaptureBuffer
,
2015 (PVOID
)GraphicsBufferInfo
->lpBitMapInfo
,
2016 GraphicsBufferInfo
->dwBitMapInfoLength
,
2017 (PVOID
*)&CreateScreenBufferRequest
->GraphicsBufferInfo
.lpBitMapInfo
);
2020 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
2022 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepCreateScreenBuffer
),
2023 sizeof(*CreateScreenBufferRequest
));
2025 if (CaptureBuffer
) CsrFreeCaptureBuffer(CaptureBuffer
);
2027 if (!NT_SUCCESS(ApiMessage
.Status
))
2029 BaseSetLastNTError(ApiMessage
.Status
);
2030 return INVALID_HANDLE_VALUE
;
2033 if (dwFlags
== CONSOLE_GRAPHICS_BUFFER
&& GraphicsBufferInfo
)
2035 GraphicsBufferInfo
->hMutex
= CreateScreenBufferRequest
->hMutex
; // CreateScreenBufferRequest->GraphicsBufferInfo.hMutex ;
2036 GraphicsBufferInfo
->lpBitMap
= CreateScreenBufferRequest
->lpBitMap
; // CreateScreenBufferRequest->GraphicsBufferInfo.lpBitMap;
2039 return CreateScreenBufferRequest
->OutputHandle
;
2043 /*--------------------------------------------------------------
2052 CONSOLE_API_MESSAGE ApiMessage
;
2053 PCONSOLE_GETINPUTOUTPUTCP GetConsoleCPRequest
= &ApiMessage
.Data
.GetConsoleCPRequest
;
2055 /* Get the Input Code Page */
2056 GetConsoleCPRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
2057 GetConsoleCPRequest
->OutputCP
= FALSE
;
2059 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
2061 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepGetCP
),
2062 sizeof(*GetConsoleCPRequest
));
2063 if (!NT_SUCCESS(ApiMessage
.Status
))
2065 BaseSetLastNTError(ApiMessage
.Status
);
2069 return GetConsoleCPRequest
->CodePage
;
2073 /*--------------------------------------------------------------
2080 SetConsoleCP(UINT wCodePageID
)
2082 CONSOLE_API_MESSAGE ApiMessage
;
2083 PCONSOLE_SETINPUTOUTPUTCP SetConsoleCPRequest
= &ApiMessage
.Data
.SetConsoleCPRequest
;
2085 /* Set the Input Code Page */
2086 SetConsoleCPRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
2087 SetConsoleCPRequest
->CodePage
= wCodePageID
;
2088 SetConsoleCPRequest
->OutputCP
= FALSE
;
2089 /* SetConsoleCPRequest->EventHandle; */
2091 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
2093 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepSetCP
),
2094 sizeof(*SetConsoleCPRequest
));
2095 if (!NT_SUCCESS(ApiMessage
.Status
))
2097 BaseSetLastNTError(ApiMessage
.Status
);
2105 /*--------------------------------------------------------------
2106 * GetConsoleOutputCP
2112 GetConsoleOutputCP(VOID
)
2114 CONSOLE_API_MESSAGE ApiMessage
;
2115 PCONSOLE_GETINPUTOUTPUTCP GetConsoleCPRequest
= &ApiMessage
.Data
.GetConsoleCPRequest
;
2117 /* Get the Output Code Page */
2118 GetConsoleCPRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
2119 GetConsoleCPRequest
->OutputCP
= TRUE
;
2121 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
2123 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepGetCP
),
2124 sizeof(*GetConsoleCPRequest
));
2125 if (!NT_SUCCESS(ApiMessage
.Status
))
2127 BaseSetLastNTError(ApiMessage
.Status
);
2131 return GetConsoleCPRequest
->CodePage
;
2135 /*--------------------------------------------------------------
2136 * SetConsoleOutputCP
2142 SetConsoleOutputCP(UINT wCodePageID
)
2144 CONSOLE_API_MESSAGE ApiMessage
;
2145 PCONSOLE_SETINPUTOUTPUTCP SetConsoleCPRequest
= &ApiMessage
.Data
.SetConsoleCPRequest
;
2147 /* Set the Output Code Page */
2148 SetConsoleCPRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
2149 SetConsoleCPRequest
->CodePage
= wCodePageID
;
2150 SetConsoleCPRequest
->OutputCP
= TRUE
;
2151 /* SetConsoleCPRequest->EventHandle; */
2153 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
2155 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepSetCP
),
2156 sizeof(*SetConsoleCPRequest
));
2157 if (!NT_SUCCESS(ApiMessage
.Status
))
2159 BaseSetLastNTError(ApiMessage
.Status
);
2167 /*--------------------------------------------------------------
2168 * GetConsoleProcessList
2174 GetConsoleProcessList(LPDWORD lpdwProcessList
,
2175 DWORD dwProcessCount
)
2177 CONSOLE_API_MESSAGE ApiMessage
;
2178 PCONSOLE_GETPROCESSLIST GetProcessListRequest
= &ApiMessage
.Data
.GetProcessListRequest
;
2179 PCSR_CAPTURE_BUFFER CaptureBuffer
;
2180 ULONG nProcesses
= 0;
2182 if (lpdwProcessList
== NULL
|| dwProcessCount
== 0)
2184 SetLastError(ERROR_INVALID_PARAMETER
);
2188 CaptureBuffer
= CsrAllocateCaptureBuffer(1, dwProcessCount
* sizeof(DWORD
));
2189 if (CaptureBuffer
== NULL
)
2191 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
2192 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
2196 GetProcessListRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
2197 GetProcessListRequest
->ProcessCount
= dwProcessCount
;
2199 CsrAllocateMessagePointer(CaptureBuffer
,
2200 dwProcessCount
* sizeof(DWORD
),
2201 (PVOID
*)&GetProcessListRequest
->ProcessIdsList
);
2203 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
2205 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepGetProcessList
),
2206 sizeof(*GetProcessListRequest
));
2207 if (!NT_SUCCESS(ApiMessage
.Status
))
2209 BaseSetLastNTError(ApiMessage
.Status
);
2213 nProcesses
= GetProcessListRequest
->ProcessCount
;
2214 if (dwProcessCount
>= nProcesses
)
2216 memcpy(lpdwProcessList
, GetProcessListRequest
->ProcessIdsList
, nProcesses
* sizeof(DWORD
));
2220 CsrFreeCaptureBuffer(CaptureBuffer
);
2225 /*--------------------------------------------------------------
2226 * GetConsoleSelectionInfo
2232 GetConsoleSelectionInfo(PCONSOLE_SELECTION_INFO lpConsoleSelectionInfo
)
2234 CONSOLE_API_MESSAGE ApiMessage
;
2235 PCONSOLE_GETSELECTIONINFO GetSelectionInfoRequest
= &ApiMessage
.Data
.GetSelectionInfoRequest
;
2237 if (lpConsoleSelectionInfo
== NULL
)
2239 SetLastError(ERROR_INVALID_PARAMETER
);
2243 GetSelectionInfoRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
2245 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
2247 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepGetSelectionInfo
),
2248 sizeof(*GetSelectionInfoRequest
));
2249 if (!NT_SUCCESS(ApiMessage
.Status
))
2251 BaseSetLastNTError(ApiMessage
.Status
);
2255 *lpConsoleSelectionInfo
= GetSelectionInfoRequest
->Info
;
2261 /*--------------------------------------------------------------
2266 * @note Strongly inspired by AllocConsole.
2270 AttachConsole(DWORD dwProcessId
)
2273 PRTL_USER_PROCESS_PARAMETERS Parameters
= NtCurrentPeb()->ProcessParameters
;
2274 CONSOLE_API_MESSAGE ApiMessage
;
2275 PCONSOLE_ATTACHCONSOLE AttachConsoleRequest
= &ApiMessage
.Data
.AttachConsoleRequest
;
2277 if (Parameters
->ConsoleHandle
)
2279 DPRINT1("AttachConsole: Attaching a console to a process already having one\n");
2280 SetLastError(ERROR_ACCESS_DENIED
);
2284 AttachConsoleRequest
->ProcessId
= dwProcessId
;
2285 AttachConsoleRequest
->CtrlDispatcher
= ConsoleControlDispatcher
;
2286 AttachConsoleRequest
->PropDispatcher
= PropDialogHandler
;
2288 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
2290 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepAttach
),
2291 sizeof(CONSOLE_ATTACHCONSOLE
));
2292 if (!NT_SUCCESS(Status
))
2294 BaseSetLastNTError(Status
);
2298 Parameters
->ConsoleHandle
= AttachConsoleRequest
->ConsoleHandle
;
2299 SetStdHandle(STD_INPUT_HANDLE
, AttachConsoleRequest
->InputHandle
);
2300 SetStdHandle(STD_OUTPUT_HANDLE
, AttachConsoleRequest
->OutputHandle
);
2301 SetStdHandle(STD_ERROR_HANDLE
, AttachConsoleRequest
->ErrorHandle
);
2303 /* Initialize Console Ctrl Handler */
2304 InitConsoleCtrlHandling();
2306 InputWaitHandle
= AttachConsoleRequest
->InputWaitHandle
;
2312 /*--------------------------------------------------------------
2319 GetConsoleWindow(VOID
)
2321 CONSOLE_API_MESSAGE ApiMessage
;
2322 PCONSOLE_GETWINDOW GetWindowRequest
= &ApiMessage
.Data
.GetWindowRequest
;
2324 GetWindowRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
2326 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
2328 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepGetConsoleWindow
),
2329 sizeof(*GetWindowRequest
));
2330 if (!NT_SUCCESS(ApiMessage
.Status
))
2332 BaseSetLastNTError(ApiMessage
.Status
);
2336 return GetWindowRequest
->WindowHandle
;
2340 /*--------------------------------------------------------------
2347 SetConsoleIcon(HICON hIcon
)
2349 CONSOLE_API_MESSAGE ApiMessage
;
2350 PCONSOLE_SETICON SetIconRequest
= &ApiMessage
.Data
.SetIconRequest
;
2352 SetIconRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
2353 SetIconRequest
->IconHandle
= hIcon
;
2355 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
2357 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepSetIcon
),
2358 sizeof(*SetIconRequest
));
2359 if (!NT_SUCCESS(ApiMessage
.Status
))
2361 BaseSetLastNTError(ApiMessage
.Status
);
2369 /******************************************************************************
2370 * \name SetConsoleInputExeNameW
2371 * \brief Sets the console input file name from a unicode string.
2372 * \param lpInputExeName Pointer to a unicode string with the name.
2373 * \return TRUE if successful, FALSE if unsuccsedful.
2374 * \remarks If lpInputExeName is 0 or the string length is 0 or greater than 255,
2375 * the function fails and sets last error to ERROR_INVALID_PARAMETER.
2379 SetConsoleInputExeNameW(LPCWSTR lpInputExeName
)
2383 if ( !lpInputExeName
||
2384 (lenName
= lstrlenW(lpInputExeName
)) == 0 ||
2385 lenName
> INPUTEXENAME_BUFLEN
- 1 )
2387 /* Fail if string is empty or too long */
2388 SetLastError(ERROR_INVALID_PARAMETER
);
2392 RtlEnterCriticalSection(&ConsoleLock
);
2395 RtlCopyMemory(InputExeName
, lpInputExeName
, lenName
* sizeof(WCHAR
));
2396 InputExeName
[lenName
] = L
'\0';
2400 RtlLeaveCriticalSection(&ConsoleLock
);
2408 /******************************************************************************
2409 * \name SetConsoleInputExeNameA
2410 * \brief Sets the console input file name from an ansi string.
2411 * \param lpInputExeName Pointer to an ansi string with the name.
2412 * \return TRUE if successful, FALSE if unsuccsedful.
2413 * \remarks If lpInputExeName is 0 or the string length is 0 or greater than 255,
2414 * the function fails and sets last error to ERROR_INVALID_PARAMETER.
2418 SetConsoleInputExeNameA(LPCSTR lpInputExeName
)
2420 WCHAR Buffer
[INPUTEXENAME_BUFLEN
];
2421 ANSI_STRING InputExeNameA
;
2422 UNICODE_STRING InputExeNameU
;
2425 RtlInitAnsiString(&InputExeNameA
, lpInputExeName
);
2427 if ( InputExeNameA
.Length
== 0 ||
2428 InputExeNameA
.Length
> INPUTEXENAME_BUFLEN
- 1 )
2430 /* Fail if string is empty or too long */
2431 SetLastError(ERROR_INVALID_PARAMETER
);
2435 InputExeNameU
.Buffer
= Buffer
;
2436 InputExeNameU
.MaximumLength
= sizeof(Buffer
);
2437 InputExeNameU
.Length
= 0;
2439 Status
= RtlAnsiStringToUnicodeString(&InputExeNameU
, &InputExeNameA
, FALSE
);
2440 if (!NT_SUCCESS(Status
))
2442 BaseSetLastNTError(Status
);
2446 return SetConsoleInputExeNameW(InputExeNameU
.Buffer
);
2450 /******************************************************************************
2451 * \name GetConsoleInputExeNameW
2452 * \brief Retrieves the console input file name as unicode string.
2453 * \param nBufferLength Length of the buffer in WCHARs.
2454 * Specify 0 to receive the needed buffer length.
2455 * \param lpBuffer Pointer to a buffer that receives the string.
2456 * \return Needed buffer size if \p nBufferLength is 0.
2457 * Otherwise 1 if successful, 2 if buffer is too small.
2458 * \remarks Sets last error value to ERROR_BUFFER_OVERFLOW if the buffer
2459 * is not big enough.
2463 GetConsoleInputExeNameW(DWORD nBufferLength
, LPWSTR lpBuffer
)
2465 ULONG lenName
= lstrlenW(InputExeName
);
2467 if (nBufferLength
== 0)
2469 /* Buffer size is requested, return it */
2473 if (lenName
+ 1 > nBufferLength
)
2475 /* Buffer is not large enough! */
2476 SetLastError(ERROR_BUFFER_OVERFLOW
);
2480 RtlEnterCriticalSection(&ConsoleLock
);
2483 RtlCopyMemory(lpBuffer
, InputExeName
, lenName
* sizeof(WCHAR
));
2484 lpBuffer
[lenName
] = '\0';
2488 RtlLeaveCriticalSection(&ConsoleLock
);
2492 /* Success, return 1 */
2497 /******************************************************************************
2498 * \name GetConsoleInputExeNameA
2499 * \brief Retrieves the console input file name as ansi string.
2500 * \param nBufferLength Length of the buffer in CHARs.
2501 * \param lpBuffer Pointer to a buffer that receives the string.
2502 * \return 1 if successful, 2 if buffer is too small.
2503 * \remarks Sets last error value to ERROR_BUFFER_OVERFLOW if the buffer
2504 * is not big enough. The buffer receives as much characters as fit.
2508 GetConsoleInputExeNameA(DWORD nBufferLength
, LPSTR lpBuffer
)
2510 WCHAR Buffer
[INPUTEXENAME_BUFLEN
];
2512 UNICODE_STRING BufferU
;
2513 ANSI_STRING BufferA
;
2515 /* Get the unicode name */
2516 Ret
= GetConsoleInputExeNameW(sizeof(Buffer
) / sizeof(Buffer
[0]), Buffer
);
2518 /* Initialize strings for conversion */
2519 RtlInitUnicodeString(&BufferU
, Buffer
);
2521 BufferA
.MaximumLength
= (USHORT
)nBufferLength
;
2522 BufferA
.Buffer
= lpBuffer
;
2524 /* Convert unicode name to ansi, copying as much chars as fit */
2525 RtlUnicodeStringToAnsiString(&BufferA
, &BufferU
, FALSE
);
2527 /* Error handling */
2528 if (nBufferLength
<= BufferU
.Length
/ sizeof(WCHAR
))
2530 SetLastError(ERROR_BUFFER_OVERFLOW
);
2539 GetConsoleCharType(HANDLE hConsole
, COORD Coord
, PDWORD Type
)
2547 GetConsoleCursorMode(HANDLE hConsole
, PBOOL pUnknown1
, PBOOL pUnknown2
)
2555 SetConsoleCursorMode(HANDLE hConsole
, BOOL Unknown1
, BOOL Unknown2
)
2563 GetConsoleNlsMode(HANDLE hConsole
, LPDWORD lpMode
)
2571 SetConsoleNlsMode(HANDLE hConsole
, DWORD dwMode
)
2579 SetConsoleLocalEUDC(DWORD Unknown1
, DWORD Unknown2
, DWORD Unknown3
, DWORD Unknown4
)
2587 RegisterConsoleIME(HWND hWnd
, LPDWORD ThreadId
)
2595 RegisterConsoleOS2(BOOL bUnknown
)
2603 SetConsoleOS2OemFormat(BOOL bUnknown
)
2611 UnregisterConsoleIME(VOID
)
2619 IntGetConsoleKeyboardLayoutName(OUT PVOID pszLayoutName
,
2622 CONSOLE_API_MESSAGE ApiMessage
;
2623 PCONSOLE_GETKBDLAYOUTNAME GetKbdLayoutNameRequest
= &ApiMessage
.Data
.GetKbdLayoutNameRequest
;
2625 /* Set up the data to send to the Console Server */
2626 GetKbdLayoutNameRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
2627 GetKbdLayoutNameRequest
->Ansi
= bAnsi
;
2629 /* Call the server */
2630 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
2632 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepGetKeyboardLayoutName
),
2633 sizeof(*GetKbdLayoutNameRequest
));
2635 /* Check for success */
2636 if (!NT_SUCCESS(ApiMessage
.Status
))
2638 BaseSetLastNTError(ApiMessage
.Status
);
2642 /* Retrieve the results */
2645 /* Copy only KL_NAMELENGTH == 9 characters, ANSI or UNICODE */
2647 strncpy(pszLayoutName
, (PCHAR
)GetKbdLayoutNameRequest
->LayoutBuffer
, KL_NAMELENGTH
);
2649 wcsncpy(pszLayoutName
, (PWCHAR
)GetKbdLayoutNameRequest
->LayoutBuffer
, KL_NAMELENGTH
);
2651 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
2653 SetLastError(ERROR_INVALID_ACCESS
);
2654 _SEH2_YIELD(return FALSE
);
2662 * @implemented (undocumented)
2666 GetConsoleKeyboardLayoutNameA(OUT LPSTR pszLayoutName
)
2668 return IntGetConsoleKeyboardLayoutName(pszLayoutName
, TRUE
);
2672 * @implemented (undocumented)
2676 GetConsoleKeyboardLayoutNameW(OUT LPWSTR pszLayoutName
)
2678 return IntGetConsoleKeyboardLayoutName(pszLayoutName
, FALSE
);
2686 SetLastConsoleEventActive(VOID
)
2688 CONSOLE_API_MESSAGE ApiMessage
;
2689 PCONSOLE_NOTIFYLASTCLOSE NotifyLastCloseRequest
= &ApiMessage
.Data
.NotifyLastCloseRequest
;
2691 /* Set the flag used by the console control dispatcher */
2692 LastCloseNotify
= TRUE
;
2694 /* Set up the input arguments */
2695 NotifyLastCloseRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
2697 /* Call CSRSS; just return the NTSTATUS cast to DWORD */
2698 return CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
2700 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepNotifyLastClose
),
2701 sizeof(*NotifyLastCloseRequest
));