0

using the GetWindowText function I am trying to get the Window Caption using C# of an error message. I am able to get the Window Caption of every button in the dialogue but not the text of the label. Using Spy++ I identified the dialogue and the Window Caption field is filled with the message in the label of the dialogue but GetWindowText of this Window Handle gives me an empty string. Compared to the button Window Handle, which gives me the a string with the Window Caption. This is the code I use:

string nameOfStuff = "";
StringBuilder lpClassName = new StringBuilder();
int index = 20;
int ct = 0;
IntPtr result = IntPtr.Zero;
do
{
    result = FindWindowEx(appHandle, result, null, null);
    if (result != IntPtr.Zero)
    {
        GetWindowText(result, lpClassName, 100);
        nameOfStuff += "      " + Convert.ToString(ct) + lpClassName.ToString() + "\n";
        ++ct;
    }
}

while (ct < index && result != IntPtr.Zero);

This code gives me all the Window Captions except the one from the label. Here is how the Spy++ looks like: enter image description here

enter image description here

Any idea why I can't get the Window Caption of the label in such a way? Thanks!

AGB
  • 2,230
  • 1
  • 14
  • 21
doom4
  • 633
  • 5
  • 19
  • [GetWindowText](https://msdn.microsoft.com/en-us/library/windows/desktop/ms633520.aspx): *"However, GetWindowText cannot retrieve the text of a control in another application."* – IInspectable Apr 12 '16 at 16:16
  • So how come I get the button label? And which is the correct function to get the Window Caption? – doom4 Apr 12 '16 at 16:53
  • *"So how come I get the button label?"* - A lucky coincidence. Asking why something appears to work when used outside its specification is not very helpful, though. *"And which is the correct function to get the Window Caption?"* - Again, from [GetWindowText](https://msdn.microsoft.com/en-us/library/windows/desktop/ms633520.aspx): *"To retrieve the text of a control in another process, send a [**WM_GETTEXT**](https://msdn.microsoft.com/en-us/library/windows/desktop/ms632627.aspx) message directly instead of calling **GetWindowText**."* – IInspectable Apr 12 '16 at 17:02
  • 2
    Looks like a Delphi program. Its TLabel class does not create a window at all, it is a window-less control. So GetWindowText or WM_GETTEXT can never work. If it is going to work at all then you'll have to use UI Automation. – Hans Passant Apr 12 '16 at 17:04

1 Answers1

0

You can use this code to get the caption:

LRESULT result = ::SendMessage(handle_of_window, WM_GETTEXT, 255, (LPARAM)charArray);
if (result > 0)
{
    // Print charArray to get result.
}