Reg Exp
Web Design
Notes Client
Get Windows Temporary Path
I've shown several examples on this web site of how to get the Notes data directory so it can be used as a temporary place to hold files. There is also a Windows temporary directory that can be used.

First, you have to declare the DLL function that will be used. This goes into the (Declarations) area of your agent or script library:

Declare Function GetTempPath Lib "kernel32.dll" Alias "GetTempPathA" _
(Byval nBufferLength As Long, Byval lpBuffer As String) As Long

Then, you just have to call that function in the "right way". That means that you have to obey some API rules when doing the call. I broke my code down into a self-contained function for reuse purposes:

Function GetTempDirectory() As String
   Dim buffer As String
   Dim bufferLen As Long

The first API rule we need to observe is that the string memory can't be allocated dynamically by the call. We have to allocate space for the string. 256 characters should be sufficient.

   buffer = Space$(256)

The next thing to do is call the function. Note that the return value from the function is the length of the string.

   bufferLen = GetTempPath(Len(buffer), buffer)

Since we allocated 256 characters and the path is most likely less than that, our code needs to trim off the extra white space. I make sure that the length returned is "in range" so an error won't happen.

   If bufferLen > 0 And bufferLen < 256 Then
      buffer = Left$(buffer, bufferLen)
   End If

Finally, API strings are usually null-terminated (character code 0). The string length returned won't include that, so the block above removed the null terminator. However, if the string length was out of range for some reason, then there still might be a null character in the buffer string. So check for that and keep only the characters to the left of the null character.

   If Instr(buffer, Chr$(0)) <> 0 Then
      GetTempDirectory = Left$(buffer, Instr(buffer, Chr$(0))-1)
      GetTempDirectory = buffer
   End If
End Function

That's everything. You should now be able to get the Windows Temp directory which can be used for temporarily creating files or doing some other file system work.