How to get focused elements of specific process in c# - c#

im stuck in my problem, i have invoke a process and now i want its focused elements.
for example. i run a process www.google.com that opens up in Google chrome
now lets say i want to search "hello" the way of doing this as far as i know is to get the keyboard focus element that will be a textbox of google.com .so how to do that through my application i.e programmatically(getting keyboard focused element of chrome process)
Process[] pr = Process.GetProcessesByName("chrome");
var target = pr[0];

Your question a is a bit vague, so I'll try to give several pointers based on several different interpretations:
1) If the process you're referring to is always a web-site, you should skip the screen-scraping techniques, like #vulkanino mentioned, and simply fetch the contents of the web-site directly. If you must get the website as parsed by a real browser (rather than the raw HTML itself), you can use screen-scraping, but remember that not all computers use the same browser.
2) If you want to screen-scrape information from a running process, you (unfortunately) don't have a simple API like the one you expected, where the Process pr object contains a list of elements you can traverse. This is because the process might have been written in .NET/WinForms, .NET/WPF, C++/MFC or any of a dozen other UI frameworks that work very differently.
What I would suggest is using a screen-reader accessibility framework, such as Microsoft's UI Automation, which is able to extract text from the UI of many common applications. This functionality exists to help read out application UI for the hard of sight, but can be used for screen-scraping as well.

Related

Citrix GUI in C#

I have been looking for a way to create a simple GUI for users to stop,start,create, list running VMs in Citrix XenServer. I will create a form that has input boxes for users to put the name of the vm in, then a button to do the wanted task for that name. I would will also create a list box that will list all VM's and another that will list all running VM's. The same for all paused VM's and so on. my biggest issue is I cant find a way to run the command on the remote linux system then list the output into the area I want.
They do have Citrix XenCenter but I am wanting a GUI that will give the users limited functionality.
Apparently this question was asked some time ago, but I'll post the answer anyway. All the
Citrix XenServer operations can be performed via XML RPC. Download the XenServer SDK, it has code samples to get you started.
Moreover, SDK also provides .NET bindings, http://docs.vmd.citrix.com/XenServer/5.6.0fp1/1.0/en_gb/sdk.html#language_bindings-c-sharp, which means you won't have to make the XML RPC calls yourself.

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.

Auto populate / screen scrape multipage webform

Firstly apologies if this is a duplicate question, I have spent a while searching and can't find anything that looks to be the same.
I need to automate the completion of a multi page web form and then process the result from within an asp.net system. The ideal solution would be to create a web service which takes in some data, then processes it through the website and returns a result - I can then use this in any app that requires this functionality. The form that needs completing is quite complicated, and also includes some if / then / else logic when going through e.g:
Complete personal details
Enter postcode
If 1 result found goto 5
Display list of possible addresses, when one selected goto 5
If time at address < 3 years, display new address page
etc.
I have created a solution which launches an ActiveX browser control and controls the website as if a user were entering the data which works well however is subject to a limit on concurrent users (anything > 1 starts to cause issues, >2 is not allowed by windows default settings), is there a better way of doing this? I've heard of Selenium although not used it, would this be a possible solution?
I have looked at using the WebClient class, and have used this in the poast for screen scraping systems however I don't know how I could chain these calls together to allow for the full workflow.
FYI - this work is all being undertaken with the full knowledge and consent of the site owner. They have a queue of development tasks which building an internal API does not feature on so whilst they are happy for this type of solution to be implemented, will not be able to change anything their end.
Thanks in advance
There is a .NET port of Watir called WatiN - it might be worth giving that a try if you haven't already.

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