home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / NPIPE.ZIP / SAMPLE.ARC / HELLO.C < prev    next >
C/C++ Source or Header  |  1991-06-11  |  4KB  |  129 lines

  1. /*
  2.  ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  3.  ::
  4.  ::    Hello world sample PM program that writes to stdout and/or stderr.
  5.  ::
  6.  ::    Will redirect stderr output to stdout so that all output can be piped
  7.  :: to a filter program.
  8.  ::
  9.  ::    (Sample code based on Charles Petzold's WELCOME1 sample program.)
  10.  ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  11. */
  12.  
  13. #define INCL_WIN
  14. #define INCL_DOS_FILEMGR
  15.  
  16. #include <os2.h>
  17. #include <stdio.h>
  18.  
  19.  
  20. MRESULT EXPENTRY ClientWndProc(HWND, USHORT, MPARAM, MPARAM) ;
  21.  
  22. int main(void)
  23. {
  24.     static CHAR  szClientClass[] = "Hello" ;
  25.     static ULONG flFrameFlags = FCF_TITLEBAR | FCF_SYSMENU | FCF_SIZEBORDER |
  26.                             FCF_MINMAX | FCF_SHELLPOSITION | FCF_TASKLIST ;
  27.     HAB    hab ;
  28.     HMQ    hmq ;
  29.     HWND    hwndFrame, hwndClient ;
  30.     QMSG    qmsg ;
  31.  
  32.     HFILE    hStdError;        /* Standard Error */
  33.     HFILE    hStdOut;        /* Standard Out */
  34.  
  35.     /* Saved Standard Error (-1 forces new handle) */
  36.     HFILE    hStdErrSave = 0xFFFF;
  37.  
  38.  
  39.     hab = WinInitialize(0) ;
  40.     hmq = WinCreateMsgQueue(hab, 0) ;
  41.  
  42.      /* Register the only window in this program. */
  43.      WinRegisterClass(hab, szClientClass, ClientWndProc,
  44.                     CS_SIZEREDRAW, 0) ;
  45.  
  46.      /* Create the main window */
  47.      hwndFrame = WinCreateStdWindow(HWND_DESKTOP, WS_VISIBLE, &flFrameFlags,
  48.                     szClientClass, NULL, 0L, NULL, 0, &hwndClient) ;
  49.  
  50.      WinSendMsg(hwndFrame, WM_SETICON,
  51.                  WinQuerySysPointer(HWND_DESKTOP, SPTR_APPICON, FALSE),
  52.                  NULL) ;
  53.  
  54.  
  55.     /*NOTE:.................................................
  56.        : Redirect stderr to stdout so that it can be piped.
  57.        :.....................................................
  58.      */
  59.  
  60.         hStdOut = fileno(stdout);    /* Get the handle for stdout */
  61.         hStdError = fileno(stderr);    /* Get the handle for stderr */
  62.  
  63.         /* Get a duplicate handle for existing stderr (to save) */
  64.         DosDupHandle(hStdError, &hStdErrSave);
  65.  
  66.         DosClose(hStdError);        /* Close the current stderr */
  67.  
  68.         DosDupHandle(hStdOut, &hStdError);    /* Redirect stderr to stdout */
  69.  
  70.  
  71.  
  72.  
  73.     /* Begin the message loop... */
  74.      while(WinGetMsg(hab, &qmsg, NULL, 0, 0))
  75.           WinDispatchMsg(hab, &qmsg) ;
  76.  
  77.      WinDestroyWindow(hwndFrame) ;
  78.      WinDestroyMsgQueue(hmq) ;
  79.      WinTerminate(hab) ;
  80.  
  81.  
  82.     /*NOTE:.................................................
  83.        : Reset stderr to its original value.
  84.        :.....................................................
  85.      */
  86.  
  87.         DosClose(hStdError);                    /* Close redirected stderr */
  88.         DosDupHandle( hStdErrSave, &hStdError);    /* Redirect stderr back.*/
  89.         DosClose( hStdErrSave);                    /* Close saved stderr */
  90.  
  91.  
  92.      return 0 ;
  93. }
  94.  
  95. MRESULT EXPENTRY ClientWndProc(HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  96. {
  97.      static CHAR szText [] = "Hello World";
  98.      HPS         hps;
  99.      RECTL       rcl ;
  100.  
  101.      switch(msg)
  102.       {
  103.           case WM_PAINT:
  104.                hps = WinBeginPaint(hwnd, NULL, NULL) ;
  105.  
  106.                WinQueryWindowRect(hwnd, &rcl) ;
  107.  
  108.                WinDrawText(hps, -1, szText, &rcl, CLR_NEUTRAL, CLR_BACKGROUND,
  109.                             DT_CENTER | DT_VCENTER | DT_ERASERECT) ;
  110.  
  111.                WinEndPaint(hps) ;
  112.  
  113.                 /* Output a message to standard out */
  114.                 fprintf(stdout, "STDOUT - Just painted 'Hello World'\n");
  115.                 fflush( stdout);
  116.  
  117.  
  118.                 /* Also Output a message to standard error */
  119.                 fprintf(stderr, "STDERR - No error occured.'\n");
  120.                 fflush( stderr);
  121.  
  122.  
  123.                return 0 ;
  124.  
  125.           }
  126.  
  127.      return WinDefWindowProc(hwnd, msg, mp1, mp2) ;
  128.      }
  129.