|
-
Oct 2nd, 2003, 03:11 PM
#1
Thread Starter
Junior Member
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.
-
Oct 5th, 2003, 01:18 PM
#2
Thread Starter
Junior Member
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.
-
Oct 16th, 2003, 01:24 PM
#3
Thread Starter
Junior Member
I'm still having problems with this, using EM_GETTEXTRANGE instead of EM_GETTEXTEX.
Here's my code so far:
VB Code:
Const WM_USER = &H400
Const EM_GETTEXTRANGE = (WM_USER + 75)
Private Type CharRange
cpMin As Long
cpMax As Long
End Type
Private Type TEXTRANGE
chrg As CharRange
lpstrText As String
End Type
Private Declare Function SendMessage Lib "user32" & _ Alias "SendMessageA" (ByVal hwnd As Long, & _
ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) & As Long
Private Declare Function FindWindowEx Lib "user32.dll" & _
Alias "FindWindowExA" (ByVal hWndParent As Long, & _
ByVal hwndChildAfter As Long, ByVal lpszClass As String, ByVal lpszWindow As String) As Long
Dim BeginWord As Long
Dim EndWord As Long
Dim LRet As Integer
Dim sBuf As String
Dim TRng As TEXTRANGE
Dim TestRTF As Long
Private Sub Command1_Click()
GetChatHwnds
BeginWord = 0
EndWord = 400
TRng.chrg.cpMin = BeginWord '--beginning char. offset
TRng.chrg.cpMax = EndWord '--ending char. offset
TRng.lpstrText = String$(EndWord - BeginWord, 0)
LRet = SendMessage(TestRTF, EM_GETTEXTRANGE, 0, TRng)
sBuf = Left$(TRng.lpstrText, LRet)
Text1.Text = sBuf
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.
-
Nov 22nd, 2003, 04:16 AM
#4
Lively Member
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.
-
Nov 25th, 2003, 04:08 PM
#5
Addicted Member
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.
-
Nov 26th, 2003, 12:49 AM
#6
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|