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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.