Announcement

Collapse
No announcement yet.

Determining application's directory

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

  • Determining application's directory

    Hello

    I would like to know, after execution of a program starts,
    in what directory the executable program is located. I have
    thought about using CURDIR$ when the programa executes but if
    the program is started from a different directory then it won't
    work.

    Any suggestions are welcomed

    Paulo Jabardo


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

  • #2
    Try this

    Code:
    ' Returns the full path name of the running application.
    FUNCTION ExeName$ EXPORT
        LOCAL zTmp AS ASCIIZ * 256
        LenExeName& = GetModuleFileName(BYVAL %NULL, zTmp, SIZEOF(zTmp))
        IF LenExeName& THEN
           LenExeName& = MIN&(LenExeName&, SIZEOF(zTmp))
           FUNCTION = LEFT$(zTmp, LenExeName&)
        END IF
    
    END FUNCTION

    ------------------
    Patrice Terrier
    mailto[email protected][email protected]</A>

    [This message has been edited by Patrice Terrier (edited January 17, 2001).]
    Patrice Terrier
    www.zapsolution.com
    www.objreader.com
    Addons: GDImage.DLL 32/64-bit (Graphic library), WinLIFT.DLL 32/64-bit (Skin Engine).

    Comment


    • #3
      Paulo,

      Instead of hard coding the path length, use
      the equate %MAX_PATH as follows in Patrice's
      code example:

      LOCAL zTmp AS ASCIIZ * %MAX_PATH

      Should the max path length change in the future,
      your already covered. BTW %MAX_PATH = 260

      Another way of doing this is as follows:

      Code:
      lpCmdLn = GetCommandLine()
      Where lpCmdLn is pointer to a sz type string
      which is the full path specifier.
      Put this code in the top of the WinMain function.

      Cheers,
      Cecil

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




      [This message has been edited by Cecil Williams (edited January 18, 2001).]

      Comment


      • #4
        This is just what I was looking for (today).

        Patrice & Cecil,

        I'm assuming the functions you are using are windows API functions. If I were creating a program using the DDT (& PBMAIN()), would I still be able to call those functions just by including the Win32api.INC file?

        Also, do either of the API functions return the DRIVE & path or just the path? The reason I ask is because I'm writing an app which should be installed on a network server and I'll need to know the drive and path from any client machine that runs the program.

        Thanks.

        Bernard Ertl

        ------------------
        Bernard Ertl
        InterPlan Systems

        Comment


        • #5
          Paulo and Bernard,

          This should answer your question.

          The Functions can be installed in a DLL,
          or you can compile as EXE in PBCC20 or PBDLL.

          You will need to use MsgBox for testing if compiling with PBDLL.

          Phil

          Code:
          'Compile Test Code With PBCC
             '                         #COMPILE DLL
             #INCLUDE "Win32api.inc"
          
          DECLARE FUNCTION PxProgramName$ ()
          DECLARE FUNCTION PxFileSpecToken$ (BYVAL Which&, Spec$)
          
          
          FUNCTION PxProgramName$ ()            'EXPORT
             #REGISTER NONE
             LOCAL appFileName AS ASCIIZ * 511
             GetModuleFileName Hmod&=GEtModuleHandle(""),appFileName,511
             FUNCTION=(appFileName)
          END FUNCTION
          
          FUNCTION PxFileSpecToken$ (BYVAL Which&, Spec$)
          
              'Which& determines what part OF the path is extracted:
              '    0: Return All Back Does Nothing, default if which& <1 or >8
              '    1: Disk only (AS D   [img]http://www.powerbasic.com/support/forums/smile.gif[/img]
              '    2: Path only
              '    3: Disk + Path
              '    4: BASE file NAME
              '    5: File extension
              '    6: File NAME + extension
              '    7: Disk + Master Path Only
              '    8: Master Path Only
                 #REGISTER NONE
                 LOCAL s$(),p&,sp$
                 DIM s$(0 TO 8)
                 IF which& < 1 OR which& > 7 THEN which&=0
                 sp$=Spec$:s$(0)=sp$
                 p& = INSTR(1,sp$,":")  ' GET DRIVE
                 IF p& THEN s$(1)=LEFT$(sp$,p&):sp$=MID$(sp$,p&+1)
                 P& = INSTR(-1,sp$,"\")
                 IF p& THEN s$(2) = LEFT$(sp$,p&):sp$=MID$(sp$,p&+1)
                 IF LEN(s$(2)) THEN s$(8)=LEFT$(s$(2),LEN(s$(2))-1)
                 s$(3)=s$(1)+s$(2)
                 p& = INSTR(1,s$(3),"\")
                 IF p& THEN s$(7)= LEFT$(s$(3),p&)    ' disk+master path
                 p& = INSTR(-1,sp$,".")
                 IF p& THEN
                    s$(5)=TRIM$(MID$(sp$,p&+1))
                    s$(4)=TRIM$(LEFT$(sp$,p&-1))
                    s$(6)=s$(4)+"."+s$(5)
                 ELSE
                    s$(4)=sp$:s$(6)=sp$
                 END IF
                 FUNCTION = s$(Which&)
          END FUNCTION
          
          FUNCTION PBMAIN() AS LONG
              LOCAL I&
              P$=PxProgramName$
              CLS
              PRINT "Program Path and Name is > "p$
              FOR I& = 1 TO 8
                  PRINT i&;TAB(10);PxFileSpecToken$(i&,p$)
              NEXT
              PRINT
                 ' just a test to see if PxFileSpecToken$(2,p$) works with \\server
              p$=LEFT$(p$,3)+"\Server\"+MID$(p$,4)
              d$=PxFileSpecToken$(2,p$)
              PRINT "Full Program Path ......> "d$
          
              LINE INPUT "Press Any Key To Continue ";a$
          
          END FUNCTION
          ------------------




          [This message has been edited by Phil Tippit (edited January 20, 2001).]
          E-Mail:
          pt AT pursuersoft DOT com

          Comment


          • #6
            I just tried using Patrice's code and it worked fine in my DDT app. Thank you. It even returned the drive.

            Note to PB --> There seems to be some duplicate definitions in the win32api.inc file and the ddt.inc file distributed with PB DLL 6.0. Is one supposed to supercede the other? Are the duplicate equate definitions the same? I had to comment out the duplicates in order to use both .INC files. Right now, everything works fine, but I am wondering if I may run into a problem later because of my editing...

            Bernard Ertl

            ------------------
            Bernard Ertl
            InterPlan Systems

            Comment


            • #7
              Hello guys. You have solved my problem all right, thanks

              Bern, as far as I know, the DDT.INC file is a small part of the
              WIN32API.INC file, so if you include WIN32API.INC you do not
              have to include DDT.INC. I hope this helps.

              Paulo

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

              Comment


              • #8
                Paulo,

                This inline asm function has been working reliably for a couple of years so
                it may be a small code replacement that will work for you.

                It returns the path of the running application minus the file name.

                Regards,

                [email protected]

                Code:
                ' #########################################################################
                  
                FUNCTION AppPath() as STRING
                  
                    LOCAL PathBuffer as ASCIIZ * 128
                  
                    GetModuleFileName ByVal %NULL,PathBuffer,128
                    adr& = VarPtr(PathBuffer)
                    var& = 0
                  
                      ! xor ecx, ecx
                      ! xor edx, edx
                      ! mov esi, adr&
                    bfrStart:
                      ! lodsb
                      ! inc ecx
                      ! cmp al, 0     ; exit on zero
                      ! je bfrOut
                      ! cmp al, "\"
                      ! jne bfrStart
                      ! mov edx, ecx  ; put position of ecx count in edx
                      ! jmp bfrStart
                    bfrOut:
                      ! mov var&, edx
                  
                    FUNCTION = lcase$(left$(PathBuffer,var&))
                  
                END FUNCTION
                
                ' #########################################################################
                ------------------


                [This message has been edited by Steve Hutchesson (edited January 20, 2001).]
                hutch at movsd dot com
                The MASM Forum - SLL Modules and PB Libraries

                http://www.masm32.com/board/index.php?board=69.0

                Comment


                • #9
                  Steve, I love your ASM samples - many thanks! But, what happens
                  if path exceeds 128 bytes? Not usual, but could happen, you know.


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

                  Comment


                  • #10
                    Thanks Steve, your code works great too.

                    Borje, you can change the constants from 128 to 256 if you like.

                    Bernard Ertl

                    ------------------
                    Bernard Ertl
                    InterPlan Systems

                    Comment


                    • #11
                      Note to PB --> There seems to be some duplicate definitions in the win32api.inc file and the ddt.inc file distributed with PB DLL 6.0. Is one supposed to supercede the other? Are the duplicate equate definitions the same? I had to comment out the duplicates in order to use both .INC files. Right now, everything works fine, but I am wondering if I may run into a problem later because of my editing...
                      DDT.INC is indeed a subset of WIN32API.INC. If you need to use WIN32API.INC, then remove the inclusion of #INCLUDE "DDT.INC" from your code.

                      PS: Windows defines a path for be a maximum of %MAX_PATH characters in length, which equals 260. Therefore, if you DIM your path string to 256, there is a small possibility of failure since your buffer is 4 characters too small.

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

                      Comment

                      Working...
                      X