Handling NULL terminated strings in Visual Basic when using LANSA Open

Date:Archived
Product/Release:LANSA Open - All Versions
Abstract:When using DLLs with Visual Basic you need to take special care of strings because Visual Basic cannot handle strings terminated by a NULL character and all DLLs terminate strings this way.
Submitted By:N/A

Like all DLLs LANSA Open uses 0 as the terminating character for strings. This however causes Visual Basic a problem as it does not recognize 0 as the terminating character in a string. Therefore you will have to remove the 0 character yourself in Visual Basic applications.

The following sample function sTrim removes any leading or trailing blanks from a given string as well as the 0 terminating character of a string.

Note: It is more efficient for sTrim to pre-allocate strings with nulls, rather than spaces.

Function sTrim(s As String) As String
     ' this function trims a string of right and left spaces
     ' it recognizes 0 as a string terminator
     Dim i As Integer
     i = InStr(s, Chr$(0))
     If (i > 0) Then
         sTrim = Trim(Left(s, i - 1))
     Else
         sTrim = Trim(s)
     End If
End Function