Announcement

Collapse
No announcement yet.

return values from shelled programs

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • return values from shelled programs

    Does anyone know how/if a program can get the integer returned
    by a program when using SHELL to execute the program (using
    pb 3.1)?

    For example:
    'Program x launches program y.
    shell "\bin\y.exe"

    'Program y returns 1 (END 1).
    'How does program x retrieve this value?

    Thanks in advance,

    Dave

  • #2
    Personally I use UltraShell from www.infoms.com which moves all but approx 5kb out to EMS or a disk file before the SHELL, and if memory serves me correctly, it can also report the ERRORLEVEL set by the child application.

    Note: you can retrieve an ERRORLEVEL from DOS and PB/CC applications, but it won't work for GUI Windows applications that are launched, since they become launched as a separate Process.

    There is some code to do this in the ABC Packets site, but you'll have to translate this to PB... http://codesearch.hypermart.net/searchabc/searchabc.pl?Type=Code+Sear ch&Code=All&Text=errorlevel&Bool=AND

    There are other low-tech techniques too, such as using a BATch file which tests ERRORLEVEL values... workable, but... UGH!

    I may also have some code for this on my DEV PC's archives - I'll check when I get back and post anything I can find.

    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>
    Lance
    mailto:[email protected]

    Comment


    • #3
      Ok, here is what I dug up from my archive... I have never actually used this code, and I did not write it. It is described as freeware.

      Code:
      $If 0
      --------------------------------------------------------------------------
      EXEC function by Keith Foster, October 9, 1995.
       
      Executes a child process from within a PowerBasic program WITHOUT opening a
      new copy of COMMAND.COM.  This allows you to read errorlevels from the child
      process.  Memory handling could be more precise, but for the sake of example,
      I used a 400K block for child process memory.  To use this within your
      program, you must take care to allow enough memory for your program AND the
      child process.
       
      The proper execution of this function would not have been possible without
      the assistance of my technical director, Greg Pencheff.  So I mentioned his
      name.
       
      This function is provided as freeware.  Have fun and keep the crashes to a
      minimum!
       
      NOTE:  DO NOT RUN THIS FROM WITHIN THE POWERBASIC IDE.  COMPILE TO AN EXE
      AND RUN FROM THE DOS PROMPT.  THE IDE MAY NOT PROVIDE ENOUGH RAM TO SUPPORT
      THIS FUNCTION!
      --------------------------------------------------------------------------
      $EndIf
       
      Declare Function Exec%(PrgName as String,PrgPrms as String)
       
      Type LoadExec
          LEEnvironment as Word
        LECmdTailOfs as Word
        LECmdTailSeg as Word
        LEFCB1 as DWord
        LEFCB2 as DWord
      End Type
       
      Dim ErCode as Integer, Y as Long
       
      Y=SetMem(-409600)
       
      'Frees 400K of heap space to run child program.  You may need more memory
      'for your program.  Set this number to allocate the number of bytes your
      'child process needs.  The SETMEM function adds the passed number to the
      'amount of far heap your program allocates.  If you pass a negative number,
      'you are in effect subtracting available RAM from your program.  Be careful
      'with this statement.
       
      'The following line runs PKZIP and attempts to zip all .EXE files in your
      'WINDOWS subdirectory.  As you can see, the parameters are passed in the
      'second string.  The first string defines the .EXE, .COM, or .BAT file to
      'execute.  ErCode returns the errorlevel from the child process.  You can
      'use this to test for whatever.
       
      ErCode = Exec%("D:\UTILS\PKWARE\PKZIP.EXE","-aex c:\temp\test c:\windows\*.ejj")
      Print "Errorlevel returned:";ErCode
       
      Function Exec%(PrgName as String,PrgPrms as String) Public
      Dim Parms as LoadExec
      Reg 1,&H4800
      Reg 2,&H1000
      Call Interrupt &H21
       
      'The strings passed to the EXEC interrupt must be ASCIIZ strings.  This means
      'terminated with a null character.
      PrgName = PrgName + Chr$(0)
      PrgPrms = PrgPrms + Chr$(0)
       
      'This statement tells the EXEC interrupt that you would like to use the
      'default DOS environment.  If you need a different environment string, you
      'must set this up yourself.  A DOS Reference Manual will be mandatory!
      Parms.LEEnvironment=0
       
      'The CmdTailSeg and CmdTailOfs variables hold the parameter string.
      Parms.LECmdTailSeg=StrSeg(PrgPrms)
       
      'I don't know why the subtraction is necessary in the following statement,
      'but if you don't subtract 1 here, the first character of the parameter
      'string will be cut off.  Go figure.
      Parms.LECmdTailOfs=StrPtr(PrgPrms)-1
       
      'The following statement block sets up the 'EXEC' DOS Interrupt.
      Reg 8,StrSeg(PrgName)'ds
      Reg 4,StrPtr(PrgName)'dx
      Reg 9,VarSeg(Parms)'es
      Reg 2,VarPtr(Parms)'bx
      Reg 1,&H4b00
      Call Interrupt &H21
       
      'The following statement block reads the return code from the child process.
      Reg 1,&H4D00
      Call Interrupt &H21
       
      'The return code is now in AL.  The following statement extracts AL from AX.
      Exec%=Reg(1) mod 256
      End Function

      ------------------
      Lance
      PowerBASIC Support
      mailto:[email protected][email protected]</A>
      Lance
      mailto:[email protected]

      Comment


      • #4
        Here is another... This code may actually be public domain rather than freeware...
        Code:
        'Pbexec.bas returns an errorlevel from a shelled program with DOS EXEC Function
        'Written By Ethan Winer for Microsoft Basics
        'Converted to Power basic 3.0 by Gary Blydenburgh
        'Errorlevel = PBEXEC(Program$,Parameter$)
         
        FUNCTION PBEXEC (Program$,par$) STATIC
          dim block as string * 14
          dim parm as string * 50
          zbuffer$=program$+chr$(0)
          lset parm$ = chr$(len(par$))+par$+chr$(13)
          lset block$ = chr$(0)+chr$(0)+mki$(varptr(parm$))+MKI$(varseg(parm$))
          dummy&=setmem(-500000)
         
          reg 1, &H4B00
          reg 9,varseg(block$)
          reg 4, strptr(zbuffer$)
          reg 8, strseg(zbuffer$)
          reg 2,varptr(block$)
          call interrupt &H21
         
          if reg(0) and 1 then
            pbexec=255 'Errorlevel is 255 for and DOS Error
            select case reg (1)
              case 1,2,3,5,8,10,11
              dummy&=setmem(500000)
              exit function
            end select
          end if
         
          reg 1, &H4D00
          call interrupt &H21
          pbexec=reg(1)
          dummy&=setmem(500000)
        END FUNCTION

        ------------------
        Lance
        PowerBASIC Support
        mailto:[email protected][email protected]</A>
        Lance
        mailto:[email protected]

        Comment


        • #5
          David, you may try changing your number to a string using
          STR$() and write it to a file and when your shelled program
          is through and your are back in you first program, you can
          load it from the file and change it back to an integer using
          VAL()



          ------------------

          Comment

          Working...
          X