Link to home
Start Free TrialLog in
Avatar of Zoplax
Zoplax

asked on

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?


Avatar of topdog770
topdog770
Flag of United States of America image

Take a look at this, shows how to create a C# app similiar to Spy


http://msdn.microsoft.com/msdnmag/issues/06/04/ManagedSpy/
Avatar of Zoplax
Zoplax

ASKER

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.
You are probably going about this in the wrong way.
Do a keyStroke monitor and only monitor when the app you want has focus.
Avatar of Zoplax

ASKER

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.  :)
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.
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.
Avatar of Zoplax

ASKER

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

cool, thanks for the links!
ASKER CERTIFIED SOLUTION
Avatar of GranMod
GranMod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial