Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

cmdline.c

Go to the documentation of this file.
00001 /*++ 00002 00003 Copyright (c) 1985 - 1999, Microsoft Corporation 00004 00005 Module Name: 00006 00007 cmdline.c 00008 00009 Abstract: 00010 00011 This file implements command line editing and aliasing. 00012 00013 Author: 00014 00015 Therese Stowell (thereses) 22-Mar-1991 00016 00017 Revision History: 00018 00019 --*/ 00020 00021 #include "precomp.h" 00022 #pragma hdrstop 00023 #pragma hdrstop 00024 00025 #define CONSOLE_NAME_PATH_SEPARATOR ((WCHAR)L'\\') 00026 #define CONSOLE_NAME_PATH_TERMINATOR ((WCHAR)L'\0') 00027 00028 #if !defined(BUILD_WOW64) 00029 00030 #define FNAME_LENGTH 256 00031 BOOLEAN ExeNameInitialized; 00032 RTL_CRITICAL_SECTION ExeNameCriticalSection; 00033 WCHAR ExeNameBuffer[FNAME_LENGTH]; 00034 USHORT ExeNameLength; // in chars, not bytes 00035 WCHAR StartDirBuffer[MAX_PATH+1]; 00036 USHORT StartDirLength; // in chars, not bytes 00037 00038 VOID 00039 InitExeName( VOID ) 00040 { 00041 00042 PPEB Peb; 00043 PLDR_DATA_TABLE_ENTRY ImageEntry; 00044 00045 if (ExeNameInitialized) { 00046 return; 00047 } 00048 00049 RtlInitializeCriticalSection(&ExeNameCriticalSection); 00050 ExeNameInitialized = TRUE; 00051 Peb = NtCurrentPeb(); 00052 ImageEntry = (PLDR_DATA_TABLE_ENTRY)Peb->Ldr->InLoadOrderModuleList.Flink; 00053 ImageEntry = CONTAINING_RECORD(ImageEntry, LDR_DATA_TABLE_ENTRY, InLoadOrderLinks); 00054 ExeNameLength = ImageEntry->BaseDllName.Length/sizeof(WCHAR); 00055 RtlCopyMemory(ExeNameBuffer, 00056 ImageEntry->BaseDllName.Buffer, 00057 ImageEntry->BaseDllName.Length 00058 ); 00059 ExeNameBuffer[ExeNameLength] = CONSOLE_NAME_PATH_TERMINATOR; 00060 StartDirLength = Peb->ProcessParameters->CurrentDirectory.DosPath.Length/sizeof(WCHAR); 00061 RtlCopyMemory(StartDirBuffer, 00062 Peb->ProcessParameters->CurrentDirectory.DosPath.Buffer, 00063 Peb->ProcessParameters->CurrentDirectory.DosPath.Length 00064 ); 00065 StartDirBuffer[StartDirLength] = CONSOLE_NAME_PATH_TERMINATOR; 00066 } 00067 00068 00069 USHORT 00070 GetCurrentExeName( 00071 OUT LPWSTR Buffer, 00072 IN ULONG BufferLength 00073 ) 00074 { 00075 if (!ExeNameInitialized) { 00076 Buffer[0] = UNICODE_NULL; 00077 return 0; 00078 } 00079 00080 RtlEnterCriticalSection(&ExeNameCriticalSection); 00081 if (BufferLength > ExeNameLength * sizeof( WCHAR )) { 00082 BufferLength = ExeNameLength * sizeof( WCHAR ); 00083 } 00084 RtlCopyMemory(Buffer,ExeNameBuffer,BufferLength); 00085 RtlLeaveCriticalSection(&ExeNameCriticalSection); 00086 return (USHORT)BufferLength; 00087 } 00088 00089 #endif 00090 00091 #if !defined(BUILD_WOW6432) 00092 00093 USHORT 00094 GetExeName( 00095 IN OUT LPVOID *ExeName, 00096 IN BOOLEAN UnicodeApi, 00097 OUT PBOOLEAN UnicodeExe 00098 ) 00099 { 00100 00101 *UnicodeExe = TRUE; 00102 if (*ExeName == NULL) { 00103 // Should not get here 00104 return 0; 00105 } else if (!UnicodeApi) { 00106 *UnicodeExe = FALSE; 00107 } 00108 00109 if (ExeName == NULL) { 00110 return 0; 00111 } 00112 00113 try { 00114 if (*UnicodeExe) 00115 return (USHORT)wcslen(*ExeName)*sizeof(WCHAR); // returns bytes, not chars 00116 else 00117 return (USHORT)strlen(*ExeName); // returns bytes, not chars 00118 } 00119 except(EXCEPTION_EXECUTE_HANDLER) { 00120 return 0; 00121 } 00122 } 00123 00124 BOOL 00125 APIENTRY 00126 AddConsoleAliasInternal( 00127 IN LPVOID Source, 00128 IN UINT SourceLength, 00129 IN LPVOID Target, 00130 IN UINT TargetLength, 00131 IN LPVOID ExeName, 00132 IN BOOLEAN Unicode 00133 ) 00134 00135 /*++ 00136 00137 Parameters: 00138 00139 Source - String to substitute alias string for, in input stream. 00140 00141 Target - String to substitute for source. NULL to delete an existing 00142 alias. 00143 00144 ExeName - Exe to set alias in. 00145 00146 Return Value: 00147 00148 TRUE - The operation was successful. 00149 00150 FALSE/NULL - The operation failed. Extended error status is available 00151 using GetLastError. 00152 00153 --*/ 00154 00155 { 00156 PCSR_CAPTURE_HEADER CaptureBuffer; 00157 CONSOLE_API_MSG m; 00158 ULONG CapturedMsgPointers; 00159 PCONSOLE_ADDALIAS_MSG a = &m.u.AddConsoleAliasW; 00160 00161 a->ConsoleHandle = GET_CONSOLE_HANDLE; 00162 a->SourceLength = (USHORT)SourceLength; 00163 a->Unicode = Unicode; 00164 CapturedMsgPointers = 2; 00165 00166 a->ExeLength = GetExeName(&ExeName,Unicode,&a->UnicodeExe); 00167 00168 if (Target != NULL) { 00169 a->TargetLength = (USHORT)TargetLength; 00170 CapturedMsgPointers += 1; 00171 } 00172 else { 00173 a->Target = NULL; 00174 a->TargetLength = 0; 00175 } 00176 00177 CaptureBuffer = CsrAllocateCaptureBuffer( CapturedMsgPointers, 00178 a->SourceLength + a->TargetLength + a->ExeLength 00179 ); 00180 if (CaptureBuffer == NULL) { 00181 SET_LAST_ERROR(ERROR_NOT_ENOUGH_MEMORY); 00182 return FALSE; 00183 } 00184 CsrCaptureMessageBuffer( CaptureBuffer, 00185 (PCHAR) Source, 00186 a->SourceLength, 00187 (PVOID *) &a->Source 00188 ); 00189 00190 CsrCaptureMessageBuffer( CaptureBuffer, 00191 (PCHAR) ExeName, 00192 a->ExeLength, 00193 (PVOID *) &a->Exe 00194 ); 00195 00196 if (Target != NULL) { 00197 CsrCaptureMessageBuffer( CaptureBuffer, 00198 (PCHAR) Target, 00199 a->TargetLength, 00200 (PVOID *) &a->Target 00201 ); 00202 } 00203 00204 CsrClientCallServer( (PCSR_API_MSG)&m, 00205 CaptureBuffer, 00206 CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX, 00207 ConsolepAddAlias 00208 ), 00209 sizeof( *a ) 00210 ); 00211 00212 CsrFreeCaptureBuffer( CaptureBuffer ); 00213 00214 if (!NT_SUCCESS( m.ReturnValue )) { 00215 SET_LAST_NT_ERROR(m.ReturnValue); 00216 return FALSE; 00217 } 00218 return TRUE; 00219 00220 } 00221 00222 #endif 00223 00224 #if !defined(BUILD_WOW64) 00225 00226 BOOL 00227 APIENTRY 00228 AddConsoleAliasW( 00229 IN LPWSTR Source, 00230 IN LPWSTR Target, 00231 IN LPWSTR ExeName 00232 ) 00233 { 00234 USHORT TargetLength; 00235 00236 if (Target != NULL) { 00237 TargetLength = (USHORT) (lstrlenW(Target)*sizeof(WCHAR)); 00238 } else { 00239 TargetLength = 0; 00240 } 00241 return AddConsoleAliasInternal(Source, 00242 lstrlenW(Source)*sizeof(WCHAR), 00243 Target, 00244 TargetLength, 00245 ExeName, 00246 TRUE 00247 ); 00248 } 00249 00250 BOOL 00251 APIENTRY 00252 AddConsoleAliasA( 00253 IN LPTSTR Source, 00254 IN LPTSTR Target, 00255 IN LPTSTR ExeName 00256 ) 00257 { 00258 USHORT TargetLength; 00259 00260 if (Target != NULL) { 00261 TargetLength = (USHORT) lstrlenA(Target); 00262 } else { 00263 TargetLength = 0; 00264 } 00265 return AddConsoleAliasInternal(Source, 00266 lstrlenA(Source), 00267 Target, 00268 TargetLength, 00269 ExeName, 00270 FALSE 00271 ); 00272 } 00273 00274 #endif 00275 00276 #if !defined(BUILD_WOW6432) 00277 00278 DWORD 00279 APIENTRY 00280 GetConsoleAliasInternal( 00281 IN LPVOID Source, 00282 IN UINT SourceLength, 00283 OUT LPVOID TargetBuffer, 00284 IN UINT TargetBufferLength, 00285 IN LPVOID ExeName, 00286 IN BOOLEAN Unicode 00287 ) 00288 00289 /*++ 00290 00291 Parameters: 00292 00293 Source - Name of alias to query. NULL means query all alias names. 00294 00295 Target - Where to store value of alias. If Source is NULL, then 00296 value is one or more null terminated strings terminated by an extra 00297 null byte. Each null terminated string is the name of an alias. 00298 00299 Return Value: 00300 00301 Non-zero - The operation was successful, and the return value is the 00302 number of characters stored in the Target buffer. 00303 00304 Zero - The operation failed. Extended error status is available 00305 using GetLastError. 00306 00307 --*/ 00308 00309 { 00310 PCSR_CAPTURE_HEADER CaptureBuffer; 00311 CONSOLE_API_MSG m; 00312 ULONG CapturedMsgPointers; 00313 PCONSOLE_GETALIAS_MSG a = &m.u.GetConsoleAliasW; 00314 00315 if (Source == NULL) { 00316 SET_LAST_ERROR(ERROR_INVALID_PARAMETER); 00317 return FALSE; 00318 } 00319 a->ConsoleHandle = GET_CONSOLE_HANDLE; 00320 a->Unicode = Unicode; 00321 00322 CapturedMsgPointers = 3; 00323 00324 a->ExeLength = GetExeName(&ExeName,Unicode,&a->UnicodeExe); 00325 00326 a->SourceLength = (USHORT) SourceLength; 00327 a->TargetLength = (USHORT) TargetBufferLength; 00328 00329 CaptureBuffer = CsrAllocateCaptureBuffer( CapturedMsgPointers, 00330 a->SourceLength + a->TargetLength + a->ExeLength 00331 ); 00332 if (CaptureBuffer == NULL) { 00333 SET_LAST_ERROR(ERROR_NOT_ENOUGH_MEMORY); 00334 return FALSE; 00335 } 00336 00337 CsrCaptureMessageBuffer( CaptureBuffer, 00338 (PCHAR) ExeName, 00339 a->ExeLength, 00340 (PVOID *) &a->Exe 00341 ); 00342 00343 CsrCaptureMessageBuffer( CaptureBuffer, 00344 (PCHAR) Source, 00345 a->SourceLength, 00346 (PVOID *) &a->Source 00347 ); 00348 00349 CsrCaptureMessageBuffer( CaptureBuffer, 00350 (PCHAR) NULL, 00351 a->TargetLength, 00352 (PVOID *) &a->Target 00353 ); 00354 00355 CsrClientCallServer( (PCSR_API_MSG)&m, 00356 CaptureBuffer, 00357 CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX, 00358 ConsolepGetAlias 00359 ), 00360 sizeof( *a ) 00361 ); 00362 00363 if (NT_SUCCESS( m.ReturnValue )) { 00364 RtlCopyMemory( TargetBuffer, a->Target, a->TargetLength ); 00365 } 00366 else { 00367 SET_LAST_NT_ERROR(m.ReturnValue); 00368 if (m.ReturnValue != STATUS_BUFFER_TOO_SMALL) { 00369 a->TargetLength = 0; 00370 } 00371 } 00372 00373 CsrFreeCaptureBuffer( CaptureBuffer ); 00374 00375 return a->TargetLength; 00376 00377 } 00378 00379 #endif !defined(BUILD_WOW6432) 00380 00381 #if !defined(BUILD_WOW64) 00382 00383 DWORD 00384 APIENTRY 00385 GetConsoleAliasW( 00386 IN LPWSTR Source, 00387 OUT LPWSTR TargetBuffer, 00388 IN DWORD TargetBufferLength, 00389 IN LPWSTR ExeName 00390 ) 00391 { 00392 return GetConsoleAliasInternal(Source, 00393 lstrlenW(Source)*sizeof(WCHAR), 00394 TargetBuffer, 00395 TargetBufferLength, 00396 ExeName, 00397 TRUE 00398 ); 00399 } 00400 00401 DWORD 00402 APIENTRY 00403 GetConsoleAliasA( 00404 IN LPTSTR Source, 00405 OUT LPTSTR TargetBuffer, 00406 IN DWORD TargetBufferLength, 00407 IN LPTSTR ExeName 00408 ) 00409 { 00410 return GetConsoleAliasInternal(Source, 00411 lstrlenA(Source), 00412 TargetBuffer, 00413 TargetBufferLength, 00414 ExeName, 00415 FALSE 00416 ); 00417 } 00418 00419 #endif 00420 00421 #if !defined(BUILD_WOW6432) 00422 00423 DWORD 00424 APIENTRY 00425 GetConsoleAliasesLengthInternal( 00426 IN LPVOID ExeName, 00427 IN BOOLEAN Unicode 00428 ) 00429 { 00430 CONSOLE_API_MSG m; 00431 PCONSOLE_GETALIASESLENGTH_MSG a = &m.u.GetConsoleAliasesLengthW; 00432 PCSR_CAPTURE_HEADER CaptureBuffer; 00433 00434 a->ConsoleHandle = GET_CONSOLE_HANDLE; 00435 a->ExeLength = GetExeName(&ExeName,Unicode,&a->UnicodeExe); 00436 a->Unicode = Unicode; 00437 00438 CaptureBuffer = CsrAllocateCaptureBuffer( 1, 00439 a->ExeLength 00440 ); 00441 if (CaptureBuffer == NULL) { 00442 SET_LAST_ERROR(ERROR_NOT_ENOUGH_MEMORY); 00443 return FALSE; 00444 } 00445 00446 CsrCaptureMessageBuffer( CaptureBuffer, 00447 (PCHAR) ExeName, 00448 a->ExeLength, 00449 (PVOID *) &a->Exe 00450 ); 00451 CsrClientCallServer( (PCSR_API_MSG)&m, 00452 CaptureBuffer, 00453 CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX, 00454 ConsolepGetAliasesLength 00455 ), 00456 sizeof( *a ) 00457 ); 00458 CsrFreeCaptureBuffer( CaptureBuffer ); 00459 if (NT_SUCCESS( m.ReturnValue )) { 00460 return a->AliasesLength; 00461 } else { 00462 SET_LAST_NT_ERROR(m.ReturnValue); 00463 return 0; 00464 } 00465 00466 } 00467 00468 #endif 00469 00470 #if !defined(BUILD_WOW64) 00471 00472 DWORD 00473 APIENTRY 00474 GetConsoleAliasesLengthW( 00475 IN LPWSTR ExeName 00476 ) 00477 { 00478 return GetConsoleAliasesLengthInternal(ExeName, 00479 TRUE 00480 ); 00481 } 00482 00483 DWORD 00484 APIENTRY 00485 GetConsoleAliasesLengthA( 00486 IN LPTSTR ExeName 00487 ) 00488 { 00489 return GetConsoleAliasesLengthInternal(ExeName, 00490 FALSE 00491 ); 00492 } 00493 00494 #endif 00495 00496 #if !defined(BUILD_WOW6432) 00497 00498 DWORD 00499 APIENTRY 00500 GetConsoleAliasExesLengthInternal( 00501 IN BOOLEAN Unicode 00502 ) 00503 { 00504 CONSOLE_API_MSG m; 00505 PCONSOLE_GETALIASEXESLENGTH_MSG a = &m.u.GetConsoleAliasExesLengthW; 00506 00507 a->ConsoleHandle = GET_CONSOLE_HANDLE; 00508 a->Unicode = Unicode; 00509 00510 CsrClientCallServer( (PCSR_API_MSG)&m, 00511 NULL, 00512 CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX, 00513 ConsolepGetAliasExesLength 00514 ), 00515 sizeof( *a ) 00516 ); 00517 if (NT_SUCCESS( m.ReturnValue )) { 00518 return a->AliasExesLength; 00519 } else { 00520 SET_LAST_NT_ERROR(m.ReturnValue); 00521 return 0; 00522 } 00523 00524 } 00525 00526 #endif 00527 00528 #if !defined(BUILD_WOW64) 00529 00530 DWORD 00531 APIENTRY 00532 GetConsoleAliasExesLengthW( VOID ) 00533 { 00534 return GetConsoleAliasExesLengthInternal( TRUE ); 00535 } 00536 00537 DWORD 00538 APIENTRY 00539 GetConsoleAliasExesLengthA( VOID ) 00540 { 00541 return GetConsoleAliasExesLengthInternal( FALSE ); 00542 } 00543 00544 #endif 00545 00546 #if !defined(BUILD_WOW6432) 00547 00548 DWORD 00549 APIENTRY 00550 GetConsoleAliasesInternal( 00551 OUT LPVOID AliasBuffer, 00552 IN DWORD AliasBufferLength, 00553 IN LPVOID ExeName, 00554 IN BOOLEAN Unicode 00555 ) 00556 { 00557 CONSOLE_API_MSG m; 00558 PCONSOLE_GETALIASES_MSG a = &m.u.GetConsoleAliasesW; 00559 PCSR_CAPTURE_HEADER CaptureBuffer; 00560 00561 a->ConsoleHandle = GET_CONSOLE_HANDLE; 00562 a->ExeLength = GetExeName(&ExeName,Unicode,&a->UnicodeExe); 00563 a->Unicode = Unicode; 00564 a->AliasesBufferLength = AliasBufferLength; 00565 00566 CaptureBuffer = CsrAllocateCaptureBuffer( 2, 00567 a->ExeLength + AliasBufferLength 00568 ); 00569 if (CaptureBuffer == NULL) { 00570 SET_LAST_ERROR(ERROR_NOT_ENOUGH_MEMORY); 00571 return FALSE; 00572 } 00573 00574 CsrCaptureMessageBuffer( CaptureBuffer, 00575 (PCHAR) ExeName, 00576 a->ExeLength, 00577 (PVOID *) &a->Exe 00578 ); 00579 CsrCaptureMessageBuffer( CaptureBuffer, 00580 (PCHAR) NULL, 00581 a->AliasesBufferLength, 00582 (PVOID *) &a->AliasesBuffer 00583 ); 00584 CsrClientCallServer( (PCSR_API_MSG)&m, 00585 CaptureBuffer, 00586 CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX, 00587 ConsolepGetAliases 00588 ), 00589 sizeof( *a ) 00590 ); 00591 if (NT_SUCCESS( m.ReturnValue )) { 00592 try { 00593 RtlCopyMemory( AliasBuffer, a->AliasesBuffer, a->AliasesBufferLength ); 00594 } except( EXCEPTION_EXECUTE_HANDLER ) { 00595 CsrFreeCaptureBuffer( CaptureBuffer ); 00596 SET_LAST_ERROR(ERROR_INVALID_ACCESS); 00597 return 0; 00598 } 00599 CsrFreeCaptureBuffer( CaptureBuffer ); 00600 return a->AliasesBufferLength; 00601 } else { 00602 CsrFreeCaptureBuffer( CaptureBuffer ); 00603 SET_LAST_NT_ERROR(m.ReturnValue); 00604 return 0; 00605 } 00606 00607 } 00608 00609 #endif 00610 00611 #if !defined(BUILD_WOW64) 00612 00613 DWORD 00614 GetConsoleAliasesW( 00615 OUT LPWSTR AliasBuffer, 00616 IN DWORD AliasBufferLength, 00617 IN LPWSTR ExeName 00618 ) 00619 { 00620 return GetConsoleAliasesInternal(AliasBuffer, 00621 AliasBufferLength, 00622 ExeName, 00623 TRUE 00624 ); 00625 } 00626 00627 DWORD 00628 GetConsoleAliasesA( 00629 OUT LPTSTR AliasBuffer, 00630 IN DWORD AliasBufferLength, 00631 IN LPTSTR ExeName 00632 ) 00633 { 00634 return GetConsoleAliasesInternal(AliasBuffer, 00635 AliasBufferLength, 00636 ExeName, 00637 FALSE 00638 ); 00639 } 00640 00641 #endif 00642 00643 #if !defined(BUILD_WOW6432) 00644 00645 DWORD 00646 APIENTRY 00647 GetConsoleAliasExesInternal( 00648 OUT LPVOID ExeNameBuffer, 00649 IN DWORD ExeNameBufferLength, 00650 IN BOOLEAN Unicode 00651 ) 00652 { 00653 CONSOLE_API_MSG m; 00654 PCONSOLE_GETALIASEXES_MSG a = &m.u.GetConsoleAliasExesW; 00655 PCSR_CAPTURE_HEADER CaptureBuffer; 00656 00657 a->ConsoleHandle = GET_CONSOLE_HANDLE; 00658 a->AliasExesBufferLength = ExeNameBufferLength; 00659 a->Unicode = Unicode; 00660 CaptureBuffer = CsrAllocateCaptureBuffer( 1, 00661 ExeNameBufferLength 00662 ); 00663 if (CaptureBuffer == NULL) { 00664 SET_LAST_ERROR(ERROR_NOT_ENOUGH_MEMORY); 00665 return FALSE; 00666 } 00667 00668 CsrCaptureMessageBuffer( CaptureBuffer, 00669 (PCHAR) NULL, 00670 a->AliasExesBufferLength, 00671 (PVOID *) &a->AliasExesBuffer 00672 ); 00673 00674 CsrClientCallServer( (PCSR_API_MSG)&m, 00675 CaptureBuffer, 00676 CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX, 00677 ConsolepGetAliasExes 00678 ), 00679 sizeof( *a ) 00680 ); 00681 if (NT_SUCCESS( m.ReturnValue )) { 00682 try { 00683 RtlCopyMemory( ExeNameBuffer, a->AliasExesBuffer, a->AliasExesBufferLength ); 00684 } except( EXCEPTION_EXECUTE_HANDLER ) { 00685 CsrFreeCaptureBuffer( CaptureBuffer ); 00686 SET_LAST_ERROR(ERROR_INVALID_ACCESS); 00687 return 0; 00688 } 00689 CsrFreeCaptureBuffer( CaptureBuffer ); 00690 return a->AliasExesBufferLength; 00691 } else { 00692 CsrFreeCaptureBuffer( CaptureBuffer ); 00693 SET_LAST_NT_ERROR(m.ReturnValue); 00694 return 0; 00695 } 00696 00697 } 00698 00699 #endif 00700 00701 #if !defined(BUILD_WOW64) 00702 00703 DWORD 00704 GetConsoleAliasExesW( 00705 OUT LPWSTR ExeNameBuffer, 00706 IN DWORD ExeNameBufferLength 00707 ) 00708 { 00709 return GetConsoleAliasExesInternal(ExeNameBuffer, 00710 ExeNameBufferLength, 00711 TRUE 00712 ); 00713 } 00714 00715 DWORD 00716 GetConsoleAliasExesA( 00717 OUT LPTSTR ExeNameBuffer, 00718 IN DWORD ExeNameBufferLength 00719 ) 00720 { 00721 return GetConsoleAliasExesInternal(ExeNameBuffer, 00722 ExeNameBufferLength, 00723 FALSE 00724 ); 00725 } 00726 00727 #endif 00728 00729 #if !defined(BUILD_WOW6432) 00730 00731 VOID 00732 APIENTRY 00733 ExpungeConsoleCommandHistoryInternal( 00734 IN LPVOID ExeName, 00735 IN BOOLEAN Unicode 00736 ) 00737 { 00738 CONSOLE_API_MSG m; 00739 PCONSOLE_EXPUNGECOMMANDHISTORY_MSG a = &m.u.ExpungeConsoleCommandHistoryW; 00740 PCSR_CAPTURE_HEADER CaptureBuffer; 00741 00742 a->ConsoleHandle = GET_CONSOLE_HANDLE; 00743 a->ExeLength = GetExeName(&ExeName,Unicode,&a->UnicodeExe); 00744 a->Unicode = Unicode; 00745 00746 CaptureBuffer = CsrAllocateCaptureBuffer( 1, 00747 a->ExeLength 00748 ); 00749 if (CaptureBuffer == NULL) { 00750 SET_LAST_ERROR(ERROR_NOT_ENOUGH_MEMORY); 00751 return; 00752 } 00753 00754 CsrCaptureMessageBuffer( CaptureBuffer, 00755 (PCHAR) ExeName, 00756 a->ExeLength, 00757 (PVOID *) &a->Exe 00758 ); 00759 00760 CsrClientCallServer( (PCSR_API_MSG)&m, 00761 CaptureBuffer, 00762 CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX, 00763 ConsolepExpungeCommandHistory 00764 ), 00765 sizeof( *a ) 00766 ); 00767 CsrFreeCaptureBuffer( CaptureBuffer ); 00768 00769 } 00770 00771 00772 #endif 00773 00774 #if !defined(BUILD_WOW64) 00775 00776 VOID 00777 ExpungeConsoleCommandHistoryW( 00778 IN LPWSTR ExeName 00779 ) 00780 { 00781 ExpungeConsoleCommandHistoryInternal(ExeName,TRUE); 00782 } 00783 00784 VOID 00785 ExpungeConsoleCommandHistoryA( 00786 IN LPTSTR ExeName 00787 ) 00788 { 00789 ExpungeConsoleCommandHistoryInternal(ExeName,FALSE); 00790 } 00791 00792 #endif 00793 00794 #if !defined(BUILD_WOW6432) 00795 00796 BOOL 00797 APIENTRY 00798 SetConsoleNumberOfCommandsInternal( 00799 IN DWORD Number, 00800 IN LPVOID ExeName, 00801 IN BOOLEAN Unicode 00802 ) 00803 { 00804 CONSOLE_API_MSG m; 00805 PCONSOLE_SETNUMBEROFCOMMANDS_MSG a = &m.u.SetConsoleNumberOfCommandsW; 00806 PCSR_CAPTURE_HEADER CaptureBuffer; 00807 00808 a->ConsoleHandle = GET_CONSOLE_HANDLE; 00809 a->NumCommands = Number; 00810 a->ExeLength = GetExeName(&ExeName,Unicode,&a->UnicodeExe); 00811 a->Unicode = Unicode; 00812 CaptureBuffer = CsrAllocateCaptureBuffer( 1, 00813 a->ExeLength 00814 ); 00815 if (CaptureBuffer == NULL) { 00816 SET_LAST_ERROR(ERROR_NOT_ENOUGH_MEMORY); 00817 return FALSE; 00818 } 00819 00820 CsrCaptureMessageBuffer( CaptureBuffer, 00821 (PCHAR) ExeName, 00822 a->ExeLength, 00823 (PVOID *) &a->Exe 00824 ); 00825 00826 CsrClientCallServer( (PCSR_API_MSG)&m, 00827 CaptureBuffer, 00828 CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX, 00829 ConsolepSetNumberOfCommands 00830 ), 00831 sizeof( *a ) 00832 ); 00833 CsrFreeCaptureBuffer( CaptureBuffer ); 00834 if (NT_SUCCESS( m.ReturnValue )) { 00835 return TRUE; 00836 } else { 00837 SET_LAST_NT_ERROR(m.ReturnValue); 00838 return FALSE; 00839 } 00840 00841 } 00842 00843 #endif 00844 00845 #if !defined(BUILD_WOW64) 00846 00847 BOOL 00848 SetConsoleNumberOfCommandsW( 00849 IN DWORD Number, 00850 IN LPWSTR ExeName 00851 ) 00852 { 00853 return SetConsoleNumberOfCommandsInternal(Number, 00854 ExeName, 00855 TRUE 00856 ); 00857 } 00858 00859 BOOL 00860 SetConsoleNumberOfCommandsA( 00861 IN DWORD Number, 00862 IN LPTSTR ExeName 00863 ) 00864 { 00865 return SetConsoleNumberOfCommandsInternal(Number, 00866 ExeName, 00867 FALSE 00868 ); 00869 } 00870 00871 #endif 00872 00873 #if !defined(BUILD_WOW6432) 00874 00875 DWORD 00876 APIENTRY 00877 GetConsoleCommandHistoryLengthInternal( 00878 IN LPVOID ExeName, 00879 IN BOOLEAN Unicode 00880 ) 00881 { 00882 CONSOLE_API_MSG m; 00883 PCONSOLE_GETCOMMANDHISTORYLENGTH_MSG a = &m.u.GetConsoleCommandHistoryLengthW; 00884 PCSR_CAPTURE_HEADER CaptureBuffer; 00885 00886 a->ConsoleHandle = GET_CONSOLE_HANDLE; 00887 a->ExeLength = GetExeName(&ExeName,Unicode,&a->UnicodeExe); 00888 a->Unicode = Unicode; 00889 CaptureBuffer = CsrAllocateCaptureBuffer( 1, 00890 a->ExeLength 00891 ); 00892 if (CaptureBuffer == NULL) { 00893 SET_LAST_ERROR(ERROR_NOT_ENOUGH_MEMORY); 00894 return 0; 00895 } 00896 00897 CsrCaptureMessageBuffer( CaptureBuffer, 00898 (PCHAR) ExeName, 00899 a->ExeLength, 00900 (PVOID *) &a->Exe 00901 ); 00902 00903 CsrClientCallServer( (PCSR_API_MSG)&m, 00904 CaptureBuffer, 00905 CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX, 00906 ConsolepGetCommandHistoryLength 00907 ), 00908 sizeof( *a ) 00909 ); 00910 CsrFreeCaptureBuffer( CaptureBuffer ); 00911 return a->CommandHistoryLength; 00912 00913 } 00914 00915 #endif 00916 00917 #if !defined(BUILD_WOW64) 00918 00919 DWORD 00920 GetConsoleCommandHistoryLengthW( 00921 IN LPWSTR ExeName 00922 ) 00923 { 00924 return GetConsoleCommandHistoryLengthInternal(ExeName, 00925 TRUE 00926 ); 00927 } 00928 00929 DWORD 00930 GetConsoleCommandHistoryLengthA( 00931 IN LPTSTR ExeName 00932 ) 00933 { 00934 return GetConsoleCommandHistoryLengthInternal(ExeName, 00935 FALSE 00936 ); 00937 } 00938 00939 #endif 00940 00941 #if !defined(BUILD_WOW6432) 00942 00943 DWORD 00944 APIENTRY 00945 GetConsoleCommandHistoryInternal( 00946 OUT LPVOID Commands, 00947 IN DWORD CommandBufferLength, 00948 IN LPVOID ExeName, 00949 IN BOOLEAN Unicode 00950 ) 00951 { 00952 CONSOLE_API_MSG m; 00953 PCONSOLE_GETCOMMANDHISTORY_MSG a = &m.u.GetConsoleCommandHistoryW; 00954 PCSR_CAPTURE_HEADER CaptureBuffer; 00955 00956 a->ConsoleHandle = GET_CONSOLE_HANDLE; 00957 a->CommandBufferLength = CommandBufferLength; 00958 a->ExeLength = GetExeName(&ExeName,Unicode,&a->UnicodeExe); 00959 a->Unicode = Unicode; 00960 CaptureBuffer = CsrAllocateCaptureBuffer( 2, 00961 CommandBufferLength + a->ExeLength 00962 ); 00963 if (CaptureBuffer == NULL) { 00964 SET_LAST_ERROR(ERROR_NOT_ENOUGH_MEMORY); 00965 return 0; 00966 } 00967 00968 00969 CsrCaptureMessageBuffer( CaptureBuffer, 00970 (PCHAR) ExeName, 00971 a->ExeLength, 00972 (PVOID *) &a->Exe 00973 ); 00974 00975 CsrCaptureMessageBuffer( CaptureBuffer, 00976 (PCHAR) NULL, 00977 a->CommandBufferLength, 00978 (PVOID *) &a->CommandBuffer 00979 ); 00980 CsrClientCallServer( (PCSR_API_MSG)&m, 00981 CaptureBuffer, 00982 CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX, 00983 ConsolepGetCommandHistory 00984 ), 00985 sizeof( *a ) 00986 ); 00987 if (NT_SUCCESS( m.ReturnValue )) { 00988 try { 00989 RtlCopyMemory( Commands, a->CommandBuffer, a->CommandBufferLength ); 00990 } except( EXCEPTION_EXECUTE_HANDLER ) { 00991 CsrFreeCaptureBuffer( CaptureBuffer ); 00992 SET_LAST_ERROR(ERROR_INVALID_ACCESS); 00993 return 0; 00994 } 00995 CsrFreeCaptureBuffer( CaptureBuffer ); 00996 return a->CommandBufferLength; 00997 } else { 00998 CsrFreeCaptureBuffer( CaptureBuffer ); 00999 SET_LAST_NT_ERROR(m.ReturnValue); 01000 return 0; 01001 } 01002 01003 } 01004 01005 #endif 01006 01007 #if !defined(BUILD_WOW64) 01008 01009 DWORD 01010 GetConsoleCommandHistoryW( 01011 OUT LPWSTR Commands, 01012 IN DWORD CommandBufferLength, 01013 IN LPWSTR ExeName 01014 ) 01015 { 01016 return GetConsoleCommandHistoryInternal(Commands, 01017 CommandBufferLength, 01018 ExeName, 01019 TRUE 01020 ); 01021 } 01022 01023 DWORD 01024 GetConsoleCommandHistoryA( 01025 OUT LPTSTR Commands, 01026 IN DWORD CommandBufferLength, 01027 IN LPTSTR ExeName 01028 ) 01029 { 01030 return GetConsoleCommandHistoryInternal(Commands, 01031 CommandBufferLength, 01032 ExeName, 01033 FALSE 01034 ); 01035 } 01036 01037 #endif 01038 01039 #if !defined(BUILD_WOW6432) 01040 01041 BOOL 01042 APIENTRY 01043 SetConsoleCommandHistoryMode( 01044 IN DWORD Flags 01045 ) 01046 { 01047 CONSOLE_API_MSG m; 01048 PCONSOLE_SETCOMMANDHISTORYMODE_MSG a = &m.u.SetConsoleCommandHistoryMode; 01049 01050 a->ConsoleHandle = GET_CONSOLE_HANDLE; 01051 a->Flags = Flags; 01052 01053 CsrClientCallServer( (PCSR_API_MSG)&m, 01054 NULL, 01055 CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX, 01056 ConsolepSetCommandHistoryMode 01057 ), 01058 sizeof( *a ) 01059 ); 01060 if (NT_SUCCESS( m.ReturnValue )) { 01061 return TRUE; 01062 } else { 01063 SET_LAST_NT_ERROR(m.ReturnValue); 01064 return FALSE; 01065 } 01066 } 01067 01068 DWORD 01069 APIENTRY 01070 GetConsoleTitleInternal( 01071 OUT PVOID lpConsoleTitle, 01072 IN DWORD nSize, 01073 IN BOOLEAN Unicode 01074 ) 01075 { 01076 CONSOLE_API_MSG m; 01077 PCONSOLE_GETTITLE_MSG a = &m.u.GetConsoleTitle; 01078 PCSR_CAPTURE_HEADER CaptureBuffer; 01079 01080 if (nSize == 0) { 01081 return 0; 01082 } 01083 a->ConsoleHandle = GET_CONSOLE_HANDLE; 01084 a->TitleLength = nSize; 01085 a->Unicode = Unicode; 01086 CaptureBuffer = CsrAllocateCaptureBuffer( 1, 01087 a->TitleLength 01088 ); 01089 if (CaptureBuffer == NULL) { 01090 return 0; 01091 } 01092 CsrCaptureMessageBuffer( CaptureBuffer, 01093 NULL, 01094 a->TitleLength, 01095 (PVOID *) &a->Title 01096 ); 01097 CsrClientCallServer( (PCSR_API_MSG)&m, 01098 CaptureBuffer, 01099 CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX, 01100 ConsolepGetTitle 01101 ), 01102 sizeof( *a ) 01103 ); 01104 if (NT_SUCCESS( m.ReturnValue )) { 01105 try { 01106 RtlCopyMemory( lpConsoleTitle, a->Title, a->TitleLength); 01107 } except( EXCEPTION_EXECUTE_HANDLER ) { 01108 CsrFreeCaptureBuffer( CaptureBuffer ); 01109 SET_LAST_ERROR(ERROR_INVALID_ACCESS); 01110 return 0; 01111 } 01112 } 01113 else { 01114 CsrFreeCaptureBuffer( CaptureBuffer ); 01115 SET_LAST_NT_ERROR (m.ReturnValue); 01116 return 0; 01117 } 01118 CsrFreeCaptureBuffer( CaptureBuffer ); 01119 return a->TitleLength; 01120 01121 } 01122 01123 #endif 01124 01125 #if !defined(BUILD_WOW64) 01126 01127 DWORD 01128 APIENTRY 01129 GetConsoleTitleA( 01130 LPSTR lpConsoleTitle, 01131 DWORD nSize 01132 ) 01133 { 01134 DWORD NumBytes; 01135 NumBytes = GetConsoleTitleInternal(lpConsoleTitle, 01136 nSize-sizeof(CHAR), // allow room for null 01137 FALSE 01138 ); 01139 if (NumBytes) { 01140 lpConsoleTitle[NumBytes] = '\0'; // NULL terminate 01141 } 01142 return NumBytes; 01143 } 01144 01145 DWORD 01146 APIENTRY 01147 GetConsoleTitleW( 01148 LPWSTR lpConsoleTitle, 01149 DWORD nSize 01150 ) 01151 { 01152 DWORD NumBytes; 01153 NumBytes = GetConsoleTitleInternal(lpConsoleTitle, 01154 (nSize-1)*sizeof(WCHAR), // allow room for null 01155 TRUE 01156 ); 01157 if (NumBytes) { 01158 NumBytes /= sizeof(WCHAR); 01159 lpConsoleTitle[NumBytes] = CONSOLE_NAME_PATH_TERMINATOR; // NULL terminate 01160 } 01161 return NumBytes; 01162 } 01163 01164 #endif 01165 01166 #if !defined(BUILD_WOW6432) 01167 01168 BOOL 01169 APIENTRY 01170 SetConsoleTitleInternal( 01171 IN CONST VOID *lpConsoleTitle, 01172 IN DWORD TitleLength, // in bytes 01173 IN BOOLEAN Unicode 01174 ) 01175 { 01176 CONSOLE_API_MSG m; 01177 PCONSOLE_SETTITLE_MSG a = &m.u.SetConsoleTitle; 01178 PCSR_CAPTURE_HEADER CaptureBuffer; 01179 01180 a->ConsoleHandle = GET_CONSOLE_HANDLE; 01181 a->TitleLength = TitleLength; 01182 a->Unicode = Unicode; 01183 CaptureBuffer = CsrAllocateCaptureBuffer( 1, 01184 a->TitleLength 01185 ); 01186 if (CaptureBuffer == NULL) { 01187 return FALSE; 01188 } 01189 CsrCaptureMessageBuffer( CaptureBuffer, 01190 (PCHAR) lpConsoleTitle, 01191 a->TitleLength, 01192 (PVOID *) &a->Title 01193 ); 01194 CsrClientCallServer( (PCSR_API_MSG)&m, 01195 CaptureBuffer, 01196 CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX, 01197 ConsolepSetTitle 01198 ), 01199 sizeof( *a ) 01200 ); 01201 01202 CsrFreeCaptureBuffer( CaptureBuffer ); 01203 01204 if (NT_SUCCESS( m.ReturnValue )) { 01205 return TRUE; 01206 } 01207 else { 01208 SET_LAST_NT_ERROR (m.ReturnValue); 01209 return FALSE; 01210 } 01211 01212 } 01213 01214 #endif 01215 01216 #if !defined(BUILD_WOW64) 01217 01218 BOOL 01219 APIENTRY 01220 SetConsoleTitleA( 01221 LPCSTR lpConsoleTitle 01222 ) 01223 { 01224 DWORD TitleLength; 01225 try { 01226 TitleLength = strlen(lpConsoleTitle); 01227 } except( EXCEPTION_EXECUTE_HANDLER ) { 01228 SET_LAST_ERROR(ERROR_INVALID_ACCESS); 01229 return FALSE; 01230 } 01231 return SetConsoleTitleInternal(lpConsoleTitle, 01232 TitleLength, 01233 FALSE); 01234 } 01235 01236 BOOL 01237 APIENTRY 01238 SetConsoleTitleW( 01239 LPCWSTR lpConsoleTitle 01240 ) 01241 { 01242 DWORD TitleLength; 01243 try { 01244 TitleLength = wcslen(lpConsoleTitle)*sizeof(WCHAR); 01245 } except( EXCEPTION_EXECUTE_HANDLER ) { 01246 SET_LAST_ERROR(ERROR_INVALID_ACCESS); 01247 return FALSE; 01248 } 01249 return SetConsoleTitleInternal(lpConsoleTitle, 01250 TitleLength, 01251 TRUE); 01252 } 01253 01254 01255 DWORD 01256 GetConsoleInputExeNameA( 01257 IN DWORD nBufferLength, 01258 OUT LPSTR lpBuffer 01259 ) 01260 { 01261 PUNICODE_STRING Unicode; 01262 ANSI_STRING AnsiString; 01263 NTSTATUS Status; 01264 UNICODE_STRING UnicodeString; 01265 WCHAR TempExeNameBuffer[FNAME_LENGTH]; 01266 DWORD n; 01267 01268 n = GetConsoleInputExeNameW(FNAME_LENGTH, TempExeNameBuffer); 01269 if (n != 0 && n < FNAME_LENGTH) { 01270 RtlInitUnicodeString(&UnicodeString, TempExeNameBuffer); 01271 AnsiString.Buffer = lpBuffer; 01272 AnsiString.Length = 0; 01273 AnsiString.MaximumLength = (USHORT)nBufferLength; 01274 Status = RtlUnicodeStringToAnsiString(&AnsiString, &UnicodeString, FALSE); 01275 if (!NT_SUCCESS(Status)) { 01276 if (Status == STATUS_BUFFER_OVERFLOW) { 01277 SET_LAST_ERROR(ERROR_BUFFER_OVERFLOW); 01278 return n+1; 01279 } else { 01280 SET_LAST_ERROR(ERROR_INVALID_PARAMETER); 01281 } 01282 } 01283 } 01284 return n; 01285 } 01286 01287 DWORD 01288 GetConsoleInputExeNameW( 01289 IN DWORD nBufferLength, 01290 OUT LPWSTR lpBuffer 01291 ) 01292 { 01293 DWORD n; 01294 01295 n = ExeNameLength; 01296 if (n >= nBufferLength) { 01297 SET_LAST_ERROR(ERROR_BUFFER_OVERFLOW); 01298 return n+1; 01299 } 01300 01301 try { 01302 RtlEnterCriticalSection(&ExeNameCriticalSection); 01303 RtlCopyMemory(lpBuffer, ExeNameBuffer, n*sizeof(WCHAR)); 01304 lpBuffer[n] = UNICODE_NULL; 01305 } finally { 01306 RtlLeaveCriticalSection(&ExeNameCriticalSection); 01307 } 01308 01309 return TRUE; 01310 } 01311 01312 BOOL 01313 SetConsoleInputExeNameA( 01314 IN LPSTR lpExeName 01315 ) 01316 { 01317 PUNICODE_STRING Unicode; 01318 ANSI_STRING AnsiString; 01319 NTSTATUS Status; 01320 01321 Unicode = &NtCurrentTeb()->StaticUnicodeString; 01322 RtlInitAnsiString(&AnsiString,lpExeName); 01323 Status = RtlAnsiStringToUnicodeString(Unicode,&AnsiString,FALSE); 01324 if (!NT_SUCCESS(Status)) { 01325 if (Status == STATUS_BUFFER_OVERFLOW) { 01326 SET_LAST_ERROR(ERROR_FILENAME_EXCED_RANGE); 01327 } else { 01328 SET_LAST_ERROR(ERROR_INVALID_PARAMETER); 01329 } 01330 return FALSE; 01331 } 01332 01333 return SetConsoleInputExeNameW(Unicode->Buffer); 01334 } 01335 01336 BOOL 01337 SetConsoleInputExeNameW( 01338 IN LPWSTR lpExeName 01339 ) 01340 { 01341 DWORD n; 01342 01343 n = lstrlenW(lpExeName); 01344 if (n == 0 || n >= FNAME_LENGTH) { 01345 SET_LAST_ERROR(ERROR_INVALID_PARAMETER); 01346 return FALSE; 01347 } 01348 01349 RtlEnterCriticalSection(&ExeNameCriticalSection); 01350 try { 01351 RtlCopyMemory(ExeNameBuffer,lpExeName,n*sizeof(WCHAR)); 01352 ExeNameLength = (USHORT)n; 01353 } finally { 01354 RtlLeaveCriticalSection(&ExeNameCriticalSection); 01355 } 01356 01357 return TRUE; 01358 } 01359 01360 #endif

Generated on Sat May 15 19:39:27 2004 for test by doxygen 1.3.7