Older Version
Newer Version
Alyce
Feb 3, 2012
=Retrieving the Temporary Directory= //[[user:ChrisIverson]], [[user:Alyce]] // [[toc|flat]] ---- =Temporary Directory= Security measures in Windows prevents programs from writing files to all areas. To insure that your program can successfully write a temporary file on the user's computer, use the function GetTempPathA to retrieve the path to a directory where temporary files should be created. =GetTempPathA= The MSDN Library tells us this about GetTempPathA: //The GetTempPath function checks for the existence of environment variables in the following order and uses the first path found: The path specified by the TMP environment variable. The path specified by the TEMP environment variable. The path specified by the USERPROFILE environment variable. The Windows directory. // The function does not verify that the path exists, so that is left to the programmer. The first argument in the function is the length of the string buffer you are passing as a PTR. The second argument is a string buffer. You must create a string long enough to hold the temporary path information. If the function fails, it returns 0. If the function succeeds, the return value is the length of the string copied to the string buffer, not including the terminating null character. If the return value is greater than the length argument, the return value is the length of the string buffer required to hold the path. [[code format="lb"]] CallDLL #kernel32, "GetTempPathA",_ length as long,_ 'length of string buffer buf$ as ptr,_ 'string buffer to receive path info ret as long 'length of path, or length needed to hold path info [[code]] [[user:ChrisIverson]] first calls GetTempPathA with a length of 0 and a null pointer. This causes the function to return the number of characters needed for the string buffer. He then creates a buffer of the correct length with the **space$()** function. He then calls the function again with the proper length argument and a string buffer of the correct length passed as a pointer. When the function returns, the temporary path information is contained in the buffer. [[code format="lb"]] CallDLL #kernel32, "GetTempPathA",_ 0 as long,_ _NULL as long,_ length as long buf$ = space$(length) CallDLL #kernel32, "GetTempPathA",_ length as long,_ buf$ as ptr,_ ret as long [[code]] =Demo= This demo program retrieves the path to the temporary directory, writes a file to that directory, then reads the file. It deletes the file after use with the KILL statement. [[code format="lb"]] TempDir$ = GetTempPath$() print "Temporary directory on this computer is " print TempDir$ 'write a file: open TempDir$ + "libertytest.txt" for output as #f print #f, "Testing temporary folder." close #f 'read text from file: open TempDir$ + "libertytest.txt" for input as #f txt$ = input$(#f, lof(#f)) close #f print "Retrieving text from temp file. Text is:" print txt$ 'delete temp file: kill TempDir$ + "libertytest.txt" end Function GetTempPath$() CallDLL #kernel32, "GetTempPathA",_ 0 as long,_ _NULL as long,_ length as long buf$ = space$(length) CallDLL #kernel32, "GetTempPathA",_ length as long,_ buf$ as ptr,_ ret as long GetTempPath$ = buf$ End Function [[code]] ---- [[toc|flat]]