|
|
1.1.1.3 ! root 1: ! 2: /******************************************************************************\ ! 3: * This is a part of the Microsoft Source Code Samples. ! 4: * Copyright (C) 1993 Microsoft Corporation. ! 5: * All rights reserved. ! 6: * This source code is only intended as a supplement to ! 7: * Microsoft Development Tools and/or WinHelp documentation. ! 8: * See these sources for detailed information regarding the ! 9: * Microsoft samples programs. ! 10: \******************************************************************************/ ! 11: 1.1 root 12: /* Standard output handle/standard error handle redirection test 13: program */ 14: 15: #include <stdio.h> 16: #include <io.h> 17: #include <string.h> 18: #include <windows.h> 19: 20: /* This function opens up a handle to CONOUT$ so that we can be 21: guaranteed to get error messages to the console even if the standard 22: handles are redirected. */ 23: 24: void myError(HANDLE h, char *api, int line) 25: { 26: char buf[512]; 27: BOOL fSuccess; 28: DWORD cBytesWritten; 29: HANDLE hConout; 30: 31: sprintf(buf, "%s: Error %d from %s on line %d\n", __FILE__, GetLastError(), 32: api, line); 33: fSuccess = WriteFile(h, buf, strlen(buf), &cBytesWritten, NULL); 34: if (!fSuccess) 35: { 36: hConout = CreateFile("CONOUT$", GENERIC_READ | GENERIC_WRITE, 37: FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 38: FILE_ATTRIBUTE_NORMAL, NULL); 39: sprintf(buf, "%s: Error %d from %s on line %d\n", __FILE__, 40: GetLastError(), api, line); 41: WriteFile(hConout, buf, strlen(buf), &cBytesWritten, NULL); 42: CloseHandle(hConout); 43: } 44: return; 45: } 46: 47: /* get the standard output handle and standard error handle and 48: write a message to each */ 49: 50: int main() 51: { 52: DWORD cBytesWritten; 53: HANDLE hStdErr, hStdOut, hConout; 54: BOOL fSuccess; 55: char *stdErrMsg = "Hello from child to standard error!\n"; 56: char *stdOutMsg = "Hello from child to standard output!\n"; 57: 58: hConout = CreateFile("CONOUT$", GENERIC_READ | GENERIC_WRITE, 59: FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 60: FILE_ATTRIBUTE_NORMAL, NULL); 61: if ((int) hConout == -1) 62: Beep(5000,500); /* no reliable way of reporting errors to the console */ 63: /* if we can't open a console handle */ 64: hStdErr = GetStdHandle(STD_ERROR_HANDLE); 1.1.1.3 ! root 65: if (hStdErr == INVALID_HANDLE_VALUE) 1.1 root 66: myError(hConout, "GetStdHandle", __LINE__); 67: hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); 1.1.1.3 ! root 68: if (hStdOut == INVALID_HANDLE_VALUE) 1.1 root 69: myError(hConout, "GetStdHandle", __LINE__); 70: fSuccess = WriteFile(hStdErr, stdErrMsg, strlen(stdErrMsg), 71: &cBytesWritten, NULL); 72: if (!fSuccess) 73: myError(hConout, "WriteFile", __LINE__); 74: fSuccess = WriteFile(hStdOut, stdOutMsg, strlen(stdOutMsg), 75: &cBytesWritten, NULL); 76: if (!fSuccess) 77: myError(hConout, "WriteFile", __LINE__); 78: CloseHandle(hConout); 79: return(0); 80: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.