b96e1e8cd6d670e567368634de3eda0f7f957c4e
[reactos.git] / dll / win32 / kernel32 / client / console / alias.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: dll/win32/kernel32/client/console/alias.c
5 * PURPOSE: Win32 Console Client Alias support functions
6 * PROGRAMMERS: David Welch (welch@cwcom.net) (welch@mcmail.com)
7 * Christoph von Wittich (christoph_vw@reactos.org)
8 * Johannes Anderwald (johannes.anderwald@reactos.org)
9 */
10
11 /* INCLUDES *******************************************************************/
12
13 #include <k32.h>
14
15 #define NDEBUG
16 #include <debug.h>
17
18
19 /* FUNCTIONS ******************************************************************/
20
21 /*
22 * @implemented
23 */
24 BOOL
25 WINAPI
26 AddConsoleAliasW(LPCWSTR lpSource,
27 LPCWSTR lpTarget,
28 LPCWSTR lpExeName)
29 {
30 NTSTATUS Status;
31 CONSOLE_API_MESSAGE ApiMessage;
32 PCONSOLE_ADDGETALIAS ConsoleAliasRequest = &ApiMessage.Data.ConsoleAliasRequest;
33 PCSR_CAPTURE_BUFFER CaptureBuffer;
34 ULONG CapturedStrings;
35
36 DPRINT("AddConsoleAliasW enterd with lpSource %S lpTarget %S lpExeName %S\n", lpSource, lpTarget, lpExeName);
37
38 /* Determine the needed sizes */
39 ConsoleAliasRequest->SourceLength = (wcslen(lpSource ) + 1) * sizeof(WCHAR);
40 ConsoleAliasRequest->ExeLength = (wcslen(lpExeName) + 1) * sizeof(WCHAR);
41 CapturedStrings = 2;
42
43 if (lpTarget) /* The target can be optional */
44 {
45 ConsoleAliasRequest->TargetLength = (wcslen(lpTarget) + 1) * sizeof(WCHAR);
46 CapturedStrings++;
47 }
48 else
49 {
50 ConsoleAliasRequest->TargetLength = 0;
51 }
52
53 /* Allocate a Capture Buffer */
54 CaptureBuffer = CsrAllocateCaptureBuffer(CapturedStrings,
55 ConsoleAliasRequest->SourceLength +
56 ConsoleAliasRequest->ExeLength +
57 ConsoleAliasRequest->TargetLength);
58 if (CaptureBuffer == NULL)
59 {
60 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
61 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
62 return FALSE;
63 }
64
65 /* Capture the strings */
66 CsrCaptureMessageBuffer(CaptureBuffer,
67 (PVOID)lpSource,
68 ConsoleAliasRequest->SourceLength,
69 (PVOID*)&ConsoleAliasRequest->Source);
70
71 CsrCaptureMessageBuffer(CaptureBuffer,
72 (PVOID)lpExeName,
73 ConsoleAliasRequest->ExeLength,
74 (PVOID*)&ConsoleAliasRequest->Exe);
75
76 if (lpTarget) /* The target can be optional */
77 {
78 CsrCaptureMessageBuffer(CaptureBuffer,
79 (PVOID)lpTarget,
80 ConsoleAliasRequest->TargetLength,
81 (PVOID*)&ConsoleAliasRequest->Target);
82 }
83 else
84 {
85 ConsoleAliasRequest->Target = NULL;
86 }
87
88 Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
89 CaptureBuffer,
90 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepAddAlias),
91 sizeof(CONSOLE_ADDGETALIAS));
92
93 CsrFreeCaptureBuffer(CaptureBuffer);
94
95 if (!NT_SUCCESS(Status))
96 {
97 BaseSetLastNTError(Status);
98 return FALSE;
99 }
100
101 return TRUE;
102 }
103
104
105 /*
106 * @implemented
107 */
108 BOOL
109 WINAPI
110 AddConsoleAliasA(LPCSTR lpSource,
111 LPCSTR lpTarget,
112 LPCSTR lpExeName)
113 {
114 LPWSTR lpSourceW = NULL;
115 LPWSTR lpTargetW = NULL;
116 LPWSTR lpExeNameW = NULL;
117 BOOL bRetVal;
118
119 if (lpSource)
120 BasepAnsiStringToHeapUnicodeString(lpSource, (LPWSTR*)&lpSourceW);
121 if (lpTarget)
122 BasepAnsiStringToHeapUnicodeString(lpTarget, (LPWSTR*)&lpTargetW);
123 if (lpExeName)
124 BasepAnsiStringToHeapUnicodeString(lpExeName, (LPWSTR*)&lpExeNameW);
125
126 bRetVal = AddConsoleAliasW(lpSourceW, lpTargetW, lpExeNameW);
127
128 /* Clean up */
129 if (lpSourceW)
130 RtlFreeHeap(GetProcessHeap(), 0, (LPWSTR*)lpSourceW);
131 if (lpTargetW)
132 RtlFreeHeap(GetProcessHeap(), 0, (LPWSTR*)lpTargetW);
133 if (lpExeNameW)
134 RtlFreeHeap(GetProcessHeap(), 0, (LPWSTR*)lpExeNameW);
135
136 return bRetVal;
137 }
138
139
140 /*
141 * @implemented
142 */
143 DWORD
144 WINAPI
145 GetConsoleAliasW(LPWSTR lpSource,
146 LPWSTR lpTargetBuffer,
147 DWORD TargetBufferLength,
148 LPWSTR lpExeName)
149 {
150 NTSTATUS Status;
151 CONSOLE_API_MESSAGE ApiMessage;
152 PCONSOLE_ADDGETALIAS ConsoleAliasRequest = &ApiMessage.Data.ConsoleAliasRequest;
153 PCSR_CAPTURE_BUFFER CaptureBuffer;
154
155 DPRINT("GetConsoleAliasW entered with lpSource %S lpExeName %S\n", lpSource, lpExeName);
156
157 if (lpTargetBuffer == NULL)
158 {
159 SetLastError(ERROR_INVALID_PARAMETER);
160 return 0;
161 }
162
163 /* Determine the needed sizes */
164 ConsoleAliasRequest->SourceLength = (wcslen(lpSource ) + 1) * sizeof(WCHAR);
165 ConsoleAliasRequest->ExeLength = (wcslen(lpExeName) + 1) * sizeof(WCHAR);
166
167 ConsoleAliasRequest->Target = NULL;
168 ConsoleAliasRequest->TargetLength = TargetBufferLength;
169
170 /* Allocate a Capture Buffer */
171 CaptureBuffer = CsrAllocateCaptureBuffer(3, ConsoleAliasRequest->SourceLength +
172 ConsoleAliasRequest->ExeLength +
173 ConsoleAliasRequest->TargetLength);
174 if (!CaptureBuffer)
175 {
176 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
177 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
178 return 0;
179 }
180
181 /* Capture the strings */
182 CsrCaptureMessageBuffer(CaptureBuffer,
183 (PVOID)lpSource,
184 ConsoleAliasRequest->SourceLength,
185 (PVOID*)&ConsoleAliasRequest->Source);
186
187 CsrCaptureMessageBuffer(CaptureBuffer,
188 (PVOID)lpExeName,
189 ConsoleAliasRequest->ExeLength,
190 (PVOID*)&ConsoleAliasRequest->Exe);
191
192 /* Allocate space for the target buffer */
193 CsrAllocateMessagePointer(CaptureBuffer,
194 ConsoleAliasRequest->TargetLength,
195 (PVOID*)&ConsoleAliasRequest->Target);
196
197 Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
198 CaptureBuffer,
199 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetAlias),
200 sizeof(CONSOLE_ADDGETALIAS));
201 if (!NT_SUCCESS(Status))
202 {
203 CsrFreeCaptureBuffer(CaptureBuffer);
204 BaseSetLastNTError(Status);
205 return 0;
206 }
207
208 /* Copy the returned target string into the user buffer */
209 // wcscpy(lpTargetBuffer, ConsoleAliasRequest->Target);
210 memcpy(lpTargetBuffer,
211 ConsoleAliasRequest->Target,
212 ConsoleAliasRequest->TargetLength);
213
214 /* Release the capture buffer and exit */
215 CsrFreeCaptureBuffer(CaptureBuffer);
216
217 return ConsoleAliasRequest->TargetLength;
218 }
219
220
221 /*
222 * @implemented
223 */
224 DWORD
225 WINAPI
226 GetConsoleAliasA(LPSTR lpSource,
227 LPSTR lpTargetBuffer,
228 DWORD TargetBufferLength,
229 LPSTR lpExeName)
230 {
231 LPWSTR lpwSource;
232 LPWSTR lpwExeName;
233 LPWSTR lpwTargetBuffer;
234 UINT dwSourceSize;
235 UINT dwExeNameSize;
236 UINT dwResult;
237
238 DPRINT("GetConsoleAliasA entered\n");
239
240 if (lpTargetBuffer == NULL)
241 {
242 SetLastError(ERROR_INVALID_PARAMETER);
243 return 0;
244 }
245
246 dwSourceSize = (strlen(lpSource)+1) * sizeof(WCHAR);
247 lpwSource = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwSourceSize);
248 if (lpwSource == NULL)
249 {
250 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
251 return 0;
252 }
253 MultiByteToWideChar(CP_ACP, 0, lpSource, -1, lpwSource, dwSourceSize);
254
255 dwExeNameSize = (strlen(lpExeName)+1) * sizeof(WCHAR);
256 lpwExeName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwExeNameSize);
257 if (lpwExeName == NULL)
258 {
259 HeapFree(GetProcessHeap(), 0, lpwSource);
260 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
261 return 0;
262 }
263 MultiByteToWideChar(CP_ACP, 0, lpExeName, -1, lpwExeName, dwExeNameSize);
264
265 lpwTargetBuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, TargetBufferLength * sizeof(WCHAR));
266 if (lpwTargetBuffer == NULL)
267 {
268 HeapFree(GetProcessHeap(), 0, lpwSource);
269 HeapFree(GetProcessHeap(), 0, lpwExeName);
270 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
271 return 0;
272 }
273
274 dwResult = GetConsoleAliasW(lpwSource, lpwTargetBuffer, TargetBufferLength * sizeof(WCHAR), lpwExeName);
275
276 HeapFree(GetProcessHeap(), 0, lpwSource);
277 HeapFree(GetProcessHeap(), 0, lpwExeName);
278
279 if (dwResult)
280 dwResult = WideCharToMultiByte(CP_ACP, 0, lpwTargetBuffer, dwResult / sizeof(WCHAR), lpTargetBuffer, TargetBufferLength, NULL, NULL);
281
282 HeapFree(GetProcessHeap(), 0, lpwTargetBuffer);
283
284 return dwResult;
285 }
286
287
288 /*
289 * @implemented
290 */
291 DWORD
292 WINAPI
293 GetConsoleAliasesW(LPWSTR AliasBuffer,
294 DWORD AliasBufferLength,
295 LPWSTR ExeName)
296 {
297 NTSTATUS Status;
298 CONSOLE_API_MESSAGE ApiMessage;
299 PCONSOLE_GETALLALIASES GetAllAliasesRequest = &ApiMessage.Data.GetAllAliasesRequest;
300 PCSR_CAPTURE_BUFFER CaptureBuffer;
301
302 DPRINT("GetConsoleAliasesW entered\n");
303
304 /* Determine the needed sizes */
305 GetAllAliasesRequest->ExeLength = GetConsoleAliasesLengthW(ExeName);
306 if (GetAllAliasesRequest->ExeLength == 0 ||
307 GetAllAliasesRequest->ExeLength > AliasBufferLength)
308 {
309 return 0;
310 }
311
312 GetAllAliasesRequest->AliasesBufferLength = AliasBufferLength;
313
314 /* Allocate a Capture Buffer */
315 CaptureBuffer = CsrAllocateCaptureBuffer(2, GetAllAliasesRequest->ExeLength +
316 GetAllAliasesRequest->AliasesBufferLength);
317 if (!CaptureBuffer)
318 {
319 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
320 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
321 return 0;
322 }
323
324 /* Capture the exe name and allocate space for the aliases buffer */
325 CsrCaptureMessageBuffer(CaptureBuffer,
326 (PVOID)ExeName,
327 GetAllAliasesRequest->ExeLength,
328 (PVOID*)&GetAllAliasesRequest->ExeName);
329
330 CsrAllocateMessagePointer(CaptureBuffer,
331 GetAllAliasesRequest->AliasesBufferLength,
332 (PVOID*)&GetAllAliasesRequest->AliasesBuffer);
333
334 Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
335 CaptureBuffer,
336 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetAliases),
337 sizeof(CONSOLE_GETALLALIASES));
338 if (!NT_SUCCESS(Status))
339 {
340 BaseSetLastNTError(Status);
341 return 0;
342 }
343
344 /* Copy the returned aliases string into the user buffer */
345 // wcscpy(AliasBuffer, GetAllAliasesRequest->AliasesBuffer);
346 memcpy(AliasBuffer,
347 GetAllAliasesRequest->AliasesBuffer,
348 GetAllAliasesRequest->AliasesBufferLength);
349
350 /* Release the capture buffer and exit */
351 CsrFreeCaptureBuffer(CaptureBuffer);
352
353 return GetAllAliasesRequest->AliasesBufferLength; // / sizeof(WCHAR); (original code)
354 }
355
356
357 /*
358 * @implemented
359 */
360 DWORD
361 WINAPI
362 GetConsoleAliasesA(LPSTR AliasBuffer,
363 DWORD AliasBufferLength,
364 LPSTR ExeName)
365 {
366 DWORD dwRetVal = 0;
367 LPWSTR lpwExeName = NULL;
368 LPWSTR lpwAliasBuffer;
369
370 DPRINT("GetConsoleAliasesA entered\n");
371
372 if (ExeName)
373 BasepAnsiStringToHeapUnicodeString(ExeName, (LPWSTR*)&lpwExeName);
374
375 lpwAliasBuffer = HeapAlloc(GetProcessHeap(), 0, AliasBufferLength * sizeof(WCHAR));
376
377 dwRetVal = GetConsoleAliasesW(lpwAliasBuffer, AliasBufferLength * sizeof(WCHAR), lpwExeName);
378
379 if (lpwExeName)
380 RtlFreeHeap(GetProcessHeap(), 0, (LPWSTR*)lpwExeName);
381
382 if (dwRetVal)
383 dwRetVal = WideCharToMultiByte(CP_ACP, 0, lpwAliasBuffer, dwRetVal /**/ / sizeof(WCHAR) /**/, AliasBuffer, AliasBufferLength, NULL, NULL);
384
385 HeapFree(GetProcessHeap(), 0, lpwAliasBuffer);
386 return dwRetVal;
387 }
388
389
390 static DWORD
391 IntGetConsoleAliasesLength(LPVOID lpExeName, BOOLEAN bUnicode)
392 {
393 CONSOLE_API_MESSAGE ApiMessage;
394 PCONSOLE_GETALLALIASESLENGTH GetAllAliasesLengthRequest = &ApiMessage.Data.GetAllAliasesLengthRequest;
395 PCSR_CAPTURE_BUFFER CaptureBuffer;
396
397 DWORD dwNumChars = (lpExeName ? (bUnicode ? wcslen(lpExeName) : strlen(lpExeName)) : 0);
398
399 if (lpExeName == NULL || dwNumChars == 0)
400 {
401 SetLastError(ERROR_INVALID_PARAMETER);
402 return 0;
403 }
404
405 GetAllAliasesLengthRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
406 GetAllAliasesLengthRequest->ExeLength = dwNumChars * (bUnicode ? sizeof(WCHAR) : sizeof(CHAR));
407 GetAllAliasesLengthRequest->Unicode =
408 GetAllAliasesLengthRequest->Unicode2 = bUnicode;
409
410 CaptureBuffer = CsrAllocateCaptureBuffer(1, GetAllAliasesLengthRequest->ExeLength);
411 if (!CaptureBuffer)
412 {
413 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
414 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
415 return 0;
416 }
417
418 CsrCaptureMessageBuffer(CaptureBuffer,
419 (PVOID)lpExeName,
420 GetAllAliasesLengthRequest->ExeLength,
421 (PVOID)&GetAllAliasesLengthRequest->ExeName);
422
423 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
424 CaptureBuffer,
425 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetAliasesLength),
426 sizeof(*GetAllAliasesLengthRequest));
427
428 CsrFreeCaptureBuffer(CaptureBuffer);
429
430 if (!NT_SUCCESS(ApiMessage.Status))
431 {
432 BaseSetLastNTError(ApiMessage.Status);
433 return 0;
434 }
435
436 return GetAllAliasesLengthRequest->Length;
437 }
438
439
440 /*
441 * @implemented
442 */
443 DWORD
444 WINAPI
445 GetConsoleAliasesLengthW(LPWSTR lpExeName)
446 {
447 return IntGetConsoleAliasesLength(lpExeName, TRUE);
448 }
449
450
451 /*
452 * @implemented
453 */
454 DWORD
455 WINAPI
456 GetConsoleAliasesLengthA(LPSTR lpExeName)
457 {
458 return IntGetConsoleAliasesLength(lpExeName, FALSE);
459 }
460
461
462 static DWORD
463 IntGetConsoleAliasExes(PVOID lpExeNameBuffer,
464 DWORD ExeNameBufferLength,
465 BOOLEAN bUnicode)
466 {
467 CONSOLE_API_MESSAGE ApiMessage;
468 PCONSOLE_GETALIASESEXES GetAliasesExesRequest = &ApiMessage.Data.GetAliasesExesRequest;
469 PCSR_CAPTURE_BUFFER CaptureBuffer;
470
471 GetAliasesExesRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
472 GetAliasesExesRequest->Length = ExeNameBufferLength;
473 GetAliasesExesRequest->Unicode = bUnicode;
474
475 CaptureBuffer = CsrAllocateCaptureBuffer(1, ExeNameBufferLength);
476 if (!CaptureBuffer)
477 {
478 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
479 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
480 return 0;
481 }
482
483 CsrAllocateMessagePointer(CaptureBuffer,
484 ExeNameBufferLength,
485 (PVOID*)&GetAliasesExesRequest->ExeNames);
486
487 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
488 CaptureBuffer,
489 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetAliasExes),
490 sizeof(*GetAliasesExesRequest));
491 if (!NT_SUCCESS(ApiMessage.Status))
492 {
493 CsrFreeCaptureBuffer(CaptureBuffer);
494 BaseSetLastNTError(ApiMessage.Status);
495 return 0;
496 }
497
498 memcpy(lpExeNameBuffer,
499 GetAliasesExesRequest->ExeNames,
500 GetAliasesExesRequest->Length);
501
502 CsrFreeCaptureBuffer(CaptureBuffer);
503
504 return GetAliasesExesRequest->Length;
505 }
506
507 /*
508 * @implemented
509 */
510 DWORD
511 WINAPI
512 GetConsoleAliasExesW(LPWSTR lpExeNameBuffer,
513 DWORD ExeNameBufferLength)
514 {
515 return IntGetConsoleAliasExes(lpExeNameBuffer, ExeNameBufferLength, TRUE);
516 }
517
518
519 /*
520 * @implemented
521 */
522 DWORD
523 WINAPI
524 GetConsoleAliasExesA(LPSTR lpExeNameBuffer,
525 DWORD ExeNameBufferLength)
526 {
527 return IntGetConsoleAliasExes(lpExeNameBuffer, ExeNameBufferLength, FALSE);
528 }
529
530
531 static DWORD
532 IntGetConsoleAliasExesLength(BOOLEAN bUnicode)
533 {
534 CONSOLE_API_MESSAGE ApiMessage;
535 PCONSOLE_GETALIASESEXESLENGTH GetAliasesExesLengthRequest = &ApiMessage.Data.GetAliasesExesLengthRequest;
536
537 GetAliasesExesLengthRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
538 GetAliasesExesLengthRequest->Unicode = bUnicode;
539
540 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
541 NULL,
542 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetAliasExesLength),
543 sizeof(*GetAliasesExesLengthRequest));
544 if (!NT_SUCCESS(ApiMessage.Status))
545 {
546 BaseSetLastNTError(ApiMessage.Status);
547 return 0;
548 }
549
550 return GetAliasesExesLengthRequest->Length;
551 }
552
553
554 /*
555 * @implemented
556 */
557 DWORD
558 WINAPI
559 GetConsoleAliasExesLengthW(VOID)
560 {
561 return IntGetConsoleAliasExesLength(TRUE);
562 }
563
564
565 /*
566 * @implemented
567 */
568 DWORD
569 WINAPI
570 GetConsoleAliasExesLengthA(VOID)
571 {
572 return IntGetConsoleAliasExesLength(FALSE);
573 }
574
575 /* EOF */