home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 046 / dosq1194.exe / WINPOST.C < prev    next >
C/C++ Source or Header  |  1994-11-01  |  5KB  |  169 lines

  1. /*
  2.  *  WinPost demonstrates how a Windows app can receive and process messages
  3.  *  from an MS-DOS app running in another VM.
  4.  */
  5.  
  6. #include <windows.h>
  7. #include <memory.h>
  8. #include <dos.h>
  9.  
  10. #define Device_ID 0x4321
  11.  
  12. long FAR PASCAL WndProc (HWND, WORD, WORD, LONG);
  13. void FAR PASCAL CallbackProc (HWND, WORD, WORD, LONG);
  14.  
  15. //
  16. // The following statement places the callback procedure in the segment
  17. // named CALLBACKSEG, which is defined as fixed and nondiscardable in the
  18. // program's module definition file.
  19. //
  20. #pragma alloc_text (CALLBACKSEG, CallbackProc)
  21.  
  22. /*
  23.  *  Function WinMain.
  24.  */
  25.  
  26. int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                     LPSTR lpszCmdLine, int nCmdShow)
  28. {
  29.     static char szAppName[] = "WinPost";
  30.     WNDCLASS wc;
  31.     HWND hwnd;
  32.     MSG msg;
  33.  
  34.     if (!hPrevInstance) {
  35.         wc.style = 0;
  36.         wc.lpfnWndProc = (WNDPROC) WndProc;
  37.         wc.cbClsExtra = 0;
  38.         wc.cbWndExtra = 0;
  39.         wc.hInstance = hInstance;
  40.         wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  41.         wc.hCursor = LoadCursor (NULL, IDC_ARROW);
  42.         wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
  43.         wc.lpszMenuName = NULL;
  44.         wc.lpszClassName = szAppName;
  45.  
  46.         RegisterClass (&wc);
  47.     }
  48.  
  49.     hwnd = CreateWindow (szAppName, szAppName, WS_OVERLAPPEDWINDOW,
  50.         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  51.         HWND_DESKTOP, NULL, hInstance, NULL);
  52.  
  53.     ShowWindow (hwnd, nCmdShow);
  54.     UpdateWindow (hwnd);
  55.  
  56.     while (GetMessage (&msg, NULL, 0, 0)) {
  57.         TranslateMessage (&msg);
  58.         DispatchMessage (&msg);
  59.     }
  60.     return msg.wParam;
  61. }
  62.  
  63. /*
  64.  *  WndProc processes messages to the main window.
  65.  */
  66.  
  67. long FAR PASCAL WndProc (HWND hwnd, WORD msg, WORD wParam, LONG lParam)
  68. {
  69.     RECT rect;
  70.     static HWND hwndListbox;
  71.     static FARPROC fpVxd;
  72.     struct _SREGS sregs;
  73.     union _REGS regs;
  74.     char szBuffer[64];
  75.     DWORD dwCallback;
  76.                             
  77.     switch (msg) {
  78.  
  79.     case WM_CREATE:
  80.         //
  81.         // Make sure we're in 386 enhanced mode.
  82.         //
  83.         if (GetWinFlags () & WF_ENHANCED == 0) {
  84.             MessageBox (hwnd, "This application must be run in 386 "\
  85.                 "enhanced mode", "Error", MB_OK);
  86.             return -1;
  87.         }
  88.         
  89.         //
  90.         // Get the protected mode entry point address and fail the
  91.         // application if the POSTMSG VxD is not loaded.
  92.         //      
  93.         memset (&sregs, 0, sizeof (sregs));
  94.         regs.x.ax = 0x1684;
  95.         regs.x.bx = Device_ID;
  96.         _int86x (0x2F, ®s, ®s, &sregs);
  97.  
  98.         _FP_SEG (fpVxd) = sregs.es;
  99.         _FP_OFF (fpVxd) = regs.x.di;
  100.  
  101.         if (!fpVxd) {
  102.             MessageBox (hwnd, "The POSTMSG VxD is not loaded",
  103.                 "Error", MB_OK);
  104.             return -1;
  105.         }
  106.  
  107.         //
  108.         // Register this application with the POSTMSG VxD.
  109.         //
  110.  
  111.         dwCallback = (DWORD) CallbackProc;
  112.         __asm mov ax, hwnd
  113.         __asm mov bx, word ptr [dwCallback + 0]
  114.         __asm mov cx, word ptr [dwCallback + 2]
  115.         (*fpVxd) ();
  116.         
  117.         //
  118.         // Create a list box that fills the window's client area.
  119.         //
  120.         GetClientRect (hwnd, &rect);
  121.         hwndListbox = CreateWindow ("listbox", NULL,
  122.             WS_CHILD | WS_VISIBLE | WS_VSCROLL | LBS_NOINTEGRALHEIGHT,
  123.             0, 0, rect.right, rect.bottom, hwnd, 100,
  124.             (HINSTANCE) (((LPCREATESTRUCT) lParam)->hInstance), NULL);
  125.         return 0;
  126.  
  127.     case WM_SIZE:
  128.         //
  129.         // Resize the list box when the main window is resized.
  130.         //
  131.         if ((wParam == SIZE_RESTORED) || (wParam == SIZE_MAXIMIZED)){
  132.             MoveWindow (hwndListbox, 0, 0, LOWORD (lParam),
  133.                 HIWORD (lParam), TRUE);
  134.             return 0;
  135.         }
  136.         break;
  137.  
  138.     case WM_USER + 0x100:
  139.         //
  140.         // Display the message parameters in the list box.
  141.         //  
  142.         wsprintf (szBuffer, "msg=%u, wParam=%u, lParam=%lu",
  143.             msg, wParam, lParam);
  144.         SendMessage (hwndListbox, LB_ADDSTRING, 0,
  145.             (LPARAM) (LPCSTR) szBuffer);
  146.         return 0;
  147.  
  148.     case WM_DESTROY:
  149.         //
  150.         // Terminate the application.
  151.         //
  152.         PostQuitMessage (0);
  153.         return 0;
  154.     }
  155.     return DefWindowProc (hwnd, msg, wParam, lParam);
  156. }
  157.  
  158. /*
  159.  *  CallbackProc gets called by the POSTMSG VxD, and in turn calls
  160.  *  PostMessage to post a message to WndProc.
  161.  */
  162.  
  163. void FAR PASCAL CallbackProc (HWND hwnd, WORD msg, WORD wParam, LONG lParam)
  164. {
  165.     PostMessage (hwnd, msg, wParam, lParam);
  166. }
  167.  
  168.  
  169.