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 aslong,_ 'length of string buffer
buf$ asptr,_ 'string buffer to receive path info
ret aslong'length of path, or length needed to hold path info
- 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.
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"foroutputas#f
print#f,"Testing temporary folder."close#f
'read text from file:open TempDir$ +"libertytest.txt"forinputas#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"endFunction GetTempPath$()CallDLL#kernel32,"GetTempPathA",_
0aslong,_
_NULL aslong,_
length aslong
buf$ =space$(length)CallDLL#kernel32,"GetTempPathA",_
length aslong,_
buf$ asptr,_
ret aslong
GetTempPath$ = buf$
EndFunction
Retrieving the Temporary Directory
-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.
-
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.
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.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.See also: GetTempFileNameA
Retrieving the Temporary Directory | Temporary Directory | GetTempPathA | Caveat | Demo