//*************************************************************** // From the book "Win32 System Services: The Heart of Windows 98 // and Windows 2000" // by Marshall Brain // Published by Prentice Hall // // Copyright 1995, by Prentice Hall. // // This code demonstrates a typical "cooked" (normal) mode // console. It accepts input from the user and echos it to // output. //*************************************************************** // cooked.cpp #include #include #include void ErrorHandler(char *s, DWORD err) { cout << s << endl; cout << "Error number: " << err << endl; ExitProcess(err); } void ProcessIO(HANDLE consoleStdin, HANDLE consoleStdout) { char buffer[1000]; DWORD numRead, numWrite; do { ReadFile(consoleStdin, buffer, 1000, &numRead, 0); buffer[numRead]='\0'; // will have CR/LF WriteFile(consoleStdout, buffer, strlen(buffer), &numWrite, 0); } while (strncmp(buffer, "exit", 4) != 0); } VOID main(void) { HANDLE consoleStdout, consoleStdin; // Get handles for standard in and out consoleStdin = GetStdHandle(STD_INPUT_HANDLE); consoleStdout = GetStdHandle(STD_OUTPUT_HANDLE); // they must be invalid if equal if (consoleStdin == consoleStdout) ErrorHandler("In GetStdHandle", GetLastError()); // Process user I/O ProcessIO(consoleStdin, consoleStdout); }