ReactOS 0.4.17-dev-116-ga4b6fe9
console.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define HANDLE_DETACHED_PROCESS   (HANDLE)-1
 
#define HANDLE_CREATE_NEW_CONSOLE   (HANDLE)-2
 
#define HANDLE_CREATE_NO_WINDOW   (HANDLE)-3
 

Functions

BOOLEAN WINAPI ConDllInitialize (IN ULONG Reason, IN PWSTR SessionDir)
 
VOID InitializeCtrlHandling (VOID)
 
DWORD WINAPI ConsoleControlDispatcher (IN LPVOID lpThreadParameter)
 
DWORD WINAPI PropDialogHandler (IN LPVOID lpThreadParameter)
 
BOOL WINAPI GetConsoleHandleInformation (IN HANDLE hHandle, OUT LPDWORD lpdwFlags)
 
BOOL WINAPI SetConsoleHandleInformation (IN HANDLE hHandle, IN DWORD dwMask, IN DWORD dwFlags)
 
HANDLE TranslateStdHandle (HANDLE hHandle)
 
VOID SetTEBLangID (VOID)
 Internal helper function used to synchronize the current thread's language ID with the one from the console.
 
VOID SetUpConsoleInfo (IN BOOLEAN CaptureTitle, IN OUT LPDWORD pTitleLength, IN OUT LPWSTR *lpTitle OPTIONAL, IN OUT LPDWORD pDesktopLength, IN OUT LPWSTR *lpDesktop OPTIONAL, IN OUT PCONSOLE_START_INFO ConsoleStartInfo)
 
VOID SetUpHandles (IN PCONSOLE_START_INFO ConsoleStartInfo)
 
VOID InitExeName (VOID)
 
VOID SetUpAppName (IN BOOLEAN CaptureStrings, IN OUT LPDWORD CurDirLength, IN OUT LPWSTR *CurDir, IN OUT LPDWORD AppNameLength, IN OUT LPWSTR *AppName)
 
USHORT GetCurrentExeName (OUT PWCHAR ExeName, IN USHORT BufferSize)
 
LPCWSTR IntCheckForConsoleFileName (IN LPCWSTR pszName, IN DWORD dwDesiredAccess)
 

Macro Definition Documentation

◆ HANDLE_CREATE_NEW_CONSOLE

#define HANDLE_CREATE_NEW_CONSOLE   (HANDLE)-2

Definition at line 14 of file console.h.

◆ HANDLE_CREATE_NO_WINDOW

#define HANDLE_CREATE_NO_WINDOW   (HANDLE)-3

Definition at line 15 of file console.h.

◆ HANDLE_DETACHED_PROCESS

#define HANDLE_DETACHED_PROCESS   (HANDLE)-1

Definition at line 13 of file console.h.

Function Documentation

◆ ConDllInitialize()

BOOLEAN WINAPI ConDllInitialize ( IN ULONG  Reason,
IN PWSTR  SessionDir 
)

Definition at line 550 of file init.c.

552{
555 BOOLEAN InServerProcess = FALSE;
556 CONSRV_API_CONNECTINFO ConnectInfo;
557
559 {
561 {
562 /* Sync the new thread's LangId with the console's one */
563 SetTEBLangID();
564 }
565 else if (Reason == DLL_PROCESS_DETACH)
566 {
567 /* Free our resources */
569 {
572 }
573 }
574
575 return TRUE;
576 }
577
578 DPRINT("ConDllInitialize for: %wZ\n"
579 "Our current console handles are: 0x%p, 0x%p, 0x%p 0x%p\n",
580 &Parameters->ImagePathName,
581 Parameters->ConsoleHandle,
582 Parameters->StandardInput,
583 Parameters->StandardOutput,
584 Parameters->StandardError);
585
586 /* Initialize our global console DLL lock */
588 if (!NT_SUCCESS(Status)) return FALSE;
590
591 /* Show by default the console window when applicable */
592 ConnectInfo.IsWindowVisible = TRUE;
593 /* If this is a console app, a console will be created/opened */
594 ConnectInfo.IsConsoleApp = IsConsoleApp();
595
596 /* Do nothing if this is not a console app... */
597 if (!ConnectInfo.IsConsoleApp)
598 {
599 DPRINT("Image is not a console application\n");
600 }
601
602 /*
603 * Handle the special flags given to us by BasePushProcessParameters.
604 */
605 if (Parameters->ConsoleHandle == HANDLE_DETACHED_PROCESS)
606 {
607 /* No console to create */
608 DPRINT("No console to create\n");
609 /*
610 * The new process does not inherit its parent's console and cannot
611 * attach to the console of its parent. The new process can call the
612 * AllocConsole function at a later time to create a console.
613 */
614 Parameters->ConsoleHandle = NULL; // Do not inherit the parent's console.
615 ConnectInfo.IsConsoleApp = FALSE; // Do not create any console.
616 }
617 else if (Parameters->ConsoleHandle == HANDLE_CREATE_NEW_CONSOLE)
618 {
619 /* We'll get the real one soon */
620 DPRINT("Creating a new separate console\n");
621 /*
622 * The new process has a new console, instead of inheriting
623 * its parent's console.
624 */
625 Parameters->ConsoleHandle = NULL; // Do not inherit the parent's console.
626 }
627 else if (Parameters->ConsoleHandle == HANDLE_CREATE_NO_WINDOW)
628 {
629 /* We'll get the real one soon */
630 DPRINT("Creating a new invisible console\n");
631 /*
632 * The process is a console application that is being run
633 * without a console window. Therefore, the console handle
634 * for the application is not set.
635 */
636 Parameters->ConsoleHandle = NULL; // Do not inherit the parent's console.
637 ConnectInfo.IsWindowVisible = FALSE; // A console is created but is not shown to the user.
638 }
639 else
640 {
641 DPRINT("Using existing console: 0x%p\n", Parameters->ConsoleHandle);
642 }
643
644 /* Do nothing if this is not a console app... */
645 if (!ConnectInfo.IsConsoleApp)
646 {
647 /* Do not inherit the parent's console if we are not a console app */
648 Parameters->ConsoleHandle = NULL;
649 }
650
651 /* Now use the proper console handle */
652 ConnectInfo.ConsoleStartInfo.ConsoleHandle = Parameters->ConsoleHandle;
653
654 /* Initialize the console dispatchers */
656 ConnectInfo.PropRoutine = PropDialogHandler;
657 ConnectInfo.ImeRoutine = ConsoleIMERoutine;
658
659 /* Set up the console properties */
660 if (ConnectInfo.IsConsoleApp && Parameters->ConsoleHandle == NULL)
661 {
662 /*
663 * We can set up the console properties only if we create a new one
664 * (we do not inherit it from our parent).
665 */
666
667 LPWSTR ConsoleTitle = ConnectInfo.ConsoleTitle;
668
669 ConnectInfo.TitleLength = sizeof(ConnectInfo.ConsoleTitle);
670 ConnectInfo.DesktopLength = 0; // SetUpConsoleInfo will give us the real length.
671
673 &ConnectInfo.TitleLength,
674 &ConsoleTitle,
675 &ConnectInfo.DesktopLength,
676 &ConnectInfo.Desktop,
677 &ConnectInfo.ConsoleStartInfo);
678 DPRINT("ConsoleTitle = '%S' - Desktop = '%S'\n",
679 ConsoleTitle, ConnectInfo.Desktop);
680 }
681 else
682 {
683 ConnectInfo.TitleLength = 0;
684 ConnectInfo.DesktopLength = 0;
685 }
686
687 /* Initialize the Input EXE name */
688 if (ConnectInfo.IsConsoleApp)
689 {
690 LPWSTR CurDir = ConnectInfo.CurDir;
691 LPWSTR AppName = ConnectInfo.AppName;
692
693 InitExeName();
694
695 ConnectInfo.CurDirLength = sizeof(ConnectInfo.CurDir);
696 ConnectInfo.AppNameLength = sizeof(ConnectInfo.AppName);
697
699 &ConnectInfo.CurDirLength,
700 &CurDir,
701 &ConnectInfo.AppNameLength,
702 &AppName);
703 DPRINT("CurDir = '%S' - AppName = '%S'\n",
704 CurDir, AppName);
705 }
706 else
707 {
708 ConnectInfo.CurDirLength = 0;
709 ConnectInfo.AppNameLength = 0;
710 }
711
712 /*
713 * Initialize Console Ctrl Handling, that needs to be supported by
714 * all applications, especially because it is used at shutdown.
715 */
717
718 /* Connect to the Console Server */
719 if (!ConnectConsole(SessionDir,
720 &ConnectInfo,
721 &InServerProcess))
722 {
723 // DPRINT1("Failed to connect to the Console Server (Status %lx)\n", Status);
724 return FALSE;
725 }
726
727 /* If we are not doing server-to-server init and if this is a console app... */
728 if (!InServerProcess && ConnectInfo.IsConsoleApp)
729 {
730 /* ... set the handles that we got */
731 if (Parameters->ConsoleHandle == NULL)
732 SetUpHandles(&ConnectInfo.ConsoleStartInfo);
733
735
736 /* Sync the current thread's LangId with the console's one */
737 SetTEBLangID();
738 }
739
740 DPRINT("Console setup: 0x%p, 0x%p, 0x%p, 0x%p\n",
741 Parameters->ConsoleHandle,
742 Parameters->StandardInput,
743 Parameters->StandardOutput,
744 Parameters->StandardError);
745
746 return TRUE;
747}
#define NtCurrentPeb()
Definition: FLS.c:22
unsigned char BOOLEAN
Definition: actypes.h:127
LONG NTSTATUS
Definition: precomp.h:26
static CHAR AppName[MAX_PATH]
Definition: dem.c:252
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
#define DLL_PROCESS_ATTACH
Definition: compat.h:131
#define DLL_PROCESS_DETACH
Definition: compat.h:130
#define DLL_THREAD_ATTACH
Definition: compat.h:132
VOID InitExeName(VOID)
Definition: console.c:216
DWORD WINAPI ConsoleControlDispatcher(IN LPVOID lpThreadParameter)
Definition: console.c:89
VOID SetTEBLangID(VOID)
Internal helper function used to synchronize the current thread's language ID with the one from the c...
Definition: console.c:3177
VOID InitializeCtrlHandling(VOID)
Definition: console.c:204
VOID SetUpAppName(IN BOOLEAN CaptureStrings, IN OUT LPDWORD CurDirLength, IN OUT LPWSTR *CurDir, IN OUT LPDWORD AppNameLength, IN OUT LPWSTR *AppName)
Definition: console.c:264
DWORD WINAPI PropDialogHandler(IN LPVOID lpThreadParameter)
Definition: init.c:35
VOID SetUpConsoleInfo(IN BOOLEAN CaptureTitle, IN OUT LPDWORD pTitleLength, IN OUT LPWSTR *lpTitle OPTIONAL, IN OUT LPDWORD pDesktopLength, IN OUT LPWSTR *lpDesktop OPTIONAL, IN OUT PCONSOLE_START_INFO ConsoleStartInfo)
Definition: init.c:135
RTL_CRITICAL_SECTION ConsoleLock
Definition: init.c:24
static BOOLEAN ConnectConsole(IN PWSTR SessionDir, IN PCONSRV_API_CONNECTINFO ConnectInfo, OUT PBOOLEAN InServerProcess)
Definition: init.c:286
DWORD WINAPI ConsoleIMERoutine(_In_ PVOID unused)
This function is called from winsrv.dll, in the context of the console application,...
Definition: init.c:489
HANDLE InputWaitHandle
Definition: console.c:38
VOID SetUpHandles(IN PCONSOLE_START_INFO ConsoleStartInfo)
Definition: init.c:251
static BOOLEAN IsConsoleApp(VOID)
Definition: init.c:277
BOOLEAN ConsoleInitialized
Definition: init.c:25
#define HANDLE_CREATE_NEW_CONSOLE
Definition: console.h:14
#define HANDLE_CREATE_NO_WINDOW
Definition: console.h:15
#define HANDLE_DETACHED_PROCESS
Definition: console.h:13
Status
Definition: gdiplustypes.h:25
PVOID PVOID PWCHAR PVOID USHORT PULONG Reason
Definition: env.c:47
NTSYSAPI NTSTATUS NTAPI RtlDeleteCriticalSection(_In_ PRTL_CRITICAL_SECTION CriticalSection)
NTSYSAPI NTSTATUS NTAPI RtlInitializeCriticalSection(_In_ PRTL_CRITICAL_SECTION CriticalSection)
#define DPRINT
Definition: sndvol32.h:73
HANDLE InputWaitHandle
Definition: conmsg.h:171
HANDLE ConsoleHandle
Definition: conmsg.h:170
BOOLEAN IsWindowVisible
Definition: conmsg.h:189
WCHAR ConsoleTitle[MAX_PATH+1]
Definition: conmsg.h:198
LPTHREAD_START_ROUTINE PropRoutine
Definition: conmsg.h:194
LPTHREAD_START_ROUTINE ImeRoutine
Definition: conmsg.h:195
WCHAR AppName[128]
Definition: conmsg.h:204
WCHAR CurDir[MAX_PATH+1]
Definition: conmsg.h:206
CONSOLE_START_INFO ConsoleStartInfo
Definition: conmsg.h:186
LPTHREAD_START_ROUTINE CtrlRoutine
Definition: conmsg.h:193
uint16_t * LPWSTR
Definition: typedefs.h:56
_Must_inspect_result_ _In_ WDFQUEUE _In_opt_ WDFREQUEST _In_opt_ WDFFILEOBJECT _Inout_opt_ PWDF_REQUEST_PARAMETERS Parameters
Definition: wdfio.h:869

Referenced by DllMain().

◆ ConsoleControlDispatcher()

DWORD WINAPI ConsoleControlDispatcher ( IN LPVOID  lpThreadParameter)

Definition at line 89 of file console.c.

90{
91 DWORD nExitCode = 0;
92 DWORD CodeAndFlag = PtrToUlong(lpThreadParameter);
93 DWORD nCode = CodeAndFlag & MAXLONG;
94 UINT i;
95 EXCEPTION_RECORD erException;
96
97 DPRINT1("Console Dispatcher Active: %lx %lx\n", CodeAndFlag, nCode);
99
100 switch(nCode)
101 {
102 case CTRL_C_EVENT:
103 case CTRL_BREAK_EVENT:
104 {
105 if (IsDebuggerPresent())
106 {
107 erException.ExceptionCode = (nCode == CTRL_C_EVENT ?
109 erException.ExceptionFlags = 0;
110 erException.ExceptionRecord = NULL;
112 erException.NumberParameters = 0;
113
115 {
116 RtlRaiseException(&erException);
117 }
119 {
121
122 if ((nCode != CTRL_C_EVENT) ||
123 (NtCurrentPeb()->ProcessParameters->ConsoleFlags != 1))
124 {
125 for (i = NrCtrlHandlers; i > 0; i--)
126 {
127 if (CtrlHandlers[i - 1](nCode)) break;
128 }
129 }
130
132 }
133 _SEH2_END;
134
135 ExitThread(0);
136 }
137 break;
138 }
139
140 case CTRL_CLOSE_EVENT:
143 break;
144
146 /*
147 * In case the console app hasn't register for last close notification,
148 * just kill this console handler thread. We don't want that such apps
149 * get killed for unexpected reasons. On the contrary apps that registered
150 * can be killed because they expect to be.
151 */
153 break;
154
155 case 4:
157 break;
158
159 default:
160 ASSERT(FALSE);
161 break;
162 }
163
165
167
168 nExitCode = 0;
169 if ((nCode != CTRL_C_EVENT) || (NtCurrentPeb()->ProcessParameters->ConsoleFlags != 1))
170 {
171 for (i = NrCtrlHandlers; i > 0; i--)
172 {
173 if ((i == 1) &&
174 (CodeAndFlag & MINLONG) &&
175 ((nCode == CTRL_LOGOFF_EVENT) || (nCode == CTRL_SHUTDOWN_EVENT)))
176 {
177 DPRINT("Skipping system/service apps\n");
178 break;
179 }
180
181 if (CtrlHandlers[i - 1](nCode))
182 {
183 switch(nCode)
184 {
185 case CTRL_CLOSE_EVENT:
189 nExitCode = CodeAndFlag;
190 break;
191 }
192 break;
193 }
194 }
195 }
196
198
199 ExitThread(nExitCode);
200 return STATUS_SUCCESS;
201}
#define DPRINT1
Definition: precomp.h:8
static ULONG NrCtrlHandlers
Definition: console.c:31
static PHANDLER_ROUTINE * CtrlHandlers
Definition: console.c:30
RTL_CRITICAL_SECTION ConsoleLock
Definition: init.c:24
static BOOL WINAPI DefaultConsoleCtrlHandler(DWORD Event)
Definition: console.c:53
static BOOLEAN LastCloseNotify
Definition: console.c:33
BOOLEAN ConsoleInitialized
Definition: init.c:25
VOID WINAPI ExitProcess(IN UINT uExitCode)
Definition: proc.c:1331
BOOL WINAPI SetThreadPriority(IN HANDLE hThread, IN int nPriority)
Definition: thread.c:700
VOID WINAPI ExitThread(IN DWORD uExitCode)
Definition: thread.c:365
BOOL WINAPI IsDebuggerPresent(void)
Definition: debug.c:167
#define PtrToUlong(u)
Definition: config.h:107
unsigned long DWORD
Definition: ntddk_ex.h:95
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:90
#define CONTROL_C_EXIT
Definition: minwinbase.h:67
#define ASSERT(a)
Definition: mode.c:44
unsigned int UINT
Definition: ndis.h:50
NTSYSAPI NTSTATUS NTAPI RtlEnterCriticalSection(_In_ PRTL_CRITICAL_SECTION CriticalSection)
NTSYSAPI NTSTATUS NTAPI RtlLeaveCriticalSection(_In_ PRTL_CRITICAL_SECTION CriticalSection)
NTSYSAPI VOID NTAPI RtlRaiseException(_In_ PEXCEPTION_RECORD ExceptionRecord)
#define DBG_CONTROL_BREAK
Definition: ntstatus.h:106
#define DBG_CONTROL_C
Definition: ntstatus.h:103
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:104
#define _SEH2_END
Definition: pseh2_64.h:194
#define _SEH2_TRY
Definition: pseh2_64.h:93
#define STATUS_SUCCESS
Definition: shellext.h:65
struct _EXCEPTION_RECORD * ExceptionRecord
Definition: compat.h:210
DWORD ExceptionCode
Definition: compat.h:208
DWORD NumberParameters
Definition: compat.h:212
DWORD ExceptionFlags
Definition: compat.h:209
PVOID ExceptionAddress
Definition: compat.h:211
#define MAXLONG
Definition: umtypes.h:116
#define MINLONG
Definition: umtypes.h:115
HANDLE WINAPI GetCurrentThread(void)
Definition: proc.c:1146
#define THREAD_PRIORITY_HIGHEST
Definition: winbase.h:301
#define CTRL_C_EVENT
Definition: wincon.h:96
#define CTRL_SHUTDOWN_EVENT
Definition: wincon.h:102
#define CTRL_BREAK_EVENT
Definition: wincon.h:97
#define CTRL_LOGOFF_EVENT
Definition: wincon.h:101
#define CTRL_CLOSE_EVENT
Definition: wincon.h:98
#define CTRL_LAST_CLOSE_EVENT
Definition: wincon_undoc.h:75

Referenced by AllocConsole(), AttachConsole(), and ConDllInitialize().

◆ GetConsoleHandleInformation()

BOOL WINAPI GetConsoleHandleInformation ( IN HANDLE  hHandle,
OUT LPDWORD  lpdwFlags 
)

Definition at line 465 of file console.c.

467{
468 CONSOLE_API_MESSAGE ApiMessage;
469 PCONSOLE_GETHANDLEINFO GetHandleInfoRequest = &ApiMessage.Data.GetHandleInfoRequest;
470
471 GetHandleInfoRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
472 GetHandleInfoRequest->Handle = hHandle;
473
475 NULL,
477 sizeof(*GetHandleInfoRequest));
478 if (!NT_SUCCESS(ApiMessage.Status))
479 {
480 BaseSetLastNTError(ApiMessage.Status);
481 return FALSE;
482 }
483
484 *lpdwFlags = GetHandleInfoRequest->Flags;
485
486 return TRUE;
487}
@ ConsolepGetHandleInformation
Definition: conmsg.h:53
#define CONSRV_SERVERDLL_INDEX
Definition: conmsg.h:15
#define CSR_CREATE_API_NUMBER(ServerId, ApiId)
Definition: csrmsg.h:37
DWORD BaseSetLastNTError(IN NTSTATUS Status)
Definition: reactos.cpp:167
NTSTATUS NTAPI CsrClientCallServer(_Inout_ PCSR_API_MESSAGE ApiMessage, _Inout_opt_ PCSR_CAPTURE_BUFFER CaptureBuffer, _In_ CSR_API_NUMBER ApiNumber, _In_ ULONG DataLength)
Definition: connect.c:366
union _CONSOLE_API_MESSAGE::@3830 Data
CONSOLE_GETHANDLEINFO GetHandleInfoRequest
Definition: conmsg.h:938
NTSTATUS Status
Definition: csrmsg.h:110

Referenced by GetHandleInformation().

◆ GetCurrentExeName()

USHORT GetCurrentExeName ( OUT PWCHAR  ExeName,
IN USHORT  BufferSize 
)

Definition at line 316 of file console.c.

318{
319 USHORT ExeLength;
320
322 {
324
325 if (BufferSize > ExeNameLength * sizeof(WCHAR))
326 BufferSize = ExeNameLength * sizeof(WCHAR);
327
329
331 ExeLength = BufferSize;
332 }
333 else
334 {
335 *ExeName = UNICODE_NULL;
336 ExeLength = 0;
337 }
338
339 return ExeLength;
340}
#define BufferSize
Definition: mmc.h:75
static WCHAR ExeNameBuffer[EXENAME_LENGTH]
Definition: console.c:43
static BOOLEAN ExeNameInitialized
Definition: console.c:42
static RTL_CRITICAL_SECTION ExeNameLock
Definition: console.c:41
static USHORT ExeNameLength
Definition: console.c:44
#define UNICODE_NULL
short WCHAR
Definition: pedump.c:58
unsigned short USHORT
Definition: pedump.c:61
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
_In_ WDFMEMORY _Out_opt_ size_t * BufferSize
Definition: wdfmemory.h:254

Referenced by IntReadConsole().

◆ InitExeName()

VOID InitExeName ( VOID  )

Definition at line 216 of file console.c.

217{
221 PLDR_DATA_TABLE_ENTRY ImageEntry;
222
223 if (ExeNameInitialized) return;
224
225 /* Initialize the EXE name lock */
227 if (!NT_SUCCESS(Status)) return;
229
232 InLoadOrderLinks);
233
234 /* Retrieve the EXE name, NULL-terminate it... */
235 ExeNameLength = min(sizeof(ExeNameBuffer)/sizeof(ExeNameBuffer[0]),
236 ImageEntry->BaseDllName.Length / sizeof(WCHAR));
238 ImageEntry->BaseDllName.Buffer,
239 ImageEntry->BaseDllName.Length);
241
242 /* ... and retrieve the current directory path and NULL-terminate it. */
243 StartDirLength = min(sizeof(StartDirBuffer)/sizeof(StartDirBuffer[0]),
244 CurrentDirectory->DosPath.Length / sizeof(WCHAR));
246 CurrentDirectory->DosPath.Buffer,
247 CurrentDirectory->DosPath.Length);
249}
WCHAR CurrentDirectory[1024]
Definition: chkdsk.c:74
static WCHAR StartDirBuffer[MAX_PATH+1]
Definition: console.c:45
static USHORT StartDirLength
Definition: console.c:46
PPEB Peb
Definition: dllmain.c:27
#define min(a, b)
Definition: monoChain.cc:55
Definition: btrfs_drv.h:1876
UNICODE_STRING BaseDllName
Definition: ldrtypes.h:149
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
LIST_ENTRY InLoadOrderModuleList
Definition: ldrtypes.h:124
PPEB_LDR_DATA Ldr
Definition: btrfs_drv.h:1912
PRTL_USER_PROCESS_PARAMETERS ProcessParameters
Definition: btrfs_drv.h:1913
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260

Referenced by AllocConsole(), and ConDllInitialize().

◆ InitializeCtrlHandling()

VOID InitializeCtrlHandling ( VOID  )

Definition at line 204 of file console.c.

205{
206 /* Initialize Console Ctrl Handler */
210}
static ULONG NrAllocatedHandlers
Definition: console.c:32
static PHANDLER_ROUTINE InitialHandler[1]
Definition: console.c:29

Referenced by AllocConsole(), AttachConsole(), and ConDllInitialize().

◆ IntCheckForConsoleFileName()

LPCWSTR IntCheckForConsoleFileName ( IN LPCWSTR  pszName,
IN DWORD  dwDesiredAccess 
)

Definition at line 345 of file console.c.

347{
348 LPCWSTR ConsoleName = pszName;
349 ULONG DeviceNameInfo;
350
351 /*
352 * Check whether we deal with a DOS device, and if so,
353 * strip the path till the file name.
354 * Therefore, things like \\.\CON or C:\some_path\CONIN$
355 * are transformed into CON or CONIN$, for example.
356 */
357 DeviceNameInfo = RtlIsDosDeviceName_U(pszName);
358 if (DeviceNameInfo != 0)
359 {
360 ConsoleName = (LPCWSTR)((ULONG_PTR)ConsoleName + ((DeviceNameInfo >> 16) & 0xFFFF));
361 }
362
363 /* Return a standard console "file" name according to what we passed in parameters */
364 if (_wcsicmp(ConsoleName, BaseConInputFileName) == 0)
365 {
367 }
368 else if (_wcsicmp(ConsoleName, BaseConOutputFileName) == 0)
369 {
371 }
372 else if (_wcsicmp(ConsoleName, BaseConFileName) == 0)
373 {
374 if ((dwDesiredAccess & (GENERIC_READ | GENERIC_WRITE)) == GENERIC_READ)
375 {
377 }
378 else if ((dwDesiredAccess & (GENERIC_READ | GENERIC_WRITE)) == GENERIC_WRITE)
379 {
381 }
382 }
383
384 /* If we are there, that means that either the file name or the desired access are wrong */
385 return NULL;
386}
#define GENERIC_READ
Definition: compat.h:135
static LPCWSTR BaseConInputFileName
Definition: console.c:25
static LPCWSTR BaseConFileName
Definition: console.c:24
static LPCWSTR BaseConOutputFileName
Definition: console.c:26
_ACRTIMP int __cdecl _wcsicmp(const wchar_t *, const wchar_t *)
Definition: wcs.c:159
NTSYSAPI ULONG NTAPI RtlIsDosDeviceName_U(_In_ PCWSTR Name)
#define GENERIC_WRITE
Definition: nt_native.h:90
const uint16_t * LPCWSTR
Definition: typedefs.h:57
uint32_t ULONG_PTR
Definition: typedefs.h:65
uint32_t ULONG
Definition: typedefs.h:59

Referenced by CreateFileW().

◆ PropDialogHandler()

DWORD WINAPI PropDialogHandler ( IN LPVOID  lpThreadParameter)

Definition at line 35 of file init.c.

36{
37 // NOTE: lpThreadParameter corresponds to the client shared section handle.
38
40 HMODULE hConsoleApplet = NULL;
42 static BOOL AlreadyDisplayingProps = FALSE;
43 WCHAR szBuffer[MAX_PATH];
44
45 /*
46 * Do not launch more than once the console property dialog applet,
47 * or (albeit less probable), if we are not initialized.
48 */
49 if (!ConsoleInitialized || AlreadyDisplayingProps)
50 {
51 /* Close the associated client shared section handle if needed */
52 if (lpThreadParameter)
53 CloseHandle((HANDLE)lpThreadParameter);
54
56 }
57
58 AlreadyDisplayingProps = TRUE;
59
60 /* Load the control applet */
62 wcscat(szBuffer, L"\\console.dll");
63 hConsoleApplet = LoadLibraryW(szBuffer);
64 if (hConsoleApplet == NULL)
65 {
66 DPRINT1("Failed to load console.dll\n");
68 goto Quit;
69 }
70
71 /* Load its main function */
72 CPlApplet = (APPLET_PROC)GetProcAddress(hConsoleApplet, "CPlApplet");
73 if (CPlApplet == NULL)
74 {
75 DPRINT1("Error: console.dll misses CPlApplet export\n");
77 goto Quit;
78 }
79
80 /* Initialize the applet */
81 if (CPlApplet(NULL, CPL_INIT, 0, 0) == FALSE)
82 {
83 DPRINT1("Error: failed to initialize console.dll\n");
85 goto Quit;
86 }
87
88 /* Check the count */
89 if (CPlApplet(NULL, CPL_GETCOUNT, 0, 0) != 1)
90 {
91 DPRINT1("Error: console.dll returned unexpected CPL count\n");
93 goto Quit;
94 }
95
96 /*
97 * Start the applet. For Windows compatibility purposes we need
98 * to pass the client shared section handle (lpThreadParameter)
99 * via the hWnd parameter of the CPlApplet function.
100 */
101 CPlApplet((HWND)lpThreadParameter, CPL_DBLCLK, 0, 0);
102
103 /* We have finished */
104 CPlApplet(NULL, CPL_EXIT, 0, 0);
105
106Quit:
107 if (hConsoleApplet) FreeLibrary(hConsoleApplet);
108 AlreadyDisplayingProps = FALSE;
109 return Status;
110}
LONG(APIENTRY * APPLET_PROC)(HWND, UINT, LPARAM, LPARAM)
Definition: cpl.h:23
#define CPL_DBLCLK
Definition: cpl.h:16
#define CPL_INIT
Definition: cpl.h:12
#define CPL_EXIT
Definition: cpl.h:18
#define CPL_GETCOUNT
Definition: cpl.h:13
#define CloseHandle
Definition: compat.h:739
#define GetProcAddress(x, y)
Definition: compat.h:753
#define FreeLibrary(x)
Definition: compat.h:748
#define MAX_PATH
Definition: compat.h:34
#define LoadLibraryW(x)
Definition: compat.h:747
UINT WINAPI GetSystemDirectoryW(OUT LPWSTR lpBuffer, IN UINT uSize)
Definition: path.c:2232
#define L(x)
Definition: resources.c:13
unsigned int BOOL
Definition: ntddk_ex.h:94
wcscat
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132

Referenced by AllocConsole(), AttachConsole(), and ConDllInitialize().

◆ SetConsoleHandleInformation()

BOOL WINAPI SetConsoleHandleInformation ( IN HANDLE  hHandle,
IN DWORD  dwMask,
IN DWORD  dwFlags 
)

Definition at line 495 of file console.c.

498{
499 CONSOLE_API_MESSAGE ApiMessage;
500 PCONSOLE_SETHANDLEINFO SetHandleInfoRequest = &ApiMessage.Data.SetHandleInfoRequest;
501
502 SetHandleInfoRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
503 SetHandleInfoRequest->Handle = hHandle;
504 SetHandleInfoRequest->Mask = dwMask;
505 SetHandleInfoRequest->Flags = dwFlags;
506
508 NULL,
510 sizeof(*SetHandleInfoRequest));
511 if (!NT_SUCCESS(ApiMessage.Status))
512 {
513 BaseSetLastNTError(ApiMessage.Status);
514 return FALSE;
515 }
516
517 return TRUE;
518}
@ ConsolepSetHandleInformation
Definition: conmsg.h:54
_In_ LPWSTR _In_ DWORD _In_ DWORD _In_ DWORD dwFlags
Definition: netsh.h:141
CONSOLE_SETHANDLEINFO SetHandleInfoRequest
Definition: conmsg.h:939

Referenced by SetHandleInformation().

◆ SetTEBLangID()

VOID SetTEBLangID ( VOID  )

Internal helper function used to synchronize the current thread's language ID with the one from the console.

Definition at line 3177 of file console.c.

3178{
3179 CONSOLE_API_MESSAGE ApiMessage;
3180 PCONSOLE_GETLANGID LangIdRequest = &ApiMessage.Data.LangIdRequest;
3181
3182 /* Retrieve the "best-suited" language ID corresponding
3183 * to the active console output code page. */
3184 LangIdRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
3185
3187 NULL,
3189 sizeof(*LangIdRequest));
3190 if (!NT_SUCCESS(ApiMessage.Status))
3191 {
3192 /*
3193 * No best console language ID: keep the current thread's one.
3194 * Since this internal function only modifies an optional setting,
3195 * don't set any last error, as it could otherwise mess with the
3196 * main last error set by the caller.
3197 */
3198 return;
3199 }
3200
3201 /*
3202 * We succeeded, set the current thread's language ID by
3203 * modifying its locale -- Windows <= 2003 does not have
3204 * the concept of a separate thread UI language.
3205 * Ignore the returned value.
3206 */
3207 if (!SetThreadLocale(MAKELCID(LangIdRequest->LangId, SORT_DEFAULT)))
3208 {
3209 DPRINT1("SetTEBLangID: Could not set thread locale to console lang ID %lu\n",
3210 LangIdRequest->LangId);
3211 }
3212}
@ ConsolepGetLangId
Definition: conmsg.h:103
BOOL WINAPI SetThreadLocale(LCID lcid)
Definition: locale.c:2822
#define SORT_DEFAULT
#define MAKELCID(lgid, srtid)
CONSOLE_GETLANGID LangIdRequest
Definition: conmsg.h:1016
HANDLE ConsoleHandle
Definition: conmsg.h:864

Referenced by AllocConsole(), AttachConsole(), ConDllInitialize(), and SetConsoleOutputCP().

◆ SetUpAppName()

VOID SetUpAppName ( IN BOOLEAN  CaptureStrings,
IN OUT LPDWORD  CurDirLength,
IN OUT LPWSTR CurDir,
IN OUT LPDWORD  AppNameLength,
IN OUT LPWSTR AppName 
)

Definition at line 264 of file console.c.

269{
271
272 /* Retrieve the needed buffer size */
273 Length = (StartDirLength + 1) * sizeof(WCHAR);
274 if (*CurDirLength > 0) Length = min(Length, *CurDirLength);
275 *CurDirLength = Length;
276
277 /* Capture the data if needed, or, return a pointer to it */
278 if (CaptureStrings)
279 {
280 /*
281 * Length is always >= sizeof(WCHAR). Copy everything but the
282 * possible trailing NULL character, and then NULL-terminate.
283 */
284 Length -= sizeof(WCHAR);
286 (*CurDir)[Length / sizeof(WCHAR)] = UNICODE_NULL;
287 }
288 else
289 {
290 *CurDir = StartDirBuffer;
291 }
292
293 /* Retrieve the needed buffer size */
294 Length = (ExeNameLength + 1) * sizeof(WCHAR);
295 if (*AppNameLength > 0) Length = min(Length, *AppNameLength);
296 *AppNameLength = Length;
297
298 /* Capture the data if needed, or, return a pointer to it */
299 if (CaptureStrings)
300 {
301 /*
302 * Length is always >= sizeof(WCHAR). Copy everything but the
303 * possible trailing NULL character, and then NULL-terminate.
304 */
305 Length -= sizeof(WCHAR);
307 (*AppName)[Length / sizeof(WCHAR)] = UNICODE_NULL;
308 }
309 else
310 {
312 }
313}
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102

Referenced by AllocConsole(), and ConDllInitialize().

◆ SetUpConsoleInfo()

VOID SetUpConsoleInfo ( IN BOOLEAN  CaptureTitle,
IN OUT LPDWORD  pTitleLength,
IN OUT LPWSTR *lpTitle  OPTIONAL,
IN OUT LPDWORD  pDesktopLength,
IN OUT LPWSTR *lpDesktop  OPTIONAL,
IN OUT PCONSOLE_START_INFO  ConsoleStartInfo 
)

Definition at line 135 of file init.c.

141{
144
145 /* Initialize the fields */
146
147 ConsoleStartInfo->IconIndex = 0;
148 ConsoleStartInfo->hIcon = NULL;
149 ConsoleStartInfo->hIconSm = NULL;
150 ConsoleStartInfo->dwStartupFlags = Parameters->WindowFlags;
151 ConsoleStartInfo->nFont = 0;
152 ConsoleStartInfo->nInputBufferSize = 0;
153 ConsoleStartInfo->uCodePage = GetOEMCP();
154
155 if (lpTitle)
156 {
158
159 /* If we don't have any title, use the default one */
160 if (Parameters->WindowTitle.Buffer == NULL)
161 {
163 Length = lstrlenW(DefaultConsoleTitle) * sizeof(WCHAR); // sizeof(DefaultConsoleTitle);
164 }
165 else
166 {
167 Title = Parameters->WindowTitle.Buffer;
168 Length = Parameters->WindowTitle.Length;
169 }
170
171 /* Retrieve the needed buffer size */
172 Length += sizeof(WCHAR);
173 if (*pTitleLength > 0) Length = min(Length, *pTitleLength);
174 *pTitleLength = Length;
175
176 /* Capture the data if needed, or, return a pointer to it */
177 if (CaptureTitle)
178 {
179 /*
180 * Length is always >= sizeof(WCHAR). Copy everything but the
181 * possible trailing NULL character, and then NULL-terminate.
182 */
183 Length -= sizeof(WCHAR);
185 (*lpTitle)[Length / sizeof(WCHAR)] = UNICODE_NULL;
186 }
187 else
188 {
189 *lpTitle = Title;
190 }
191 }
192 else
193 {
194 *pTitleLength = 0;
195 }
196
197 if (lpDesktop && Parameters->DesktopInfo.Buffer && *Parameters->DesktopInfo.Buffer)
198 {
199 /* Retrieve the needed buffer size */
200 Length = Parameters->DesktopInfo.Length + sizeof(WCHAR);
201 if (*pDesktopLength > 0) Length = min(Length, *pDesktopLength);
202 *pDesktopLength = Length;
203
204 /* Return a pointer to the data */
205 *lpDesktop = Parameters->DesktopInfo.Buffer;
206 }
207 else
208 {
209 *pDesktopLength = 0;
210 if (lpDesktop) *lpDesktop = NULL;
211 }
212
213 if (Parameters->WindowFlags & STARTF_USEFILLATTRIBUTE)
214 {
215 ConsoleStartInfo->wFillAttribute = (WORD)Parameters->FillAttribute;
216 }
217 if (Parameters->WindowFlags & STARTF_USECOUNTCHARS)
218 {
219 ConsoleStartInfo->dwScreenBufferSize.X = (SHORT)Parameters->CountCharsX;
220 ConsoleStartInfo->dwScreenBufferSize.Y = (SHORT)Parameters->CountCharsY;
221 }
222 if (Parameters->WindowFlags & STARTF_USESHOWWINDOW)
223 {
224 ConsoleStartInfo->wShowWindow = (WORD)Parameters->ShowWindowFlags;
225 }
226 if (Parameters->WindowFlags & STARTF_USEPOSITION)
227 {
228 ConsoleStartInfo->dwWindowOrigin.X = (SHORT)Parameters->StartingX;
229 ConsoleStartInfo->dwWindowOrigin.Y = (SHORT)Parameters->StartingY;
230 }
231 if (Parameters->WindowFlags & STARTF_USESIZE)
232 {
233 ConsoleStartInfo->dwWindowSize.X = (SHORT)Parameters->CountX;
234 ConsoleStartInfo->dwWindowSize.Y = (SHORT)Parameters->CountY;
235 }
236
237 /* Get shell information (ShellInfo.Buffer is NULL-terminated) */
238 if (Parameters->ShellInfo.Buffer != NULL)
239 {
240 ConsoleStartInfo->IconIndex = ParseShellInfo(Parameters->ShellInfo.Buffer, L"dde.");
241
242 if ((Parameters->WindowFlags & STARTF_USEHOTKEY) == 0)
243 ConsoleStartInfo->dwHotKey = ParseShellInfo(Parameters->ShellInfo.Buffer, L"hotkey.");
244 else
245 ConsoleStartInfo->dwHotKey = HandleToUlong(Parameters->StandardInput);
246 }
247}
#define HandleToUlong(h)
Definition: basetsd.h:73
TCHAR lpTitle[80]
Definition: ctm.c:69
static const WCHAR Title[]
Definition: oid.c:1259
#define lstrlenW
Definition: compat.h:750
static INT ParseShellInfo(LPCWSTR lpszShellInfo, LPCWSTR lpszKeyword)
Definition: init.c:114
static const PWSTR DefaultConsoleTitle
Definition: init.c:29
UINT WINAPI GetOEMCP(void)
Definition: locale.c:2062
unsigned short WORD
Definition: ntddk_ex.h:93
#define STARTF_USEHOTKEY
Definition: pch.h:41
short SHORT
Definition: pedump.c:59
#define STARTF_USEPOSITION
Definition: winbase.h:470
#define STARTF_USESHOWWINDOW
Definition: winbase.h:468
#define STARTF_USECOUNTCHARS
Definition: winbase.h:471
#define STARTF_USEFILLATTRIBUTE
Definition: winbase.h:472
#define STARTF_USESIZE
Definition: winbase.h:469

Referenced by AllocConsole(), AttachConsole(), and ConDllInitialize().

◆ SetUpHandles()

VOID SetUpHandles ( IN PCONSOLE_START_INFO  ConsoleStartInfo)

Definition at line 251 of file init.c.

252{
254
255 if (ConsoleStartInfo->dwStartupFlags & STARTF_USEHOTKEY)
256 {
257 Parameters->WindowFlags &= ~STARTF_USEHOTKEY;
258 }
259 if (ConsoleStartInfo->dwStartupFlags & STARTF_SHELLPRIVATE)
260 {
261 Parameters->WindowFlags &= ~STARTF_SHELLPRIVATE;
262 }
263
264 /* We got the handles, let's set them */
265 Parameters->ConsoleHandle = ConsoleStartInfo->ConsoleHandle;
266
267 if ((ConsoleStartInfo->dwStartupFlags & STARTF_USESTDHANDLES) == 0)
268 {
269 Parameters->StandardInput = ConsoleStartInfo->InputHandle;
270 Parameters->StandardOutput = ConsoleStartInfo->OutputHandle;
271 Parameters->StandardError = ConsoleStartInfo->ErrorHandle;
272 }
273}
#define STARTF_USESTDHANDLES
Definition: winbase.h:476

Referenced by AllocConsole(), AttachConsole(), and ConDllInitialize().

◆ TranslateStdHandle()

HANDLE TranslateStdHandle ( HANDLE  hHandle)