We help IT Professionals succeed at work.

Read the caption text from a programs dialog box

856 Views
Last Modified: 2013-12-03
I want to know if it is possible to get the text caption of an emerging dialog box of a program by using the Windows API or something else, the only constant data that I know it has is the "Class" value (#32770 (Dialog): is the value obtained on the Microsoft Spy++) and the caption of the Main Program Window of course, the control handle value of the dialog box is changing every time the program loads, if the Spy++ is able to read this value there should be a way to automate this, I would like to do this using C#... Any help would be greatly appreciated.

Thanks.
Comment
Watch Question

There is more that one way to do that. For example, you can use a Windows API call to enumerate all top-level windows (EnumWindows), find the window that contains the previously known caption (GetWindowText), find the child of this window (EnumChildWindows), and then read the window text (GetWindowText).

_______________

  Nayer Naguib

Author

Commented:
Any example?

Or something to get started with...?
Here is an example using C#. The example uses FindWindow to locate an app and then closes the associated Window. Once you the dialog, instead of closing it, you can use GetWindowText.

http://www.codeproject.com/csharp/cskillapp.asp project with same C# code that demonstrates using FindWindow.

http://www.csharphelp.com/archives2/archive301.html another example demonstrating the GetWindowText function
Looks like EnumChildWindows does not enumerate dialog boxes, and we do not also need EnumWindows.

The example below will display a message box containing the title of the warning dialog box that is displayed when you want to exit Notepad without saving text.

Notes:

1. The dialog box should be already open when the code runs.
2. You need to replace "Untitled - Notepad" with the caption of the parent window.

___________________________________________________________

using System;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace TextCapture
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll")]
        public static extern int FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll")]
        public static extern int GetWindow(int handle, int cmd);
        [DllImport("User32.Dll")]
        public static extern void GetWindowText(int h, StringBuilder s, int nMaxCount);

        const int GW_HWNDPREV = 3;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int handle = FindWindow(null, "Untitled - Notepad");
            int childHandle = GetWindow(handle, GW_HWNDPREV);
            StringBuilder title = new StringBuilder(256);
            string text = "";
            GetWindowText(childHandle, title, 256);
            text = title.ToString().Trim();
            MessageBox.Show("Title of dialog box: " + text);
        }
    }
}

___________________________________________________________

_______________

  Nayer Naguib

Author

Commented:
Maybe I'm not clear enough, read the message (the caption from a label inside the DialogBox, not the DialogBox title).. I want to be able to read the message from the DialogBox (Just like the MessageBox) the message box will show some crucial data to me in this message...

Anyway thanks for your answers!...
The following piece of code will capture and display the text of every control on the dialog box window. You will need to do some changes to the code in order to have the application only process the required label text.

________________________________________________________

using System;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace TextCapture
{
      public partial class Form1 : Form
      {
            [DllImport("user32.dll")]
            public static extern int FindWindow(string lpClassName, string lpWindowName);
            [DllImport("user32.dll")]
            public static extern int GetWindow(int handle, int cmd);
            [DllImport("User32.Dll")]
            public static extern void GetWindowText(int h, StringBuilder s, int nMaxCount);
            [DllImport("User32.dll")]
            public static extern Boolean EnumChildWindows(int hWndParent, Delegate lpEnumFunc, int lParam);

            const int GW_HWNDPREV = 3;

            public delegate void Callback(int hWnd,int lParam);

            public Form1()
            {
                  InitializeComponent();
            }

            private void Form1_Load(object sender, EventArgs e)
            {
                  int handle = FindWindow(null, "Untitled - Notepad");
                  int childHandle = GetWindow(handle, GW_HWNDPREV);
                  Callback myCallBack = new Callback(GetMessage);
                  EnumChildWindows(childHandle, myCallBack, 0);
            }

            public void GetMessage(int hWnd, int lParam)
            {
                  StringBuilder contents = new StringBuilder(256);
                  string txt="";
                  GetWindowText(hWnd, contents, 256);
                  txt = contents.ToString().Trim();
                  MessageBox.Show("Text shown on control: "+ txt);
            }
      }
}

________________________________________________________

_______________

  Nayer Naguib

Author

Commented:
The last code you have provided me it just doesn't do anything, even with the Notepad example, but anyway thanks.

This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Forced accept.

Computer101
EE Admin

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.