Getting in message loop of external application - c#

I need to retrieve values from an external application but it does not provide any API to do so. The values are ever-changing and not fixed.
Is there a way to retrieve the values from the application?
Maybe getting into the message loop for that particular application and filtering for certain window messages which contains the value. Or perhaps, using Microsoft Automation Toolkit to search for the relevant controls and getting the values of it.
Thanks for taking your time to read this.
P.S. I was looking into something like SetWindowsHookEx or anything similar.
Does it help to use RegisterWindowMessage if I were to know the string which the application used to register?

You'll need to setup a global Windows Hook and you need to write a C++ DLL for receiving the callback. I'd suggest looking at this MSDN Magazine article. It describes a tool called ManagedSpy but even if you want to "spy" on an unmanaged app the hook code should be the same.

Related

How to capture WM_MINIMIZE for another application?

I am currently looking for a solution in C# how to capture window-messages like WM_MINIMIZE for another application for which I do not have any source code, for example Notepad. My goal is to prevent a user or the system from minimizing a certain application. So my basic idea is to have a process running that filters all Window-messages, and just does nothing, unless a message is posted to the application I define (for example by knowing the window-handle of the applications mainwindow), and then look if this message is a certain size-operation, like WM_MINIMIZE. Only in this case, the message should be removed from the Windows message queue.
What would be a first approach to achieve this? At the moment I am stuck a little bit at finding online resources because most topics concerning resizing deal about messages of own applications, but not to control messages posted to other, external applications running on the same computer.
Either you trap all messages of windows by creating a dll which does a global hook (in c++) (risk of slowdown the OS)
Either You have something to hook the program and in this case:
EasyHook is your friend

Find which process accesses registry?

Ok if anyone can solve this they must be a genius because its nowhere on the web!
http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx
This program detects registry accesses and tells you which process did it.
Does anyone know how i can do this? I can detect changes, but i cant get the process.
Thanks in advance
It's done by hooking system calls. It hooks calls to RegOpenKey (etc) and records all the information about the process accessing the registry before passing the call to the real RegOpenKey.
See EasyHook for a library that makes it relatively easy to write API hooks in Windows.
However, when I say "easy", I actually mean "here be monsters!" API hooking is not for the faint of heart and you should have a very good working knowledge of the internals of Windows before you attempt it.
If you just want to find out what's doing it, then you can use ProcessMonitor, or one of the other derivations of those tools from Sysinternals, now run by Microsoft as Winternals. See the list of programs here.
If you want to write a program to do it, you'll have to hook the registry access functions. The source code for that can be found in archives of the Sysinternals tools. It doesn't seem to be available anymore with the program.

How to access a public field on process? C#

I'm using C# and Process newProcess = Process.Start("someProgram.exe");
This someProgram.exe has a form that has public text boxes.
Is there anyway that I can use newProcess to set the textBoxes?
I was hoping there would be something like newProcess.GetField(textField1).Text = "Awesome";
I've looked at the Process API and properties while debugging and nothing jumped out.
EDIT:
I do have the source to someProgram.exe, so I know the text box fields are public. I can't edit someProgram's source.
The code that uses Process.Start was handed down and I didn't want to spend time changing how it works if I could pass some parameters to the new process.
My real goal is when Process.Start("someProgram.exe") runs I can place text in the text fields so I can be lazy and not type in a user name and pw everytime. :)
Thanks
You cannot. See this question.
IPC Mechanisms in C# - Usage and Best Practices..
You cannot access this directly.
Members are only public within the application in which they are running. You cannot access types within other applications (directly), in this fashion.
This, by the way, is a very good thing. If you were allowed to mess with the internal operations of other applications, you'd be able to completely violate any security model present within just about any application. System security and stability would suffer greatly.
There are two options for this scenario:
If the other application is yours, build it as a library instead of a separate application. Just show the form from your application directly. You'll then have access to the types.
Use some form of Interprocess Communication to allow the two processes to "talk" to each other, and pass values as requested. Windows Communication Foundation works very well for this.
The short answer is probably no. The long answer is maybe. If you open up someProgram.exe and use Spy++, you might be able to glean information about the window information, and then send a WM_* message to the right window handle which would mimic the typing of text to that text box.
I would only go this route if you don't have the source to someProgram.exe which would allow you to use more conventional means.

How to write my own Global Snippets program with .NET?

I want to write my own global snippets tool for Windows XP and higher. This would be an always running in the background tool that would pop-up on a globally-defined hotkey, allow me to select a snippet with substitution arguments, and then paste the expanded snippet into the text input of whatever control I had been in when activated it, and finally, return me to that previous app/input box.
I know how to do most of the algorithmic aspects, but I do not know how to accomplish these windows-based features:
1 - Global Hotkey: how do I define a key-sequence in windows (from .net?) that will work, even when entering data in another apps textbox? (Usually this will be a browser window)
2 - Pasting Into Another App: I could use the paste-buffer and Ctrl-C, but I want to avoid the extra keystrokes.
3 - Return Control to Original Window: Seamlessly return back into my input stream: how do I do that? In fact, how does my tool even know where I was before it popped up?
The reasons that I want to write this myself is first to learn how (because there are other tools like this I would like to make) and secondly, I don't know of any snippets tools that have the argument substitution that I want.
So, the two (2)questions are A) What should be my general approach? and B) how best can I accomplish items 1 to 3 above?
You'll need to use global system keyboard hooks to capture your hotkey. There is a CodeProject article showing how to do this from within .NET.
Once you've "trapped" your keystroke, you can use the Windows API to get the current windows handle. However, I'd try to avoid activating your application. You should be able to just paste your new text, and allow the application to handle it.
The disadvantage of using the Windows API is that it doesn't work in all cases, and the "broken" cases are getting more and more common. For example, WPF applications do not provide a HWND for each element within a window, so getting the current "control"s handle will just give you the window, not the appropriate element.
Edit: Another reference source is this article in MSDN Magazine. It shows how to do this via C# using P/Invoke.

How does someone go about writing a "macro" in C#?

I know something about MACROS. I don't mean the ASSEMBLY language kind. I am talking about those programs that you can use perform repetitions actions on another program. I am talking about those programs that you can use to record a series of events on your computer, like, mouse movements and button clicks and then you can play them back. Some of them are elaborate enough to run only on a paricular app that you designate.
I wrote one of sorts once. It was a program that launched an Excel sessions and then used the dynamic data exchage pipe of some kind to feed the excell session script commands. It worked.
But something on the level of the operating system, I imagine, is a whole different story.
How does someone go about writing a "macro" in C#?
I think the approach I will take is to use the spy routine that comes with the development environment to get a list of the proper messages and parameters (wm_lbuttondown for example) and then use dynamic data exchange to send those messages to the app.
So I have three questions.
Is this the best way to do this?
How do I get a handle to an app that is already running?
How do I send user-like messages to an app that is already running?
There are different answers based on many following factors:
is it 3rd party or your own
application?
does it have automation interface
GUI toolkit used in app
If it is a 3rd party app then you need to work on Windows API level via PInvoke - subclassing WinMain proc, capturing and sending input messages, etc. There are 3rd party library for that task. C# obviously is not a right choice for such task.
In case application has automation model (like Excel) it's a pretty straight forward to write program that will be interact with this app.
If it's your own application you want to enhance with macros functionality then you should take this into account on design state. If you use Command pattern from the beginning then it's not hard to program macro recording.
You should provide more details to get a better answer.
Oh, I almost forgot to answer those three questions
Is this the best way to do this?
Depends on concrete scenario
How do I get a handle to an app that is already running?
Depends on application. If it's a native Win app you can easily get process Id and window's handle via WinApi.
How do I send user-like messages to an app that is already running?
Once again it depends on application type. For native win apps you can easily send WM_XXX messages via WinAPI
Unless its something you need to add in your own program you can just download a keyboard/mouse macro program and use it to perform these repeatable actions.
On the other hand to perform macro's in your own program you would want to find a way to record the buttons clicked and write them to a temporary list that can be saved and then run the list by clicking the buttons (programmically).

Categories

Resources