Page 107 - Open Soource Technologies 304.indd
P. 107

Unit 5: VB String and Operators



               ResString = String$(NumberOfCharacters + 2, vbNullChar)
               ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
               ’ Shorten the text with PathCompactPathEx
               ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
               Res = PathCompactPathEx(ResString, InputText, NumberOfCharacters, 0&)
               If Res = 0 Then
                ErrorNumber = Err.LastDllError
                ErrorText = GetSystemErrorMessageText(ErrorNumber)
                MsgBox “An error occurred with PathCompactPathEx” & vbCrLf & _
               ”Error Number: “ & CStr(ErrorNumber) & vbCrLf & _
               ”Descrition: “ & ErrorText
               ShortenTextToChars = vbNullString
               Exit Function
               End If

               ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
               ’ trim to get the charcters to the left
               ’ of the vbNullChar character.
               ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
               ResString = TrimToNull(Text:=ResString)
               ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
                return the result string
               ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
               ShortenTextToChars = ResString
               End Function

               5.1.7 TrimToNull
               When you are calling Windows API functions, you are often required to pass in a string variable
               that will be populated with the result of the function. An example is the  GetWindowsDirectory
               function. In these cases, you must first initialize the string to a given length (often the length
               is defined by the constant MAX_PATH = 260, a Windows-mandated value of the maximum length
               of a fully-qualified file name).  You can use either the String$ or Space$ function to  initialize
               the string to a given length:

               Public Const MAX_PATH = 260 ‘ Windows mandated value

               StrValue = String$(MAX_PATH,vbNullChar) ‘ initialize StrValue to MAX_PATH vbNullChars.
               ’ or
               StrValue = Space$(MAX_PATH) ‘ initialize StrValue to MAX_PATH spaces.
               When the API call returns, the buffer remains at its original length (the API functions never resize
               the result buffer). The API function will put a null character (Chr(0) or vbNullChar) at the end


                                      LOVELY PROFESSIONAL UNIVERSITY                        101
   102   103   104   105   106   107   108   109   110   111   112