Who are my parents? 1/2

 BBS: Inland Empire Archive
Date: 09-05-92 (18:28)             Number: 322
From: LOGAN ASHBY                  Refer#: NONE
  To: FRANCOIS ROY                  Recvd: NO  
Subj: Who are my parents? 1/2        Conf: (2) Quik_Bas
04 Sep 92, Francois Roy writes to All:

 FR> Is it possible for a spawned program (via SHELL or more importantly
 FR> via QBSWAP) to know the name of the program which spawned it? (or
 FR> even to know that it is a shelled program and not a "top-level"
 FR> program?) How?

 Francois, I haven't tried this code snippet with QBSwap, but I think it
should work.  If you have any questions on how, shoot me
another message, and I'll go into more detail on it.
'SHELL'ed programs will report the parent process as
COMMAND.COM, as it gets loaded to run them.

 ' ----------------------- PrntName.bas -------------------------
 '|  This program gets the name of the parent process of the     |
 '|  current program.                                            |
 '|  Released into the public domain, 9/5/92 by Logan Ashby      |
 ' --------------------------------------------------------------
 ' $INCLUDE: 'QBX.BI'

 DECLARE FUNCTION GetProgName$ (PSPSeg%)
 DECLARE FUNCTION GetPSP% ()

 PSPSeg% = GetPSP%                   '| Get Current PSP
 DEF SEG = PSPSeg%
                                     '| Get Parent's PSP from offset
                                     '|   &H16 in the current PSP
 PrntPSP% = (PEEK(&H17) * 256) + PEEK(&H16)
 ProgName$ = GetProgName$(PrntPSP%)  '| Get Prg Name
 PRINT "ProgName$ = "; ProgName$
 END

 ' ---------------------- GetProgName$ --------------------------
 '|  Takes a PSP segment and uses it to search the local env.    |
 '|  table for the program's name.                               |
 ' --------------------------------------------------------------
 FUNCTION GetProgName$ (PSPSeg%)

 DEF SEG = PSPSeg%
     '| If the parent PSP Seg of the input PSP = 0 or points to
     '|   itself, then the parent is the command processor, but
     '|   the name won't always be after the environment table.
     '|   We handle this by getting the name from the COMSPEC
     '|   environment variable.
 TestPSPSeg% = (PEEK(&H17) * 256) + PEEK(&H16)
 IF (TestPSPSeg% = PSPSeg%) OR (TestPSPSeg% = 0) THEN
     GetProgName$ = ENVIRON$("COMSPEC")
     EXIT FUNCTION
 END IF

     '| Get the Segment for the Environment
 ENVSeg% = (PEEK(&H2D) * 256) + PEEK(&H2C)

 DEF SEG = ENVSeg%

 Done% = 0
 EnvPtr% = -1
 FirstNulFlag = 0
 TmpName$ = ""

 WHILE NOT Done%                     '| Search for 2 consecutive
     EnvPtr% = EnvPtr% + 1           '|   0 bytes. (End of Env.
     EnvChar% = PEEK(EnvPtr%)        '|   table.)
     IF EnvChar% THEN
         IF FirstNulFlag% THEN
             FirstNulFlag% = 0       '| Found a single nul char,
         END IF                      '|   reset flag.
     ELSE
         IF FirstNulFlag% THEN
             Done% = -1              '| Found end of Env. table
             EnvPtr% = EnvPtr% + 3   '| Skip next two bytes
             EnvChar% = PEEK(EnvPtr%)
             WHILE EnvChar%          '| Build Prog Name $
                 EnvPtr% = EnvPtr% + 1
                 TmpName$ = TmpName$ + CHR$(EnvChar%)
                 EnvChar% = PEEK(EnvPtr%)
             WEND
         ELSE
             FirstNulFlag% = -1      '| Set flag showing we found
         END IF                      '|   the first 0 byte
     END IF
 WEND

 DEF SEG

 GetProgName$ = TmpName$

 END FUNCTION

 ' ___ continued next message ___

--- GoldED 2.40
 * Origin: Big Nerd's Nest  (1:398/1.2)
Outer Court
Echo Basic Postings

Books at Amazon:

Back to BASIC: The History, Corruption, and Future of the Language

Hackers: Heroes of the Computer Revolution (including Tiny BASIC)

Go to: The Story of the Math Majors, Bridge Players, Engineers, Chess Wizards, Scientists and Iconoclasts who were the Hero Programmers of the Software Revolution

The Advent of the Algorithm: The Idea that Rules the World

Moths in the Machine: The Power and Perils of Programming

Mastering Visual Basic .NET