home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / advos2 / ch12 / exec3.asm < prev    next >
Encoding:
Assembly Source File  |  1988-12-12  |  4.9 KB  |  124 lines

  1.         title   EXEC3.ASM Demo of DosStartSession
  2.         page    55,132
  3.         .286
  4. ; EXEC3.ASM  Demonstration of DosStartSession, runs CMD.EXE
  5. ;            in a new background session to display a directory,
  6. ;            then waits for a keypress before exiting.
  7. ;            You can use the Task Manager to switch to the
  8. ;            new session and end that session by entering EXIT.
  9. ;            Child session will also be terminated unilaterally
  10. ;            when you press a key to terminate this process.
  11. ; Copyright (C) 1987 Ray Duncan
  12. ;
  13. ; Build:        MASM EXEC3;
  14. ;               LINK EXEC3,,,OS2,EXEC3
  15. ;
  16. ; Usage:        EXEC3
  17. ;               
  18. ; Note:         File CMD.EXE must be available in PATH.
  19.  
  20. stdin   equ     0                       ; standard input handle
  21. stdout  equ     1                       ; standard output handle
  22. stderr  equ     2                       ; standard error handle
  23.  
  24. cr      equ     0dh                     ; ASCII carriage return
  25. lf      equ     0ah                     ; ASCII line feed
  26.  
  27.         extrn   DosExit:far             ; references to OS/2 API
  28.         extrn   DosStartSession:far
  29.         extrn   DosWrite:far
  30.         extrn   KbdCharIn:far
  31.  
  32. DGROUP  group   _DATA
  33.  
  34. _DATA   segment word public 'DATA'
  35.  
  36. argstr  db      ' /k dir/w',0           ; argument string for child
  37. pname   db      'cmd.exe',0             ; pathname for child
  38. stitle  db      'Child Command Processor',0
  39.  
  40. childID dw      0                       ; child process ID
  41. sesID   dw      0                       ; child session ID
  42.  
  43.                                         ; session data...
  44. sinfo   dw      24                      ; length of structure
  45.         dw      1                       ; 0=unrelated, 1=related
  46.         dw      1                       ; 0=foreground, 1=background
  47.         dw      0                       ; 0=nontraceable, 1=traceable
  48.         dd      stitle                  ; session title
  49.         dd      pname                   ; program pathname
  50.         dd      argstr                  ; argument strings
  51.         dd      0                       ; termination queue (not used)
  52.  
  53. kbdinfo db      10 dup (0)              ; character input data
  54. wlen    dw      ?                       ; receives actual bytes written
  55.  
  56. msg1    db      cr,lf,'Unexpected OS/2 Error',cr,lf
  57. msg1_len equ $-msg1
  58.  
  59. msg2    db      cr,lf,'Press any key to terminate parent...',0
  60. msg2_len equ $-msg2
  61.  
  62. _DATA   ends
  63.  
  64.  
  65. _TEXT   segment word public 'CODE'
  66.  
  67.         assume  cs:_TEXT,ds:DGROUP
  68.  
  69. main    proc    far
  70.  
  71.                                         ; run CMD.EXE in a new
  72.                                         ; session as a child process
  73.         push    ds                      ; address of session data
  74.         push    offset DGROUP:sinfo
  75.         push    ds                      ; receives new session ID
  76.         push    offset DGROUP:sesID
  77.         push    ds                      ; receives child process ID
  78.         push    offset DGROUP:childID
  79.         call    DosStartSession         ; transfer to OS/2
  80.         or      ax,ax                   ; did call succeed?
  81.         jnz     error                   ; jump if function failed
  82.  
  83.                                         ; display message,
  84.                                         ; 'Press any key'...
  85.         push    stdout                  ; standard output handle
  86.         push    ds                      ; address of message
  87.         push    offset msg2
  88.         push    msg2_len                ; length of message
  89.         push    ds                      ; receives bytes written 
  90.         push    offset wlen
  91.         call    DosWrite                ; transfer to OS/2
  92.  
  93.                                         ; now wait for key...
  94.         push    ds                      ; receives keyboard data
  95.         push    offset DGROUP:kbdinfo
  96.         push    0                       ; 0=wait for character
  97.         push    0                       ; keyboard handle
  98.         call    KbdCharIn               ; transfer to OS/2
  99.  
  100. exit:                                   ; final exit to OS/2...
  101.         push    1                       ; terminate all threads
  102.         push    0                       ; return success code
  103.         call    DosExit                 ; exit program
  104.  
  105. error:                                  ; display error message
  106.         push    stdout                  ; standard output handle
  107.         push    ds                      ; message address
  108.         push    offset msg1
  109.         push    msg1_len                ; message length
  110.         push    ds                      ; receives bytes written
  111.         push    offset wlen
  112.         call    DosWrite                ; transfer to OS/2
  113.  
  114.         push    1                       ; terminate all threads
  115.         push    1                       ; return error code
  116.         call    DosExit                 ; exit program
  117.  
  118. main    endp
  119.  
  120. _TEXT   ends
  121.  
  122.         end     main
  123.