Announcement

Collapse

New Sub-Forum

In an effort to help make sure there are appropriate categories for topics of discussion that are happening, there is now a sub-forum for databases and database programming under Special Interest groups. Please direct questions, etc., about this topic to that sub-forum moving forward. Thank you.
See more
See less

Detect running on UNC drive?

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

  • Detect running on UNC drive?

    Hi folks,

    Does anyone here have a way to determine if a PB/DOS app is running from a non-mapped drive? ie, if I launch a DOS executable from a shared network drive, what is the easiest way to detect that the drive is not mapped to a drive letter?

    On my Win2K system, launching a PB/DOS EXE directly from a network drive, CURDIR$ reports "C:\". In the same test, a PB/DLL 2.0 app returns "I:\WINNT\TEMP"... these apps were launched from \\server\misc\noname.exe

    As you can see, I also need a solution for a PB/DLL 2.0 application too...

    Any ideas out there?


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

  • #2
    Ahha! I solved it for the PB/DOS app by testing the home path of the EXE... I just test the return string, and if it starts with a double backslash, then I know it is a non-mapped drive.
    Code:
    sub ExeWhere(ExeName$, ExePath$) local public
    ' PATH.BAS - Sub to locate where the current EXE was run from...
    ' This code could be optimized a bit but works reliably as-is.
    ' Upon return, ExeName$ contains the name of the EXE, and ExePath$ holds the path.
    ' in the case of a UNC drive, ExePath will contain something like "\\server\share"
     
        local PspSeg??, EnvSeg??, EnvOff??, Char?
     
        REG 1, &H6200               'get PSP location
        CALL INTERRUPT &H21          'call DOS
        PspSeg?? = BITS??(REG(2))   'returned in BX
     
        '  Use the PSP segment to find the environment segment.  It's location is at
        '  offset &H2C in the PSP.
     
        DEF SEG = PspSeg??
        EnvSeg?? = bits??(PEEKI(&H2C))
     
        '  Now that we have found the location of our environment, find the end of
        '  the environment which is indicated by two nulls (CHR$(0)'s).
     
        DEF SEG = EnvSeg??
        EnvOff?? = 0
        WHILE istrue PEEKI(EnvOff??)   'short cut for checking two consecutive
                                            'bytes to see if they are both zero
                INCR EnvOff??
            WEND
     
        '  Now EnvOff?? points to the last two bytes of the environment variable
        '  table, the name and location of the EXE is only four bytes away:
     
        INCR EnvOff??, 4
     
        ' Now we can read the EXE name into a string, just keep reading bytes until
        '  we hit another null (CHR$(0)):
     
        Char? = PEEK(EnvOff??)
        WHILE Char? > 0
            ExeName$ = ExeName$ + CHR$(Char?)
            INCR EnvOff??
                Char? = PEEK(EnvOff??)
        WEND
        ExePath$ = ""
        x% = tally(ExeName$, "\")
        for y% = 1 to x%
            ExePath$ = ExePath$ + left$(ExeName$, instr(ExeName$, "\"))
            ExeName$ = mid$(ExeName$, instr(ExeName$, "\") + 1)
        next y%
            if len(ExePath$) > 3 then ExePath$ = left$(ExePath$, len(ExePath$) - 1)     ' remove last "\"
            ' Now ExeName$ = filename & ExePath$ = Drive + Path
    end sub
    I still need a PB/DLL 2.0 solution though...


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

    Comment


    • #3
      Lance,

      Why shouldn't GetModuleFileName return the information you
      need. MSDN states that GetModuleFileName returns the
      full path and program name of the current program.
      They also state that it work in 95,98, nt4, nt5, 2000.

      Check this out and let me know the results.
      The UNC name should be returned by PxFileSpecToken$(2,p$)
      as in the example.

      Compile this example with PBCC20, it will also work as
      a DLL in PBDLL60.

      Code:
         '                         #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  
          '    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 October 30, 2000).]
      E-Mail:
      pt AT pursuersoft DOT com

      Comment


      • #4
        Thanks Phil... that obvious solution evaded me at 2am when I needed a solution... Edwin Knoppert kindly gave me the nudge I needed a couple of days ago in the PB/DLL forum.

        Note that I needed a 16-bit solution, and your code was for 32-bit, but the GetModuleFileName() API was what I had a mental block about...

        Thanks again!

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

        Comment

        Working...
        X