get last line

i have this function to get the last line from pal room..it work fine under compile target net framework 3.5 . but when i compile the app for target framework 4
some times it return value as text and other times it dosn’t return text at all…any explnation or how to make it work for net framework 4

 

Private Function GetLastLineTextChat(ByVal hwnd As IntPtr, ByRef iLastLine As Integer) As String()
Dim lngCount As Integer
Dim lngLength As Integer
Dim strBuffer As New StringBuilder(255)
Dim sText() As String = Nothing
'Get Line count
lngCount = SendMessage(hwnd, EM_GETLINECOUNT, 0, 0)
lngLength = SendMessage(hwnd, EM_LINELENGTH, lngCount - 2, 0)

'get line text
If iLastLine = 0 Then iLastLine = lngCount
If lngCount > iLastLine Then
Do While iLastLine <= lngCount
iLastLine = iLastLine + 1
If sText Is Nothing Then
ReDim sText(0)
Else
Dim iCount As Integer = sText.Length
ReDim Preserve sText(iCount)
End If

'get line text
Call SendMessageString(hwnd, EM_GETLINE, iLastLine - 2, strBuffer)
sText(sText.Length - 1) = strBuffer.ToString
If iLastLine = lngCount Then
Exit Do
End If
Loop
ElseIf iLastLine <> lngCount Then
iLastLine = lngCount
ReDim sText(0)
'get line text
Call SendMessageString(hwnd, EM_GETLINE, iLastLine - 2, strBuffer)
sText(sText.Length - 1) = strBuffer.ToString
ElseIf iLastLine = lngCount Then
End If
Return sText
End Function

 

3 thoughts on “get last line

  1. What does this mean


    sText(sText.Length - 1) = strBuffer.ToString

    and this


    Dim iCount As Integer = sText.Length
    ReDim Preserve sText(iCount)

    Use a string builder to collect all lines in the loop, it’s surely more efficient, readable, and tou won’t need to ask “why”.
    And after the elseif a sipmle


    sText = strBuffer.ToString

    would do.

  2. Ah it’s stupid line array.
    still would make sense to


    Redim sText(lngCount - iLastLine )

    once, and
    [code)
    sText(0) = strBuffer.ToString
    [/code]

    So the problem must be the first word of the string buffer that should be set to the capacity before sending the EM_GETLINE message


    strBuffer.Chars(0) = Chr(255)
    strBuffer.Chars(1) = Chr(0)

    If the project is set to ansi, or just the first line if using unicode

  3. This code was part an example I posted here a long time ago.

    @Chike wrote:

    And after the elseif a sipmle

    sText = strBuffer.ToString

    would do.

    If you are just putting all lines into a single text object, that may work. However, the code he has provided adds each line to the end of a string array. So for each line, it increases the size of the array by 1 and then adds the line to the end of the array.

    Therefore, to answer:
    @Chike wrote:

    What does this mean

    sText(sText.Length - 1) = strBuffer.ToString

    It means to load the last entry of the string array with the current content of the stringbuilder.

    and this:
    @Chike wrote:

    Dim iCount As Integer = sText.Length
    ReDim Preserve sText(iCount)

    “ReDim” re-sizes the string array
    “Preserve” saves the current content.

    It could be condensed into a single line like this:

    ReDim Preserve sText(sText.Length)

    It is possible, I guess, to get all the lines in a single StringBuilder and then use .Split to add them into the String Array, but I have never approached it that way.

    As to why you are having problems with .NET4, I have no ideas. The code is all compatable with .NET4 and I dont see anything in it that should cause what you are reporting. You may want to add some Try/Catch statements to see what errors (if any) are being reported back.

Leave a Reply

You must Register or Login to comment on get last line