CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2008
    Posts
    4

    Question using handles to get RichEdit text

    Hi there,

    I'm trying to get the text from two RichEdit controls which are located in external application, but I can't get the the text with GetWindowText. What is my mistake?

    Any help would be much appreciated... Thanks in advance.

    Here's the code:

    #include <iostream>
    #include <fstream>
    #include <Windows.h>
    #include <Winuser.h>

    using namespace std;


    HWND hwndApp;
    HWND hwndRichEditControl1 ;
    HWND hwndRichEditControl2;

    void GetWindowAndControls(){

    // get the handle for the application, works fine
    hwndApp= FindWindow(NULL, L"MyExternalApplication");

    // get the handles for the two RichEdit controls, works ok (tested with winID).
    hwndRichEditControl1 = FindWindowEx(hwndApp, NULL, L"WindowsForms10.RichEdit20W.app.0.33c0d9d", NULL);
    hwndRichEditControl2= FindWindowEx(hwndApp, hwndRichEditControl1, L"WindowsForms10.RichEdit20W.app.0.33c0d9d", NULL);

    }


    void main()
    {

    // declare the strings
    TCHAR txtApp[10], txt1[10], txt2[10];

    // get the application handle and the rich edit handles
    GetWindowAndControls();

    // this works fine, and get the text from main window
    ::GetWindowText(hwndApp, txtApp, 10);

    // this doesn't work - doesn't get text from RichEdit controls inside main window
    //Why? How can I make it work?
    ::GetWindowTextW(hwndRichEditControl1 , txt1, 10);
    ::GetWindowTextW(hwndRichEditControl2 , txt2, 10);

    }

  2. #2
    Join Date
    Jan 2008
    Posts
    178

    Re: using handles to get RichEdit text

    Read MSDN on GetWindowText () ...

  3. #3
    Join Date
    Dec 2008
    Posts
    4

    Re: using handles to get RichEdit text

    I know GetWindowText doesn't work with controls.

    That's why I'm asking what function I CAN use for this purpose.

  4. #4
    Join Date
    Dec 2008
    Posts
    4

    Re: using handles to get RichEdit text

    Quote Originally Posted by fred100 View Post
    Read MSDN on GetWindowText () ...
    I tried using the SendMessage:

    char txt[512]="";
    ::SendMessage(hwndRichEditControl1 ,WM_GETTEXT,512,(LPARAM)txt);

    But even this doesn't work.. any ideas?

    I'm using winID to watch this control and I can see the text under "Title" property.
    Its class is WindowsForms10.RichEdit20W.app.0.33c0d9d.

    Why can't I retrieve the text with SendMessage(..., WM_GETTEXT, ...)??

  5. #5
    Join Date
    Dec 2008
    Posts
    4

    Smile Re: using handles to get RichEdit text

    I solved the problem with

    TCHAR txt[32]=L"";
    ::SendMessage(hwndRichEdit, WM_GETTEXT, 32, (LPARAM)txt);

    It seems that the class type wasn't correct:
    It suppose to be TCHAR and not char class, and must be converted to LPARAM.

    Ok, the thread can be closed now.

  6. #6
    Join Date
    Jan 2001
    Posts
    253

    Re: using handles to get RichEdit text

    You may want to change the method that you are using to declare your txt variable. You are mixing the TCHAR support (a Microsoft invention which allows building applications which support multiple character set widths) with a direct literal wide string.

    You used the following to declare txt:
    TCHAR txt[32]=L"";
    TCHAR is substituted with either wchar_t or char (depending on what the character set option is for the project). The right side (L"") requests an empty wide string (each character is a wchar_t).

    Since this compiles, your character set must be set to Unicode. If you set your character set to Multi-Byte, this would produce a compile error.

    If you need to support building the source using both settings, then the following could be used:
    Code:
    #include <tchar.h>
    
    TCHAR txt[32]=_T("");
    If you are only going to build the source with one setting for character set, then the following would work for unicode:
    Code:
    wchar_t txt[32]=L"";

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured