VB.NET 2010 SendMessage/WM_GETTEXT hangs

I am using this to grab my text from external richtextbox. It works fine when theres not much text, but when I get alot of text, like 5000 characters, it seems to freeze up! It is getting the length correctly, so I'm not sure what the issue is.

Any suggestions? Basically I'm getting text from a richtextbox and displaying it in my textbox!

        Dim length As IntPtr = SendMessage(ChatWindowHandler, WM_GETTEXTLENGTH, Nothing, 0)
        Dim Hndl As IntPtr = Marshal.AllocHGlobal(length)
        Dim NumText As Integer = SendMessage(ChatWindowHandler, WM_GETTEXT, length, Hndl)

        'copy the characters from the unmanaged memory to a managed string
        TextBox1.Text = Marshal.PtrToStringUni(Hndl)

LVL 7
VallerianiAsked:
Who is Participating?
 
MedievalWarriorCommented:
Try using this example
RichTexting.GetText(CType(&H12345, IntPtr))

Open in new window

Imports System.Runtime.InteropServices
Imports System.Text

Public Class RichTexting
    Private Sub New()
    End Sub
    <DllImport("User32.dll", ExactSpelling:=False, SetLastError:=True, CharSet:=CharSet.Unicode)> _
    Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByVal lParam As System.Text.StringBuilder) As IntPtr
    End Function
    Private Const WM_GETTEXT As Integer = &HD
    Public Shared Sub GetText(ByVal handle As IntPtr)
        Dim data As New StringBuilder(65536)
        Dim result As IntPtr = SendMessage(handle, WM_GETTEXT, New IntPtr(data.Capacity), data)
        Debug.Print(data.ToString)
    End Sub
End Class

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.