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>
' #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
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
We process personal data about users of our site, through the use of cookies and other technologies, to deliver our services, and to analyze site activity. For additional details, refer to our Privacy Policy.
By clicking "I AGREE" below, you agree to our Privacy Policy and our personal data processing and cookie practices as described therein. You also acknowledge that this forum may be hosted outside your country and you consent to the collection, storage, and processing of your data in the country where this forum is hosted.
Leave a comment: