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 * PROGRAMMER: 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
;
34 HANDLE InputWaitHandle
= INVALID_HANDLE_VALUE
;
36 #define INPUTEXENAME_BUFLEN 256
37 static WCHAR InputExeName
[INPUTEXENAME_BUFLEN
];
40 /* Default Console Control Handler ********************************************/
44 DefaultConsoleCtrlHandler(DWORD Event
)
46 DPRINT("Default handler called: %lx\n", Event
);
50 DPRINT("Ctrl-C Event\n");
53 case CTRL_BREAK_EVENT
:
54 DPRINT("Ctrl-Break Event\n");
57 case CTRL_CLOSE_EVENT
:
58 DPRINT("Ctrl Close Event\n");
61 case CTRL_LOGOFF_EVENT
:
62 DPRINT("Ctrl Logoff Event\n");
65 case CTRL_SHUTDOWN_EVENT
:
66 DPRINT("Ctrl Shutdown Event\n");
70 ExitProcess(CONTROL_C_EXIT
);
76 ConsoleControlDispatcher(IN LPVOID lpThreadParameter
)
79 DWORD CodeAndFlag
= PtrToUlong(lpThreadParameter
);
80 DWORD nCode
= CodeAndFlag
& MAXLONG
;
82 EXCEPTION_RECORD erException
;
84 DPRINT1("Console Dispatcher Active: %lx %lx\n", CodeAndFlag
, nCode
);
85 SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST
);
90 case CTRL_BREAK_EVENT
:
92 if (IsDebuggerPresent())
94 erException
.ExceptionCode
= (nCode
== CTRL_C_EVENT
?
95 DBG_CONTROL_C
: DBG_CONTROL_BREAK
);
96 erException
.ExceptionFlags
= 0;
97 erException
.ExceptionRecord
= NULL
;
98 erException
.ExceptionAddress
= DefaultConsoleCtrlHandler
;
99 erException
.NumberParameters
= 0;
103 RtlRaiseException(&erException
);
105 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
107 RtlEnterCriticalSection(&ConsoleLock
);
109 if ((nCode
!= CTRL_C_EVENT
) ||
110 (NtCurrentPeb()->ProcessParameters
->ConsoleFlags
!= 1))
112 for (i
= NrCtrlHandlers
; i
> 0; i
--)
114 if (CtrlHandlers
[i
- 1](nCode
)) break;
118 RtlLeaveCriticalSection(&ConsoleLock
);
127 case CTRL_CLOSE_EVENT
:
128 case CTRL_LOGOFF_EVENT
:
129 case CTRL_SHUTDOWN_EVENT
:
137 ExitProcess(CONTROL_C_EXIT
);
145 ASSERT(ConsoleInitialized
);
147 RtlEnterCriticalSection(&ConsoleLock
);
150 if ((nCode
!= CTRL_C_EVENT
) || (NtCurrentPeb()->ProcessParameters
->ConsoleFlags
!= 1))
152 for (i
= NrCtrlHandlers
; i
> 0; i
--)
155 (CodeAndFlag
& MINLONG
) &&
156 ((nCode
== CTRL_LOGOFF_EVENT
) || (nCode
== CTRL_SHUTDOWN_EVENT
)))
158 DPRINT("Skipping system/service apps\n");
162 if (CtrlHandlers
[i
- 1](nCode
))
166 case CTRL_CLOSE_EVENT
:
167 case CTRL_LOGOFF_EVENT
:
168 case CTRL_SHUTDOWN_EVENT
:
170 nExitCode
= CodeAndFlag
;
178 RtlLeaveCriticalSection(&ConsoleLock
);
180 ExitThread(nExitCode
);
181 return STATUS_SUCCESS
;
186 InitConsoleCtrlHandling(VOID
)
188 /* Initialize Console Ctrl Handler */
189 NrAllocatedHandlers
= NrCtrlHandlers
= 1;
190 CtrlHandlers
= InitialHandler
;
191 CtrlHandlers
[0] = DefaultConsoleCtrlHandler
;
195 /* FUNCTIONS ******************************************************************/
198 IntCheckForConsoleFileName(IN LPCWSTR pszName
,
199 IN DWORD dwDesiredAccess
)
201 LPCWSTR ConsoleName
= pszName
;
202 ULONG DeviceNameInfo
;
205 * Check whether we deal with a DOS device, and if so,
206 * strip the path till the file name.
207 * Therefore, things like \\.\CON or C:\some_path\CONIN$
208 * are transformed into CON or CONIN$, for example.
210 DeviceNameInfo
= RtlIsDosDeviceName_U(pszName
);
211 if (DeviceNameInfo
!= 0)
213 ConsoleName
= (LPCWSTR
)((ULONG_PTR
)ConsoleName
+ ((DeviceNameInfo
>> 16) & 0xFFFF));
216 /* Return a standard console "file" name according to what we passed in parameters */
217 if (_wcsicmp(ConsoleName
, BaseConInputFileName
) == 0)
219 return BaseConInputFileName
;
221 else if (_wcsicmp(ConsoleName
, BaseConOutputFileName
) == 0)
223 return BaseConOutputFileName
;
225 else if (_wcsicmp(ConsoleName
, BaseConFileName
) == 0)
227 if ((dwDesiredAccess
& (GENERIC_READ
| GENERIC_WRITE
)) == GENERIC_READ
)
229 return BaseConInputFileName
;
231 else if ((dwDesiredAccess
& (GENERIC_READ
| GENERIC_WRITE
)) == GENERIC_WRITE
)
233 return BaseConOutputFileName
;
237 /* If we are there, that means that either the file name or the desired access are wrong */
243 * @unimplemented (Undocumented)
247 ConsoleMenuControl(HANDLE hConsole
,
251 DPRINT1("ConsoleMenuControl(0x%x, 0x%x, 0x%x) UNIMPLEMENTED!\n", hConsole
, Unknown1
, Unknown2
);
252 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
262 DuplicateConsoleHandle(HANDLE hConsole
,
263 DWORD dwDesiredAccess
,
268 CONSOLE_API_MESSAGE ApiMessage
;
269 PCONSOLE_DUPLICATEHANDLE DuplicateHandleRequest
= &ApiMessage
.Data
.DuplicateHandleRequest
;
271 if ( (dwOptions
& ~(DUPLICATE_CLOSE_SOURCE
| DUPLICATE_SAME_ACCESS
)) ||
272 (!(dwOptions
& DUPLICATE_SAME_ACCESS
) &&
273 (dwDesiredAccess
& ~(GENERIC_READ
| GENERIC_WRITE
))) )
275 SetLastError (ERROR_INVALID_PARAMETER
);
276 return INVALID_HANDLE_VALUE
;
279 DuplicateHandleRequest
->ConsoleHandle
= hConsole
;
280 DuplicateHandleRequest
->Access
= dwDesiredAccess
;
281 DuplicateHandleRequest
->Inheritable
= bInheritHandle
;
282 DuplicateHandleRequest
->Options
= dwOptions
;
284 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
286 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepDuplicateHandle
),
287 sizeof(CONSOLE_DUPLICATEHANDLE
));
288 if (!NT_SUCCESS(Status
))
290 BaseSetLastNTError(Status
);
291 return INVALID_HANDLE_VALUE
;
294 return DuplicateHandleRequest
->ConsoleHandle
;
303 GetConsoleDisplayMode(LPDWORD lpdwMode
)
305 * FUNCTION: Get the console display mode
307 * lpdwMode - Address of variable that receives the current value
309 * STATUS: Undocumented
312 DPRINT1("GetConsoleDisplayMode(0x%x) UNIMPLEMENTED!\n", lpdwMode
);
313 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
319 * @unimplemented (Undocumented)
323 GetConsoleFontInfo(DWORD Unknown0
,
328 DPRINT1("GetConsoleFontInfo(0x%x, 0x%x, 0x%x, 0x%x) UNIMPLEMENTED!\n", Unknown0
, Unknown1
, Unknown2
, Unknown3
);
329 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
339 GetConsoleFontSize(HANDLE hConsoleOutput
,
342 COORD Empty
= {0, 0};
343 DPRINT1("GetConsoleFontSize(0x%x, 0x%x) UNIMPLEMENTED!\n", hConsoleOutput
, nFont
);
344 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
350 * @implemented (Undocumented)
354 GetConsoleHardwareState(HANDLE hConsoleOutput
,
359 CONSOLE_API_MESSAGE ApiMessage
;
360 PCONSOLE_GETSETHWSTATE HardwareStateRequest
= &ApiMessage
.Data
.HardwareStateRequest
;
362 HardwareStateRequest
->OutputHandle
= hConsoleOutput
;
364 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
366 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepGetHardwareState
),
367 sizeof(CONSOLE_GETSETHWSTATE
));
368 if (!NT_SUCCESS(Status
))
370 BaseSetLastNTError(Status
);
374 *State
= HardwareStateRequest
->State
;
380 * @implemented (Undocumented)
384 GetConsoleInputWaitHandle(VOID
)
386 return InputWaitHandle
;
395 GetCurrentConsoleFont(HANDLE hConsoleOutput
,
397 PCONSOLE_FONT_INFO lpConsoleCurrentFont
)
399 DPRINT1("GetCurrentConsoleFont(0x%x, 0x%x, 0x%x) UNIMPLEMENTED!\n", hConsoleOutput
, bMaximumWindow
, lpConsoleCurrentFont
);
400 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
406 * @unimplemented (Undocumented)
410 GetNumberOfConsoleFonts(VOID
)
412 DPRINT1("GetNumberOfConsoleFonts() UNIMPLEMENTED!\n");
413 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
419 * @unimplemented (Undocumented)
423 InvalidateConsoleDIBits(DWORD Unknown0
,
426 DPRINT1("InvalidateConsoleDIBits(0x%x, 0x%x) UNIMPLEMENTED!\n", Unknown0
, Unknown1
);
427 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
433 * @unimplemented (Undocumented)
437 OpenConsoleW(LPCWSTR wsName
,
438 DWORD dwDesiredAccess
,
442 NTSTATUS Status
= STATUS_SUCCESS
;
443 CONSOLE_API_MESSAGE ApiMessage
;
444 PCONSOLE_OPENCONSOLE OpenConsoleRequest
= &ApiMessage
.Data
.OpenConsoleRequest
;
445 CONSOLE_HANDLE_TYPE HandleType
;
447 if (wsName
&& 0 == _wcsicmp(wsName
, BaseConInputFileName
))
449 HandleType
= HANDLE_INPUT
;
451 else if (wsName
&& 0 == _wcsicmp(wsName
, BaseConOutputFileName
))
453 HandleType
= HANDLE_OUTPUT
;
457 SetLastError(ERROR_INVALID_PARAMETER
);
458 return INVALID_HANDLE_VALUE
;
461 if ( (dwDesiredAccess
& ~(GENERIC_READ
| GENERIC_WRITE
)) ||
462 (dwShareMode
& ~(FILE_SHARE_READ
| FILE_SHARE_WRITE
)) )
464 SetLastError(ERROR_INVALID_PARAMETER
);
465 return INVALID_HANDLE_VALUE
;
468 OpenConsoleRequest
->HandleType
= HandleType
;
469 OpenConsoleRequest
->Access
= dwDesiredAccess
;
470 OpenConsoleRequest
->Inheritable
= bInheritHandle
;
471 OpenConsoleRequest
->ShareMode
= dwShareMode
;
473 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
475 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepOpenConsole
),
476 sizeof(CONSOLE_OPENCONSOLE
));
477 if (!NT_SUCCESS(Status
))
479 BaseSetLastNTError(Status
);
480 return INVALID_HANDLE_VALUE
;
483 return OpenConsoleRequest
->ConsoleHandle
;
488 * @unimplemented (Undocumented)
492 SetConsoleCursor(DWORD Unknown0
,
495 DPRINT1("SetConsoleCursor(0x%x, 0x%x) UNIMPLEMENTED!\n", Unknown0
, Unknown1
);
496 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
506 SetConsoleDisplayMode(HANDLE hOut
,
510 * FUNCTION: Set the console display mode.
512 * hOut - Standard output handle.
513 * dwNewMode - New mode.
514 * lpdwOldMode - Address of a variable that receives the old mode.
517 DPRINT1("SetConsoleDisplayMode(0x%x, 0x%x, 0x%p) UNIMPLEMENTED!\n", hOut
, dwNewMode
, lpdwOldMode
);
518 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
524 * @unimplemented (Undocumented)
528 SetConsoleFont(DWORD Unknown0
,
531 DPRINT1("SetConsoleFont(0x%x, 0x%x) UNIMPLEMENTED!\n", Unknown0
, Unknown1
);
532 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
538 * @implemented (Undocumented)
542 SetConsoleHardwareState(HANDLE hConsoleOutput
,
547 CONSOLE_API_MESSAGE ApiMessage
;
548 PCONSOLE_GETSETHWSTATE HardwareStateRequest
= &ApiMessage
.Data
.HardwareStateRequest
;
550 HardwareStateRequest
->OutputHandle
= hConsoleOutput
;
551 HardwareStateRequest
->State
= State
;
553 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
555 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepSetHardwareState
),
556 sizeof(CONSOLE_GETSETHWSTATE
));
557 if (!NT_SUCCESS(Status
))
559 BaseSetLastNTError(Status
);
568 * @unimplemented (Undocumented)
572 SetConsoleKeyShortcuts(DWORD Unknown0
,
577 DPRINT1("SetConsoleKeyShortcuts(0x%x, 0x%x, 0x%x, 0x%x) UNIMPLEMENTED!\n", Unknown0
, Unknown1
, Unknown2
, Unknown3
);
578 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
584 * @unimplemented (Undocumented)
588 SetConsoleMaximumWindowSize(DWORD Unknown0
,
591 DPRINT1("SetConsoleMaximumWindowSize(0x%x, 0x%x) UNIMPLEMENTED!\n", Unknown0
, Unknown1
);
592 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
598 * @unimplemented (Undocumented)
602 SetConsoleMenuClose(DWORD Unknown0
)
604 DPRINT1("SetConsoleMenuClose(0x%x) UNIMPLEMENTED!\n", Unknown0
);
605 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
611 * @unimplemented (Undocumented)
615 SetConsolePalette(DWORD Unknown0
,
619 DPRINT1("SetConsolePalette(0x%x, 0x%x, 0x%x) UNIMPLEMENTED!\n", Unknown0
, Unknown1
, Unknown2
);
620 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
625 * @unimplemented (Undocumented)
629 ShowConsoleCursor(DWORD Unknown0
,
632 DPRINT1("ShowConsoleCursor(0x%x, 0x%x) UNIMPLEMENTED!\n", Unknown0
, Unknown1
);
633 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
639 * FUNCTION: Checks whether the given handle is a valid console handle.
642 * Handle - Handle to be checked
645 * TRUE: Handle is a valid console handle
646 * FALSE: Handle is not a valid console handle.
648 * STATUS: Officially undocumented
654 VerifyConsoleIoHandle(HANDLE Handle
)
657 CONSOLE_API_MESSAGE ApiMessage
;
659 ApiMessage
.Data
.VerifyHandleRequest
.ConsoleHandle
= Handle
;
661 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
663 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepVerifyIoHandle
),
664 sizeof(CONSOLE_VERIFYHANDLE
));
665 if (!NT_SUCCESS(Status
))
667 BaseSetLastNTError(Status
);
680 WriteConsoleInputVDMA(DWORD Unknown0
,
685 DPRINT1("WriteConsoleInputVDMA(0x%x, 0x%x, 0x%x, 0x%x) UNIMPLEMENTED!\n", Unknown0
, Unknown1
, Unknown2
, Unknown3
);
686 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
696 WriteConsoleInputVDMW(DWORD Unknown0
,
701 DPRINT1("WriteConsoleInputVDMW(0x%x, 0x%x, 0x%x, 0x%x) UNIMPLEMENTED!\n", Unknown0
, Unknown1
, Unknown2
, Unknown3
);
702 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
708 * @implemented (Undocumented)
712 CloseConsoleHandle(HANDLE Handle
)
715 CONSOLE_API_MESSAGE ApiMessage
;
717 ApiMessage
.Data
.CloseHandleRequest
.ConsoleHandle
= Handle
;
719 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
721 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepCloseHandle
),
722 sizeof(CONSOLE_CLOSEHANDLE
));
723 if (!NT_SUCCESS(Status
))
725 BaseSetLastNTError(Status
);
738 GetStdHandle(DWORD nStdHandle
)
740 * FUNCTION: Get a handle for the standard input, standard output
741 * and a standard error device.
744 * nStdHandle - Specifies the device for which to return the handle.
746 * RETURNS: If the function succeeds, the return value is the handle
747 * of the specified device. Otherwise the value is INVALID_HANDLE_VALUE.
750 PRTL_USER_PROCESS_PARAMETERS Ppb
= NtCurrentPeb()->ProcessParameters
;
754 case STD_INPUT_HANDLE
:
755 return Ppb
->StandardInput
;
757 case STD_OUTPUT_HANDLE
:
758 return Ppb
->StandardOutput
;
760 case STD_ERROR_HANDLE
:
761 return Ppb
->StandardError
;
764 SetLastError(ERROR_INVALID_HANDLE
);
765 return INVALID_HANDLE_VALUE
;
774 SetStdHandle(DWORD nStdHandle
,
777 * FUNCTION: Set the handle for the standard input, standard output or
778 * the standard error device.
781 * nStdHandle - Specifies the handle to be set.
782 * hHandle - The handle to set.
784 * RETURNS: TRUE if the function succeeds, FALSE otherwise.
787 PRTL_USER_PROCESS_PARAMETERS Ppb
= NtCurrentPeb()->ProcessParameters
;
789 /* no need to check if hHandle == INVALID_HANDLE_VALUE */
793 case STD_INPUT_HANDLE
:
794 Ppb
->StandardInput
= hHandle
;
797 case STD_OUTPUT_HANDLE
:
798 Ppb
->StandardOutput
= hHandle
;
801 case STD_ERROR_HANDLE
:
802 Ppb
->StandardError
= hHandle
;
806 /* Windows for whatever reason sets the last error to ERROR_INVALID_HANDLE here */
807 SetLastError(ERROR_INVALID_HANDLE
);
812 /*--------------------------------------------------------------
822 PRTL_USER_PROCESS_PARAMETERS Parameters
= NtCurrentPeb()->ProcessParameters
;
823 CONSOLE_API_MESSAGE ApiMessage
;
824 PCONSOLE_ALLOCCONSOLE AllocConsoleRequest
= &ApiMessage
.Data
.AllocConsoleRequest
;
825 PCSR_CAPTURE_BUFFER CaptureBuffer
;
828 if (Parameters
->ConsoleHandle
)
830 DPRINT1("AllocConsole: Allocating a console to a process already having one\n");
831 SetLastError(ERROR_ACCESS_DENIED
);
835 CaptureBuffer
= CsrAllocateCaptureBuffer(2, sizeof(CONSOLE_START_INFO
) +
836 (MAX_PATH
+ 1) * sizeof(WCHAR
));
837 if (CaptureBuffer
== NULL
)
839 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
840 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
844 CsrAllocateMessagePointer(CaptureBuffer
,
845 sizeof(CONSOLE_START_INFO
),
846 (PVOID
*)&AllocConsoleRequest
->ConsoleStartInfo
);
848 CsrAllocateMessagePointer(CaptureBuffer
,
849 (MAX_PATH
+ 1) * sizeof(WCHAR
),
850 (PVOID
*)&AllocConsoleRequest
->AppPath
);
852 /** Copied from BasepInitConsole **********************************************/
853 InitConsoleInfo(AllocConsoleRequest
->ConsoleStartInfo
);
855 Length
= min(MAX_PATH
, Parameters
->ImagePathName
.Length
/ sizeof(WCHAR
));
856 wcsncpy(AllocConsoleRequest
->AppPath
, Parameters
->ImagePathName
.Buffer
, Length
);
857 AllocConsoleRequest
->AppPath
[Length
] = L
'\0';
858 /******************************************************************************/
860 AllocConsoleRequest
->Console
= NULL
;
861 AllocConsoleRequest
->CtrlDispatcher
= ConsoleControlDispatcher
;
862 AllocConsoleRequest
->PropDispatcher
= PropDialogHandler
;
864 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
866 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepAlloc
),
867 sizeof(CONSOLE_ALLOCCONSOLE
));
869 CsrFreeCaptureBuffer(CaptureBuffer
);
871 if (!NT_SUCCESS(Status
))
873 BaseSetLastNTError(Status
);
877 Parameters
->ConsoleHandle
= AllocConsoleRequest
->Console
;
878 SetStdHandle(STD_INPUT_HANDLE
, AllocConsoleRequest
->InputHandle
);
879 SetStdHandle(STD_OUTPUT_HANDLE
, AllocConsoleRequest
->OutputHandle
);
880 SetStdHandle(STD_ERROR_HANDLE
, AllocConsoleRequest
->ErrorHandle
);
882 /* Initialize Console Ctrl Handler */
883 InitConsoleCtrlHandling();
885 InputWaitHandle
= AllocConsoleRequest
->InputWaitHandle
;
891 /*--------------------------------------------------------------
900 // AG: I'm not sure if this is correct (what happens to std handles?)
901 // but I just tried to reverse what AllocConsole() does...
904 CONSOLE_API_MESSAGE ApiMessage
;
906 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
908 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepFree
),
909 sizeof(CONSOLE_FREECONSOLE
));
910 if (!NT_SUCCESS(Status
))
912 BaseSetLastNTError(Status
);
916 NtCurrentPeb()->ProcessParameters
->ConsoleHandle
= NULL
;
918 CloseHandle(InputWaitHandle
);
919 InputWaitHandle
= INVALID_HANDLE_VALUE
;
925 /*--------------------------------------------------------------
926 * GetConsoleScreenBufferInfo
932 GetConsoleScreenBufferInfo(HANDLE hConsoleOutput
,
933 PCONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo
)
936 CONSOLE_API_MESSAGE ApiMessage
;
938 if (lpConsoleScreenBufferInfo
== NULL
)
940 SetLastError(ERROR_INVALID_PARAMETER
);
944 ApiMessage
.Data
.ScreenBufferInfoRequest
.OutputHandle
= hConsoleOutput
;
946 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
948 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepGetScreenBufferInfo
),
949 sizeof(CONSOLE_GETSCREENBUFFERINFO
));
950 if (!NT_SUCCESS(Status
))
952 BaseSetLastNTError(Status
);
956 *lpConsoleScreenBufferInfo
= ApiMessage
.Data
.ScreenBufferInfoRequest
.Info
;
962 /*--------------------------------------------------------------
963 * SetConsoleCursorPosition
969 SetConsoleCursorPosition(HANDLE hConsoleOutput
,
970 COORD dwCursorPosition
)
973 CONSOLE_API_MESSAGE ApiMessage
;
975 ApiMessage
.Data
.SetCursorPositionRequest
.OutputHandle
= hConsoleOutput
;
976 ApiMessage
.Data
.SetCursorPositionRequest
.Position
= dwCursorPosition
;
978 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
980 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepSetCursorPosition
),
981 sizeof(CONSOLE_SETCURSORPOSITION
));
982 if (!NT_SUCCESS(Status
))
984 BaseSetLastNTError(Status
);
992 /*--------------------------------------------------------------
999 GetConsoleMode(HANDLE hConsoleHandle
,
1003 CONSOLE_API_MESSAGE ApiMessage
;
1004 PCONSOLE_GETSETCONSOLEMODE ConsoleModeRequest
= &ApiMessage
.Data
.ConsoleModeRequest
;
1006 ConsoleModeRequest
->ConsoleHandle
= hConsoleHandle
;
1008 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1010 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepGetMode
),
1011 sizeof(CONSOLE_GETSETCONSOLEMODE
));
1012 if (!NT_SUCCESS(Status
))
1014 BaseSetLastNTError(Status
);
1018 *lpMode
= ConsoleModeRequest
->ConsoleMode
;
1024 /*--------------------------------------------------------------
1025 * GetNumberOfConsoleInputEvents
1031 GetNumberOfConsoleInputEvents(HANDLE hConsoleInput
,
1032 LPDWORD lpNumberOfEvents
)
1035 CONSOLE_API_MESSAGE ApiMessage
;
1036 PCONSOLE_GETNUMINPUTEVENTS GetNumInputEventsRequest
= &ApiMessage
.Data
.GetNumInputEventsRequest
;
1038 GetNumInputEventsRequest
->InputHandle
= hConsoleInput
;
1039 GetNumInputEventsRequest
->NumInputEvents
= 0;
1041 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1043 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepGetNumberOfInputEvents
),
1044 sizeof(CONSOLE_GETNUMINPUTEVENTS
));
1045 if (!NT_SUCCESS(Status
))
1047 BaseSetLastNTError(Status
);
1051 if (lpNumberOfEvents
== NULL
)
1053 SetLastError(ERROR_INVALID_ACCESS
);
1057 *lpNumberOfEvents
= GetNumInputEventsRequest
->NumInputEvents
;
1063 /*--------------------------------------------------------------
1064 * GetLargestConsoleWindowSize
1070 GetLargestConsoleWindowSize(HANDLE hConsoleOutput
)
1072 COORD Coord
= {80,25};
1073 DPRINT1("GetLargestConsoleWindowSize(0x%x) UNIMPLEMENTED!\n", hConsoleOutput
);
1074 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1079 /*--------------------------------------------------------------
1080 * GetConsoleCursorInfo
1086 GetConsoleCursorInfo(HANDLE hConsoleOutput
,
1087 PCONSOLE_CURSOR_INFO lpConsoleCursorInfo
)
1090 CONSOLE_API_MESSAGE ApiMessage
;
1092 if (!lpConsoleCursorInfo
)
1094 if (!hConsoleOutput
)
1095 SetLastError(ERROR_INVALID_HANDLE
);
1097 SetLastError(ERROR_INVALID_ACCESS
);
1102 ApiMessage
.Data
.CursorInfoRequest
.OutputHandle
= hConsoleOutput
;
1104 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1106 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepGetCursorInfo
),
1107 sizeof(CONSOLE_GETSETCURSORINFO
));
1108 if (!NT_SUCCESS(Status
))
1110 BaseSetLastNTError(Status
);
1114 *lpConsoleCursorInfo
= ApiMessage
.Data
.CursorInfoRequest
.Info
;
1120 /*--------------------------------------------------------------
1121 * GetNumberOfConsoleMouseButtons
1127 GetNumberOfConsoleMouseButtons(LPDWORD lpNumberOfMouseButtons
)
1129 DPRINT1("GetNumberOfConsoleMouseButtons(0x%x) UNIMPLEMENTED!\n", lpNumberOfMouseButtons
);
1130 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1135 /*--------------------------------------------------------------
1142 SetConsoleMode(HANDLE hConsoleHandle
,
1146 CONSOLE_API_MESSAGE ApiMessage
;
1147 PCONSOLE_GETSETCONSOLEMODE ConsoleModeRequest
= &ApiMessage
.Data
.ConsoleModeRequest
;
1149 ConsoleModeRequest
->ConsoleHandle
= hConsoleHandle
;
1150 ConsoleModeRequest
->ConsoleMode
= dwMode
;
1152 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1154 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepSetMode
),
1155 sizeof(CONSOLE_GETSETCONSOLEMODE
));
1156 if (!NT_SUCCESS(Status
))
1158 BaseSetLastNTError(Status
);
1166 /*--------------------------------------------------------------
1167 * SetConsoleActiveScreenBuffer
1173 SetConsoleActiveScreenBuffer(HANDLE hConsoleOutput
)
1176 CONSOLE_API_MESSAGE ApiMessage
;
1178 ApiMessage
.Data
.SetScreenBufferRequest
.OutputHandle
= hConsoleOutput
;
1180 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1182 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepSetActiveScreenBuffer
),
1183 sizeof(CONSOLE_SETACTIVESCREENBUFFER
));
1184 if (!NT_SUCCESS(Status
))
1186 BaseSetLastNTError(Status
);
1194 /*--------------------------------------------------------------
1195 * FlushConsoleInputBuffer
1201 FlushConsoleInputBuffer(HANDLE hConsoleInput
)
1204 CONSOLE_API_MESSAGE ApiMessage
;
1206 ApiMessage
.Data
.FlushInputBufferRequest
.InputHandle
= hConsoleInput
;
1208 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1210 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepFlushInputBuffer
),
1211 sizeof(CONSOLE_FLUSHINPUTBUFFER
));
1212 if (!NT_SUCCESS(Status
))
1214 BaseSetLastNTError(Status
);
1222 /*--------------------------------------------------------------
1223 * SetConsoleScreenBufferSize
1229 SetConsoleScreenBufferSize(HANDLE hConsoleOutput
,
1233 CONSOLE_API_MESSAGE ApiMessage
;
1235 ApiMessage
.Data
.SetScreenBufferSizeRequest
.OutputHandle
= hConsoleOutput
;
1236 ApiMessage
.Data
.SetScreenBufferSizeRequest
.Size
= dwSize
;
1238 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1240 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepSetScreenBufferSize
),
1241 sizeof(CONSOLE_SETSCREENBUFFERSIZE
));
1242 if (!NT_SUCCESS(Status
))
1244 BaseSetLastNTError(Status
);
1252 /*--------------------------------------------------------------
1253 * SetConsoleCursorInfo
1259 SetConsoleCursorInfo(HANDLE hConsoleOutput
,
1260 CONST CONSOLE_CURSOR_INFO
*lpConsoleCursorInfo
)
1263 CONSOLE_API_MESSAGE ApiMessage
;
1265 ApiMessage
.Data
.CursorInfoRequest
.OutputHandle
= hConsoleOutput
;
1266 ApiMessage
.Data
.CursorInfoRequest
.Info
= *lpConsoleCursorInfo
;
1268 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1270 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepSetCursorInfo
),
1271 sizeof(CONSOLE_GETSETCURSORINFO
));
1272 if (!NT_SUCCESS(Status
))
1274 BaseSetLastNTError(Status
);
1284 IntScrollConsoleScreenBuffer(HANDLE hConsoleOutput
,
1285 const SMALL_RECT
*lpScrollRectangle
,
1286 const SMALL_RECT
*lpClipRectangle
,
1287 COORD dwDestinationOrigin
,
1288 const CHAR_INFO
*lpFill
,
1292 CONSOLE_API_MESSAGE ApiMessage
;
1293 PCONSOLE_SCROLLSCREENBUFFER ScrollScreenBufferRequest
= &ApiMessage
.Data
.ScrollScreenBufferRequest
;
1295 ScrollScreenBufferRequest
->OutputHandle
= hConsoleOutput
;
1296 ScrollScreenBufferRequest
->Unicode
= bUnicode
;
1297 ScrollScreenBufferRequest
->ScrollRectangle
= *lpScrollRectangle
;
1299 if (lpClipRectangle
!= NULL
)
1301 ScrollScreenBufferRequest
->UseClipRectangle
= TRUE
;
1302 ScrollScreenBufferRequest
->ClipRectangle
= *lpClipRectangle
;
1306 ScrollScreenBufferRequest
->UseClipRectangle
= FALSE
;
1309 ScrollScreenBufferRequest
->DestinationOrigin
= dwDestinationOrigin
;
1310 ScrollScreenBufferRequest
->Fill
= *lpFill
;
1312 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1314 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepScrollScreenBuffer
),
1315 sizeof(CONSOLE_SCROLLSCREENBUFFER
));
1317 if (!NT_SUCCESS(Status
))
1319 BaseSetLastNTError(Status
);
1327 /*--------------------------------------------------------------
1328 * ScrollConsoleScreenBufferA
1334 ScrollConsoleScreenBufferA(HANDLE hConsoleOutput
,
1335 CONST SMALL_RECT
*lpScrollRectangle
,
1336 CONST SMALL_RECT
*lpClipRectangle
,
1337 COORD dwDestinationOrigin
,
1338 CONST CHAR_INFO
*lpFill
)
1340 return IntScrollConsoleScreenBuffer(hConsoleOutput
,
1341 (PSMALL_RECT
)lpScrollRectangle
,
1342 (PSMALL_RECT
)lpClipRectangle
,
1343 dwDestinationOrigin
,
1349 /*--------------------------------------------------------------
1350 * ScrollConsoleScreenBufferW
1356 ScrollConsoleScreenBufferW(HANDLE hConsoleOutput
,
1357 CONST SMALL_RECT
*lpScrollRectangle
,
1358 CONST SMALL_RECT
*lpClipRectangle
,
1359 COORD dwDestinationOrigin
,
1360 CONST CHAR_INFO
*lpFill
)
1362 return IntScrollConsoleScreenBuffer(hConsoleOutput
,
1365 dwDestinationOrigin
,
1371 /*--------------------------------------------------------------
1372 * SetConsoleWindowInfo
1378 SetConsoleWindowInfo(HANDLE hConsoleOutput
,
1380 CONST SMALL_RECT
*lpConsoleWindow
)
1382 DPRINT1("SetConsoleWindowInfo(0x%x, 0x%x, 0x%x) UNIMPLEMENTED!\n", hConsoleOutput
, bAbsolute
, lpConsoleWindow
);
1383 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1388 /*--------------------------------------------------------------
1389 * SetConsoleTextAttribute
1395 SetConsoleTextAttribute(HANDLE hConsoleOutput
,
1399 CONSOLE_API_MESSAGE ApiMessage
;
1401 ApiMessage
.Data
.SetTextAttribRequest
.OutputHandle
= hConsoleOutput
;
1402 ApiMessage
.Data
.SetTextAttribRequest
.Attrib
= wAttributes
;
1404 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1406 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepSetTextAttribute
),
1407 sizeof(CONSOLE_SETTEXTATTRIB
));
1408 if (!NT_SUCCESS(Status
))
1410 BaseSetLastNTError(Status
);
1420 AddConsoleCtrlHandler(PHANDLER_ROUTINE HandlerRoutine
)
1422 PHANDLER_ROUTINE
* NewCtrlHandlers
= NULL
;
1424 if (HandlerRoutine
== NULL
)
1426 NtCurrentPeb()->ProcessParameters
->ConsoleFlags
= TRUE
;
1430 if (NrCtrlHandlers
== NrAllocatedHandlers
)
1432 NewCtrlHandlers
= RtlAllocateHeap(RtlGetProcessHeap(),
1434 (NrCtrlHandlers
+ 4) * sizeof(PHANDLER_ROUTINE
));
1435 if (NewCtrlHandlers
== NULL
)
1437 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1441 memmove(NewCtrlHandlers
, CtrlHandlers
, sizeof(PHANDLER_ROUTINE
) * NrCtrlHandlers
);
1443 if (NrAllocatedHandlers
> 1) RtlFreeHeap(RtlGetProcessHeap(), 0, CtrlHandlers
);
1445 CtrlHandlers
= NewCtrlHandlers
;
1446 NrAllocatedHandlers
+= 4;
1449 ASSERT(NrCtrlHandlers
< NrAllocatedHandlers
);
1451 CtrlHandlers
[NrCtrlHandlers
++] = HandlerRoutine
;
1458 RemoveConsoleCtrlHandler(PHANDLER_ROUTINE HandlerRoutine
)
1462 if (HandlerRoutine
== NULL
)
1464 NtCurrentPeb()->ProcessParameters
->ConsoleFlags
= FALSE
;
1468 for (i
= 0; i
< NrCtrlHandlers
; i
++)
1470 if (CtrlHandlers
[i
] == HandlerRoutine
)
1472 if (i
< (NrCtrlHandlers
- 1))
1474 memmove(&CtrlHandlers
[i
],
1476 (NrCtrlHandlers
- i
+ 1) * sizeof(PHANDLER_ROUTINE
));
1484 SetLastError(ERROR_INVALID_PARAMETER
);
1494 SetConsoleCtrlHandler(PHANDLER_ROUTINE HandlerRoutine
,
1499 RtlEnterCriticalSection(&BaseDllDirectoryLock
);
1502 Ret
= AddConsoleCtrlHandler(HandlerRoutine
);
1506 Ret
= RemoveConsoleCtrlHandler(HandlerRoutine
);
1509 RtlLeaveCriticalSection(&BaseDllDirectoryLock
);
1514 /*--------------------------------------------------------------
1515 * GenerateConsoleCtrlEvent
1521 GenerateConsoleCtrlEvent(DWORD dwCtrlEvent
,
1522 DWORD dwProcessGroupId
)
1525 CONSOLE_API_MESSAGE ApiMessage
;
1527 if (dwCtrlEvent
!= CTRL_C_EVENT
&& dwCtrlEvent
!= CTRL_BREAK_EVENT
)
1529 SetLastError(ERROR_INVALID_PARAMETER
);
1533 ApiMessage
.Data
.GenerateCtrlEventRequest
.Event
= dwCtrlEvent
;
1534 ApiMessage
.Data
.GenerateCtrlEventRequest
.ProcessGroup
= dwProcessGroupId
;
1536 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1538 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepGenerateCtrlEvent
),
1539 sizeof(CONSOLE_GENERATECTRLEVENT
));
1540 if (!NT_SUCCESS(Status
))
1542 BaseSetLastNTError(Status
);
1551 IntGetConsoleTitle(LPVOID lpConsoleTitle
, DWORD nSize
, BOOL bUnicode
)
1554 CONSOLE_API_MESSAGE ApiMessage
;
1555 PCONSOLE_GETSETCONSOLETITLE TitleRequest
= &ApiMessage
.Data
.TitleRequest
;
1556 PCSR_CAPTURE_BUFFER CaptureBuffer
;
1558 if (nSize
== 0) return 0;
1560 TitleRequest
->Length
= nSize
* (bUnicode
? 1 : sizeof(WCHAR
));
1561 CaptureBuffer
= CsrAllocateCaptureBuffer(1, TitleRequest
->Length
);
1562 if (CaptureBuffer
== NULL
)
1564 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
1565 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1569 CsrAllocateMessagePointer(CaptureBuffer
,
1570 TitleRequest
->Length
,
1571 (PVOID
*)&TitleRequest
->Title
);
1573 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1575 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepGetTitle
),
1576 sizeof(CONSOLE_GETSETCONSOLETITLE
));
1577 if (!NT_SUCCESS(Status
))
1579 CsrFreeCaptureBuffer(CaptureBuffer
);
1580 BaseSetLastNTError(Status
);
1586 if (nSize
>= sizeof(WCHAR
))
1587 wcscpy((LPWSTR
)lpConsoleTitle
, TitleRequest
->Title
);
1591 if (nSize
< TitleRequest
->Length
/ sizeof(WCHAR
) ||
1592 !WideCharToMultiByte(CP_ACP
, // ANSI code page
1593 0, // performance and mapping flags
1594 TitleRequest
->Title
, // address of wide-character string
1595 -1, // number of characters in string
1596 (LPSTR
)lpConsoleTitle
, // address of buffer for new string
1597 nSize
, // size of buffer
1601 /* Yes, if the buffer isn't big enough, it returns 0... Bad API */
1602 *(LPSTR
)lpConsoleTitle
= '\0';
1603 TitleRequest
->Length
= 0;
1606 CsrFreeCaptureBuffer(CaptureBuffer
);
1608 return TitleRequest
->Length
/ sizeof(WCHAR
);
1612 /*--------------------------------------------------------------
1619 GetConsoleTitleW(LPWSTR lpConsoleTitle
,
1622 return IntGetConsoleTitle(lpConsoleTitle
, nSize
, TRUE
);
1626 /*--------------------------------------------------------------
1633 GetConsoleTitleA(LPSTR lpConsoleTitle
,
1636 return IntGetConsoleTitle(lpConsoleTitle
, nSize
, FALSE
);
1640 /*--------------------------------------------------------------
1647 SetConsoleTitleW(LPCWSTR lpConsoleTitle
)
1650 CONSOLE_API_MESSAGE ApiMessage
;
1651 PCONSOLE_GETSETCONSOLETITLE TitleRequest
= &ApiMessage
.Data
.TitleRequest
;
1652 PCSR_CAPTURE_BUFFER CaptureBuffer
;
1654 TitleRequest
->Length
= wcslen(lpConsoleTitle
) * sizeof(WCHAR
);
1656 CaptureBuffer
= CsrAllocateCaptureBuffer(1, TitleRequest
->Length
);
1657 if (CaptureBuffer
== NULL
)
1659 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
1660 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1664 CsrCaptureMessageBuffer(CaptureBuffer
,
1665 (PVOID
)lpConsoleTitle
,
1666 TitleRequest
->Length
,
1667 (PVOID
*)&TitleRequest
->Title
);
1669 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1671 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepSetTitle
),
1672 sizeof(CONSOLE_GETSETCONSOLETITLE
));
1674 CsrFreeCaptureBuffer(CaptureBuffer
);
1676 if (!NT_SUCCESS(Status
))
1678 BaseSetLastNTError(Status
);
1686 /*--------------------------------------------------------------
1693 SetConsoleTitleA(LPCSTR lpConsoleTitle
)
1696 ULONG Length
= strlen(lpConsoleTitle
) + 1;
1697 LPWSTR WideTitle
= HeapAlloc(GetProcessHeap(), 0, Length
* sizeof(WCHAR
));
1701 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1705 MultiByteToWideChar(CP_ACP
, 0, lpConsoleTitle
, -1, WideTitle
, Length
);
1707 Ret
= SetConsoleTitleW(WideTitle
);
1709 HeapFree(GetProcessHeap(), 0, WideTitle
);
1714 /*--------------------------------------------------------------
1715 * CreateConsoleScreenBuffer
1721 CreateConsoleScreenBuffer(DWORD dwDesiredAccess
,
1723 CONST SECURITY_ATTRIBUTES
*lpSecurityAttributes
,
1725 LPVOID lpScreenBufferData
)
1728 CONSOLE_API_MESSAGE ApiMessage
;
1730 if ( (dwDesiredAccess
& ~(GENERIC_READ
| GENERIC_WRITE
)) ||
1731 (dwShareMode
& ~(FILE_SHARE_READ
| FILE_SHARE_WRITE
)) ||
1732 (dwFlags
!= CONSOLE_TEXTMODE_BUFFER
) )
1734 SetLastError(ERROR_INVALID_PARAMETER
);
1735 return INVALID_HANDLE_VALUE
;
1738 ApiMessage
.Data
.CreateScreenBufferRequest
.Access
= dwDesiredAccess
;
1739 ApiMessage
.Data
.CreateScreenBufferRequest
.ShareMode
= dwShareMode
;
1740 ApiMessage
.Data
.CreateScreenBufferRequest
.Inheritable
=
1741 (lpSecurityAttributes
? lpSecurityAttributes
->bInheritHandle
: FALSE
);
1743 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1745 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepCreateScreenBuffer
),
1746 sizeof(CONSOLE_CREATESCREENBUFFER
));
1747 if (!NT_SUCCESS(Status
))
1749 BaseSetLastNTError(Status
);
1750 return INVALID_HANDLE_VALUE
;
1753 return ApiMessage
.Data
.CreateScreenBufferRequest
.OutputHandle
;
1757 /*--------------------------------------------------------------
1767 CONSOLE_API_MESSAGE ApiMessage
;
1769 /* Get the Input Code Page */
1770 ApiMessage
.Data
.ConsoleCPRequest
.InputCP
= TRUE
;
1772 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1774 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepGetCP
),
1775 sizeof(CONSOLE_GETSETINPUTOUTPUTCP
));
1776 if (!NT_SUCCESS(Status
))
1778 BaseSetLastNTError(Status
);
1782 return ApiMessage
.Data
.ConsoleCPRequest
.CodePage
;
1786 /*--------------------------------------------------------------
1793 SetConsoleCP(UINT wCodePageID
)
1796 CONSOLE_API_MESSAGE ApiMessage
;
1798 /* Set the Input Code Page */
1799 ApiMessage
.Data
.ConsoleCPRequest
.InputCP
= TRUE
;
1800 ApiMessage
.Data
.ConsoleCPRequest
.CodePage
= wCodePageID
;
1802 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1804 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepSetCP
),
1805 sizeof(CONSOLE_GETSETINPUTOUTPUTCP
));
1806 if (!NT_SUCCESS(Status
))
1808 BaseSetLastNTError(Status
);
1811 return NT_SUCCESS(Status
);
1815 /*--------------------------------------------------------------
1816 * GetConsoleOutputCP
1822 GetConsoleOutputCP(VOID
)
1825 CONSOLE_API_MESSAGE ApiMessage
;
1827 /* Get the Output Code Page */
1828 ApiMessage
.Data
.ConsoleCPRequest
.InputCP
= FALSE
;
1830 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1832 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepGetCP
),
1833 sizeof(CONSOLE_GETSETINPUTOUTPUTCP
));
1834 if (!NT_SUCCESS(Status
))
1836 BaseSetLastNTError (Status
);
1840 return ApiMessage
.Data
.ConsoleCPRequest
.CodePage
;
1844 /*--------------------------------------------------------------
1845 * SetConsoleOutputCP
1851 SetConsoleOutputCP(UINT wCodePageID
)
1854 CONSOLE_API_MESSAGE ApiMessage
;
1856 /* Set the Output Code Page */
1857 ApiMessage
.Data
.ConsoleCPRequest
.InputCP
= FALSE
;
1858 ApiMessage
.Data
.ConsoleCPRequest
.CodePage
= wCodePageID
;
1860 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1862 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepSetCP
),
1863 sizeof(CONSOLE_GETSETINPUTOUTPUTCP
));
1864 if (!NT_SUCCESS(Status
))
1866 BaseSetLastNTError(Status
);
1869 return NT_SUCCESS(Status
);
1873 /*--------------------------------------------------------------
1874 * GetConsoleProcessList
1880 GetConsoleProcessList(LPDWORD lpdwProcessList
,
1881 DWORD dwProcessCount
)
1884 CONSOLE_API_MESSAGE ApiMessage
;
1885 PCONSOLE_GETPROCESSLIST GetProcessListRequest
= &ApiMessage
.Data
.GetProcessListRequest
;
1886 PCSR_CAPTURE_BUFFER CaptureBuffer
;
1889 if (lpdwProcessList
== NULL
|| dwProcessCount
== 0)
1891 SetLastError(ERROR_INVALID_PARAMETER
);
1895 CaptureBuffer
= CsrAllocateCaptureBuffer(1, dwProcessCount
* sizeof(DWORD
));
1896 if (CaptureBuffer
== NULL
)
1898 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
1899 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1903 GetProcessListRequest
->nMaxIds
= dwProcessCount
;
1905 CsrAllocateMessagePointer(CaptureBuffer
,
1906 dwProcessCount
* sizeof(DWORD
),
1907 (PVOID
*)&GetProcessListRequest
->pProcessIds
);
1909 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1911 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepGetProcessList
),
1912 sizeof(CONSOLE_GETPROCESSLIST
));
1913 if (!NT_SUCCESS(Status
))
1915 BaseSetLastNTError (Status
);
1920 nProcesses
= GetProcessListRequest
->nProcessIdsTotal
;
1921 if (dwProcessCount
>= nProcesses
)
1923 memcpy(lpdwProcessList
, GetProcessListRequest
->pProcessIds
, nProcesses
* sizeof(DWORD
));
1927 CsrFreeCaptureBuffer(CaptureBuffer
);
1932 /*--------------------------------------------------------------
1933 * GetConsoleSelectionInfo
1939 GetConsoleSelectionInfo(PCONSOLE_SELECTION_INFO lpConsoleSelectionInfo
)
1942 CONSOLE_API_MESSAGE ApiMessage
;
1944 if (lpConsoleSelectionInfo
== NULL
)
1946 SetLastError(ERROR_INVALID_PARAMETER
);
1950 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1952 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepGetSelectionInfo
),
1953 sizeof(CONSOLE_GETSELECTIONINFO
));
1954 if (!NT_SUCCESS(Status
))
1956 BaseSetLastNTError(Status
);
1960 *lpConsoleSelectionInfo
= ApiMessage
.Data
.GetSelectionInfoRequest
.Info
;
1965 /*--------------------------------------------------------------
1970 * @note Strongly inspired by AllocConsole.
1974 AttachConsole(DWORD dwProcessId
)
1977 PRTL_USER_PROCESS_PARAMETERS Parameters
= NtCurrentPeb()->ProcessParameters
;
1978 CONSOLE_API_MESSAGE ApiMessage
;
1979 PCONSOLE_ATTACHCONSOLE AttachConsoleRequest
= &ApiMessage
.Data
.AttachConsoleRequest
;
1981 if (Parameters
->ConsoleHandle
)
1983 DPRINT1("AttachConsole: Attaching a console to a process already having one\n");
1984 SetLastError(ERROR_ACCESS_DENIED
);
1988 AttachConsoleRequest
->ProcessId
= dwProcessId
;
1989 AttachConsoleRequest
->CtrlDispatcher
= ConsoleControlDispatcher
;
1990 AttachConsoleRequest
->PropDispatcher
= PropDialogHandler
;
1992 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
1994 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepAttach
),
1995 sizeof(CONSOLE_ATTACHCONSOLE
));
1996 if (!NT_SUCCESS(Status
))
1998 BaseSetLastNTError(Status
);
2002 Parameters
->ConsoleHandle
= AttachConsoleRequest
->Console
;
2003 SetStdHandle(STD_INPUT_HANDLE
, AttachConsoleRequest
->InputHandle
);
2004 SetStdHandle(STD_OUTPUT_HANDLE
, AttachConsoleRequest
->OutputHandle
);
2005 SetStdHandle(STD_ERROR_HANDLE
, AttachConsoleRequest
->ErrorHandle
);
2007 /* Initialize Console Ctrl Handler */
2008 InitConsoleCtrlHandling();
2010 InputWaitHandle
= AttachConsoleRequest
->InputWaitHandle
;
2016 /*--------------------------------------------------------------
2023 GetConsoleWindow(VOID
)
2026 CONSOLE_API_MESSAGE ApiMessage
;
2028 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
2030 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepGetConsoleWindow
),
2031 sizeof(CONSOLE_GETWINDOW
));
2032 if (!NT_SUCCESS(Status
))
2034 BaseSetLastNTError(Status
);
2038 return ApiMessage
.Data
.GetWindowRequest
.WindowHandle
;
2042 /*--------------------------------------------------------------
2049 SetConsoleIcon(HICON hicon
)
2052 CONSOLE_API_MESSAGE ApiMessage
;
2054 ApiMessage
.Data
.SetIconRequest
.WindowIcon
= hicon
;
2056 Status
= CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
2058 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepSetIcon
),
2059 sizeof(CONSOLE_SETICON
));
2060 if (!NT_SUCCESS(Status
))
2062 BaseSetLastNTError(Status
);
2066 return NT_SUCCESS(Status
);
2070 /******************************************************************************
2071 * \name SetConsoleInputExeNameW
2072 * \brief Sets the console input file name from a unicode string.
2073 * \param lpInputExeName Pointer to a unicode string with the name.
2074 * \return TRUE if successful, FALSE if unsuccsedful.
2075 * \remarks If lpInputExeName is 0 or the string length is 0 or greater than 255,
2076 * the function fails and sets last error to ERROR_INVALID_PARAMETER.
2080 SetConsoleInputExeNameW(LPCWSTR lpInputExeName
)
2084 if ( !lpInputExeName
||
2085 (lenName
= lstrlenW(lpInputExeName
)) == 0 ||
2086 lenName
> INPUTEXENAME_BUFLEN
- 1 )
2088 /* Fail if string is empty or too long */
2089 SetLastError(ERROR_INVALID_PARAMETER
);
2093 RtlEnterCriticalSection(&ConsoleLock
);
2096 RtlCopyMemory(InputExeName
, lpInputExeName
, lenName
* sizeof(WCHAR
));
2097 InputExeName
[lenName
] = L
'\0';
2101 RtlLeaveCriticalSection(&ConsoleLock
);
2109 /******************************************************************************
2110 * \name SetConsoleInputExeNameA
2111 * \brief Sets the console input file name from an ansi string.
2112 * \param lpInputExeName Pointer to an ansi string with the name.
2113 * \return TRUE if successful, FALSE if unsuccsedful.
2114 * \remarks If lpInputExeName is 0 or the string length is 0 or greater than 255,
2115 * the function fails and sets last error to ERROR_INVALID_PARAMETER.
2119 SetConsoleInputExeNameA(LPCSTR lpInputExeName
)
2121 WCHAR Buffer
[INPUTEXENAME_BUFLEN
];
2122 ANSI_STRING InputExeNameA
;
2123 UNICODE_STRING InputExeNameU
;
2126 RtlInitAnsiString(&InputExeNameA
, lpInputExeName
);
2128 if ( InputExeNameA
.Length
== 0 ||
2129 InputExeNameA
.Length
> INPUTEXENAME_BUFLEN
- 1 )
2131 /* Fail if string is empty or too long */
2132 SetLastError(ERROR_INVALID_PARAMETER
);
2136 InputExeNameU
.Buffer
= Buffer
;
2137 InputExeNameU
.MaximumLength
= sizeof(Buffer
);
2138 InputExeNameU
.Length
= 0;
2140 Status
= RtlAnsiStringToUnicodeString(&InputExeNameU
, &InputExeNameA
, FALSE
);
2141 if (!NT_SUCCESS(Status
))
2143 BaseSetLastNTError(Status
);
2147 return SetConsoleInputExeNameW(InputExeNameU
.Buffer
);
2151 /******************************************************************************
2152 * \name GetConsoleInputExeNameW
2153 * \brief Retrieves the console input file name as unicode string.
2154 * \param nBufferLength Length of the buffer in WCHARs.
2155 * Specify 0 to recieve the needed buffer length.
2156 * \param lpBuffer Pointer to a buffer that recieves the string.
2157 * \return Needed buffer size if \p nBufferLength is 0.
2158 * Otherwise 1 if successful, 2 if buffer is too small.
2159 * \remarks Sets last error value to ERROR_BUFFER_OVERFLOW if the buffer
2160 * is not big enough.
2164 GetConsoleInputExeNameW(DWORD nBufferLength
, LPWSTR lpBuffer
)
2166 int lenName
= lstrlenW(InputExeName
);
2168 if (nBufferLength
== 0)
2170 /* Buffer size is requested, return it */
2174 if (lenName
+ 1 > nBufferLength
)
2176 /* Buffer is not large enough! */
2177 SetLastError(ERROR_BUFFER_OVERFLOW
);
2181 RtlEnterCriticalSection(&ConsoleLock
);
2184 RtlCopyMemory(lpBuffer
, InputExeName
, lenName
* sizeof(WCHAR
));
2185 lpBuffer
[lenName
] = '\0';
2189 RtlLeaveCriticalSection(&ConsoleLock
);
2193 /* Success, return 1 */
2198 /******************************************************************************
2199 * \name GetConsoleInputExeNameA
2200 * \brief Retrieves the console input file name as ansi string.
2201 * \param nBufferLength Length of the buffer in CHARs.
2202 * \param lpBuffer Pointer to a buffer that recieves the string.
2203 * \return 1 if successful, 2 if buffer is too small.
2204 * \remarks Sets last error value to ERROR_BUFFER_OVERFLOW if the buffer
2205 * is not big enough. The buffer recieves as much characters as fit.
2209 GetConsoleInputExeNameA(DWORD nBufferLength
, LPSTR lpBuffer
)
2211 WCHAR Buffer
[INPUTEXENAME_BUFLEN
];
2213 UNICODE_STRING BufferU
;
2214 ANSI_STRING BufferA
;
2216 /* Get the unicode name */
2217 Ret
= GetConsoleInputExeNameW(sizeof(Buffer
) / sizeof(Buffer
[0]), Buffer
);
2219 /* Initialize strings for conversion */
2220 RtlInitUnicodeString(&BufferU
, Buffer
);
2222 BufferA
.MaximumLength
= nBufferLength
;
2223 BufferA
.Buffer
= lpBuffer
;
2225 /* Convert unicode name to ansi, copying as much chars as fit */
2226 RtlUnicodeStringToAnsiString(&BufferA
, &BufferU
, FALSE
);
2228 /* Error handling */
2229 if (nBufferLength
<= BufferU
.Length
/ sizeof(WCHAR
))
2231 SetLastError(ERROR_BUFFER_OVERFLOW
);
2240 GetConsoleCharType(HANDLE hConsole
, COORD Coord
, PDWORD Type
)
2248 GetConsoleCursorMode(HANDLE hConsole
, PBOOL pUnknown1
, PBOOL pUnknown2
)
2256 GetConsoleNlsMode(HANDLE hConsole
, LPDWORD lpMode
)
2264 SetConsoleCursorMode(HANDLE hConsole
, BOOL Unknown1
, BOOL Unknown2
)
2272 SetConsoleLocalEUDC(DWORD Unknown1
, DWORD Unknown2
, DWORD Unknown3
, DWORD Unknown4
)
2280 SetConsoleNlsMode(HANDLE hConsole
, DWORD dwMode
)
2288 RegisterConsoleIME(HWND hWnd
, LPDWORD ThreadId
)
2296 RegisterConsoleOS2(BOOL bUnknown
)
2304 SetConsoleOS2OemFormat(BOOL bUnknown
)
2312 UnregisterConsoleIME(VOID
)
2322 BOOL WINAPI
GetConsoleKeyboardLayoutNameA(LPSTR name
)
2331 BOOL WINAPI
GetConsoleKeyboardLayoutNameW(LPWSTR name
)
2342 SetLastConsoleEventActive(VOID
)