The MASM Forum Archive 2004 to 2012

Miscellaneous Forums => 16 bit DOS Programming => Topic started by: sihotaamarpal on July 18, 2006, 06:37:39 AM

Title: read executing program name with full path
Post by: sihotaamarpal on July 18, 2006, 06:37:39 AM
hi all
i want to read the program name with full path in 16 bit asm
plz help how i do this

example:
in command line argument like command>c:\abc\pro1.exe
or when run program want to fetch progname with full path

plz provide some code snippet


Title: Re: read executing program name with full path
Post by: sihotaamarpal on July 18, 2006, 06:25:09 PM
i try following code but it
prints the command line arguments not the file name

i want something like

void main(int argsc,char args[])
{
  printf("%s",args[0]);
getch();
}

when u run program just by clicking it prints the "filename with whole path"

MODEL small

STACK 256

DATASEG

CODESEG

START:


    MAIN PROC FAR
           
           PUSH ES    ; save es and
           PUSH DS    ; ds
           
           MOV AH,51H ;regeuest of PSP address into BX register
           INT 21H
           
           MOV ES,BX  ;copied PSP into es
           MOV DS,BX  ;and ds
           
           MOV AL,0DH  ;serach for <enter> char
           MOV CX,21   ;no of bytes
           MOV DI,82H  ;start address of PSP
           REPNZ SCASB ;sacn for <enter>
           
           JNZ EXIT_   ;not found error
           
           DEC DI      ;found
           
         
           MOV ES:BYTE PTR[DI],'$' ;repalce with $ char
           
           MOV AH,09H  ;request to print a command line
           MOV DX,82H  ;string addressed by ds:dx
           INT 21H
           
           POP DS
           POP ES
           
   
   EXIT_:
          MOV AX,4C00H
          INT 21H 
     
    MAIN ENDP
   
END MAIN
Title: Re: read executing program name with full path
Post by: MichaelW on July 19, 2006, 10:34:12 AM
I cannot recall any documented method of doing this. I think you can get the full path and filename from the program's environment block. You can get the segment address of the environment block from offset 2Ch in the PSP. Basically, you scan from the start of the environment block until you find the double null byte that marks the end of the last environment string. The next word following the double null specifies the number of items that follow, usually 1, and the next byte after this is the start of the null-terminated path.

Title: Re: read executing program name with full path
Post by: sinsi on July 19, 2006, 02:28:04 PM
All of this is for DOS 3+

Use INT 21, AH=62 to get the PSP segment in BX.
Get the WORD at BX:[2Ch], this is the environment segment.
Now envseg:0000 points to a structure...

(this is from Ralf Brown's Interrupt List 6.1)

Format of environment block:
Offset   Size   Description   (Table 01379)
00h  N BYTEs   first environment variable, ASCIZ string of form "var=value"
      N BYTEs   second environment variable, ASCIZ string
   ...
      N BYTEs   last environment variable, ASCIZ string of form "var=value"
   BYTE   00h
---DOS 3.0+ ---
   WORD   number of strings following environment (normally 1)
      N BYTEs   ASCIZ full pathname of program owning this environment   <-----------this is it
      other strings may follow

Just remember that the strings are zero-terminated, so the end of the list is 00 00.

This is what the C compiler does to give you the argc and argv (I don't know for
sure because I'm an asm programmer heh heh)