home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_03_05 / 3n05024c < prev    next >
Text File  |  1992-01-31  |  5KB  |  124 lines

  1. #include <dos.h>
  2. #include "windows.h"
  3. /*
  4. ******************************************************************
  5. Title:      TSRWIN.C - Windows Component of TSR Interface
  6. Author:     Thomas W. Olsen
  7. Version:    3.0
  8. Compiler:   Microsoft C 6.0
  9.             cl /c /AL /Gsw /W3 /Oas /Zpe /Zi tsrwin.c
  10.             link /CO /NOD tsrwin,,, libw llibcew kernel, tsrwin.def
  11.             rc tsrwin.exe
  12. ******************************************************************
  13. */
  14. long FAR    PASCAL WindowProc(HWND hWnd, unsigned message, WORD wParam, LONG lParam);
  15. int         PASCAL SetSelectorBase(int, DWORD);
  16. int         PASCAL SetSelectorLimit(int, DWORD);
  17. HANDLE      PASCAL AllocateSelector(unsigned segment, unsigned offset, DWORD length);
  18.  
  19. int PASCAL WinMain(HANDLE hInst, HANDLE hPrevInst, LPSTR lpCmdLine, int numCmdShow )
  20. {
  21.     MSG       msg;
  22.     HWND      hWnd;
  23.     HDC       hContext;
  24.     HANDLE    selector = 0;
  25.     LPSTR     lpBuffer = NULL;
  26.     WNDCLASS  wc;
  27.     union     REGS regs;
  28.  
  29.     /************************* Setup Window ************************/
  30.  
  31.     wc.style         = NULL;
  32.     wc.lpfnWndProc   = WindowProc;
  33.     wc.cbClsExtra    = 0;
  34.     wc.cbWndExtra    = 0;
  35.     wc.hInstance     = hInst;
  36.     wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  37.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  38.     wc.hbrBackground = GetStockObject(WHITE_BRUSH); 
  39.     wc.lpszMenuName  = NULL;
  40.     wc.lpszClassName = "TsrWinClass";
  41.  
  42.     if (!RegisterClass(&wc))
  43.         return(FALSE);
  44.  
  45.     hWnd = CreateWindow( "TsrWinClass", "Windows & TSR Example",
  46.                           WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
  47.                           CW_USEDEFAULT, CW_USEDEFAULT,
  48.                           CW_USEDEFAULT, NULL, NULL, hInst, NULL );
  49.     if (!hWnd)
  50.         return (FALSE);
  51.  
  52.     ShowWindow( hWnd, numCmdShow );
  53.     hContext = GetDC( hWnd );                   /* Need Display Context */
  54.  
  55.     /*************************** Call TSR **************************/
  56.  
  57.     regs.x.ax = 0xBEEF;                         /* AX = Multiplex Handle */
  58.     regs.x.bx = 0;
  59.     int86(0x2F, ®s, ®s);                  /* Call DOS App via INT 2FH */
  60.  
  61.     /************************ Display Buffer ***********************/
  62.  
  63.     if (regs.x.ax != 0xBEEF)                    /* Where's the Beef? */
  64.     {
  65.         if (GetWinFlags() & WF_PMODE)           /* Running in Protected Mode? */
  66.         {
  67.             selector = AllocateSelector(regs.x.dx, regs.x.ax, (DWORD) regs.x.cx);
  68.             lpBuffer = (LPSTR) MAKELONG(0, selector);
  69.         }
  70.         else                                    /* No ... Must Be Real Mode */
  71.             lpBuffer = (LPSTR) MAKELONG(regs.x.ax, regs.x.dx);
  72.  
  73.         if (lpBuffer)
  74.             TextOut( hContext, 0, 0, lpBuffer, regs.x.cx ); /* Print Buffer */
  75.  
  76.         if (selector)                           /* If Selector Allocated, */
  77.             FreeSelector(selector);             /* Free it ... Demo's Over */
  78.     }
  79.     else
  80.     {
  81.         lpBuffer = "TSR Not Loaded.";
  82.         TextOut( hContext, 0, 0, lpBuffer, lstrlen(lpBuffer) );
  83.     }
  84.  
  85.     ReleaseDC( hWnd, hContext );                /* Free Display Context */
  86.     UpdateWindow( hWnd );
  87.  
  88.     while (GetMessage(&msg, NULL, NULL, NULL))  /* Typical Message Loop */
  89.     {
  90.         TranslateMessage(&msg);
  91.         DispatchMessage(&msg);
  92.     }
  93.     return (msg.wParam);
  94. }
  95.  
  96. long FAR PASCAL WindowProc(HWND hWnd, unsigned message, WORD wParam, LONG lParam)
  97. {
  98.     switch (message)
  99.     {
  100.         case WM_DESTROY:
  101.             PostQuitMessage( NULL );
  102.             break;
  103.         default:
  104.             return (DefWindowProc(hWnd, message, wParam, lParam));
  105.     }
  106. }
  107.  
  108. HANDLE PASCAL AllocateSelector(unsigned segment, unsigned offset, DWORD length)
  109. {                                               
  110.     DWORD physicalAddress, linearAddress;       /* This function allocs & maps */
  111.     HANDLE selector;                            /* a protected mode selector */
  112.     struct SREGS sregs;                         /* for a real mode address */
  113.  
  114.     segread(&sregs);                            /* Copy DS selector */
  115.  
  116.     if ((selector = AllocSelector(sregs.ds)))   /* Successful Allocate? */
  117.     {                                           /* Phys = Lin In 1st Megabyte */
  118.         linearAddress = physicalAddress = ((DWORD) segment << 4L) + offset;
  119.         SetSelectorBase(selector, linearAddress);  /* Set New Base Address */
  120.         SetSelectorLimit(selector, length);     /* Set New Segment Range */
  121.     }
  122.     return(selector);
  123. }
  124.