Sync with trunk r58687.
[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 (janderwald@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 /*
391 * @implemented
392 */
393 DWORD
394 WINAPI
395 GetConsoleAliasesLengthW(LPWSTR lpExeName)
396 {
397 NTSTATUS Status;
398 CONSOLE_API_MESSAGE ApiMessage;
399 PCONSOLE_GETALLALIASESLENGTH GetAllAliasesLengthRequest = &ApiMessage.Data.GetAllAliasesLengthRequest;
400 PCSR_CAPTURE_BUFFER CaptureBuffer;
401
402 DPRINT("GetConsoleAliasesLengthW entered\n");
403
404 if (lpExeName == NULL)
405 {
406 SetLastError(ERROR_INVALID_PARAMETER);
407 return 0;
408 }
409
410 GetAllAliasesLengthRequest->ExeLength = (wcslen(lpExeName) + 1) * sizeof(WCHAR);
411 GetAllAliasesLengthRequest->Length = 0;
412
413 /* Allocate a Capture Buffer */
414 CaptureBuffer = CsrAllocateCaptureBuffer(1, GetAllAliasesLengthRequest->ExeLength);
415 if (!CaptureBuffer)
416 {
417 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
418 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
419 return 0;
420 }
421
422 /* Capture the exe name */
423 CsrCaptureMessageBuffer(CaptureBuffer,
424 (PVOID)lpExeName,
425 GetAllAliasesLengthRequest->ExeLength,
426 (PVOID)&GetAllAliasesLengthRequest->ExeName);
427
428 Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
429 CaptureBuffer,
430 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetAliasesLength),
431 sizeof(CONSOLE_GETALLALIASESLENGTH));
432
433 CsrFreeCaptureBuffer(CaptureBuffer);
434
435 if (!NT_SUCCESS(Status))
436 {
437 BaseSetLastNTError(Status);
438 return 0;
439 }
440
441 return GetAllAliasesLengthRequest->Length;
442 }
443
444
445 /*
446 * @implemented
447 */
448 DWORD
449 WINAPI
450 GetConsoleAliasesLengthA(LPSTR lpExeName)
451 {
452 DWORD dwRetVal = 0;
453 LPWSTR lpExeNameW = NULL;
454
455 if (lpExeName)
456 BasepAnsiStringToHeapUnicodeString(lpExeName, (LPWSTR*)&lpExeNameW);
457
458 dwRetVal = GetConsoleAliasesLengthW(lpExeNameW);
459 if (dwRetVal)
460 dwRetVal /= sizeof(WCHAR);
461
462 /* Clean up */
463 if (lpExeNameW)
464 RtlFreeHeap(GetProcessHeap(), 0, (LPWSTR*)lpExeNameW);
465
466 return dwRetVal;
467 }
468
469
470 /*
471 * @implemented
472 */
473 DWORD
474 WINAPI
475 GetConsoleAliasExesW(LPWSTR lpExeNameBuffer,
476 DWORD ExeNameBufferLength)
477 {
478 NTSTATUS Status;
479 CONSOLE_API_MESSAGE ApiMessage;
480 PCONSOLE_GETALIASESEXES GetAliasesExesRequest = &ApiMessage.Data.GetAliasesExesRequest;
481 PCSR_CAPTURE_BUFFER CaptureBuffer;
482
483 DPRINT("GetConsoleAliasExesW entered\n");
484
485 /* Allocate a Capture Buffer */
486 CaptureBuffer = CsrAllocateCaptureBuffer(1, ExeNameBufferLength);
487 if (!CaptureBuffer)
488 {
489 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
490 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
491 return 0;
492 }
493
494 GetAliasesExesRequest->Length = ExeNameBufferLength;
495
496 /* Allocate space for the exe name buffer */
497 CsrAllocateMessagePointer(CaptureBuffer,
498 ExeNameBufferLength,
499 (PVOID*)&GetAliasesExesRequest->ExeNames);
500
501 Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
502 CaptureBuffer,
503 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetAliasExes),
504 sizeof(CONSOLE_GETALIASESEXES));
505 if (!NT_SUCCESS(Status))
506 {
507 CsrFreeCaptureBuffer(CaptureBuffer);
508 BaseSetLastNTError(Status);
509 return 0;
510 }
511
512 /* Copy the returned target string into the user buffer */
513 memcpy(lpExeNameBuffer,
514 GetAliasesExesRequest->ExeNames,
515 GetAliasesExesRequest->Length);
516
517 /* Release the capture buffer and exit */
518 CsrFreeCaptureBuffer(CaptureBuffer);
519
520 return GetAliasesExesRequest->Length;
521 }
522
523
524 /*
525 * @implemented
526 */
527 DWORD
528 WINAPI
529 GetConsoleAliasExesA(LPSTR lpExeNameBuffer,
530 DWORD ExeNameBufferLength)
531 {
532 LPWSTR lpwExeNameBuffer;
533 DWORD dwResult;
534
535 DPRINT("GetConsoleAliasExesA entered\n");
536
537 lpwExeNameBuffer = HeapAlloc(GetProcessHeap(), 0, ExeNameBufferLength * sizeof(WCHAR));
538
539 dwResult = GetConsoleAliasExesW(lpwExeNameBuffer, ExeNameBufferLength * sizeof(WCHAR));
540
541 if (dwResult)
542 dwResult = WideCharToMultiByte(CP_ACP, 0, lpwExeNameBuffer, dwResult / sizeof(WCHAR), lpExeNameBuffer, ExeNameBufferLength, NULL, NULL);
543
544 HeapFree(GetProcessHeap(), 0, lpwExeNameBuffer);
545 return dwResult;
546 }
547
548
549 /*
550 * @implemented
551 */
552 DWORD
553 WINAPI
554 GetConsoleAliasExesLengthW(VOID)
555 {
556 NTSTATUS Status;
557 CONSOLE_API_MESSAGE ApiMessage;
558 PCONSOLE_GETALIASESEXESLENGTH GetAliasesExesLengthRequest = &ApiMessage.Data.GetAliasesExesLengthRequest;
559
560 DPRINT("GetConsoleAliasExesLengthW entered\n");
561
562 GetAliasesExesLengthRequest->Length = 0;
563
564 Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
565 NULL,
566 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetAliasExesLength),
567 sizeof(CONSOLE_GETALIASESEXESLENGTH));
568
569 if (!NT_SUCCESS(Status))
570 {
571 BaseSetLastNTError(Status);
572 return 0;
573 }
574
575 return GetAliasesExesLengthRequest->Length;
576 }
577
578
579 /*
580 * @implemented
581 */
582 DWORD
583 WINAPI
584 GetConsoleAliasExesLengthA(VOID)
585 {
586 DWORD dwLength;
587
588 DPRINT("GetConsoleAliasExesLengthA entered\n");
589
590 dwLength = GetConsoleAliasExesLengthW();
591
592 if (dwLength)
593 dwLength /= sizeof(WCHAR);
594
595 return dwLength;
596 }
597
598 /* EOF */