I'm not real sure why this doesn't work because I'm getting the correct window. Here is the code. Maybe someone can point out something that I'm missing. Any ideas?
private void bStatus_Click(object sender, EventArgs e) { //Find the console edit box aswell as send the status command - works fine. string message = "status"; StringBuilder sb = new StringBuilder(message); IntPtr MW4CONSOLE = FindWindow("IW4 WinConsole", null); IntPtr MW4EDIT = FindWindowEx(MW4CONSOLE, IntPtr.Zero, "Edit", IntPtr.Zero); SendMessage(MW4EDIT, WM_SETTEXT, IntPtr.Zero, sb); PostMessage(MW4EDIT, WM_KEYDOWN, VK_RETURN, IntPtr.Zero); PostMessage(MW4EDIT, WM_KEYUP, VK_RETURN, IntPtr.Zero);
//Get the console text - works and finds the correct window - cross checked with api spy IntPtr MW4TEXT = FindWindowEx(MW4CONSOLE, MW4EDIT, "Edit", IntPtr.Zero);
//does not work int length = GetWindowTextLength(MW4TEXT); StringBuilder sbtext = new StringBuilder(length + 1);
GetWindowText(MW4TEXT, sbtext, sbtext.Capacity); lbUserNames.Items.Add(sbtext.ToString());
}
|
Edit: Here is a screen shot of the windows showing what is going on.
http://i29.tinypic.com/1z72dxi.jpg
Girish BarjePosted Jul 23, 2010, 12:17 AM
theLizardPosted Jul 22, 2010, 11:04 PM
If the original question had stated explicitly that the other windows are in another process, I would have said that GetWindowText would not work.
End Quote.
My posts ALL related to an external process.
The pure fact that FindWindowEx is used in the question should be enough for you to know that the question is related to finding and getting text from a window in another process, if it was not then you would not need to find the window because you already know what your windows are and any control in those windows can be accessed without having to use a DLL Import.
Note well GetWindowText WILL get the Title bars text from a window in another process but not the text of an edit control in another process.
FindWindowEx, GetWindowText, SendMessage etc.. are for the purpose of getting almost anything that you want from a window in another process.
I have used these in a component written in c++ to do what Donald wants so I am NOT inexperienced in their use.
You need to understand the question before you give advice, it makes you look silly...
Finally,
you said "That's correct, and I never said that GetWindowText works across processes." & " theLizard often posts criticisms and misinformation."
Exactly where is my miss information in any of my posts for this question? There is NONE isn't that right Sam.
Yes there are criticisms, only because you gave the wrong information based on miss understanding of the question so I corrected you is this not what forums are about, getting the right answer to a question, not sending some one on a wild goose chase?
I hope you have learned something today...
Sam HobbsPosted Jul 22, 2010, 10:34 PM
If the original question had stated explicitly that the other windows are in another process, I would have said that GetWindowText would not work.
theLizardPosted Jul 22, 2010, 10:00 PM
To retrieve the text of a control in another process, send a WM_GETTEXT message directly instead of calling GetWindowText.
And I see that Donald IS using the recommended way of getting the text from the control, that is using SendMessage with the WM_GETTEXT parameter and NOT GetWindowText and yes Sam, while GetWindowText gets the text from a text box in the same process it does not mean that you can get the text from a text box in another process.
Donal I am assuming that the console application is running in another process is this right or wrong?
If it is the same process then you would not need to send a message or getwindow text as it can be obtained directly.
As I said previously, Sam must know more about GetWindowText than Microsoft, these are the guys who wrote the API's.
I am not arguing, simply stating the facts...
Sam HobbsPosted Jul 22, 2010, 8:39 PM
I hope you are able to get better assistance in the future from these forums, but you will need to provide a more complete description of the question/problem.
And in case you are interested, there are other ways of doing what you are doing and some of tjhe alternatives are probably easier and/or more reliable. The thing you should have said in the original question is if the other windows are in a separate application/process.
Sam HobbsPosted Jul 22, 2010, 8:31 PM
Donald RamseyPosted Jul 22, 2010, 8:00 PM
Here is the fixed code for reference.
theLizardPosted Jul 22, 2010, 7:49 PM
theLizard, you said "get a windows title which a text box does not have".
End quote
Yes that is what I said so what is your point? Which part of the statement do you not understand?
For those who do not know...
A Form is a Window, a Textbox is a window.
A Form has a title bar that can show a caption
A Text box does NOT have a title bar so it CANNOT show a caption
GetWindowText ONLY gets the text value of the Title bar if it has a title, it does NOT get the text from a Textbox.
Sam HobbsPosted Jul 22, 2010, 2:38 AM
theLizardPosted Jul 22, 2010, 1:35 AM
GetWindowText can retrieve the text of a control for many controls; especially for an edit control, which is a textbox.
End Quote
Take special note Sam, especially the text in red.
Sam, you must know more about
GetWindowText(MW4TEXT, sbtext, sbtext.Capacity) than Microsoft. (More miss leading information)Copies the text of the specified window's title bar (if it has one) into a buffer. If the specified window is a control, the text of the control is copied. However, GetWindowText cannot retrieve the text of a control in another application.
If the target window is owned by the current process, GetWindowText causes a WM_GETTEXT message to be sent to the specified window or control. If the target window is owned by another process and has a caption, GetWindowText retrieves the window caption text. If the window does not have a caption, the return value is a null string. This behavior is by design. It allows applications to call GetWindowText without becoming unresponsive if the process that owns the target window is not responding. However, if the target window is not responding and it belongs to the calling application, GetWindowText will cause the calling application to become unresponsive.
To retrieve the text of a control in another process, send a WM_GETTEXT message directly instead of calling GetWindowText.
Sam HobbsPosted Jul 22, 2010, 12:08 AM
theLizardPosted Jul 21, 2010, 11:55 PM
GetWindowTextcannot be used for this purpose because it is used to get a windows title which a text box does not have.You first need to get the handle of the textbox you want to process and then access its properties,.
Sam HobbsPosted Jul 21, 2010, 9:33 PM
You are not likely to get help in any forum that does not charge money. Most forums get most of the help from volunteers, so you need to supply them with enough information to make it possible for them to answer. In all such forums, when you say "doesn't work" or something such as that, you nearly always must be clear about what does not work, in the sense of explaining what happens that is not supposed to happen or what does not happen that is supposed to happen. You don't provide much more than code and you expect us to understand.
Donald RamseyPosted Jul 21, 2010, 12:53 PM