Thread: RickEdit control question

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    16

    RickEdit control question

    My program needs to read text from a RichEdit control in another windows
    application. All I have is the Handle of the RichEdit control.

    So first I tried:
    lResult = SendMessage( hwnd, WM_GETTEXT, 5000, (long)bufferBig);

    This works, however the size of the text in the RichEdit may exceed 64k, also
    I prefer not to read the whole text everytime for performance reasons,
    therefore I try to use the messages for the RichEdit control, for example:
    SendMessage(hwnd, EM_EXGETSEL, 0, (LPARAM) &crange);
    or
    lResult = SendMessage( hwnd, EM_EXSETSEL, 0, (long)(&crange) );
    or
    lResult = SendMessage( hwnd, EM_GETSELTEXT, 0, (long)bufferBig );

    crange and bufferBig are defined and initiated properly.

    When I have any of the above in my program, the program that includes the
    RichEdit control will crash.

    Does anyone know what is the problem? Any workaround to get the data over 64k
    ?

    Thanks in advance!

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Send a WM_GETTEXTLENGTH message first so you know how big of a buffer you need. Then fill the buffer with WM_GETTEXT. This is far more efficient than getting the text piece by piece using EM_GETSELTEXT.

    EDIT: Somehow I missed the part where you stated you needed to reteive text greater than 64K. What line is your program crashing on when you use EM_GETSELTEXT?
    Last edited by bithub; 03-02-2006 at 08:04 PM.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    16
    Thanks bithub !

    My program doesn't crash, the windows application that I tried to get the text from crashes --- the application is a blackbox for me so I don't know how and why it crashes --- OS throws a message saying something like "error writing memory 0x????????"

    I am just trying to find a workaround, so that the other program doesn't crash.

    Do you know if I use WM_GETTEXT and there are 500k text, will I get the 1st 64k text or the last 64k text or it will simply fail?

    Thanks!

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Do you know if I use WM_GETTEXT and there are 500k text, will I get the 1st 64k text or the last 64k text or it will simply fail?
    Probably just the first 64K. Does the other application crash on the EM_EXSETSEL message, or the EM_GETSELTEXT message. Also, are you sure it's a rich edit text box?

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    16
    The other application crash on either EM_EXSETSEL or EM_GETSELTEXT (I put 1 line at a time to try to identify the issue).

    I use the following to retrieve the class name:
    GetClassName(hwnd, ch, 150);
    and the output is "RICHEDIT".

    I also use WinDowse software and it also identify the control as "RICHEDIT".

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    For backwards compatibility, Windows allows you to send certain messages across processes. It allows this by doing marshalling. The messages that are marshalled are those that were designed for the basic Win32 controls (BUTTON, EDIT, etc). Specifically, only Microsoft defined messages in the range 0 to WM_USER are marshalled.

    Messages designed for the common controls (including richedit) are above WM_USER and are not marshalled. This means that when you send or post one of these messages to another process it is passed unaltered. If it contains a pointer parameter, the pointer will not be valid in the target process and it will crash, corrupt the process, or return incorrect results.

    Therefore, unless you do custom marshalling, EM_EXSETSEL or EM_GETSELTEXT are out. I suggest that you have a look at EM_GETLINE. This message was originally designed for the edit control so is marshalled but can also be used with the richedit control.

    For sending messages to another process, you should consider using SendMessageTimeout. This will avoid freezing your thread if the other window is not responding to messages.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    16
    Just tested it and it doesn't make the other application crash! --- this is amazing! like a hidden treasure.

    And if it works, it would be actually better than using EM_GETSELTEXT, as I don't have to worry about LF and CR.

    BTW I googled "custom marshalling" and seems the complexity is beyond what I want to commit and not to mention I have no control or kownledge on the target process --- thus "custom marshalling" might be impossible.

    Thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. (Multiline) Edit Control Limit
    By P4R4N01D in forum Windows Programming
    Replies: 9
    Last Post: 05-17-2008, 11:56 AM
  2. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  3. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  4. Scrolling Image Control
    By Punnie in forum Windows Programming
    Replies: 4
    Last Post: 11-30-2005, 02:50 AM
  5. Lame question on dialog controls appearance
    By Templario in forum Windows Programming
    Replies: 2
    Last Post: 03-18-2003, 08:22 PM