home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 11 Util
/
11-Util.zip
/
NPIPE.ZIP
/
SAMPLE.ARC
/
HELLO.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-06-11
|
4KB
|
129 lines
/*
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: Hello world sample PM program that writes to stdout and/or stderr.
::
:: Will redirect stderr output to stdout so that all output can be piped
:: to a filter program.
::
:: (Sample code based on Charles Petzold's WELCOME1 sample program.)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
*/
#define INCL_WIN
#define INCL_DOS_FILEMGR
#include <os2.h>
#include <stdio.h>
MRESULT EXPENTRY ClientWndProc(HWND, USHORT, MPARAM, MPARAM) ;
int main(void)
{
static CHAR szClientClass[] = "Hello" ;
static ULONG flFrameFlags = FCF_TITLEBAR | FCF_SYSMENU | FCF_SIZEBORDER |
FCF_MINMAX | FCF_SHELLPOSITION | FCF_TASKLIST ;
HAB hab ;
HMQ hmq ;
HWND hwndFrame, hwndClient ;
QMSG qmsg ;
HFILE hStdError; /* Standard Error */
HFILE hStdOut; /* Standard Out */
/* Saved Standard Error (-1 forces new handle) */
HFILE hStdErrSave = 0xFFFF;
hab = WinInitialize(0) ;
hmq = WinCreateMsgQueue(hab, 0) ;
/* Register the only window in this program. */
WinRegisterClass(hab, szClientClass, ClientWndProc,
CS_SIZEREDRAW, 0) ;
/* Create the main window */
hwndFrame = WinCreateStdWindow(HWND_DESKTOP, WS_VISIBLE, &flFrameFlags,
szClientClass, NULL, 0L, NULL, 0, &hwndClient) ;
WinSendMsg(hwndFrame, WM_SETICON,
WinQuerySysPointer(HWND_DESKTOP, SPTR_APPICON, FALSE),
NULL) ;
/*NOTE:.................................................
: Redirect stderr to stdout so that it can be piped.
:.....................................................
*/
hStdOut = fileno(stdout); /* Get the handle for stdout */
hStdError = fileno(stderr); /* Get the handle for stderr */
/* Get a duplicate handle for existing stderr (to save) */
DosDupHandle(hStdError, &hStdErrSave);
DosClose(hStdError); /* Close the current stderr */
DosDupHandle(hStdOut, &hStdError); /* Redirect stderr to stdout */
/* Begin the message loop... */
while(WinGetMsg(hab, &qmsg, NULL, 0, 0))
WinDispatchMsg(hab, &qmsg) ;
WinDestroyWindow(hwndFrame) ;
WinDestroyMsgQueue(hmq) ;
WinTerminate(hab) ;
/*NOTE:.................................................
: Reset stderr to its original value.
:.....................................................
*/
DosClose(hStdError); /* Close redirected stderr */
DosDupHandle( hStdErrSave, &hStdError); /* Redirect stderr back.*/
DosClose( hStdErrSave); /* Close saved stderr */
return 0 ;
}
MRESULT EXPENTRY ClientWndProc(HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
{
static CHAR szText [] = "Hello World";
HPS hps;
RECTL rcl ;
switch(msg)
{
case WM_PAINT:
hps = WinBeginPaint(hwnd, NULL, NULL) ;
WinQueryWindowRect(hwnd, &rcl) ;
WinDrawText(hps, -1, szText, &rcl, CLR_NEUTRAL, CLR_BACKGROUND,
DT_CENTER | DT_VCENTER | DT_ERASERECT) ;
WinEndPaint(hps) ;
/* Output a message to standard out */
fprintf(stdout, "STDOUT - Just painted 'Hello World'\n");
fflush( stdout);
/* Also Output a message to standard error */
fprintf(stderr, "STDERR - No error occured.'\n");
fflush( stderr);
return 0 ;
}
return WinDefWindowProc(hwnd, msg, mp1, mp2) ;
}