| | | | ' Allocate string buffer sText = String $ (256, vbNullChar) ' Save the BSTR and Unicode character array locations lngV = VarPtr(sText) lngS = StrPtr(sText) ' Search for window with a given class hnd = FindWindow("ThunderRT5Form", vbNullString) ' If window found, get title If hnd > 0 Then cTitle = GetWindowText(hnd, sText, 255) sText = Left $ (sText, cTitle) Debug.Print sText ' Compare the BSTR and character array locations ' to look for changes Debug.Print VarPtr(sText), lngV Debug.Print StrPtr(sText), lngS Else Debug.Print "No window with this class name.", vbInformation End If End Sub | | | | | | The output of one run is: | | | | | | RunHelp - Unregistered Copy - Monday, December 7, 1998 10:11:53 AM 1243480 1243480 2165764 2012076 | | | | | | (Don't worry this unregistered program is mine own.) | | | | | | We first allocate a string buffer for the window title. We will discuss this important point further in a moment. Then we use FindWindow to search for a window with class name ThunderRT5Form a VB5 runtime form. If such a window is found, its handle is returned in the hnd parameter. We can then call GetWindowText, passing it hnd as well as our text buffer sText and its size. Since the GetWindowText function returns the number of characters placed in the buffer, not including the terminating null, that is, the number of characters in the window title, we can use the Left function to extract just the title from the string buffer. | | | | | | Note also that we have saved both the BSTR address (in lngV) and the character array address (in lngS), so that we can compare these values to the same values after calling GetWindowText. Lo and behold, the BSTR has not moved, but its contents have changed, that is, the character array has moved, as we discussed earlier. | | | | | | Incidentally, since the returned string is null terminated and contains no embedded nulls, the following function also extracts the portion of the buffer that contains the title. This little utility is generic, and I use it often (in this book as well as in my programs). | | |