home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / edos / v86api / tstwinex.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-13  |  4.5 KB  |  242 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <dos.h>
  5. #include <conio.h>
  6.  
  7.  
  8.  
  9. typedef unsigned int BOOL;
  10. typedef unsigned int WORD;
  11. typedef unsigned long DWORD;
  12. #define FAR  _far
  13.  
  14.  
  15. int WinExec(unsigned long vxd_api, char *pname,
  16.       char *argname, int show_num, BOOL validate);
  17.  
  18.  
  19. /*
  20.  
  21. This works fine with Borland C, version 3.1 compiled with small model.
  22.  
  23. It should work ok with other memory models and except for libray function
  24. calls it might work with microsoft.
  25.  
  26.  
  27. */
  28.  
  29.  
  30.  
  31. char badexec_msg[] = { "\nTSTWINEX: failed to load windows application\n" };
  32.  
  33. unsigned long GetVxDAPI(void);
  34.  
  35. main( int argc, char **argv)
  36. {
  37. int result = 0;
  38. char show_type[5];
  39. int show_num;
  40. unsigned long vxd_api;
  41. unsigned int len;
  42.  
  43. char Prog_line[128];
  44.  
  45.  
  46. char Arg_line[128];
  47.  
  48.  
  49.  
  50.  
  51.     vxd_api = GetVxDAPI();    // returns a seg:offset, pointer in a long
  52.     if(!vxd_api) goto noesdi;
  53.  
  54.  
  55.         fputs("\nEnter program name: ", stdout);
  56.         fgets(Prog_line, sizeof(Prog_line)-1, stdin);    // delete trailing \r
  57.         len = strlen(Prog_line);
  58.         if(len)
  59.         Prog_line[len-1] = 0;
  60.  
  61. //        strcpy(Prog_line, "G:\\win31\\clock.exe");
  62.  
  63.  
  64.         fputs("\nEnter program args: ", stdout);
  65.         fgets(Arg_line, sizeof(Arg_line), stdin);
  66.  
  67.         len = strlen(Arg_line);
  68.         if(len)
  69.         Arg_line[len-1] = 0;
  70.  
  71. new_num:
  72.  
  73.         fputs("\nEnter Show Type: ", stdout);
  74.         fgets(show_type, 2, stdin);
  75.         show_type[strlen(show_type)] = 0;
  76.  
  77.  
  78.  
  79.        show_num = atoi(show_type);
  80.         if((show_num < 0) || (show_num > 9)) {
  81.             fputs("\nNot a legal show number (0-9)\n", stdout);
  82.              goto new_num;
  83.         }
  84.  
  85. // the args: Prog_line and Arg_line are zero terminated, normal c strings
  86.  
  87.     result = WinExec(vxd_api, Prog_line, Arg_line, show_num, 0);
  88.  
  89.  
  90.     if(result == -1) goto err;
  91.     if(result > 32-1) goto ok;
  92.  
  93.     fputs(badexec_msg,stdout);
  94.  
  95.  
  96.  goto ok;
  97. noesdi:
  98.     fputs("TESTPRI error: Can't run without EDOS & Windows\n", stdout);
  99.     return 1;
  100.  
  101. err:
  102.  
  103.     printf("%s \n", "Error: TstWinexe failed");
  104.     getch();
  105.     return 1;
  106. ok:
  107.  
  108. return result;
  109. }
  110.  
  111. unsigned long GetVxDAPI(void)
  112. {
  113.  
  114. #define EDOS_ID 0x2925
  115.  
  116. unsigned long vxd_api;
  117.     _asm {
  118.  
  119.     push es
  120.     mov di,0                        // make sure es,di are set to zero
  121.     mov es,di
  122.     mov ax,0x1684                    // the int 2f call number
  123.  
  124.     mov bx,EDOS_ID           //    edos VxD ID number
  125.  
  126.     int 0x2f
  127.     mov word ptr [vxd_api+2],es    // segment of v86 entry point in edos
  128.     mov word ptr [vxd_api],di        // offset of entry point
  129.     mov ax,es
  130.     or ax,di
  131.     pop es
  132.     mov al,10
  133.     jz noesdi
  134.    }
  135.     return vxd_api;
  136.  
  137. noesdi:
  138.     return 0L;
  139.  
  140. }
  141.  
  142.  
  143.  
  144.  
  145. int WinExec(
  146. unsigned long vxd_api,    // the entry point of the edos v86 api
  147. char *pname,        // the program to run, far pointer, maxlen=128
  148. char *argname,        // the argument string, far pointer, maxlen=127
  149. int show_num,        // SW_NORMAL, type number
  150. BOOL validate        // do extra validation, not implemented yet
  151. )
  152.  
  153. {
  154.  
  155. typedef struct {
  156.     WORD seg_env;
  157.     WORD off_arg;
  158.     WORD seg_arg;
  159.     WORD Shownum;  //off_fcb;
  160.     WORD seg_fcb;
  161.     DWORD dummy;
  162.  
  163. } exec_block;
  164.  
  165.  
  166. unsigned int len;
  167. exec_block parm;
  168.  
  169. char Arg_line[128+6];
  170. char FAR *arg_fptr, FAR *name_fptr;
  171. char *arg_ptr;
  172. int retval;
  173.  
  174.  
  175.     parm.seg_env = 0;    // clear the parm block
  176.     parm.off_arg = 0;
  177.     parm.seg_arg = 0;
  178.     parm.Shownum = 0;
  179.     parm.seg_fcb = 0;
  180.     parm.dummy = 0L;
  181.  
  182.     Arg_line[0] = 0;        // first byte of line contains the len, init to 0
  183.     arg_ptr = (char *)Arg_line;
  184.     arg_ptr++;        // make sure we point past the len byte
  185.  
  186.     arg_fptr = (char FAR*)Arg_line;    // make a far pointer to the arg line
  187.     parm.off_arg = FP_OFF(arg_fptr);
  188.     parm.seg_arg = FP_SEG(arg_fptr);
  189.  
  190.     strcpy(arg_ptr, argname);        // add the real arg data string
  191.     len = strlen(arg_ptr);            // get it's length
  192.  
  193. // len > 127 problems
  194.  
  195.  
  196.     Arg_line[0] = len;        // finish making the final arg line
  197.     arg_ptr[len] = 13;        // adde carriage return
  198.     arg_ptr[len+1]= 0;        // make sure the line is zero terminated
  199.  
  200.  
  201.     arg_fptr = (char FAR*)&parm;
  202.  
  203.     parm.seg_fcb = (WORD)FP_SEG(arg_fptr);
  204.  
  205.     name_fptr = (char FAR *)pname;
  206.  
  207.  
  208. // max len of pname and arg_line strings combined is 128
  209.  
  210.  
  211. _asm {
  212.  
  213.     les  bx, ss:[arg_fptr]
  214.     // these 3 variable names are ASSUMED to be on the stack
  215.  
  216.     lds  dx, ss:[name_fptr]
  217.  
  218.     mov  cx, ss:[show_num]
  219.  
  220.     mov  ax,3        //; exec windows app function call
  221.  
  222.    call vxd_api
  223.  
  224.     cmp ax, -1        // be sure didn't get original 3
  225.     jne err
  226.  
  227.     cmp bx, 32        // bx = the handle returned from WinExec
  228.     mov retval,bx
  229.     jg ok
  230.     mov retval,bx
  231.   }
  232.     return retval;
  233.  
  234.  
  235. err:
  236.     return -1;
  237.  
  238. ok:
  239.     return retval;
  240.  
  241. }
  242.