How to get information about a Window, and trouble with Process.WaitForExit

Riemann

Newcomer
Joined
Sep 24, 2003
Messages
13
Greetings all,
I have recently begun working in .NET (after many years of the Visual Studio 6.0 languages) for the purpose of automating some test processes at work. While many of the tasks involved have turned out to be much simpler in managed code (I am using VB.NET) I have encountered some difficulties. Specificially, I would like to be able to find out what window currently has input focus (and be able to find what process it is attached to). Some other tasks which are also being troublesome in .NET are:
Getting information about a window, such as its title, position, size etc...
Getting information from a control on a window, such as text out of a text box or the state of a checkbox.
Giving a window input focus.

Note that all these tasks are for a window running in another process. EG: I use the process class to run another program and I need to manipulate and monitor that program.

I know how to do all of these things with the WinAPI in non-managed code. However, if these things can be done with the .NET libraries I would prefer to do so.

By the way: I have noticed something very odd about the Process.WaitForExit and Process.WaitForInputIdle methods. The problem with WaitForInputIdle being that it does not seem to do anything at all (it never seems to "wait" when I use it). With WaitForExit I have been getting some very odd behavior indeed. Take the following for example:
Code:
myProcess.Start()
Sendkeys("Weeee")
myProcess.CloseMainWindow()
myProcess.WaitForExit(10000)
myProcess.Close()

Assume that myProcess is set to open an instance of Notepad.
Now it seems to mee that this should open Notepad, then print "Weeee" into it, then close notepad (waiting up to 10 seconds before proceeding for notepad to close). What is happening for me is that notepad opens, then immediately waits 10 seconds, then when the 10 seconds are up types in "Weee" and closes. EG: The WaitForExit is happening BEFORE the SendKeys and myProcess.CloseMainWindow commands. Very strange.
 
Derek Stone said:
You'll have to use the Win32 API from .NET to interact with windows bound to other processes.

VB.NET: Platform Invoke

As for SendKeys you just simply shouldn't use it. Call the SendMessage API function with WM_SETTEXT as a parameter instead.

Yes, I have come much to the same conclusion myself. I figure I will just create a wrapper class to handle all the transitions to and from unmanged memory. *sigh*

As for SendKeys, I was refering to System.Windows.Forms.SendKeys. Since this is the method suggested by the MSDN help files, what is the advantage of using SendMessage instead?
 
SendKeys relies on the window to be focused and active, an assumption that simply shouldn't be made by any programmer wanting to develop a solid application. Calling SendKeys may fail, while SendMessage is ensured to succeed.
 
Derek Stone said:
SendKeys relies on the window to be focused and active, an assumption that simply shouldn't be made by any programmer wanting to develop a solid application. Calling SendKeys may fail, while SendMessage is ensured to succeed.


Well, it is a very simple matter to check which window has input focus and, if it is not the intended one, change that.

However, I would much prefer to use SendMessage. The only problem being that it does not seem to work worth a damn.

For instance, I wish to send a ENTER keystroke to a given window. Near as I can tell I need to send a WM_KEYDOWN followed by a WM_KEYUP where the wParam is the virtual key code (in this case &H0D for ENTER) and the lParam is zero.

The thing is when I use SendKeys in this way it does not work. But when I ensure that the window is the Foreground window and use SendKeys it does.

I am all for not making assumptions. Good stuff that. But I prefer my code to work.
 
May I have some help with this process? I am trying to do the same thing here at work. I need to automate many software installations, but we have been using Winbatch, and I want to use Visual Basic instead. I know Visual Basic can handle it. I am encouraged by your comments. Can I see your sample code?

Please help. thanks much
 
Back
Top