Hackers Guide to Visual FoxPro 7.0
Program(), Sys(16), AStackInfo()
These functions tell you about the program currently executing and the execution chain. They're especially handy when you're debugging.| Usage | nProgDepth = PROGRAM( -1 ) cProgInChain = PROGRAM( [ nLevel ] ) cProgInChain = SYS( 16 [ , nLevel ] ) |
| | SYS(16, 1) is the key to finding out how you got started. It returns the name of the APP or EXE file that was run (assuming that's how you ran the application). This is especially important when working with apps that have a form marked as main—in that case, the file portion of SYS(16) indicates the SCT, not the APP or EXE. |
PROGRAM(–1), added in VFP 6, tells you how deep you are in the program chain. Before that, you had to test for EMPTY(PROGRAM()) to know when you'd reached the end. Watch out for one thing here—PROGRAM(–1) returns a numeric value, while passing any other value gives a character return.
SYS(16) is incredibly useful for locating files in an interactive development environment. Doug often builds tools that need to open a table or access some other file that belongs to the tool. Although all these files are in the same directory, it isn't usually the current directory when the tool is running, so code like USE Table or LOADPICTURE(SomePicture) bombs out. To avoid hard-coding paths, he uses code like:lcDir = JustPath(SYS(16)) && Or in VFP 5 and before, lcDir = LEFT(SYS(16), AT('\', SYS(16))) USE (lcDir + 'MYTABLE') The example below is a function that traces the entire execution chain and fills an array with the results. In VFP 6 and earlier versions, you might call this function from an error handler to determine what was going on when the program failed. In VFP 7, you don't need this function; use ASTACKINFO(), instead.
| Example | * AProgram.PRG * Fill an array with the programs in use, except this one. * Column 1 contains the program name returned by PROGRAM(). * Column 2 contains the detailed information returned by * SYS(16). * Return the size of the array. * If parameter isn't an array, return -1. LPARAMETERS aProgs * Check parameter IF TYPE("aProgs[1]")="U" RETURN -1 ENDIF * Trace the calling chain. LOCAL nLevel FOR nLevel = 1 TO PROGRAM(-1) DIMENSION aProgs[nLevel,2] aProgs[nLevel,1] = PROGRAM(nLevel) aProgs[nLevel,2] = SYS(16,nLevel) ENDFOR * Now remove last row which represents this program. DIMENSION aProgs[nLevel-2,2] RETURN nLevel-2 |
| Usage | nLevels = ASTACKINFO( aProgChain ) |
| Column | Contents |
| 1 | The stack level. |
| 2 | The name of the file executing at this level. |
| 3 | The name of the module or object executing at this level. |
| 4 | The name of the source code file for the code executing at this level. |
| 5 | The line number executing at this level. |
| 6 | The line of code executing at this level. |
| | When you're running an APP or EXE, the second column of the array should contain the name of the APP or EXE in every row. When you call AStackInfo() from a form or class, the second column contains the name of the SCT or VCT, instead. |
| See Also | Array Manipulation, Do, On Error, Set FullPath, StartMode |
View Updates
Copyright © 2002 by Tamar E. Granor, Ted Roche, Doug Hennig, and Della Martin. All Rights Reserved.
Категории