Annotation of q_a/samples/inherit/child.c, revision 1.1.1.2

1.1       root        1: /* Standard output handle/standard error handle redirection test
                      2: program */
                      3: 
1.1.1.2 ! root        4: /* Microsoft Developer Support
        !             5:    Copyright (c) 1992 Microsoft Corporation */
        !             6: 
1.1       root        7: #include <stdio.h>
                      8: #include <io.h>
                      9: #include <string.h>
                     10: #include <windows.h>
                     11: 
                     12: /* This function opens up a handle to CONOUT$ so that we can be
                     13: guaranteed to get error messages to the console even if the standard
                     14: handles are redirected. */
                     15: 
                     16: void myError(HANDLE h, char *api, int line)
                     17: {
                     18:   char buf[512];
                     19:   BOOL fSuccess;
                     20:   DWORD cBytesWritten;
                     21:   HANDLE hConout;
                     22: 
                     23:   sprintf(buf, "%s: Error %d from %s on line %d\n", __FILE__, GetLastError(),
                     24:       api, line);
                     25:   fSuccess = WriteFile(h, buf, strlen(buf), &cBytesWritten, NULL);
                     26:   if (!fSuccess)
                     27:     {
                     28:     hConout = CreateFile("CONOUT$", GENERIC_READ | GENERIC_WRITE,
                     29:         FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
                     30:         FILE_ATTRIBUTE_NORMAL, NULL);
                     31:     sprintf(buf, "%s: Error %d from %s on line %d\n", __FILE__,
                     32:       GetLastError(), api, line);
                     33:     WriteFile(hConout, buf, strlen(buf), &cBytesWritten, NULL);
                     34:     CloseHandle(hConout);
                     35:     }
                     36:   return;
                     37: }
                     38: 
                     39: /* get the standard output handle and standard error handle and
                     40: write a message to each */
                     41: 
                     42: int main()
                     43: {
                     44:   DWORD cBytesWritten;
                     45:   HANDLE hStdErr, hStdOut, hConout;
                     46:   BOOL fSuccess;
                     47:   char *stdErrMsg = "Hello from child to standard error!\n";
                     48:   char *stdOutMsg = "Hello from child to standard output!\n";
                     49: 
                     50:   hConout = CreateFile("CONOUT$", GENERIC_READ | GENERIC_WRITE,
                     51:       FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
                     52:       FILE_ATTRIBUTE_NORMAL, NULL);
                     53:   if ((int) hConout == -1)
                     54:     Beep(5000,500); /* no reliable way of reporting errors to the console */
                     55:                     /* if we can't open a console handle */
                     56:   hStdErr = GetStdHandle(STD_ERROR_HANDLE);
                     57:   if ((int) hStdErr == -1)
                     58:     myError(hConout, "GetStdHandle", __LINE__);
                     59:   hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
                     60:   if ((int) hStdOut == -1)
                     61:     myError(hConout, "GetStdHandle", __LINE__);
                     62:   fSuccess = WriteFile(hStdErr, stdErrMsg, strlen(stdErrMsg),
                     63:       &cBytesWritten, NULL);
                     64:   if (!fSuccess)
                     65:     myError(hConout, "WriteFile", __LINE__);
                     66:   fSuccess = WriteFile(hStdOut, stdOutMsg, strlen(stdOutMsg),
                     67:       &cBytesWritten, NULL);
                     68:   if (!fSuccess)
                     69:     myError(hConout, "WriteFile", __LINE__);
                     70:   CloseHandle(hConout);
                     71:   return(0);
                     72: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.