home *** CD-ROM | disk | FTP | other *** search
/ Troubleshooting Netware Systems / CSTRIAL0196.BIN / attach / msj / v10n05 / dqa0595.exe / HVM.C next >
C/C++ Source or Header  |  1995-05-01  |  4KB  |  159 lines

  1. /*
  2.  *  Windows program that uses an MS-DOS app running in a hidden VM
  3.  *  to help out with its chores.
  4.  */
  5.  
  6. #include <windows.h>
  7. #include <toolhelp.h>
  8.  
  9. #define ID_BUTTON 100
  10.  
  11. long FAR PASCAL WndProc (HWND, WORD, WORD, LONG);
  12. BOOL FAR PASCAL NotifyCallBackProc (WORD, DWORD);
  13.  
  14. HWND hwndGlobal;
  15.  
  16. /*
  17.  *  Function WinMain.
  18.  */
  19.  
  20. int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  21.                     LPSTR lpszCmdLine, int nCmdShow)
  22. {
  23.     static char szAppName[] = "HVM";
  24.     WNDCLASS wc;
  25.     HWND hwnd;
  26.     MSG msg;
  27.  
  28.     if (!hPrevInstance) {
  29.         wc.style = 0;
  30.         wc.lpfnWndProc = (WNDPROC) WndProc;
  31.         wc.cbClsExtra = 0;
  32.         wc.cbWndExtra = 0;
  33.         wc.hInstance = hInstance;
  34.         wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  35.         wc.hCursor = LoadCursor (NULL, IDC_ARROW);
  36.         wc.hbrBackground = COLOR_WINDOW + 1;
  37.         wc.lpszMenuName = NULL;
  38.         wc.lpszClassName = szAppName;
  39.  
  40.         RegisterClass (&wc);
  41.     }
  42.  
  43.     hwnd = CreateWindow (szAppName, szAppName, WS_OVERLAPPEDWINDOW,
  44.         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  45.         HWND_DESKTOP, NULL, hInstance, NULL);
  46.  
  47.     hwndGlobal = hwnd;
  48.     ShowWindow (hwnd, nCmdShow);
  49.     UpdateWindow (hwnd);
  50.  
  51.     while (GetMessage (&msg, NULL, 0, 0)) {
  52.         TranslateMessage (&msg);
  53.         DispatchMessage (&msg);
  54.     }
  55.     return msg.wParam;
  56. }
  57.  
  58. /*
  59.  *  WndProc processes messages to the main window.
  60.  */
  61.  
  62. long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  63. {
  64.     TASKENTRY te;
  65.     static HTASK hTask;
  66.     static LPFNNOTIFYCALLBACK lpfnNotifyProc;
  67.     static HINSTANCE hInstance, hvm;
  68.     BOOL bResult;
  69.  
  70.     switch (message) {
  71.  
  72.     case WM_CREATE:
  73.         //
  74.         // Save the instance handle and create a push button.
  75.         //
  76.         hInstance = ((LPCREATESTRUCT) lParam)->hInstance;
  77.         CreateWindow ("button", "Do It!", WS_CHILD | WS_VISIBLE |
  78.             BS_PUSHBUTTON, 16, 16, 144, 80, hwnd, ID_BUTTON, hInstance, NULL);
  79.         return 0;
  80.  
  81.     case WM_COMMAND:
  82.         if (wParam == ID_BUTTON) {
  83.             //
  84.             // Register to receive ToolHelp notifications.
  85.             //
  86.             lpfnNotifyProc = (LPFNNOTIFYCALLBACK) MakeProcInstance
  87.                 (NotifyCallBackProc, hInstance);
  88.  
  89.             if (!NotifyRegister (NULL, lpfnNotifyProc, NF_NORMAL))
  90.                 MessageBox (hwnd, "NotifyRegister failed", "Error", MB_OK);
  91.  
  92.             //
  93.             // Launch the MS-DOS program from a PIF file. (Be sure to
  94.             // specify that the program will be run in a window.)
  95.             //
  96.             if ((hvm = (HINSTANCE) WinExec
  97.                 ("c:\\samples\\upcase.pif c:\\samples\\anyfile.c",
  98.                 SW_HIDE)) < 32) {
  99.  
  100.                 NotifyUnRegister (NULL);
  101.                 FreeProcInstance (lpfnNotifyProc);
  102.                 MessageBox (hwnd, "WinExec failed", "Error", MB_OK);
  103.                 return 0;
  104.             }
  105.  
  106.             //
  107.             // Use the instance handle returned by WinExec to get
  108.             // the task handle of the program that was just launched.
  109.             //
  110.             te.dwSize = sizeof (te);
  111.             bResult = TaskFirst (&te);
  112.  
  113.             while (bResult) {
  114.                 if (te.hInst == hvm) {
  115.                     hTask = te.hTask;
  116.                     return 0;
  117.                 }
  118.                 bResult = TaskNext (&te);
  119.             }
  120.  
  121.             MessageBox (hwnd, "Task not found", "Error", MB_OK);
  122.             return 0;
  123.         }
  124.         break;
  125.  
  126.     case WM_USER:
  127.         //
  128.         // See if the MS-DOS app is done.
  129.         //
  130.         if (wParam == (WPARAM) hTask) {
  131.             NotifyUnRegister (NULL);
  132.             FreeProcInstance (lpfnNotifyProc);
  133.             MessageBox (hwnd, "Done!", "Notification Received", MB_OK);
  134.             return 0;
  135.         }
  136.         break;
  137.  
  138.     case WM_DESTROY:
  139.         //
  140.         // Terminate the program.
  141.         //
  142.         PostQuitMessage (0);
  143.         return 0;
  144.     }
  145.     return DefWindowProc (hwnd, message, wParam, lParam);
  146. }
  147.  
  148. /*
  149.  *  NotifyCallBackProc processes ToolHelp notifications.
  150.  */
  151.  
  152. BOOL FAR PASCAL NotifyCallBackProc (WORD wID, DWORD dwData)
  153. {
  154.     if (wID == NFY_EXITTASK)
  155.         PostMessage (hwndGlobal, WM_USER, (WPARAM) GetCurrentTask (), 0);
  156.     return FALSE;
  157. }
  158.  
  159.