Contents Previous Next Subchapters

Windows Main Program for Server Examples

Syntax
void Setup()
void ShutDown()
void AddText(char *
add)

Description
Every OMDDE server is event driven, that is it does not initiate its actions. For this reason, even the simplest examples require a windows main program. This section describes using the windows main program that is used for the OMDDE server examples.

Setup
Each example has a Setup function that is specific for the example. The function has no arguments and does not return a value. A call is made to this function at the beginning of program execution.

ShutDown
Each example also has a ShutDown function that is specific for the example. This function has no arguments and does not return a value. A call is made to this function at the end of program execution.

AddText
The windows main program includes the AddText function. A call to this function adds the text specified by the '\0' terminated character vector add to the programs output window.

Windows Main Program
The following is the code for the windows main program:

# include <windows.h>

extern void Setup(void);
extern void ShutDown(void);

static HWND *hWindow;
static char *Text;

void AddText(char *add)
{    int  nOld;
     int  nAdd;
     char *old;

     old  = Text;
     nAdd = strlen(add);
     nOld = strlen(old);
     Text = malloc(nOld + nAdd + 1);

     strcpy(Text, old);
     strcpy(Text + nOld, add);

     free(old);
     InvalidateRect(hWindow, NULL, TRUE);
     return;
}

LRESULT CALLBACK WndProc(
     HWND   hWnd,
     UINT   message,
     WPARAM wParam,
     LPARAM lParam
)
{
     HDC         hdc;
     PAINTSTRUCT ps;
     RECT        rect;

     switch(message)
     {
          case WM_PAINT:
          hdc = BeginPaint(hWnd, &ps);
          SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
          GetClientRect(hWnd, &rect);
          DrawText(
               hdc,
               Text,
               -1,
               &rect,
               DT_LEFT | DT_NOPREFIX
          );
          EndPaint(hWnd, &ps);
          return 0;

          case WM_DESTROY:
          PostQuitMessage(0);
          return 0;
     }
     return DefWindowProc(hWnd, message, wParam, lParam);
}

int WINAPI WinMain(
     HINSTANCE hInstance,
     HINSTANCE hPrevInstance,
     LPSTR     lpszCmdParm,
     int       nCmdShow
)
{
     static LPCTSTR szAppName = "Simple Output";
     MSG       msg;
     WNDCLASS  wndclass;

     wndclass.style          = CS_HREDRAW | CS_VREDRAW;
     wndclass.lpfnWndProc    = WndProc;
     wndclass.cbClsExtra     = 0;
     wndclass.cbWndExtra     = 0;
     wndclass.hInstance      = hInstance;
     wndclass.hIcon          = LoadIcon(NULL, IDI_APPLICATION);
     wndclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
     wndclass.hbrBackground  = GetStockObject(WHITE_BRUSH);
     wndclass.lpszMenuName   = NULL;
     wndclass.lpszClassName  = szAppName;

     RegisterClass ( &wndclass);

     hWindow = CreateWindow(
          szAppName,               // window name
          "Output",                // window caption
          WS_OVERLAPPEDWINDOW,     // window style
          CW_USEDEFAULT,           // initial x position
          CW_USEDEFAULT,           // initial y position
          CW_USEDEFAULT,           // initial x size
          CW_USEDEFAULT,           // initial y size
          NULL,                    // parent window handle
          NULL,                    // window menu handle
          hInstance,               // program instance handle
          NULL                     // creation parameters
     );

     Text    = (char *) malloc(1);
     Text[0] = '\0';

     ShowWindow(hWindow, nCmdShow);
     UpdateWindow(hWindow);

     Setup();

     while(GetMessage(&msg, NULL, 0, 0) == TRUE)
     {    TranslateMessage(&msg);
          DispatchMessage(&msg);
     }

     ShutDown();
     free(Text);

     return msg.wParam;
}