Printing a RichTextBox RRS feed

  • Question

  • I use VB in VS2010

    I am attempting to print the contents of a RichTextBox using the EM_FORMATRANGE message.
    I am using code that appears often in posts about this topic.

    The API call is:

    Private Declare Function SendMessage Lib "user32" Alias _
        "SendMessageA" (ByVal hWnd As Long, ByVal msg As Long, ByVal wp As Long, ByVal lp As Long) As Long

    The codeline that sends the message is:

    nNextCharPos = SendMessage(MyText.Handle, EM_FORMATRANGE, wparam, lparam)

    nNextCharPos, SendMessage, EM_FORMATRANGE are all declared as long
    MyText is IntPtr
    wparam, and lparam are all declared as IntPtr as follows

    Dim wparam As IntPtr = New IntPtr(1)
    Dim  lparam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fr))
    Marshal.StructureToPtr(fr, lparam, False) 

    The code fails with the message .... unbalanced stack ..... possibly due to signature mismatch ....

    If I try

    nNextCharPos = SendMessage(clng(MyText.Handle), EM_FORMATRANGE, clng(wparam), clng(lparam))

    I get the same message


    I have also tried re-declaring SendMessage as

    Private Declare Function SendMessage Lib "user32" Alias _
        "SendMessageA" (ByVal hWnd As IntPtr, ByVal msg As IntPtr, ByVal wp As IntPtr, ByVal lp As IntPtr) As IntPtr

    and declaring nNextCharPos as IntPtr

    with the codeline

    nNextCharPos = SendMessage(MyText.Handle, cInt(EM_FORMATRANGE), wparam), lparam)

    and this time the error message comes out as Trying to read or write to protected memory.


    So I am stuck.

    Can anyone help?



    Monday, June 24, 2013 6:43 PM


All replies