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?
Java
privatevoidbStatus_Click(object sender, EventArgs e)
{
//Find the console edit box aswell as send the status command - works fine.stringmessage="status";
StringBuildersb=newStringBuilder(message);
IntPtrMW4CONSOLE= FindWindow("IW4 WinConsole", null);
IntPtrMW4EDIT= 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 spyIntPtrMW4TEXT= FindWindowEx(MW4CONSOLE, MW4EDIT, "Edit", IntPtr.Zero);
//does not workintlength= GetWindowTextLength(MW4TEXT);
StringBuildersbtext=newStringBuilder(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
14 Answers
Know the answer? Post it. Somebody with the same question will find it here.
I am sorry, but I do not undersand the question. There is so much that I don't understand, that I did not even know where to start trying to get clarification, so I did not try.
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.
What exactly are you trying to get? is it the value from the text box in your screen shot? I am guessing that it is, if so GetWindowText cannot 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,.
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.
Donald Ramsey7 reputationPosted Jul 22, 2010, 4:00 PM EDT
0
No need to argue guys. I shouldn't of come off like that. But, it was my own fault for not understanding the code it self. Instead of just reading the first few lines of whatever article it was I was reading. I should read more then one source.
Here is the fixed code for reference.
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespaceMW2KICKER
{
publicpartialclassMW2KICKER : Form
{
[DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Ansi)]
publicstaticextern IntPtr FindWindow(string className, string windowName);
[DllImport("user32.dll", SetLastError = true)]
publicstaticextern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
privatestaticexternintSendMessage(IntPtr hwnd, int msg, int wParam, StringBuilder sb);
[DllImport("User32.Dll", EntryPoint = "PostMessageA")]
publicstaticexternboolPostMessage(IntPtr hWnd, uint msg, int wParam, IntPtr lParam);
publicconstint WM_SETTEXT = 0x000C;
publicconstint VK_RETURN = 0x0D;
publicconstint WM_KEYDOWN = 0x100;
publicconstint WM_KEYUP = 0x101;
publicconstint WM_GETTEXTLENGTH = 0x000E;
publicconstint GW_CHILD = 5;
publicconstint GW_HWNDNEXT = 2;
publicconstint WM_GETTEXT = 0x000D;
publicMW2KICKER()
{
InitializeComponent();
}
publicvoidStatus()
{
//Find the console edit box aswell as send the status command.
tbData.Refresh();
string message = "status";
StringBuilder sb = new StringBuilder(message);
IntPtr MW4CONSOLE = FindWindow("IW4 WinConsole", null);
IntPtr MW4EDIT = FindWindowEx(MW4CONSOLE, IntPtr.Zero, "Edit", IntPtr.Zero);
IntPtr MW4TEXT = FindWindowEx(MW4CONSOLE, MW4EDIT, "Edit", IntPtr.Zero);
switch ((int)(MW4CONSOLE) == 0 == false)
{
casetrue:
SendMessage(MW4EDIT, WM_SETTEXT, 0, sb);
PostMessage(MW4EDIT, WM_KEYDOWN, VK_RETURN, IntPtr.Zero);
PostMessage(MW4EDIT, WM_KEYUP, VK_RETURN, IntPtr.Zero);
//Get the console text and wait for response.
System.Threading.Thread.Sleep(1000);
int length = SendMessage(MW4TEXT, WM_GETTEXTLENGTH, 0, null);
StringBuilder sbtext = new StringBuilder(length + 1);
SendMessage(MW4TEXT, WM_GETTEXT, sbtext.Capacity, sbtext);
tbData.Text = sbtext.ToString();
lbUserNames.Items.Add(sbtext.ToString());
break;
casefalse:
MessageBox.Show("No Modern Warfare 2 console was found!");
break;
}
}
privatevoidbStatus_Click(object sender, EventArgs e)
{
Status();
}
}
}
Note that theLizard will never admit mistakes. For those that want the truth, you can use the following code or something sufficiently similar to see that GetWindowText can get the text of a textbox. In this code, textBox1 is a textbox.
Donald: I am sorry for the "arguing". theLizard often posts criticisms and misinformation. There is not a lot I can do about that except I can post corrections; I hope that helps to reduce the misinformation. Since theLizard is knowledgeable, experienced and capable of very useful guidance and assistance, management I assume is tolerant of theLizard. It is unfortunate when members such as you get caught in exchanges such as this.
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.
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.
theLizard: You said "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". That's correct, and I never said that GetWindowText works across processes. Note that you made the first comment about GetWindowText, which was "GetWindowText cannot be used for this purpose because it is used to get a windows title which a text box does not have".
If the original question had stated explicitly that the other windows are in another process, I would have said that GetWindowText would not work.
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?
Donald Ramsey7 reputationPosted Jul 21, 2010, 8:53 AM EDT
Sam HobbsRank #107 · 20.5K reputationPosted Jul 21, 2010, 5:33 PM EDT
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.
theLizardRank #1,079 · 1.8K reputationPosted Jul 21, 2010, 7:55 PM EDT
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 HobbsRank #107 · 20.5K reputationPosted Jul 21, 2010, 8:08 PM EDT
theLizardRank #1,079 · 1.8K reputationPosted Jul 21, 2010, 9:35 PM EDT
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 HobbsRank #107 · 20.5K reputationPosted Jul 21, 2010, 10:38 PM EDT
theLizardRank #1,079 · 1.8K reputationPosted Jul 22, 2010, 3:49 PM EDT
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.
Donald Ramsey7 reputationPosted Jul 22, 2010, 4:00 PM EDT
Here is the fixed code for reference.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Diagnostics; namespace MW2KICKER { public partial class MW2KICKER : Form { [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Ansi)] public static extern IntPtr FindWindow(string className, string windowName); [DllImport("user32.dll", SetLastError = true)] public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle); [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern int SendMessage(IntPtr hwnd, int msg, int wParam, StringBuilder sb); [DllImport("User32.Dll", EntryPoint = "PostMessageA")] public static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, IntPtr lParam); public const int WM_SETTEXT = 0x000C; public const int VK_RETURN = 0x0D; public const int WM_KEYDOWN = 0x100; public const int WM_KEYUP = 0x101; public const int WM_GETTEXTLENGTH = 0x000E; public const int GW_CHILD = 5; public const int GW_HWNDNEXT = 2; public const int WM_GETTEXT = 0x000D; public MW2KICKER() { InitializeComponent(); } public void Status() { //Find the console edit box aswell as send the status command. tbData.Refresh(); string message = "status"; StringBuilder sb = new StringBuilder(message); IntPtr MW4CONSOLE = FindWindow("IW4 WinConsole", null); IntPtr MW4EDIT = FindWindowEx(MW4CONSOLE, IntPtr.Zero, "Edit", IntPtr.Zero); IntPtr MW4TEXT = FindWindowEx(MW4CONSOLE, MW4EDIT, "Edit", IntPtr.Zero); switch ((int)(MW4CONSOLE) == 0 == false) { case true: SendMessage(MW4EDIT, WM_SETTEXT, 0, sb); PostMessage(MW4EDIT, WM_KEYDOWN, VK_RETURN, IntPtr.Zero); PostMessage(MW4EDIT, WM_KEYUP, VK_RETURN, IntPtr.Zero); //Get the console text and wait for response. System.Threading.Thread.Sleep(1000); int length = SendMessage(MW4TEXT, WM_GETTEXTLENGTH, 0, null); StringBuilder sbtext = new StringBuilder(length + 1); SendMessage(MW4TEXT, WM_GETTEXT, sbtext.Capacity, sbtext); tbData.Text = sbtext.ToString(); lbUserNames.Items.Add(sbtext.ToString()); break; case false: MessageBox.Show("No Modern Warfare 2 console was found!"); break; } } private void bStatus_Click(object sender, EventArgs e) { Status(); } } }Sam HobbsRank #107 · 20.5K reputationPosted Jul 22, 2010, 4:31 PM EDT
Sam HobbsRank #107 · 20.5K reputationPosted Jul 22, 2010, 4:39 PM EDT
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.
theLizardRank #1,079 · 1.8K reputationPosted Jul 22, 2010, 6:00 PM EDT
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 HobbsRank #107 · 20.5K reputationPosted Jul 22, 2010, 6:34 PM EDT
If the original question had stated explicitly that the other windows are in another process, I would have said that GetWindowText would not work.
theLizardRank #1,079 · 1.8K reputationPosted Jul 22, 2010, 7:04 PM EDT
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...
Girish Barje7 reputationPosted Jul 22, 2010, 8:17 PM EDT