home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 18.ddi / SAMPLES / TDOSMEM / TDOSMEM.C_ / TDOSMEM.C
Encoding:
C/C++ Source or Header  |  1993-02-08  |  17.3 KB  |  470 lines

  1. /****************************************************************************
  2.  
  3.     PROGRAM: Tdosmem.c
  4.  
  5.     PURPOSE: Demo for accessing a TSR's buffer
  6.  
  7.     FUNCTIONS:
  8.  
  9.         WinMain() - calls initialization function, processes message loop
  10.         InitApplication() - initializes window data and registers window
  11.         InitInstance() - saves instance handle and creates main window
  12.         MainWndProc() - processes messages
  13.         About() - processes messages for "About" dialog box
  14.  
  15.     COMMENTS:
  16.  
  17.         This application demonstrates the technique of accessing a
  18.         buffer in a global DOS TSR. It communicates with the TTSR program
  19.         to retrieve the SEGMENT:OFFSET of a local buffer allocated
  20.         by the TSR. Then, it uses Windows LDT functions to create
  21.         a selector to access that memory.
  22.  
  23.         Notice that this technique could be used as a very simple method
  24.         for communication not only among Windows applications, but other
  25.         virtual machines as well. Since the buffer is in the TSR, it is
  26.         global to all virtual machines, so a DOS application that uses
  27.         the same method for accessing the buffer as TDOSMEM would be
  28.         able to modify the same memory.
  29.  
  30.  
  31. ****************************************************************************/
  32.  
  33. #include "windows.h"                /* required for all Windows applications */
  34. #include "tdosmem.h"                /* specific to this program              */
  35.  
  36. HANDLE hInst;                       /* current instance                      */
  37.  
  38. WORD  wDataSelector;
  39. int   nHandler_Installed = 0;
  40. WORD  wSelector = 0;
  41. WORD  wSegment;
  42. WORD  wOffset;
  43. WORD FAR *pPointer;
  44. int   nCount;
  45. char  szName[] = "GDOSMem";
  46. DWORD dWinFlags;
  47.  
  48. /****************************************************************************
  49.  
  50.     FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  51.  
  52.     PURPOSE: calls initialization function, processes message loop
  53.  
  54.     COMMENTS:
  55.  
  56.         Windows recognizes this function by name as the initial entry point
  57.         for the program.  This function calls the application initialization
  58.         routine, if no other instance of the program is running, and always
  59.         calls the instance initialization routine.  It then executes a message
  60.         retrieval and dispatch loop that is the top-level control structure
  61.         for the remainder of execution.  The loop is terminated when a WM_QUIT
  62.         message is received, at which time this function exits the application
  63.         instance by returning the value passed by PostQuitMessage().
  64.  
  65.         If this function must abort before entering the message loop, it
  66.         returns the conventional value NULL.
  67.  
  68. ****************************************************************************/
  69.  
  70. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  71. HANDLE hInstance;                            /* current instance             */
  72. HANDLE hPrevInstance;                        /* previous instance            */
  73. LPSTR lpCmdLine;                             /* command line                 */
  74. int nCmdShow;                                /* show-window type (open/icon) */
  75. {
  76.     MSG msg;                                 /* message                      */
  77.  
  78.     if (!hPrevInstance)                  /* Other instances of app running? */
  79.         if (!InitApplication(hInstance)) /* Initialize shared things */
  80.             return (FALSE);              /* Exits if unable to initialize     */
  81.  
  82.     /* Perform initializations that apply to a specific instance */
  83.  
  84.     if (!InitInstance(hInstance, nCmdShow))
  85.         return (FALSE);
  86.  
  87.     /* Acquire and dispatch messages until a WM_QUIT message is received. */
  88.  
  89.     while (GetMessage(&msg,        /* message structure                      */
  90.             NULL,                  /* handle of window receiving the message */
  91.             NULL,                  /* lowest message to examine              */
  92.             NULL))                 /* highest message to examine             */
  93.         {
  94.         TranslateMessage(&msg);    /* Translates virtual key codes           */
  95.         DispatchMessage(&msg);     /* Dispatches message to window           */
  96.     }
  97.     return (msg.wParam);           /* Returns the value from PostQuitMessage */
  98. }
  99.  
  100.  
  101. /****************************************************************************
  102.  
  103.     FUNCTION: InitApplication(HANDLE)
  104.  
  105.     PURPOSE: Initializes window data and registers window class
  106.  
  107.     COMMENTS:
  108.  
  109.         This function is called at initialization time only if no other
  110.         instances of the application are running.  This function performs
  111.         initialization tasks that can be done once for any number of running
  112.         instances.
  113.  
  114.         In this case, we initialize a window class by filling out a data
  115.         structure of type WNDCLASS and calling the Windows RegisterClass()
  116.         function.  Since all instances of this application use the same window
  117.         class, we only need to do this when the first instance is initialized.
  118.  
  119.  
  120. ****************************************************************************/
  121.  
  122. BOOL InitApplication(hInstance)
  123. HANDLE hInstance;                              /* current instance           */
  124. {
  125.     WNDCLASS  wc;
  126.  
  127.     /* Fill in window class structure with parameters that describe the       */
  128.     /* main window.                                                           */
  129.  
  130.     wc.style = NULL;                    /* Class style(s).                    */
  131.     wc.lpfnWndProc = MainWndProc;       /* Function to retrieve messages for  */
  132.                                         /* windows of this class.             */
  133.     wc.cbClsExtra = 0;                  /* No per-class extra data.           */
  134.     wc.cbWndExtra = 0;                  /* No per-window extra data.          */
  135.     wc.hInstance = hInstance;           /* Application that owns the class.   */
  136.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  137.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  138.     wc.hbrBackground = COLOR_WINDOW+1;
  139.     wc.lpszMenuName =  "TdosmemMenu";   /* Name of menu resource in .RC file. */
  140.     wc.lpszClassName = "TdosmemWClass"; /* Name used in call to CreateWindow. */
  141.  
  142.     /* Register the window class and return success/failure code. */
  143.  
  144.     return (RegisterClass(&wc));
  145.  
  146. }
  147.  
  148.  
  149. /****************************************************************************
  150.  
  151.     FUNCTION:  InitInstance(HANDLE, int)
  152.  
  153.     PURPOSE:  Saves instance handle and creates main window
  154.  
  155.     COMMENTS:
  156.  
  157.         This function is called at initialization time for every instance of
  158.         this application.  This function performs initialization tasks that
  159.         cannot be shared by multiple instances.
  160.  
  161.         In this case, we save the instance handle in a static variable and
  162.         create and display the main program window.
  163.  
  164. ****************************************************************************/
  165.  
  166. BOOL InitInstance(hInstance, nCmdShow)
  167.     HANDLE          hInstance;          /* Current instance identifier.       */
  168.     int             nCmdShow;           /* Param for first ShowWindow() call. */
  169. {
  170.     HWND            hWnd;               /* Main window handle.                */
  171.     short   xClient, yClient;
  172.  
  173.     /* Save the instance handle in static variable, which will be used in  */
  174.     /* many subsequence calls from this application to Windows.            */
  175.  
  176.     hInst = hInstance;
  177.  
  178.     SizeWindow (&xClient, &yClient);
  179.  
  180.  
  181.     /* Create a main window for this application instance.  */
  182.  
  183.     hWnd = CreateWindow(
  184.         "TdosmemWClass",                /* See RegisterClass() call.          */
  185.         "TSR Buffer Demo",              /* Text for window title bar.         */
  186.         WS_OVERLAPPEDWINDOW,            /* Window style.                      */
  187.         CW_USEDEFAULT,                  /* Default horizontal position.       */
  188.         CW_USEDEFAULT,                  /* Default vertical position.         */
  189.         xClient, yClient,
  190.         NULL,                           /* Overlapped windows have no parent. */
  191.         NULL,                           /* Use the window class menu.         */
  192.         hInstance,                      /* This instance owns this window.    */
  193.         NULL                            /* Pointer not needed.                */
  194.     );
  195.  
  196.     /* If window could not be created, return "failure" */
  197.  
  198.     if (!hWnd)
  199.         return (FALSE);
  200.  
  201.     /* Make the window visible; update its client area; and return "success" */
  202.  
  203.     ShowWindow(hWnd, nCmdShow);  /* Show the window                        */
  204.     UpdateWindow(hWnd);          /* Sends WM_PAINT message                 */
  205.     return (TRUE);               /* Returns the value from PostQuitMessage */
  206.  
  207. }
  208.  
  209. void SizeWindow (short *pxClient, short *pyClient)
  210.    {
  211.    HDC hdc;
  212.    TEXTMETRIC tm;
  213.    short xSize = 35;
  214.    short ySize = 20;
  215.  
  216.    hdc = CreateIC ("DISPLAY", NULL, NULL, NULL);
  217.    GetTextMetrics (hdc, &tm);
  218.    DeleteDC (hdc);
  219.  
  220.    *pxClient = 2* GetSystemMetrics (SM_CXBORDER) + xSize*tm.tmAveCharWidth;
  221.    *pyClient = 2* GetSystemMetrics (SM_CXBORDER) +
  222.                                ySize*(tm.tmHeight+tm.tmExternalLeading);
  223.  
  224.    }
  225.  
  226. /****************************************************************************
  227.  
  228.     FUNCTION: MainWndProc(HWND, UINT, WPARAM, LPARAM)
  229.  
  230.     PURPOSE:  Processes messages
  231.  
  232.  
  233. ****************************************************************************/
  234.  
  235. long FAR PASCAL __export MainWndProc(hWnd, message, wParam, lParam)
  236. HWND hWnd;                                /* window handle                   */
  237. UINT message;                  /* type of message         */
  238. WPARAM wParam;                    /* additional information           */
  239. LPARAM lParam;                    /* additional information           */
  240. {
  241.     FARPROC lpProcAbout;                  /* pointer to the "About" function */
  242.  
  243.     short y;
  244.     static short cxChar, cyChar;
  245.     HDC     hdc;
  246.     PAINTSTRUCT ps;
  247.     TEXTMETRIC tm;
  248.     char    szBuffer [132];
  249.  
  250.     switch (message) {
  251.  
  252.         /*------------------------ C R E A T E -----------------------*/
  253.         case WM_CREATE:
  254.             hdc = GetDC (hWnd);
  255.             GetTextMetrics (hdc, &tm);
  256.             cxChar = tm.tmAveCharWidth;
  257.             cyChar = tm.tmHeight + tm.tmExternalLeading;
  258.             y = 0;
  259.             ReleaseDC (hWnd, hdc);
  260.  
  261.             if ((dWinFlags = GetWinFlags()) & WF_PMODE)
  262.                 {
  263.                 wDataSelector = HIWORD ((DWORD) (WORD FAR *) &wDataSelector);
  264.                 wSelector = AllocSelector (wDataSelector);
  265.                 SetSelectorLimit (wSelector, 2);
  266.                 SetTimer (hWnd, 1, 100, NULL);
  267.                 }
  268.  
  269.             break;
  270.  
  271.         /*------------------------ P A I N T -------------------------*/
  272.         case WM_PAINT:
  273.             hdc = BeginPaint (hWnd, &ps);
  274.             y = 0;
  275.  
  276.             if (! (dWinFlags & WF_PMODE))
  277.                 {
  278.                 TextOut (hdc, cxChar, cyChar*y++, szBuffer,
  279.                   wsprintf (szBuffer, "Windows in Real mode."));
  280.                 TextOut (hdc, cxChar, cyChar*y++, szBuffer,
  281.                   wsprintf (szBuffer, "Just use addresses directly."));
  282.                 break;
  283.                 }
  284.  
  285.             TSR_Check();
  286.  
  287.             if (0==nHandler_Installed)
  288.                 TextOut (hdc, cxChar, cyChar*y++, szBuffer,
  289.                   wsprintf (szBuffer, "TSR not installed."));
  290.             else
  291.                 {
  292.                 TextOut (hdc, cxChar, cyChar*y++, szBuffer,
  293.                   wsprintf (szBuffer, "TSR installed."));
  294.  
  295.                 TextOut (hdc, cxChar, cyChar*y++, szBuffer,
  296.                   wsprintf (szBuffer, "Buffer is located at:"));
  297.  
  298.                 TextOut (hdc, cxChar, cyChar*y++, szBuffer,
  299.                   wsprintf (szBuffer, "  Phys addr=%.4X:%.4X",
  300.                                             wSegment,wOffset));
  301.  
  302.                 TextOut (hdc, cxChar, cyChar*y++, szBuffer,
  303.                   wsprintf (szBuffer, "  Prot addr=%.4X:%.4X",wSelector,0));
  304.  
  305.                 y++;
  306.                 SetSelectorBase(wSelector, (((DWORD)wSegment) << 4)+wOffset);
  307.                 pPointer = (WORD FAR *) ( (DWORD)wSelector << 16);
  308.         nCount = *pPointer;
  309.                 TextOut (hdc, cxChar, cyChar*y++, szBuffer,
  310.                   wsprintf (szBuffer, "Contents = %.4X",nCount));
  311.                 }
  312.  
  313.             EndPaint (hWnd, &ps);
  314.             break;
  315.  
  316.         /*------------------------ T I M E R -------------------------*/
  317.         case WM_TIMER:
  318.             pPointer = (WORD FAR *) ( (DWORD)wSelector << 16);
  319.         if (nCount != (int)*pPointer)
  320.                 InvalidateRect (hWnd, NULL, TRUE);
  321.             break;
  322.  
  323.  
  324.         /*------------------------ C O M M A N D ---------------------*/
  325.         case WM_COMMAND:           /* message: command from application menu */
  326.  
  327.          if (wParam == IDM_ABOUT)
  328.             {
  329.             lpProcAbout = MakeProcInstance(About, hInst);
  330.  
  331.             DialogBox(hInst,                 /* current instance         */
  332.                     "AboutBox",                  /* resource to use          */
  333.                     hWnd,                        /* parent handle            */
  334.                     lpProcAbout);                /* About() instance address */
  335.  
  336.             FreeProcInstance(lpProcAbout);
  337.             break;
  338.             }
  339.  
  340.          else if (wParam == IDM_INT) {
  341.             if (0==nHandler_Installed)
  342.                MessageBox (hWnd, "GDOS TSR is not installed", szName, MB_OK);
  343.             else if (! (dWinFlags & WF_PMODE))
  344.                MessageBox (hWnd, "Not available in real mode", szName, MB_OK);
  345.             else
  346.                 {
  347.                 pPointer = (WORD FAR *) ( (DWORD)wSelector << 16);
  348.                 (*pPointer)++;
  349.                 InvalidateRect (hWnd, NULL, TRUE);
  350.                 }
  351.             break;
  352.             }
  353.  
  354.          else
  355.                 return (DefWindowProc(hWnd, message, wParam, lParam));
  356.  
  357.  
  358.         /*------------------------ D E S T R O Y ---------------------*/
  359.         case WM_DESTROY:                  /* message: window being destroyed */
  360.             if (dWinFlags & WF_PMODE)
  361.                 {
  362.                 if (0!=wSelector) {
  363.                     FreeSelector (wSelector);
  364.                     }
  365.                 KillTimer (hWnd, 1);
  366.                 }
  367.             PostQuitMessage(0);
  368.             break;
  369.  
  370.         default:                          /* Passes it on if unproccessed    */
  371.             return (DefWindowProc(hWnd, message, wParam, lParam));
  372.     }
  373.     return (NULL);
  374. }
  375.  
  376.  
  377. /****************************************************************************
  378.  
  379.     FUNCTION: About(HWND, unsigned, WORD, LONG)
  380.  
  381.     PURPOSE:  Processes messages for "About" dialog box
  382.  
  383.     MESSAGES:
  384.  
  385.         WM_INITDIALOG - initialize dialog box
  386.         WM_COMMAND    - Input received
  387.  
  388.     COMMENTS:
  389.  
  390.         No initialization is needed for this particular dialog box, but TRUE
  391.         must be returned to Windows.
  392.  
  393.         Wait for user to click on "Ok" button, then close the dialog box.
  394.  
  395. ****************************************************************************/
  396.  
  397. BOOL FAR PASCAL __export About(hDlg, message, wParam, lParam)
  398. HWND hDlg;                                /* window handle of the dialog box */
  399. unsigned message;                         /* type of message                 */
  400. WORD wParam;                              /* message-specific information    */
  401. LONG lParam;
  402. {
  403.     switch (message) {
  404.         case WM_INITDIALOG:                /* message: initialize dialog box */
  405.             return (TRUE);
  406.  
  407.         case WM_COMMAND:                      /* message: received a command */
  408.             if (wParam == IDOK                /* "OK" box selected?          */
  409.                 || wParam == IDCANCEL) {      /* System menu close command? */
  410.                 EndDialog(hDlg, TRUE);        /* Exits the dialog box        */
  411.                 return (TRUE);
  412.             }
  413.             break;
  414.     }
  415.     return (FALSE);                           /* Didn't process a message    */
  416. }
  417.  
  418. #pragma optimize("",off)
  419. /*************************************************************************
  420.  
  421.     FUNCTION: TSR_Check()
  422.  
  423.     This routine checks to see if TTSR has been loaded. If so, it saves
  424.     the address of the TSR's buffer.
  425.  
  426.  *************************************************************************/
  427.  
  428. void TSR_Check()
  429. {
  430.     _asm{
  431.  
  432.         mov     ax, 0200h       ; get real mode interrupt vector
  433.         mov     bl, 60h         ; our handler
  434.         int     31h             ; DPMI Call
  435.  
  436.         or      cx, dx          ; anything there?
  437.         jz      short notsr
  438.         mov     ax, 899bh       ; TTSR Signature
  439.         mov     bx, 0           ; install check
  440.         int     60h
  441.  
  442.         cmp     bx, 899bh       ; Signature: did it do it?
  443.         jnz     short notsr
  444.         mov     nHandler_Installed, -1
  445.         mov     wSegment, cx
  446.         mov     wOffset, dx
  447.  
  448. notsr:
  449.         }
  450. }
  451.  
  452. /*************************************************************************
  453.  
  454.     FUNCTION: TSR_Request()
  455.  
  456.  
  457.     This routine requests TTSR to increment the WORD in the TSR's buffer.
  458.  
  459.  
  460.  *************************************************************************/
  461.  
  462. void TSR_Request()
  463. {
  464.     _asm{
  465.         mov     ax, 899bh               ; TTSR Signature
  466.         mov     bx, 1                   ; issue request
  467.         int     60h
  468.         }
  469. }
  470.