Results 1 to 6 of 6

Thread: Get text from another apps RichTextBox?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2003
    Posts
    28

    Get text from another apps RichTextBox?

    I'm trying to make a trivia program for a chat application. I've got it working but I think it might not be the best way of doing it, and it crashes chat app occasionally. The chat app is able to keep a log of the chat window and save it to a file. At the moment, my program opens the log every second if the contents have changed, and checks each line for the correct answer to the question. I'm thinking that it crashes because both programs are trying to use the file at once, or something like that?
    So I'm going back to the original way I had in mind for checking for the corret answer, by getting the text from the RTB in the chat app. Maybe this isn't the best way of doing it either, so if you have any ideas, I'd like to hear them

    Anyway, if this is the best way to do it, here's my problem...


    I'm trying to get the text from another apps RTB using EM_GETTEXTEX.

    I looked on MSDN and this is the structure for it:
    Code:
    typedef struct _gettextex {
        DWORD cb;
        DWORD flags;
        UINT codepage;
        LPCSTR lpDefaultChar;
        LPBOOL lpUsedDefChar;
    } GETTEXTEX;
    I have a feeling this isn't the correct translation to VB, so what should it be?
    Code:
    Public Type GETTEXTEX
        cb As Long
        flags As Long
        codepage As Integer
        lpDefaultChar As String
        lpUsedDefChar As Boolean
    End Type
    From here, I just get even more lost, and structures always seem to confuse me. The only part of the structure that I think I understand is flags.

    ---------------------
    ---------------------
    Fom MSDN:
    cb
    Count of bytes in the fetched string. The size of the output buffer is lParam in the EM_GETTEXTEX message.

    flags
    Value specifying a text operation. This member can be one of the following values.

    GT_DEFAULT
    No CR translation.
    GT_SELECTION
    Retrieves the text for the current selection.
    GT_USECRLF
    Indicates that when copying text, each CR should be translated into a CRLF.

    codepage
    Code page used in the translation. It is CP_ACP for ANSI Code Page and 1200 for Unicode.

    lpDefaultChar
    Points to the character used if a wide character cannot be represented in the specified code page. It is used only if the code page is not 1200 (Unicode). If this member is NULL, a system default value is used.

    lpUsedDefChar
    Points to a flag that indicates whether a default character was used. It is used only if the code page is not 1200 (Unicode). The flag is set to TRUE if one or more wide characters in the source string cannot be represented in the specified code page. Otherwise, the flag is set to FALSE. This member may be NULL.

    ---------------------
    ---------------------

    I tried having a go at it anyway, but managed to crash the program I was trying to get the text from.
    Code:
    'Module
    Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
    
    Public Type GETTEXTEX
        cb As Long
        flags As Long
        codepage As Integer
        lpDefaultChar As String
        lpUsedDefChar As Boolean
    End Type
    
    Public Const GT_USECRLF = 1&
    Public Const WM_USER As Long = &H400
    Public Const EM_GETTEXTEX As Long = (WM_USER + 94)
    
    '============================
    '============================
    
    'Form1
    Dim Str1 As String
    Private Sub Command1_Click()
    Str1 = Space(20)
    GetChatHwnds
    SendMessage ChatWindow, EM_GETTEXTEX, GETTEXTEX, Str1
    Text1.Text = Str1
    End Sub

    I realise I should probaby use GETTEXTLENGTHEX for the text length, but I thought I'd try and understand this first.

    I haven't been able to find an example of this to see how it's done, so does anyone have an example they could share?

    I've done most of the code for the rest of the trivia program and would hate to abandon it just because I can't figure this part out.

    Thanks for any help with this
    Last edited by Boka; Oct 3rd, 2003 at 02:18 PM.

  2. #2

    Thread Starter
    Junior Member
    Join Date
    May 2003
    Posts
    28
    It has been suggested to me that I should try EM_GETTEXTRANGE instead, which might be more suitable for what I want.

    There's an example of it here. The exaple works fine, but when I change the handle of the RTB belonging to another program, it crashes the program I'm trying to get the text from.

    Does this have something to do with reading or writing to another programs memory?

    Surely someone must have some ideas, or was my post too long that no one bothered to read it?

    Thanks.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2003
    Posts
    28
    I'm still having problems with this, using EM_GETTEXTRANGE instead of EM_GETTEXTEX.

    Here's my code so far:

    VB Code:
    1. Const WM_USER = &H400
    2. Const EM_GETTEXTRANGE = (WM_USER + 75)
    3.  
    4. Private Type CharRange
    5.     cpMin As Long
    6.     cpMax As Long
    7. End Type
    8.  
    9. Private Type TEXTRANGE
    10.     chrg As CharRange
    11.     lpstrText As String
    12. End Type
    13.  
    14. Private Declare Function SendMessage Lib "user32" & _ Alias "SendMessageA" (ByVal hwnd As Long, & _
    15. ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) & As Long
    16. Private Declare Function FindWindowEx Lib "user32.dll" & _
    17. Alias "FindWindowExA" (ByVal hWndParent As Long, & _
    18. ByVal hwndChildAfter As Long, ByVal lpszClass As String, ByVal lpszWindow As String) As Long
    19.  
    20. Dim BeginWord As Long
    21. Dim EndWord As Long
    22. Dim LRet As Integer
    23. Dim sBuf As String
    24.  
    25. Dim TRng As TEXTRANGE
    26. Dim TestRTF As Long
    27.  
    28. Private Sub Command1_Click()
    29.  
    30. GetChatHwnds
    31.  
    32. BeginWord = 0
    33. EndWord = 400
    34.  
    35. TRng.chrg.cpMin = BeginWord   '--beginning char. offset
    36. TRng.chrg.cpMax = EndWord   '--ending char. offset
    37.  
    38. TRng.lpstrText = String$(EndWord - BeginWord, 0)
    39. LRet = SendMessage(TestRTF, EM_GETTEXTRANGE, 0, TRng)
    40.  
    41. sBuf = Left$(TRng.lpstrText, LRet)
    42.  
    43. Text1.Text = sBuf
    44.  
    45. End Sub

    It works fine when I put a RichTextBox on the form and use RichTextBox1.hwnd instead of TestRTF, but it doesn't work when the handle belongs to another app. I've checked that TestRTF is the correct handle, and it is.

    It's something to to with cross prcoess memory, which I don't really understand that well.

    Any help would be greatly appreciated.
    Thanks.

  4. #4
    Lively Member
    Join Date
    Jul 2002
    Posts
    91
    Hi Boka,

    Have you been able to solve the problem?

    I am facing the same problem!

    If you have been able to solve the problem, can you share it with me.

    Thanks.

  5. #5
    Addicted Member
    Join Date
    Aug 2003
    Location
    USA
    Posts
    145
    Well from what I know, you cannot get the text of another processes's richedit because of memory issues. If you inject a dll or whatever into the process, then have that dll perform the messages then it should work.

    I believe there are some discussions about this here, a good search may help.

  6. #6
    Lively Member
    Join Date
    Jul 2002
    Posts
    91
    Can u plz. send me the search URL

    Thankz

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