e8003a4f1ad0f0907d69be9f3d079784146ca380
[reactos.git] / reactos / dll / win32 / kernel32 / client / console / readwrite.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: dll/win32/kernel32/client/console/readwrite.c
5 * PURPOSE: Win32 Console Client read-write functions
6 * PROGRAMMERS: Emanuele Aliberti
7 * Marty Dill
8 * Filip Navara (xnavara@volny.cz)
9 * Thomas Weidenmueller (w3seek@reactos.org)
10 * Jeffrey Morlan
11 */
12
13 /* INCLUDES *******************************************************************/
14
15 #include <k32.h>
16
17 #define NDEBUG
18 #include <debug.h>
19
20
21 /* PRIVATE FUNCTIONS **********************************************************/
22
23 /******************
24 * Read functions *
25 ******************/
26
27 static
28 BOOL
29 IntReadConsole(HANDLE hConsoleInput,
30 PVOID lpBuffer,
31 DWORD nNumberOfCharsToRead,
32 LPDWORD lpNumberOfCharsRead,
33 PCONSOLE_READCONSOLE_CONTROL pInputControl,
34 BOOL bUnicode)
35 {
36 NTSTATUS Status;
37 CONSOLE_API_MESSAGE ApiMessage;
38 PCONSOLE_READCONSOLE ReadConsoleRequest = &ApiMessage.Data.ReadConsoleRequest;
39 PCSR_CAPTURE_BUFFER CaptureBuffer;
40 ULONG CharSize;
41
42 /* Determine the needed size */
43 CharSize = (bUnicode ? sizeof(WCHAR) : sizeof(CHAR));
44 ReadConsoleRequest->BufferSize = nNumberOfCharsToRead * CharSize;
45
46 /* Allocate a Capture Buffer */
47 CaptureBuffer = CsrAllocateCaptureBuffer(1, ReadConsoleRequest->BufferSize);
48 if (CaptureBuffer == NULL)
49 {
50 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
51 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
52 return FALSE;
53 }
54
55 /* Allocate space in the Buffer */
56 CsrAllocateMessagePointer(CaptureBuffer,
57 ReadConsoleRequest->BufferSize,
58 (PVOID*)&ReadConsoleRequest->Buffer);
59
60 /* Set up the data to send to the Console Server */
61 ReadConsoleRequest->InputHandle = hConsoleInput;
62 ReadConsoleRequest->Unicode = bUnicode;
63 ReadConsoleRequest->NrCharactersToRead = nNumberOfCharsToRead;
64 ReadConsoleRequest->NrCharactersRead = 0;
65 ReadConsoleRequest->CtrlWakeupMask = 0;
66 if (pInputControl && pInputControl->nLength == sizeof(CONSOLE_READCONSOLE_CONTROL))
67 {
68 /*
69 * From MSDN (ReadConsole function), the description
70 * for pInputControl says:
71 * "This parameter requires Unicode input by default.
72 * For ANSI mode, set this parameter to NULL."
73 */
74 ReadConsoleRequest->NrCharactersRead = pInputControl->nInitialChars;
75 memcpy(ReadConsoleRequest->Buffer,
76 lpBuffer,
77 pInputControl->nInitialChars * sizeof(WCHAR));
78 ReadConsoleRequest->CtrlWakeupMask = pInputControl->dwCtrlWakeupMask;
79 }
80
81 /* Call the server */
82 Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
83 CaptureBuffer,
84 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepReadConsole),
85 sizeof(CONSOLE_READCONSOLE));
86
87 /* Check for success */
88 if (NT_SUCCESS(Status))
89 {
90 memcpy(lpBuffer,
91 ReadConsoleRequest->Buffer,
92 ReadConsoleRequest->NrCharactersRead * CharSize);
93
94 if (lpNumberOfCharsRead != NULL)
95 *lpNumberOfCharsRead = ReadConsoleRequest->NrCharactersRead;
96
97 if (pInputControl && pInputControl->nLength == sizeof(CONSOLE_READCONSOLE_CONTROL))
98 pInputControl->dwControlKeyState = ReadConsoleRequest->ControlKeyState;
99 }
100 else
101 {
102 DPRINT1("CSR returned error in ReadConsole\n");
103
104 if (lpNumberOfCharsRead != NULL)
105 *lpNumberOfCharsRead = 0;
106
107 /* Error out */
108 BaseSetLastNTError(Status);
109 }
110
111 CsrFreeCaptureBuffer(CaptureBuffer);
112
113 /* Return TRUE or FALSE */
114 // return TRUE;
115 return (ReadConsoleRequest->NrCharactersRead > 0);
116 // return NT_SUCCESS(Status);
117 }
118
119
120 static
121 BOOL
122 IntGetConsoleInput(HANDLE hConsoleInput,
123 BOOL bRead,
124 PINPUT_RECORD lpBuffer,
125 DWORD nLength,
126 LPDWORD lpNumberOfEventsRead,
127 BOOL bUnicode)
128 {
129 NTSTATUS Status;
130 CONSOLE_API_MESSAGE ApiMessage;
131 PCONSOLE_GETINPUT GetInputRequest = &ApiMessage.Data.GetInputRequest;
132 PCSR_CAPTURE_BUFFER CaptureBuffer;
133 ULONG Size;
134
135 if (lpBuffer == NULL)
136 {
137 SetLastError(ERROR_INVALID_PARAMETER);
138 return FALSE;
139 }
140
141 Size = nLength * sizeof(INPUT_RECORD);
142
143 DPRINT("IntGetConsoleInput: %lx %p\n", Size, lpNumberOfEventsRead);
144
145 /* Allocate a Capture Buffer */
146 CaptureBuffer = CsrAllocateCaptureBuffer(1, Size);
147 if (CaptureBuffer == NULL)
148 {
149 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
150 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
151 return FALSE;
152 }
153
154 /* Allocate space in the Buffer */
155 CsrAllocateMessagePointer(CaptureBuffer,
156 Size,
157 (PVOID*)&GetInputRequest->InputRecord);
158
159 /* Set up the data to send to the Console Server */
160 GetInputRequest->InputHandle = hConsoleInput;
161 GetInputRequest->Unicode = bUnicode;
162 GetInputRequest->bRead = bRead;
163 GetInputRequest->InputsRead = 0;
164 GetInputRequest->Length = nLength;
165
166 /* Call the server */
167 Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
168 CaptureBuffer,
169 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetConsoleInput),
170 sizeof(CONSOLE_GETINPUT));
171 DPRINT("Server returned: %x\n", Status);
172
173 /* Check for success */
174 if (NT_SUCCESS(Status))
175 {
176 /* Return the number of events read */
177 DPRINT("Events read: %lx\n", GetInputRequest->InputsRead);
178
179 if (lpNumberOfEventsRead != NULL)
180 *lpNumberOfEventsRead = GetInputRequest->InputsRead;
181
182 /* Copy into the buffer */
183 DPRINT("Copying to buffer\n");
184 RtlCopyMemory(lpBuffer,
185 GetInputRequest->InputRecord,
186 sizeof(INPUT_RECORD) * GetInputRequest->InputsRead);
187 }
188 else
189 {
190 if (lpNumberOfEventsRead != NULL)
191 *lpNumberOfEventsRead = 0;
192
193 /* Error out */
194 BaseSetLastNTError(Status);
195 }
196
197 /* Release the capture buffer */
198 CsrFreeCaptureBuffer(CaptureBuffer);
199
200 /* Return TRUE or FALSE */
201 return (GetInputRequest->InputsRead > 0);
202 // return NT_SUCCESS(Status);
203 }
204
205
206 static
207 BOOL
208 IntReadConsoleOutput(HANDLE hConsoleOutput,
209 PCHAR_INFO lpBuffer,
210 COORD dwBufferSize,
211 COORD dwBufferCoord,
212 PSMALL_RECT lpReadRegion,
213 BOOL bUnicode)
214 {
215 CONSOLE_API_MESSAGE ApiMessage;
216 PCONSOLE_READOUTPUT ReadOutputRequest = &ApiMessage.Data.ReadOutputRequest;
217 PCSR_CAPTURE_BUFFER CaptureBuffer;
218 DWORD Size, SizeX, SizeY;
219
220 if (lpBuffer == NULL)
221 {
222 SetLastError(ERROR_INVALID_PARAMETER);
223 return FALSE;
224 }
225
226 Size = dwBufferSize.X * dwBufferSize.Y * sizeof(CHAR_INFO);
227
228 DPRINT("IntReadConsoleOutput: %lx %p\n", Size, lpReadRegion);
229
230 /* Allocate a Capture Buffer */
231 CaptureBuffer = CsrAllocateCaptureBuffer(1, Size);
232 if (CaptureBuffer == NULL)
233 {
234 DPRINT1("CsrAllocateCaptureBuffer failed with size 0x%x!\n", Size);
235 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
236 return FALSE;
237 }
238
239 /* Allocate space in the Buffer */
240 CsrAllocateMessagePointer(CaptureBuffer,
241 Size,
242 (PVOID*)&ReadOutputRequest->CharInfo);
243
244 /* Set up the data to send to the Console Server */
245 ReadOutputRequest->OutputHandle = hConsoleOutput;
246 ReadOutputRequest->Unicode = bUnicode;
247 ReadOutputRequest->BufferSize = dwBufferSize;
248 ReadOutputRequest->BufferCoord = dwBufferCoord;
249 ReadOutputRequest->ReadRegion = *lpReadRegion;
250
251 /* Call the server */
252 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
253 CaptureBuffer,
254 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepReadConsoleOutput),
255 sizeof(CONSOLE_READOUTPUT));
256 DPRINT("Server returned: %x\n", ApiMessage.Status);
257
258 /* Check for success */
259 if (NT_SUCCESS(ApiMessage.Status))
260 {
261 /* Copy into the buffer */
262 DPRINT("Copying to buffer\n");
263 SizeX = ReadOutputRequest->ReadRegion.Right -
264 ReadOutputRequest->ReadRegion.Left + 1;
265 SizeY = ReadOutputRequest->ReadRegion.Bottom -
266 ReadOutputRequest->ReadRegion.Top + 1;
267 RtlCopyMemory(lpBuffer,
268 ReadOutputRequest->CharInfo,
269 sizeof(CHAR_INFO) * SizeX * SizeY);
270 }
271 else
272 {
273 /* Error out */
274 BaseSetLastNTError(ApiMessage.Status);
275 }
276
277 /* Return the read region */
278 DPRINT("read region: %p\n", ReadOutputRequest->ReadRegion);
279 *lpReadRegion = ReadOutputRequest->ReadRegion;
280
281 /* Release the capture buffer */
282 CsrFreeCaptureBuffer(CaptureBuffer);
283
284 /* Return TRUE or FALSE */
285 return NT_SUCCESS(ApiMessage.Status);
286 }
287
288
289 static
290 BOOL
291 IntReadConsoleOutputCode(HANDLE hConsoleOutput,
292 CODE_TYPE CodeType,
293 PVOID pCode,
294 DWORD nLength,
295 COORD dwReadCoord,
296 LPDWORD lpNumberOfCodesRead)
297 {
298 NTSTATUS Status;
299 BOOL bRet = TRUE;
300 CONSOLE_API_MESSAGE ApiMessage;
301 PCONSOLE_READOUTPUTCODE ReadOutputCodeRequest = &ApiMessage.Data.ReadOutputCodeRequest;
302 PCSR_CAPTURE_BUFFER CaptureBuffer;
303 ULONG SizeBytes, CodeSize;
304 DWORD CodesRead;
305
306 /* Determine the needed size */
307 switch (CodeType)
308 {
309 case CODE_ASCII:
310 CodeSize = sizeof(CHAR);
311 break;
312
313 case CODE_UNICODE:
314 CodeSize = sizeof(WCHAR);
315 break;
316
317 case CODE_ATTRIBUTE:
318 CodeSize = sizeof(WORD);
319 break;
320
321 default:
322 SetLastError(ERROR_INVALID_PARAMETER);
323 return FALSE;
324 }
325 SizeBytes = nLength * CodeSize;
326
327 /* Allocate a Capture Buffer */
328 CaptureBuffer = CsrAllocateCaptureBuffer(1, SizeBytes);
329 if (CaptureBuffer == NULL)
330 {
331 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
332 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
333 return FALSE;
334 }
335
336 /* Allocate space in the Buffer */
337 CsrAllocateMessagePointer(CaptureBuffer,
338 SizeBytes,
339 (PVOID*)&ReadOutputCodeRequest->pCode.pCode);
340
341 /* Start reading */
342 ReadOutputCodeRequest->OutputHandle = hConsoleOutput;
343 ReadOutputCodeRequest->CodeType = CodeType;
344 ReadOutputCodeRequest->ReadCoord = dwReadCoord;
345
346 ReadOutputCodeRequest->NumCodesToRead = nLength;
347
348 /* Call the server */
349 Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
350 CaptureBuffer,
351 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepReadConsoleOutputString),
352 sizeof(CONSOLE_READOUTPUTCODE));
353
354 /* Check for success */
355 if (NT_SUCCESS(Status))
356 {
357 CodesRead = ReadOutputCodeRequest->CodesRead;
358 memcpy(pCode, ReadOutputCodeRequest->pCode.pCode, CodesRead * CodeSize);
359
360 // ReadOutputCodeRequest->ReadCoord = ReadOutputCodeRequest->EndCoord;
361
362 if (lpNumberOfCodesRead != NULL)
363 *lpNumberOfCodesRead = CodesRead;
364
365 bRet = TRUE;
366 }
367 else
368 {
369 if (lpNumberOfCodesRead != NULL)
370 *lpNumberOfCodesRead = 0;
371
372 /* Error out */
373 BaseSetLastNTError(Status);
374 bRet = FALSE;
375 }
376
377 CsrFreeCaptureBuffer(CaptureBuffer);
378
379 return bRet;
380 }
381
382
383 /*******************
384 * Write functions *
385 *******************/
386
387 static
388 BOOL
389 IntWriteConsole(HANDLE hConsoleOutput,
390 PVOID lpBuffer,
391 DWORD nNumberOfCharsToWrite,
392 LPDWORD lpNumberOfCharsWritten,
393 LPVOID lpReserved,
394 BOOL bUnicode)
395 {
396 NTSTATUS Status;
397 BOOL bRet = TRUE;
398 CONSOLE_API_MESSAGE ApiMessage;
399 PCONSOLE_WRITECONSOLE WriteConsoleRequest = &ApiMessage.Data.WriteConsoleRequest;
400 PCSR_CAPTURE_BUFFER CaptureBuffer;
401 ULONG CharSize;
402
403 /* Determine the needed size */
404 CharSize = (bUnicode ? sizeof(WCHAR) : sizeof(CHAR));
405 WriteConsoleRequest->BufferSize = nNumberOfCharsToWrite * CharSize;
406
407 /* Allocate a Capture Buffer */
408 CaptureBuffer = CsrAllocateCaptureBuffer(1, WriteConsoleRequest->BufferSize);
409 if (CaptureBuffer == NULL)
410 {
411 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
412 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
413 return FALSE;
414 }
415
416 /* Capture the buffer to write */
417 CsrCaptureMessageBuffer(CaptureBuffer,
418 (PVOID)lpBuffer,
419 WriteConsoleRequest->BufferSize,
420 (PVOID*)&WriteConsoleRequest->Buffer);
421
422 /* Start writing */
423 WriteConsoleRequest->NrCharactersToWrite = nNumberOfCharsToWrite;
424 WriteConsoleRequest->OutputHandle = hConsoleOutput;
425 WriteConsoleRequest->Unicode = bUnicode;
426
427 /* Call the server */
428 Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
429 CaptureBuffer,
430 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepWriteConsole),
431 sizeof(CONSOLE_WRITECONSOLE));
432
433 /* Check for success */
434 if (NT_SUCCESS(Status))
435 {
436 if (lpNumberOfCharsWritten != NULL)
437 *lpNumberOfCharsWritten = WriteConsoleRequest->NrCharactersWritten;
438
439 bRet = TRUE;
440 }
441 else
442 {
443 if (lpNumberOfCharsWritten != NULL)
444 *lpNumberOfCharsWritten = 0;
445
446 /* Error out */
447 BaseSetLastNTError(Status);
448 bRet = FALSE;
449 }
450
451 CsrFreeCaptureBuffer(CaptureBuffer);
452
453 return bRet;
454 }
455
456
457 static
458 BOOL
459 IntWriteConsoleInput(HANDLE hConsoleInput,
460 PINPUT_RECORD lpBuffer,
461 DWORD nLength,
462 LPDWORD lpNumberOfEventsWritten,
463 BOOL bUnicode,
464 BOOL bAppendToEnd)
465 {
466 CONSOLE_API_MESSAGE ApiMessage;
467 PCONSOLE_WRITEINPUT WriteInputRequest = &ApiMessage.Data.WriteInputRequest;
468 PCSR_CAPTURE_BUFFER CaptureBuffer;
469 DWORD Size;
470
471 Size = nLength * sizeof(INPUT_RECORD);
472
473 DPRINT("IntWriteConsoleInput: %lx %p\n", Size, lpNumberOfEventsWritten);
474
475 /* Allocate a Capture Buffer */
476 CaptureBuffer = CsrAllocateCaptureBuffer(1, Size);
477 if (CaptureBuffer == NULL)
478 {
479 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
480 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
481 return FALSE;
482 }
483
484 /* Capture the user buffer */
485 CsrCaptureMessageBuffer(CaptureBuffer,
486 lpBuffer,
487 Size,
488 (PVOID*)&WriteInputRequest->InputRecord);
489
490 /* Set up the data to send to the Console Server */
491 WriteInputRequest->InputHandle = hConsoleInput;
492 WriteInputRequest->Length = nLength;
493 WriteInputRequest->Unicode = bUnicode;
494 WriteInputRequest->AppendToEnd = bAppendToEnd;
495
496 /* Call the server */
497 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
498 CaptureBuffer,
499 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepWriteConsoleInput),
500 sizeof(CONSOLE_WRITEINPUT));
501 DPRINT("Server returned: %x\n", ApiMessage.Status);
502
503 /* Check for success */
504 if (NT_SUCCESS(ApiMessage.Status))
505 {
506 /* Return the number of events read */
507 DPRINT("Events read: %lx\n", WriteInputRequest->Length);
508
509 if (lpNumberOfEventsWritten != NULL)
510 *lpNumberOfEventsWritten = WriteInputRequest->Length;
511 }
512 else
513 {
514 if (lpNumberOfEventsWritten != NULL)
515 *lpNumberOfEventsWritten = 0;
516
517 /* Error out */
518 BaseSetLastNTError(ApiMessage.Status);
519 }
520
521 /* Release the capture buffer */
522 CsrFreeCaptureBuffer(CaptureBuffer);
523
524 /* Return TRUE or FALSE */
525 return NT_SUCCESS(ApiMessage.Status);
526 }
527
528
529 static
530 BOOL
531 IntWriteConsoleOutput(HANDLE hConsoleOutput,
532 CONST CHAR_INFO *lpBuffer,
533 COORD dwBufferSize,
534 COORD dwBufferCoord,
535 PSMALL_RECT lpWriteRegion,
536 BOOL bUnicode)
537 {
538 CONSOLE_API_MESSAGE ApiMessage;
539 PCONSOLE_WRITEOUTPUT WriteOutputRequest = &ApiMessage.Data.WriteOutputRequest;
540 PCSR_CAPTURE_BUFFER CaptureBuffer;
541 ULONG Size;
542
543 if ((lpBuffer == NULL) || (lpWriteRegion == NULL))
544 {
545 SetLastError(ERROR_INVALID_PARAMETER);
546 return FALSE;
547 }
548 /*
549 if (lpWriteRegion == NULL)
550 {
551 SetLastError(ERROR_INVALID_PARAMETER);
552 return FALSE;
553 }
554 */
555
556 Size = dwBufferSize.Y * dwBufferSize.X * sizeof(CHAR_INFO);
557
558 DPRINT("IntWriteConsoleOutput: %lx %p\n", Size, lpWriteRegion);
559
560 /* Allocate a Capture Buffer */
561 CaptureBuffer = CsrAllocateCaptureBuffer(1, Size);
562 if (CaptureBuffer == NULL)
563 {
564 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
565 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
566 return FALSE;
567 }
568
569 /* Capture the user buffer */
570 CsrCaptureMessageBuffer(CaptureBuffer,
571 (PVOID)lpBuffer,
572 Size,
573 (PVOID*)&WriteOutputRequest->CharInfo);
574
575 /* Set up the data to send to the Console Server */
576 WriteOutputRequest->OutputHandle = hConsoleOutput;
577 WriteOutputRequest->Unicode = bUnicode;
578 WriteOutputRequest->BufferSize = dwBufferSize;
579 WriteOutputRequest->BufferCoord = dwBufferCoord;
580 WriteOutputRequest->WriteRegion = *lpWriteRegion;
581
582 /* Call the server */
583 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
584 CaptureBuffer,
585 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepWriteConsoleOutput),
586 sizeof(CONSOLE_WRITEOUTPUT));
587 DPRINT("Server returned: %x\n", ApiMessage.Status);
588
589 /* Check for success */
590 if (!NT_SUCCESS(ApiMessage.Status))
591 {
592 /* Error out */
593 BaseSetLastNTError(ApiMessage.Status);
594 }
595
596 /* Return the read region */
597 DPRINT("read region: %p\n", WriteOutputRequest->WriteRegion);
598 *lpWriteRegion = WriteOutputRequest->WriteRegion;
599
600 /* Release the capture buffer */
601 CsrFreeCaptureBuffer(CaptureBuffer);
602
603 /* Return TRUE or FALSE */
604 return NT_SUCCESS(ApiMessage.Status);
605 }
606
607
608 static
609 BOOL
610 IntWriteConsoleOutputCode(HANDLE hConsoleOutput,
611 CODE_TYPE CodeType,
612 CONST VOID *pCode,
613 DWORD nLength,
614 COORD dwWriteCoord,
615 LPDWORD lpNumberOfCodesWritten)
616 {
617 NTSTATUS Status;
618 BOOL bRet = TRUE;
619 CONSOLE_API_MESSAGE ApiMessage;
620 PCONSOLE_WRITEOUTPUTCODE WriteOutputCodeRequest = &ApiMessage.Data.WriteOutputCodeRequest;
621 PCSR_CAPTURE_BUFFER CaptureBuffer;
622 ULONG CodeSize;
623
624 /* Determine the needed size */
625 switch (CodeType)
626 {
627 case CODE_ASCII:
628 CodeSize = sizeof(CHAR);
629 break;
630
631 case CODE_UNICODE:
632 CodeSize = sizeof(WCHAR);
633 break;
634
635 case CODE_ATTRIBUTE:
636 CodeSize = sizeof(WORD);
637 break;
638
639 default:
640 SetLastError(ERROR_INVALID_PARAMETER);
641 return FALSE;
642 }
643 WriteOutputCodeRequest->BufferSize = nLength * CodeSize;
644
645 /* Allocate a Capture Buffer */
646 CaptureBuffer = CsrAllocateCaptureBuffer(1, WriteOutputCodeRequest->BufferSize);
647 if (CaptureBuffer == NULL)
648 {
649 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
650 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
651 return FALSE;
652 }
653
654 /* Capture the buffer to write */
655 CsrCaptureMessageBuffer(CaptureBuffer,
656 (PVOID)pCode,
657 WriteOutputCodeRequest->BufferSize,
658 (PVOID*)&WriteOutputCodeRequest->pCode.pCode);
659
660 /* Start writing */
661 WriteOutputCodeRequest->OutputHandle = hConsoleOutput;
662 WriteOutputCodeRequest->CodeType = CodeType;
663 WriteOutputCodeRequest->Coord = dwWriteCoord;
664
665 WriteOutputCodeRequest->Length = (USHORT)nLength;
666
667 /* Call the server */
668 Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
669 CaptureBuffer,
670 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepWriteConsoleOutputString),
671 sizeof(CONSOLE_WRITEOUTPUTCODE));
672
673 /* Check for success */
674 if (NT_SUCCESS(Status))
675 {
676 // WriteOutputCodeRequest->Coord = WriteOutputCodeRequest->EndCoord;
677
678 if (lpNumberOfCodesWritten != NULL)
679 // *lpNumberOfCodesWritten = WriteOutputCodeRequest->NrCharactersWritten;
680 *lpNumberOfCodesWritten = WriteOutputCodeRequest->Length;
681
682 bRet = TRUE;
683 }
684 else
685 {
686 if (lpNumberOfCodesWritten != NULL)
687 *lpNumberOfCodesWritten = 0;
688
689 /* Error out */
690 BaseSetLastNTError(Status);
691 bRet = FALSE;
692 }
693
694 CsrFreeCaptureBuffer(CaptureBuffer);
695
696 return bRet;
697 }
698
699
700 static
701 BOOL
702 IntFillConsoleOutputCode(HANDLE hConsoleOutput,
703 CODE_TYPE CodeType,
704 PVOID pCode,
705 DWORD nLength,
706 COORD dwWriteCoord,
707 LPDWORD lpNumberOfCodesWritten)
708 {
709 NTSTATUS Status;
710 CONSOLE_API_MESSAGE ApiMessage;
711 PCONSOLE_FILLOUTPUTCODE FillOutputRequest = &ApiMessage.Data.FillOutputRequest;
712
713 FillOutputRequest->OutputHandle = hConsoleOutput;
714 FillOutputRequest->CodeType = CodeType;
715
716 switch (CodeType)
717 {
718 case CODE_ASCII:
719 FillOutputRequest->Code.AsciiChar = *(PCHAR)pCode;
720 break;
721
722 case CODE_UNICODE:
723 FillOutputRequest->Code.UnicodeChar = *(PWCHAR)pCode;
724 break;
725
726 case CODE_ATTRIBUTE:
727 FillOutputRequest->Code.Attribute = *(PWORD)pCode;
728 break;
729
730 default:
731 SetLastError(ERROR_INVALID_PARAMETER);
732 return FALSE;
733 }
734
735 /* Set up the data to send to the Console Server */
736 FillOutputRequest->Coord = dwWriteCoord;
737 FillOutputRequest->Length = nLength;
738
739 /* Call the server */
740 Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
741 NULL,
742 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepFillConsoleOutput),
743 sizeof(CONSOLE_FILLOUTPUTCODE));
744
745 /* Check for success */
746 if (NT_SUCCESS(Status))
747 {
748 if (lpNumberOfCodesWritten != NULL)
749 *lpNumberOfCodesWritten = FillOutputRequest->Length;
750 // *lpNumberOfCodesWritten = Request.Data.FillOutputRequest.NrCharactersWritten;
751
752 return TRUE;
753 }
754 else
755 {
756 if (lpNumberOfCodesWritten != NULL)
757 *lpNumberOfCodesWritten = 0;
758
759 BaseSetLastNTError(Status);
760 return FALSE;
761 }
762 }
763
764
765 /* FUNCTIONS ******************************************************************/
766
767 /******************
768 * Read functions *
769 ******************/
770
771 /*--------------------------------------------------------------
772 * ReadConsoleW
773 *
774 * @implemented
775 */
776 BOOL
777 WINAPI
778 ReadConsoleW(HANDLE hConsoleInput,
779 LPVOID lpBuffer,
780 DWORD nNumberOfCharsToRead,
781 LPDWORD lpNumberOfCharsRead,
782 PCONSOLE_READCONSOLE_CONTROL pInputControl)
783 {
784 return IntReadConsole(hConsoleInput,
785 lpBuffer,
786 nNumberOfCharsToRead,
787 lpNumberOfCharsRead,
788 pInputControl,
789 TRUE);
790 }
791
792
793 /*--------------------------------------------------------------
794 * ReadConsoleA
795 *
796 * @implemented
797 */
798 BOOL
799 WINAPI
800 ReadConsoleA(HANDLE hConsoleInput,
801 LPVOID lpBuffer,
802 DWORD nNumberOfCharsToRead,
803 LPDWORD lpNumberOfCharsRead,
804 PCONSOLE_READCONSOLE_CONTROL pInputControl)
805 {
806 return IntReadConsole(hConsoleInput,
807 lpBuffer,
808 nNumberOfCharsToRead,
809 lpNumberOfCharsRead,
810 NULL,
811 FALSE);
812 }
813
814
815 /*--------------------------------------------------------------
816 * PeekConsoleInputW
817 *
818 * @implemented
819 */
820 BOOL
821 WINAPI
822 PeekConsoleInputW(HANDLE hConsoleInput,
823 PINPUT_RECORD lpBuffer,
824 DWORD nLength,
825 LPDWORD lpNumberOfEventsRead)
826 {
827 return IntGetConsoleInput(hConsoleInput,
828 FALSE,
829 lpBuffer,
830 nLength,
831 lpNumberOfEventsRead,
832 TRUE);
833 }
834
835
836 /*--------------------------------------------------------------
837 * PeekConsoleInputA
838 *
839 * @implemented
840 */
841 BOOL
842 WINAPI
843 PeekConsoleInputA(HANDLE hConsoleInput,
844 PINPUT_RECORD lpBuffer,
845 DWORD nLength,
846 LPDWORD lpNumberOfEventsRead)
847 {
848 return IntGetConsoleInput(hConsoleInput,
849 FALSE,
850 lpBuffer,
851 nLength,
852 lpNumberOfEventsRead,
853 FALSE);
854 }
855
856
857 /*--------------------------------------------------------------
858 * ReadConsoleInputW
859 *
860 * @implemented
861 */
862 BOOL
863 WINAPI
864 ReadConsoleInputW(HANDLE hConsoleInput,
865 PINPUT_RECORD lpBuffer,
866 DWORD nLength,
867 LPDWORD lpNumberOfEventsRead)
868 {
869 return IntGetConsoleInput(hConsoleInput,
870 TRUE,
871 lpBuffer,
872 nLength,
873 lpNumberOfEventsRead,
874 TRUE);
875 }
876
877
878 /*--------------------------------------------------------------
879 * ReadConsoleInputA
880 *
881 * @implemented
882 */
883 BOOL
884 WINAPI
885 ReadConsoleInputA(HANDLE hConsoleInput,
886 PINPUT_RECORD lpBuffer,
887 DWORD nLength,
888 LPDWORD lpNumberOfEventsRead)
889 {
890 return IntGetConsoleInput(hConsoleInput,
891 TRUE,
892 lpBuffer,
893 nLength,
894 lpNumberOfEventsRead,
895 FALSE);
896 }
897
898
899 BOOL
900 WINAPI
901 ReadConsoleInputExW(HANDLE hConsole, LPVOID lpBuffer, DWORD dwLen, LPDWORD Unknown1, DWORD Unknown2)
902 {
903 STUB;
904 return FALSE;
905 }
906
907
908 BOOL
909 WINAPI
910 ReadConsoleInputExA(HANDLE hConsole, LPVOID lpBuffer, DWORD dwLen, LPDWORD Unknown1, DWORD Unknown2)
911 {
912 STUB;
913 return FALSE;
914 }
915
916
917 /*--------------------------------------------------------------
918 * ReadConsoleOutputW
919 *
920 * @implemented
921 */
922 BOOL
923 WINAPI
924 ReadConsoleOutputW(HANDLE hConsoleOutput,
925 PCHAR_INFO lpBuffer,
926 COORD dwBufferSize,
927 COORD dwBufferCoord,
928 PSMALL_RECT lpReadRegion)
929 {
930 return IntReadConsoleOutput(hConsoleOutput,
931 lpBuffer,
932 dwBufferSize,
933 dwBufferCoord,
934 lpReadRegion,
935 TRUE);
936 }
937
938
939 /*--------------------------------------------------------------
940 * ReadConsoleOutputA
941 *
942 * @implemented
943 */
944 BOOL
945 WINAPI
946 ReadConsoleOutputA(HANDLE hConsoleOutput,
947 PCHAR_INFO lpBuffer,
948 COORD dwBufferSize,
949 COORD dwBufferCoord,
950 PSMALL_RECT lpReadRegion)
951 {
952 return IntReadConsoleOutput(hConsoleOutput,
953 lpBuffer,
954 dwBufferSize,
955 dwBufferCoord,
956 lpReadRegion,
957 FALSE);
958 }
959
960
961 /*--------------------------------------------------------------
962 * ReadConsoleOutputCharacterW
963 *
964 * @implemented
965 */
966 BOOL
967 WINAPI
968 ReadConsoleOutputCharacterW(HANDLE hConsoleOutput,
969 LPWSTR lpCharacter,
970 DWORD nLength,
971 COORD dwReadCoord,
972 LPDWORD lpNumberOfCharsRead)
973 {
974 return IntReadConsoleOutputCode(hConsoleOutput,
975 CODE_UNICODE,
976 lpCharacter,
977 nLength,
978 dwReadCoord,
979 lpNumberOfCharsRead);
980 }
981
982
983 /*--------------------------------------------------------------
984 * ReadConsoleOutputCharacterA
985 *
986 * @implemented
987 */
988 BOOL
989 WINAPI
990 ReadConsoleOutputCharacterA(HANDLE hConsoleOutput,
991 LPSTR lpCharacter,
992 DWORD nLength,
993 COORD dwReadCoord,
994 LPDWORD lpNumberOfCharsRead)
995 {
996 return IntReadConsoleOutputCode(hConsoleOutput,
997 CODE_ASCII,
998 lpCharacter,
999 nLength,
1000 dwReadCoord,
1001 lpNumberOfCharsRead);
1002 }
1003
1004
1005 /*--------------------------------------------------------------
1006 * ReadConsoleOutputAttribute
1007 *
1008 * @implemented
1009 */
1010 BOOL
1011 WINAPI
1012 ReadConsoleOutputAttribute(HANDLE hConsoleOutput,
1013 LPWORD lpAttribute,
1014 DWORD nLength,
1015 COORD dwReadCoord,
1016 LPDWORD lpNumberOfAttrsRead)
1017 {
1018 return IntReadConsoleOutputCode(hConsoleOutput,
1019 CODE_ATTRIBUTE,
1020 lpAttribute,
1021 nLength,
1022 dwReadCoord,
1023 lpNumberOfAttrsRead);
1024 }
1025
1026
1027 /*******************
1028 * Write functions *
1029 *******************/
1030
1031 /*--------------------------------------------------------------
1032 * WriteConsoleW
1033 *
1034 * @implemented
1035 */
1036 BOOL
1037 WINAPI
1038 WriteConsoleW(HANDLE hConsoleOutput,
1039 CONST VOID *lpBuffer,
1040 DWORD nNumberOfCharsToWrite,
1041 LPDWORD lpNumberOfCharsWritten,
1042 LPVOID lpReserved)
1043 {
1044 return IntWriteConsole(hConsoleOutput,
1045 (PVOID)lpBuffer,
1046 nNumberOfCharsToWrite,
1047 lpNumberOfCharsWritten,
1048 lpReserved,
1049 TRUE);
1050 }
1051
1052
1053 /*--------------------------------------------------------------
1054 * WriteConsoleA
1055 *
1056 * @implemented
1057 */
1058 BOOL
1059 WINAPI
1060 WriteConsoleA(HANDLE hConsoleOutput,
1061 CONST VOID *lpBuffer,
1062 DWORD nNumberOfCharsToWrite,
1063 LPDWORD lpNumberOfCharsWritten,
1064 LPVOID lpReserved)
1065 {
1066 return IntWriteConsole(hConsoleOutput,
1067 (PVOID)lpBuffer,
1068 nNumberOfCharsToWrite,
1069 lpNumberOfCharsWritten,
1070 lpReserved,
1071 FALSE);
1072 }
1073
1074
1075 /*--------------------------------------------------------------
1076 * WriteConsoleInputW
1077 *
1078 * @implemented
1079 */
1080 BOOL
1081 WINAPI
1082 WriteConsoleInputW(HANDLE hConsoleInput,
1083 CONST INPUT_RECORD *lpBuffer,
1084 DWORD nLength,
1085 LPDWORD lpNumberOfEventsWritten)
1086 {
1087 return IntWriteConsoleInput(hConsoleInput,
1088 (PINPUT_RECORD)lpBuffer,
1089 nLength,
1090 lpNumberOfEventsWritten,
1091 TRUE,
1092 TRUE);
1093 }
1094
1095
1096 /*--------------------------------------------------------------
1097 * WriteConsoleInputA
1098 *
1099 * @implemented
1100 */
1101 BOOL
1102 WINAPI
1103 WriteConsoleInputA(HANDLE hConsoleInput,
1104 CONST INPUT_RECORD *lpBuffer,
1105 DWORD nLength,
1106 LPDWORD lpNumberOfEventsWritten)
1107 {
1108 return IntWriteConsoleInput(hConsoleInput,
1109 (PINPUT_RECORD)lpBuffer,
1110 nLength,
1111 lpNumberOfEventsWritten,
1112 FALSE,
1113 TRUE);
1114 }
1115
1116
1117 /*--------------------------------------------------------------
1118 * WriteConsoleInputVDMW
1119 *
1120 * @implemented
1121 */
1122 BOOL
1123 WINAPI
1124 WriteConsoleInputVDMW(HANDLE hConsoleInput,
1125 CONST INPUT_RECORD *lpBuffer,
1126 DWORD nLength,
1127 LPDWORD lpNumberOfEventsWritten)
1128 {
1129 return IntWriteConsoleInput(hConsoleInput,
1130 (PINPUT_RECORD)lpBuffer,
1131 nLength,
1132 lpNumberOfEventsWritten,
1133 TRUE,
1134 FALSE);
1135 }
1136
1137
1138 /*--------------------------------------------------------------
1139 * WriteConsoleInputVDMA
1140 *
1141 * @implemented
1142 */
1143 BOOL
1144 WINAPI
1145 WriteConsoleInputVDMA(HANDLE hConsoleInput,
1146 CONST INPUT_RECORD *lpBuffer,
1147 DWORD nLength,
1148 LPDWORD lpNumberOfEventsWritten)
1149 {
1150 return IntWriteConsoleInput(hConsoleInput,
1151 (PINPUT_RECORD)lpBuffer,
1152 nLength,
1153 lpNumberOfEventsWritten,
1154 FALSE,
1155 FALSE);
1156 }
1157
1158
1159 /*--------------------------------------------------------------
1160 * WriteConsoleOutputW
1161 *
1162 * @implemented
1163 */
1164 BOOL
1165 WINAPI
1166 WriteConsoleOutputW(HANDLE hConsoleOutput,
1167 CONST CHAR_INFO *lpBuffer,
1168 COORD dwBufferSize,
1169 COORD dwBufferCoord,
1170 PSMALL_RECT lpWriteRegion)
1171 {
1172 return IntWriteConsoleOutput(hConsoleOutput,
1173 lpBuffer,
1174 dwBufferSize,
1175 dwBufferCoord,
1176 lpWriteRegion,
1177 TRUE);
1178 }
1179
1180
1181 /*--------------------------------------------------------------
1182 * WriteConsoleOutputA
1183 *
1184 * @implemented
1185 */
1186 BOOL
1187 WINAPI
1188 WriteConsoleOutputA(HANDLE hConsoleOutput,
1189 CONST CHAR_INFO *lpBuffer,
1190 COORD dwBufferSize,
1191 COORD dwBufferCoord,
1192 PSMALL_RECT lpWriteRegion)
1193 {
1194 return IntWriteConsoleOutput(hConsoleOutput,
1195 lpBuffer,
1196 dwBufferSize,
1197 dwBufferCoord,
1198 lpWriteRegion,
1199 FALSE);
1200 }
1201
1202
1203 /*--------------------------------------------------------------
1204 * WriteConsoleOutputCharacterW
1205 *
1206 * @implemented
1207 */
1208 BOOL
1209 WINAPI
1210 WriteConsoleOutputCharacterW(HANDLE hConsoleOutput,
1211 LPCWSTR lpCharacter,
1212 DWORD nLength,
1213 COORD dwWriteCoord,
1214 LPDWORD lpNumberOfCharsWritten)
1215 {
1216 return IntWriteConsoleOutputCode(hConsoleOutput,
1217 CODE_UNICODE,
1218 lpCharacter,
1219 nLength,
1220 dwWriteCoord,
1221 lpNumberOfCharsWritten);
1222 }
1223
1224
1225 /*--------------------------------------------------------------
1226 * WriteConsoleOutputCharacterA
1227 *
1228 * @implemented
1229 */
1230 BOOL
1231 WINAPI
1232 WriteConsoleOutputCharacterA(HANDLE hConsoleOutput,
1233 LPCSTR lpCharacter,
1234 DWORD nLength,
1235 COORD dwWriteCoord,
1236 LPDWORD lpNumberOfCharsWritten)
1237 {
1238 return IntWriteConsoleOutputCode(hConsoleOutput,
1239 CODE_ASCII,
1240 lpCharacter,
1241 nLength,
1242 dwWriteCoord,
1243 lpNumberOfCharsWritten);
1244 }
1245
1246
1247 /*--------------------------------------------------------------
1248 * WriteConsoleOutputAttribute
1249 *
1250 * @implemented
1251 */
1252 BOOL
1253 WINAPI
1254 WriteConsoleOutputAttribute(HANDLE hConsoleOutput,
1255 CONST WORD *lpAttribute,
1256 DWORD nLength,
1257 COORD dwWriteCoord,
1258 LPDWORD lpNumberOfAttrsWritten)
1259 {
1260 return IntWriteConsoleOutputCode(hConsoleOutput,
1261 CODE_ATTRIBUTE,
1262 lpAttribute,
1263 nLength,
1264 dwWriteCoord,
1265 lpNumberOfAttrsWritten);
1266 }
1267
1268
1269 /*--------------------------------------------------------------
1270 * FillConsoleOutputCharacterW
1271 *
1272 * @implemented
1273 */
1274 BOOL
1275 WINAPI
1276 FillConsoleOutputCharacterW(HANDLE hConsoleOutput,
1277 WCHAR cCharacter,
1278 DWORD nLength,
1279 COORD dwWriteCoord,
1280 LPDWORD lpNumberOfCharsWritten)
1281 {
1282 return IntFillConsoleOutputCode(hConsoleOutput,
1283 CODE_UNICODE,
1284 &cCharacter,
1285 nLength,
1286 dwWriteCoord,
1287 lpNumberOfCharsWritten);
1288 }
1289
1290
1291 /*--------------------------------------------------------------
1292 * FillConsoleOutputCharacterA
1293 *
1294 * @implemented
1295 */
1296 BOOL
1297 WINAPI
1298 FillConsoleOutputCharacterA(HANDLE hConsoleOutput,
1299 CHAR cCharacter,
1300 DWORD nLength,
1301 COORD dwWriteCoord,
1302 LPDWORD lpNumberOfCharsWritten)
1303 {
1304 return IntFillConsoleOutputCode(hConsoleOutput,
1305 CODE_ASCII,
1306 &cCharacter,
1307 nLength,
1308 dwWriteCoord,
1309 lpNumberOfCharsWritten);
1310 }
1311
1312
1313 /*--------------------------------------------------------------
1314 * FillConsoleOutputAttribute
1315 *
1316 * @implemented
1317 */
1318 BOOL
1319 WINAPI
1320 FillConsoleOutputAttribute(HANDLE hConsoleOutput,
1321 WORD wAttribute,
1322 DWORD nLength,
1323 COORD dwWriteCoord,
1324 LPDWORD lpNumberOfAttrsWritten)
1325 {
1326 return IntFillConsoleOutputCode(hConsoleOutput,
1327 CODE_ATTRIBUTE,
1328 &wAttribute,
1329 nLength,
1330 dwWriteCoord,
1331 lpNumberOfAttrsWritten);
1332 }
1333
1334 /* EOF */