2121
2222static constexpr int buf_sz = 256 ;
2323
24+ static void RedirectHandle (HANDLE hSource, HANDLE hDest) {
25+ char buf[buf_sz];
26+ DWORD nBytesRead;
27+ while (ReadFile (hSource, buf, sizeof (buf), &nBytesRead, NULL )) {
28+ DWORD nBytesWritten;
29+ DWORD nBytesRemain = nBytesRead;
30+ while (nBytesRemain > 0 ) {
31+ if (WriteFile (hDest, &buf[nBytesRead - nBytesRemain], nBytesRemain, &nBytesWritten, nullptr )) {
32+ nBytesRemain -= nBytesWritten;
33+ }
34+ }
35+ }
36+ }
37+
2438DWORD WINAPI BatchStdInRedirector (_In_ LPVOID lpParameter) {
2539 HANDLE hStdIn = GetStdHandle (STD_INPUT_HANDLE);
2640 if (hStdIn == INVALID_HANDLE_VALUE) {
@@ -35,18 +49,7 @@ DWORD WINAPI BatchStdInRedirector(_In_ LPVOID lpParameter) {
3549 SetConsoleMode (hStdIn, mode);
3650
3751 HANDLE hSerial = reinterpret_cast <HANDLE>(lpParameter);
38- char buf[buf_sz];
39- DWORD nBytesRead;
40- while (ReadFile (hStdIn, buf, sizeof (buf), &nBytesRead, NULL )) {
41- DWORD nBytesWritten;
42- DWORD nBytesRemain = nBytesRead;
43- while (nBytesRemain > 0 ) {
44- if (WriteFile (hSerial, &buf[nBytesRead - nBytesRemain], nBytesRemain, &nBytesWritten, nullptr )) {
45- nBytesRemain -= nBytesWritten;
46- }
47- }
48- }
49-
52+ RedirectHandle (hStdIn, hSerial);
5053 return 0 ;
5154}
5255
@@ -58,18 +61,7 @@ DWORD WINAPI BatchStdOutRedirector(_In_ LPVOID lpParameter) {
5861 }
5962
6063 HANDLE hSerial = reinterpret_cast <HANDLE>(lpParameter);
61- char buf[buf_sz];
62- DWORD nBytesRead;
63- while (ReadFile (hSerial, buf, sizeof (buf), &nBytesRead, NULL )) {
64- DWORD nBytesWritten;
65- DWORD nBytesRemain = nBytesRead;
66- while (nBytesRemain > 0 ) {
67- if (WriteFile (hStdOut, &buf[nBytesRead - nBytesRemain], nBytesRemain, &nBytesWritten, nullptr )) {
68- nBytesRemain -= nBytesWritten;
69- }
70- }
71- }
72-
64+ RedirectHandle (hSerial, hStdOut);
7365 return 0 ;
7466}
7567
0 commit comments