home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / DOSSWT.ZIP / DOSSWTCH.C
Text File  |  1991-07-10  |  4KB  |  146 lines

  1.  
  2.  
  3.  
  4. /************************************************************************
  5.  *  Originally Taken from DOSSYS.C by Peter Fitzsimmons.  Code fragment
  6.  *  of PM application.  The command line to pass to the DOS box is passed
  7.  *  to Launch_Dos and written to the DOS.SYS device driver for execution.
  8.  *
  9.  *  Changes to jump2dos by Bryan Walker, Memphis Tn. instructing PM to
  10.  *  return your PM application back to foreground after DOS App. is done.
  11.  ************************************************************************/
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21. VOID Launch_Dos(CHAR *cmd)
  22. {
  23.     HFILE hf;
  24.     USHORT rc, dummy;
  25.     int i;
  26.  
  27.  
  28.     /**************
  29.      * Open Dos.sys and write the command line
  30.      * desired.  I usually use a batch file so that
  31.      * a short command line accomplishes several
  32.      * functions.  Useful for launching a DOS based
  33.      * report writer such as Clipper against data
  34.      * gathered into a DB3 compatible database.
  35.      **************/
  36.  
  37.     rc = DosOpen("DOS$", &hf, &dummy, 0L, FILE_NORMAL, FILE_OPEN,
  38.         OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYNONE |
  39.         OPEN_FLAGS_FAIL_ON_ERROR, 0L);
  40.  
  41.     if(!rc)
  42.     {
  43.         rc = DosWrite(hf, cmd, strlen(cmd)+1, &dummy);
  44.         DosClose(hf);
  45.         if(!rc)
  46.             jump2dos(); //successfully opened DOS.sys and wrote command
  47.  
  48.         else if(rc == ERROR_NOT_READY)
  49.             {
  50.             WinMessageBox(HWND_DESKTOP, HWND_DESKTOP,
  51.               "A program is already running in the DOS box",
  52.               "Reports", 0, MB_OK|MB_ICONEXCLAMATION);
  53.             return;
  54.             }
  55.         else
  56.             {
  57.             WinMessageBox(HWND_DESKTOP, HWND_DESKTOP,
  58.                "Unexpected OS/2 error",
  59.                "Reports", 0, MB_OK|MB_ICONEXCLAMATION);
  60.             return;
  61.             }
  62.     }
  63.     else
  64.     {
  65.     WinMessageBox(HWND_DESKTOP, HWND_DESKTOP,
  66.           "DOS.SYS not installed",
  67.           "Reports", 0, MB_OK|MB_ICONEXCLAMATION);
  68.     return;
  69.     }
  70. }
  71.  
  72.  
  73.  
  74. void jump2dos(void)
  75. {
  76.     USHORT num, i;
  77.     void *buf = malloc(32000);
  78.     SWENTRY *swe;
  79.     HSWITCH fgHandle;
  80.     SEL selGlobalSeg, selLocalSeg;
  81.     GINFOSEG FAR * volatile pgis;
  82.     UCHAR sgDos;
  83.  
  84.     /*****************************************
  85.      * Here is where we get the Switch handle
  86.      * for our application.  The Window Handle
  87.      * hwndFrame is for the frame of this PM
  88.      * application. When the DOS app is complete
  89.      * switching back to this switch handle
  90.      * will bring our app back to the foreground.
  91.      *
  92.      * The original code got the switch handle for
  93.      * the current foreground application which may
  94.      * not match our app. but instead be the Task List.
  95.      * Getting the switch handle for our app insures
  96.      * that it will be in the foreground when DOS
  97.      * finishes, even if it weren't when we started
  98.      * DOS.
  99.      *******************************************/
  100.  
  101.     DosGetInfoSeg(&selGlobalSeg, &selLocalSeg);
  102.     pgis = MAKEPGINFOSEG(selGlobalSeg);
  103.     fgHandle = WinQuerySwitchHandle(hwndFrame, 0);  //Switch handle
  104.  
  105.  
  106.  
  107.     /* Find the switch list handle of the DOS box: */
  108.     num = WinQuerySwitchList(0L, buf, 32000);
  109.     swe = (SWENTRY *)((USHORT *)buf + 1);
  110.     for(i=0; i<num; i++)
  111.         {
  112.         if(!strcmp("DOS", swe[i].swctl.szSwtitle))
  113.             {
  114.             WinSwitchToProgram(swe[i].hswitch);
  115.             break;
  116.             }
  117.         }
  118.     free(buf);
  119.     if(i==num)
  120.         {
  121.         WinMessageBox(HWND_DESKTOP, HWND_DESKTOP,
  122.           "CANNOT Run Reports\nThis system is configured without a dos box!",
  123.           "Reports", 0, MB_OK|MB_ICONEXCLAMATION);
  124.         return;
  125.         }
  126.     if(fgHandle)
  127.         {
  128.         DosSleep(1000L);     /* give a moment for the task switch */
  129.         sgDos = pgis->sgCurrent;
  130.  
  131.         /* Peter Fitzsimmons' Comment (BCW)
  132.          * Wait (poll) current screen group -- waiting for the DOS
  133.          * program to end.  I don't like polling,  but this is the only
  134.          * solution I can come up with.  If you don't mind the "Task
  135.          * List" always being brought the the fore after the dos progam
  136.          * ends,  you can forget about all of this and exit() after the
  137.          * WinSwitchToProgram(), above.
  138.          */
  139.  
  140.         while( sgDos == pgis->sgCurrent )
  141.             DosSleep(500L);
  142.  
  143.         WinSwitchToProgram(fgHandle);   /* back to our PM Program */
  144.         }
  145. }
  146.