00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
#include "precomp.h"
00024
#pragma hdrstop
00025
#pragma hdrstop
00026
00027
#if !defined(BUILD_WOW64)
00028
00029 HANDLE
InputWaitHandle = (HANDLE)-1;
00030
00031 HANDLE
00032
APIENTRY
00033 GetConsoleInputWaitHandle( VOID )
00034 {
00035
return InputWaitHandle;
00036 }
00037
00038
#endif
00039
00040
#if !defined(BUILD_WOW6432)
00041
00042 HANDLE
00043
APIENTRY
00044 OpenConsoleWInternal(
00045 IN ULONG HandleType,
00046 IN ULONG DesiredAccess,
00047 IN BOOL InheritHandle,
00048 IN ULONG ShareMode
00049 )
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 {
00066
00067
CONSOLE_API_MSG m;
00068
PCONSOLE_OPENCONSOLE_MSG a = &m.
u.OpenConsole;
00069
00070 a->
ConsoleHandle =
GET_CONSOLE_HANDLE;
00071 a->
HandleType = HandleType;
00072 a->
DesiredAccess = DesiredAccess;
00073 a->
InheritHandle = InheritHandle;
00074 a->
ShareMode= ShareMode;
00075
CsrClientCallServer( (PCSR_API_MSG)&m,
00076
NULL,
00077 CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX,
00078
ConsolepOpenConsole
00079 ),
00080
sizeof( *a )
00081 );
00082
if (!
NT_SUCCESS( m.ReturnValue)) {
00083
SET_LAST_NT_ERROR(m.ReturnValue);
00084
return (HANDLE)
INVALID_HANDLE_VALUE;
00085 }
00086
else {
00087
return a->Handle;
00088 }
00089
00090 }
00091
00092
#endif // !defined(BUILD_WOW6432)
00093
00094
#if !defined(BUILD_WOW64)
00095
00096 HANDLE
00097
APIENTRY
00098 OpenConsoleW(
00099 IN LPWSTR lpConsoleDevice,
00100 IN DWORD dwDesiredAccess,
00101 IN BOOL bInheritHandle,
00102 IN DWORD dwShareMode
00103 )
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153 {
00154 ULONG HandleType;
00155
00156
try {
00157
if (!lstrcmpiW(CONSOLE_INPUT_STRING,lpConsoleDevice)) {
00158 HandleType =
CONSOLE_INPUT_HANDLE;
00159 }
00160
else if (!lstrcmpiW(CONSOLE_OUTPUT_STRING,lpConsoleDevice)) {
00161 HandleType =
CONSOLE_OUTPUT_HANDLE;
00162 }
00163
else {
00164
SET_LAST_ERROR(ERROR_INVALID_PARAMETER);
00165
return (HANDLE)
INVALID_HANDLE_VALUE;
00166 }
00167 } except(
EXCEPTION_EXECUTE_HANDLER ) {
00168
SET_LAST_ERROR(ERROR_INVALID_ACCESS);
00169
return (HANDLE)
INVALID_HANDLE_VALUE;
00170 }
00171
if (dwDesiredAccess & ~
VALID_ACCESSES ||
00172 dwShareMode & ~
VALID_SHARE_ACCESSES) {
00173
SET_LAST_ERROR(ERROR_INVALID_PARAMETER);
00174
return (HANDLE)
INVALID_HANDLE_VALUE;
00175 }
00176
00177
return OpenConsoleWInternal(HandleType,
00178 dwDesiredAccess,
00179 bInheritHandle,
00180 dwShareMode
00181 );
00182
00183 }
00184
00185
#endif // !defined(BUILD_WOW64)
00186
00187
#if !defined(BUILD_WOW6432)
00188
00189
BOOL
00190
APIENTRY
00191 ReadConsoleInternal(
00192 IN HANDLE hConsoleInput,
00193 OUT LPVOID lpBuffer,
00194 IN DWORD nNumberOfCharsToRead,
00195 OUT LPDWORD lpNumberOfCharsRead,
00196 IN OUT LPVOID lpReserved,
00197 IN BOOLEAN Unicode,
00198 IN USHORT ExeNameLength,
00199 IN LPWSTR ExeName
00200 )
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228 {
00229 PCSR_CAPTURE_HEADER CaptureBuffer;
00230
CONSOLE_API_MSG m;
00231
PCONSOLE_READCONSOLE_MSG a = &m.
u.ReadConsole;
00232 BOOLEAN Dummy;
00233 PCONSOLE_READCONSOLE_CONTROL pInputControl;
00234
NTSTATUS Status;
00235
00236 a->
ConsoleHandle =
GET_CONSOLE_HANDLE;
00237 a->
InputHandle = hConsoleInput;
00238 a->
ExeNameLength =
ExeNameLength;
00239 RtlCopyMemory(a->
Buffer, ExeName,
ExeNameLength);
00240 a->
Unicode =
Unicode;
00241
00242
00243
00244
00245
00246
00247 a->
CaptureBufferSize = a->
NumBytes = nNumberOfCharsToRead *
sizeof(WCHAR);
00248
if (a->
CaptureBufferSize >
BUFFER_SIZE) {
00249 CaptureBuffer =
CsrAllocateCaptureBuffer( 1,
00250 a->
CaptureBufferSize
00251 );
00252
if (CaptureBuffer ==
NULL) {
00253
SET_LAST_ERROR(ERROR_NOT_ENOUGH_MEMORY);
00254
return FALSE;
00255 }
00256
CsrCaptureMessageBuffer( CaptureBuffer,
00257
NULL,
00258 a->
CaptureBufferSize,
00259 (PVOID *) &a->
BufPtr
00260 );
00261
00262 }
00263
else {
00264 a->
BufPtr = a->
Buffer;
00265 CaptureBuffer =
NULL;
00266 }
00267
00268
00269 pInputControl = (PCONSOLE_READCONSOLE_CONTROL)lpReserved;
00270 a->
InitialNumBytes = 0;
00271 a->
CtrlWakeupMask = 0;
00272 a->
ControlKeyState = 0;
00273
Status = STATUS_SUCCESS;
00274
try {
00275
if (
Unicode &&
00276 ARGUMENT_PRESENT(lpReserved) &&
00277 NtCurrentPeb()->ImageSubsystemMajorVersion >= 4 &&
00278 pInputControl->nLength ==
sizeof(*pInputControl)
00279 ) {
00280
if ((pInputControl->nInitialChars > nNumberOfCharsToRead)) {
00281
Status = STATUS_INVALID_PARAMETER;
00282 }
else {
00283 a->
InitialNumBytes = pInputControl->nInitialChars *
sizeof(WCHAR);
00284
if (pInputControl->nInitialChars != 0) {
00285 RtlCopyMemory( a->
BufPtr, lpBuffer, a->
InitialNumBytes );
00286 }
00287 a->
CtrlWakeupMask = pInputControl->dwCtrlWakeupMask;
00288 }
00289 }
else {
00290 pInputControl =
NULL;
00291 }
00292 } except(
EXCEPTION_EXECUTE_HANDLER ) {
00293
Status = GetExceptionCode();
00294 }
00295
00296
if (!
NT_SUCCESS(
Status) && pInputControl !=
NULL) {
00297
if (CaptureBuffer !=
NULL) {
00298
CsrFreeCaptureBuffer( CaptureBuffer );
00299 }
00300
SET_LAST_NT_ERROR(
Status);
00301
return FALSE;
00302 }
00303
00304
CsrClientCallServer( (PCSR_API_MSG)&m,
00305 CaptureBuffer,
00306 CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX,
00307
ConsolepReadConsole
00308 ),
00309
sizeof( *a )
00310 );
00311
if (
NT_SUCCESS( m.ReturnValue )) {
00312
try {
00313 *lpNumberOfCharsRead = a->NumBytes;
00314
if (
Unicode) {
00315 *lpNumberOfCharsRead /=
sizeof(WCHAR);
00316
if (pInputControl !=
NULL) {
00317 pInputControl->dwControlKeyState = a->ControlKeyState;
00318 }
00319 }
00320 RtlCopyMemory( lpBuffer, a->BufPtr, a->NumBytes );
00321 } except(
EXCEPTION_EXECUTE_HANDLER ) {
00322
if (CaptureBuffer !=
NULL) {
00323
CsrFreeCaptureBuffer( CaptureBuffer );
00324 }
00325
SET_LAST_ERROR(ERROR_INVALID_ACCESS);
00326
return FALSE;
00327 }
00328 }
00329
if (CaptureBuffer !=
NULL) {
00330
CsrFreeCaptureBuffer( CaptureBuffer );
00331 }
00332
if (!
NT_SUCCESS( m.ReturnValue )) {
00333
SET_LAST_NT_ERROR(m.ReturnValue);
00334
return FALSE;
00335 }
else if (m.ReturnValue == STATUS_ALERTED) {
00336
00337
NtYieldExecution();
00338
SET_LAST_ERROR(ERROR_OPERATION_ABORTED);
00339 }
00340
return TRUE;
00341 }
00342
00343
#endif
00344
00345
#if !defined(BUILD_WOW64)
00346
00347
BOOL
00348
APIENTRY
00349 ReadConsoleA(
00350 IN HANDLE hConsoleInput,
00351 OUT LPVOID lpBuffer,
00352 IN DWORD nNumberOfCharsToRead,
00353 OUT LPDWORD lpNumberOfCharsRead,
00354 IN OUT LPVOID lpReserved
00355 )
00356 {
00357
00358 WCHAR ExeName[
BUFFER_SIZE/2];
00359
USHORT ExeNameLength;
00360
00361
ExeNameLength =
GetCurrentExeName(ExeName,
sizeof(ExeName));
00362
00363
return ReadConsoleInternal(hConsoleInput,
00364 lpBuffer,
00365 nNumberOfCharsToRead,
00366 lpNumberOfCharsRead,
00367
NULL,
00368
FALSE,
00369
ExeNameLength,
00370 ExeName
00371 );
00372 UNREFERENCED_PARAMETER(lpReserved);
00373 }
00374
00375
BOOL
00376
APIENTRY
00377 ReadConsoleW(
00378 IN HANDLE hConsoleInput,
00379 OUT LPVOID lpBuffer,
00380 IN DWORD nNumberOfCharsToRead,
00381 OUT LPDWORD lpNumberOfCharsRead,
00382 IN OUT LPVOID lpReserved
00383 )
00384 {
00385 WCHAR ExeName[
BUFFER_SIZE/2];
00386
USHORT ExeNameLength;
00387
00388
ExeNameLength =
GetCurrentExeName(ExeName,
sizeof(ExeName));
00389
00390
return ReadConsoleInternal(hConsoleInput,
00391 lpBuffer,
00392 nNumberOfCharsToRead,
00393 lpNumberOfCharsRead,
00394 lpReserved,
00395
TRUE,
00396
ExeNameLength,
00397 ExeName
00398 );
00399 UNREFERENCED_PARAMETER(lpReserved);
00400 }
00401
00402
#endif
00403
00404
#if !defined(BUILD_WOW6432)
00405
00406
BOOL
00407
APIENTRY
00408 WriteConsoleInternal(
00409 IN HANDLE hConsoleOutput,
00410 IN CONST VOID *lpBuffer,
00411 IN DWORD nNumberOfCharsToWrite,
00412 OUT LPDWORD lpNumberOfCharsWritten,
00413 IN BOOLEAN Unicode
00414 )
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436 {
00437
00438 PCSR_CAPTURE_HEADER CaptureBuffer;
00439
CONSOLE_API_MSG m;
00440
PCONSOLE_WRITECONSOLE_MSG a = &m.
u.WriteConsole;
00441
00442 a->
ConsoleHandle =
GET_CONSOLE_HANDLE;
00443 a->
OutputHandle = hConsoleOutput;
00444
00445
if (
Unicode) {
00446 a->
NumBytes = nNumberOfCharsToWrite *
sizeof(WCHAR);
00447 }
else {
00448 a->
NumBytes = nNumberOfCharsToWrite;
00449 }
00450
00451 a->
Unicode =
Unicode;
00452
if (a->
NumBytes >
BUFFER_SIZE) {
00453 CaptureBuffer =
CsrAllocateCaptureBuffer( 1,
00454 a->
NumBytes
00455 );
00456
if (CaptureBuffer ==
NULL) {
00457
SET_LAST_ERROR(ERROR_NOT_ENOUGH_MEMORY);
00458
return FALSE;
00459 }
00460
CsrCaptureMessageBuffer( CaptureBuffer,
00461 (PCHAR) lpBuffer,
00462 a->
NumBytes,
00463 (PVOID *) &a->
BufPtr
00464 );
00465 a->
BufferInMessage =
FALSE;
00466 }
00467
else {
00468 a->
BufPtr = a->
Buffer;
00469
try {
00470 RtlCopyMemory( a->
BufPtr, lpBuffer, a->
NumBytes);
00471 } except(
EXCEPTION_EXECUTE_HANDLER ) {
00472
SET_LAST_ERROR(ERROR_INVALID_ACCESS);
00473
return FALSE;
00474 }
00475 CaptureBuffer =
NULL;
00476 a->
BufferInMessage =
TRUE;
00477 }
00478
CsrClientCallServer( (PCSR_API_MSG)&m,
00479 CaptureBuffer,
00480 CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX,
00481
ConsolepWriteConsole
00482 ),
00483
sizeof( *a )
00484 );
00485
if (CaptureBuffer !=
NULL) {
00486
CsrFreeCaptureBuffer( CaptureBuffer );
00487 }
00488
if (!
NT_SUCCESS( m.ReturnValue )) {
00489
SET_LAST_NT_ERROR(m.ReturnValue);
00490
return FALSE;
00491 }
00492
try {
00493 *lpNumberOfCharsWritten = a->NumBytes;
00494
if (
Unicode) {
00495 *lpNumberOfCharsWritten /=
sizeof(WCHAR);
00496 }
00497 } except(
EXCEPTION_EXECUTE_HANDLER ) {
00498
SET_LAST_ERROR(ERROR_INVALID_ACCESS);
00499
return FALSE;
00500 }
00501
return TRUE;
00502
00503 }
00504
00505
#endif
00506
00507
#if !defined(BUILD_WOW64)
00508
00509
BOOL
00510
APIENTRY
00511 WriteConsoleA(
00512 IN HANDLE hConsoleOutput,
00513 IN CONST VOID *lpBuffer,
00514 IN DWORD nNumberOfCharsToWrite,
00515 OUT LPDWORD lpNumberOfCharsWritten,
00516 IN OUT LPVOID lpReserved
00517 )
00518 {
00519
return WriteConsoleInternal(hConsoleOutput,
00520 lpBuffer,
00521 nNumberOfCharsToWrite,
00522 lpNumberOfCharsWritten,
00523
FALSE
00524 );
00525 UNREFERENCED_PARAMETER(lpReserved);
00526 }
00527
00528
BOOL
00529
APIENTRY
00530 WriteConsoleW(
00531 IN HANDLE hConsoleOutput,
00532 IN CONST VOID *lpBuffer,
00533 IN DWORD nNumberOfCharsToWrite,
00534 OUT LPDWORD lpNumberOfCharsWritten,
00535 IN OUT LPVOID lpReserved
00536 )
00537 {
00538
return WriteConsoleInternal(hConsoleOutput,
00539 lpBuffer,
00540 nNumberOfCharsToWrite,
00541 lpNumberOfCharsWritten,
00542
TRUE
00543 );
00544 UNREFERENCED_PARAMETER(lpReserved);
00545 }
00546
00547
#endif
00548
00549
#if !defined(BUILD_WOW6432)
00550
00551
BOOL
00552
APIENTRY
00553 CloseConsoleHandle(
00554 IN HANDLE hConsole
00555 )
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572 {
00573
00574
CONSOLE_API_MSG m;
00575
PCONSOLE_CLOSEHANDLE_MSG a = &m.
u.CloseHandle;
00576
00577 a->
ConsoleHandle =
GET_CONSOLE_HANDLE;
00578 a->
Handle = hConsole;
00579
CsrClientCallServer( (PCSR_API_MSG)&m,
00580
NULL,
00581 CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX,
00582
ConsolepCloseHandle
00583 ),
00584
sizeof( *a )
00585 );
00586
if (
NT_SUCCESS( m.ReturnValue )) {
00587
return TRUE;
00588 }
else {
00589
SET_LAST_NT_ERROR (m.ReturnValue);
00590
return FALSE;
00591 }
00592
00593 }
00594
00595 HANDLE
00596
APIENTRY
00597 DuplicateConsoleHandle(
00598 IN HANDLE hSourceHandle,
00599 IN DWORD dwDesiredAccess,
00600 IN BOOL bInheritHandle,
00601 IN DWORD dwOptions
00602 )
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641 {
00642
00643
CONSOLE_API_MSG m;
00644
PCONSOLE_DUPHANDLE_MSG a = &m.
u.DuplicateHandle;
00645
00646
if (dwOptions & ~
VALID_DUP_OPTIONS) {
00647
SET_LAST_ERROR(ERROR_INVALID_PARAMETER);
00648
return (HANDLE)
INVALID_HANDLE_VALUE;
00649 }
00650
if (((dwOptions & DUPLICATE_SAME_ACCESS) == 0) &&
00651 (dwDesiredAccess & ~
VALID_ACCESSES)) {
00652
SET_LAST_ERROR(ERROR_INVALID_PARAMETER);
00653
return (HANDLE)
INVALID_HANDLE_VALUE;
00654 }
00655
00656 a->
ConsoleHandle =
GET_CONSOLE_HANDLE;
00657 a->
SourceHandle = hSourceHandle;
00658 a->
DesiredAccess = dwDesiredAccess;
00659 a->
InheritHandle = (BOOLEAN) bInheritHandle;
00660 a->
Options = dwOptions;
00661
CsrClientCallServer( (PCSR_API_MSG)&m,
00662
NULL,
00663 CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX,
00664
ConsolepDupHandle
00665 ),
00666
sizeof( *a )
00667 );
00668
if (
NT_SUCCESS( m.ReturnValue )) {
00669
return a->TargetHandle;
00670 }
else {
00671
SET_LAST_NT_ERROR (m.ReturnValue);
00672
return (HANDLE)
INVALID_HANDLE_VALUE;
00673 }
00674
00675 }
00676
00677
00678
BOOL
00679
APIENTRY
00680 GetConsoleHandleInformation(
00681 IN HANDLE hObject,
00682 OUT LPDWORD lpdwFlags
00683 )
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701 {
00702
00703
CONSOLE_API_MSG m;
00704
PCONSOLE_GETHANDLEINFORMATION_MSG a = &m.
u.GetHandleInformation;
00705
00706 a->
ConsoleHandle =
GET_CONSOLE_HANDLE;
00707 a->
Handle = hObject;
00708
00709
CsrClientCallServer( (PCSR_API_MSG)&m,
00710
NULL,
00711 CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX,
00712
ConsolepGetHandleInformation
00713 ),
00714
sizeof( *a )
00715 );
00716
if (
NT_SUCCESS( m.ReturnValue )) {
00717
try {
00718 *lpdwFlags = a->Flags;
00719 } except(
EXCEPTION_EXECUTE_HANDLER ) {
00720
SET_LAST_ERROR(ERROR_INVALID_ACCESS);
00721
return FALSE;
00722 }
00723
return TRUE;
00724 }
else {
00725
SET_LAST_NT_ERROR (m.ReturnValue);
00726
return FALSE;
00727 }
00728
00729 }
00730
00731
00732
BOOL
00733
APIENTRY
00734 SetConsoleHandleInformation(
00735 IN HANDLE hObject,
00736 IN DWORD dwMask,
00737 IN DWORD dwFlags
00738 )
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758 {
00759
00760
CONSOLE_API_MSG m;
00761
PCONSOLE_SETHANDLEINFORMATION_MSG a = &m.
u.SetHandleInformation;
00762
00763 a->
ConsoleHandle =
GET_CONSOLE_HANDLE;
00764 a->
Handle = hObject;
00765 a->
Mask = dwMask;
00766 a->
Flags =
dwFlags;
00767
00768
CsrClientCallServer( (PCSR_API_MSG)&m,
00769
NULL,
00770 CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX,
00771
ConsolepSetHandleInformation
00772 ),
00773
sizeof( *a )
00774 );
00775
if (
NT_SUCCESS( m.ReturnValue )) {
00776
return TRUE;
00777 }
else {
00778
SET_LAST_NT_ERROR (m.ReturnValue);
00779
return FALSE;
00780 }
00781
00782 }
00783
00784
00785
BOOL
00786
APIENTRY
00787 VerifyConsoleIoHandle(
00788 IN HANDLE hIoHandle
00789 )
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804 {
00805
00806
CONSOLE_API_MSG m;
00807
PCONSOLE_VERIFYIOHANDLE_MSG a = &m.
u.VerifyConsoleIoHandle;
00808
00809 a->
ConsoleHandle =
GET_CONSOLE_HANDLE;
00810 a->
Handle = hIoHandle;
00811
00812
00813
00814
00815
00816
if (a->
ConsoleHandle ==
NULL) {
00817
return FALSE;
00818 }
00819
00820
CsrClientCallServer( (PCSR_API_MSG)&m,
00821
NULL,
00822 CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX,
00823
ConsolepVerifyIoHandle
00824 ),
00825
sizeof( *a )
00826 );
00827
if (
NT_SUCCESS( m.ReturnValue )) {
00828
return a->Valid;
00829 }
else {
00830
SET_LAST_NT_ERROR (m.ReturnValue);
00831
return FALSE;
00832 }
00833
00834
00835 }
00836
00837
#endif