Code to monitor a chat window?

I need code which will process messages I grab from a control within a chat window (a RichEdit control).  The code needs to monitor the control and copy text meeting certain criteria (e.g. with certain usernames in the line).

One hitch, the control exists outside the scope of my C# program.  I am "spying" on a process in a separate chat program to be able to pluck out the data from the RichEdit control.  Because of this I don't think I would be able to access the event model (e.g. Change event) of the foreign application, instead I need to read the control (using SendMessage along with EM_GETLINE and others), load the data into a StringCollection or other object within the scope of my C# app, and then process the data.

Would it be best to use a WHILE loop, within which I constantly poll the RichEdit control for new messages, and only act if a new message arrives that needs to be processed?  Or is there some other way?


LVL 6
ZoplaxAsked:
Who is Participating?
 
GranModConnect With a Mentor Commented:
PAQed with points refunded (222)

GranMod
Community Support Moderator
0
 
topdog770Commented:
Take a look at this, shows how to create a C# app similiar to Spy


http://msdn.microsoft.com/msdnmag/issues/06/04/ManagedSpy/
0
 
ZoplaxAuthor Commented:
That's helpful but I need something more specific to what I'm trying to do.

The chat window I want to monitor will be changing constantly, new lines will appear in the RichEdit control while the chat program is running.

I need to have it so that my C# application knows when a new line of text has been added to the chat window (the RichEdit control in a foreign process), so it can then use SendMessage to retrieve that line of text and place it in a StringCollection or other array type of object for further processing.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
jj819430Commented:
You are probably going about this in the wrong way.
Do a keyStroke monitor and only monitor when the app you want has focus.
0
 
ZoplaxAuthor Commented:
Well the thing is I already have functional C# code which gets the data I need; I have code which uses the Windows API to find a handle to the chat window, find a handle to the RichEdit control, and then uses SendMessage along with EM_GETLINE to get the text contained in the control.

The main issue for me now is how to get the data out of the control as it's getting updated.  Do I constantly poll using a WHILE loop (doable but wasteful) or can I somehow tap into the events of the foreign RichEdit control so that my program knows when new text has been added, and only acts at that point in time?  

I know this can be done, I've seen articles related to this, but I don't know enough about hooks to code this myself.  Before I invest the time to research and figure out a way myself I'm offering up the points to see if anyone here will be able to provide sample code for me to serve this need.  :)
0
 
topdog770Commented:
Assuming that the program you are trying to monitor is a .NET app, the code in that spy program should do exactly what you are trying to accomplish.
0
 
jj819430Commented:
well you can specify a delay. Unfortunately you are not going to get an event for this that you can hook into. You can listen for keystrokes and use that as a means of triggering, but essentially no matter what you are going to end up with a loop checking system.
0
 
ZoplaxAuthor Commented:
topdog770:  No the app in question isn't a .NET app.

jj819430:  I found a link to an example where someone created a DLL using C++ to hook into a program, then had that DLL interface with a separate C# program so that the DLL served as a kind of middleman to forward events from the program to the C# app.  Here are the links that person provided:

The C++ DLL project:
http://evemon.evercrest.com/svn/eve...EVEMon.WinHook/

A C# class that loads it up, installs the global hook, and listens for the signals from it:
http://evemon.evercrest.com/svn/eve...kup&pathrev=202

0
 
topdog770Commented:
cool, thanks for the links!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.