#include #include #define MY_BUF_SIZE 1024 DWORD WINAPI read_stdin(LPVOID lpParameter) { BOOL bResult = FALSE; char szBuf[MY_BUF_SIZE] = ""; int nIndex = 0; DWORD nNumOfBytesRead = 0; HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); if (hStdin == NULL || hStdin == INVALID_HANDLE_VALUE) { printf("GetStdHandle failed for STD_INPUT_HANDLE\n"); exit(2); } nIndex = 0; for (;;) { nNumOfBytesRead = 0; szBuf[nIndex] = '\0'; bResult = ReadFile(hStdin, &szBuf[nIndex], 1, &nNumOfBytesRead, NULL); if (bResult) { if (nNumOfBytesRead == 0) { // End of the file was reached // Print the data read if they exist if (nIndex > 0) { printf("Input data = \"%s\"\n", szBuf); } break; } if ( (szBuf[nIndex] == '\n') || (nIndex == MY_BUF_SIZE - 1) ) { // End of the input has been detected // Print the input data printf("Input data = \"%s\"\n", szBuf); // Reset the index back to the beginning of the input buffer nIndex = 0; } else { // Increment the index and continue the reading ++nIndex; } } else { DWORD dwErrorCode = GetLastError(); printf("ReadFile failed ===== LastErrorCode = %d\n", dwErrorCode); exit(3); } } // infinite for cycle return 0; } // read_stdin int main(int argc, char *argv[]) { BOOL bResult = FALSE; DWORD dwThreadID = 0; HANDLE hStdinReaderThread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)read_stdin, NULL, 0, &dwThreadID); if (!hStdinReaderThread) { printf("CreateThread failed\n"); exit(1); } // Simulate work in the main thread Sleep(1000); // Close the stdin => the stdin reader thread should exit in that case CloseHandle(GetStdHandle(STD_INPUT_HANDLE)); if (WaitForSingleObject(hStdinReaderThread, 1000) != WAIT_OBJECT_0) { printf("Stdin_Reader_Thread has NOT been completed. So, it will be terminated.\n"); TerminateThread(hStdinReaderThread, 1); } else { printf("Stdin_Reader_Thread has been completed\n"); } CloseHandle(hStdinReaderThread); return 0; } // main