Retrieving the Temporary Directory

- ChrisIverson ChrisIverson, - Alyce Alyce
Retrieving the Temporary Directory | Temporary Directory | GetTempPathA | Caveat | Demo

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.


    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

- ChrisIverson 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.

    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

Caveat

The documentation for Liberty BASIC specifies that a string passed "as ptr" is passed by reference only if the programmer adds a null terminator. We have found this to be unnecessary for recent versions of Liberty BASIC. You may, if you'd like, add the null termination to the buffer string in the following way.

    buf$ = space$(length) + chr$(0)

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.
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

See also: GetTempFileNameA

Retrieving the Temporary Directory | Temporary Directory | GetTempPathA | Caveat | Demo