Domino Code Fragment

Code Name*
Converting C Strings To Lotusscript Strings
Date*
04/28/2024
Source (or email address if you prefer)*
Rlatulippe@romac.com
IP address:.18.188.40.207
Description*
When calling Windows API functions, you usually have to pass in a fixed length buffer in which the API call returns a null-terminated (C style) string. To convert this string back to a normal LotusScript string, use the Instr function to search for Chr$(0).
Type*
LotusScript
Categories*
(Misc)
Implementation:
Required Client:
Server:
Limitations:
Comments:
Files/Graphics attachments (if applicable): Code:

When calling Windows API functions, you usually have to pass in a fixed length buffer in which the API call returns a null-terminated (C style) string. To convert this string back to a normal LotusScript string, use the Instr function to search for Chr$(0).


For instance, calling:


Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (Byval nBufferLength As Long, Byval lpBuffer As String) As Long
Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" (Byval lpszPath As String, Byval lpPrefixString As String, Byval wUnique As Long, Byval lpTempFileName As String) As Long


Sub DoIt()
  Dim tempPath As String * 256
   Dim TempFileName As String * 260
   Call GetTempPath(256, tempPath)
   Call GetTempFileName(tempPath, "SM", 0, TempFileName)
   tempFileName = tempFileName & ".123" ' to add a .123 extension to the tempfile
  Print tempFileName
End Sub


... does not do what you'd expect. Instead, the .123 extension is appended after the 260th character. Instead

Sub DoIt()
  Dim tempPath As String * 256
   Dim cTempFileName As String * 260
   Dim tempFileName As String
   Call GetTempPath(256, tempPath)
   Call GetTempFileName(tempPath, "SM", 0, cTempFileName)
   tempFileName = Left(cTempFileName, Instr(cTempFileName, Chr$(0)) - 1) & ".123"
  Print tempFileName
End Sub


... is needed to chop out the null space from the buffer.