Search code examples
c#.netwindowswinapimessagebox

How do I read MessageBox text using WinAPI


How do I read a message of standard Win message box (Info)?

Using

SendMessage(this.HandleControl, WM_GETTEXT, builder.Capacity, builder);

I can only read the header of the message box or the text of the button, but not the message itself.

thanks.

Notes (from Q&A):

this.HandleControl is a handler to the message box window

Spy++ shows no child controls bar the button. That's what it made me thinking that Message Boxes have their own way of keeping text w/out using labels

It's a legacy app written with delphi, the button's class is TButton as per Spy++, but still there's no controls except of button inside the dialog window.

After checking a notepad window, both Image & Text are 'selectable', I guess my app doesn't use a std MessageBox. still, how do I go about extracting the text out of the thing? I can see that no labels in my delphi app can be selected by Spy++ Finder tool.


Solution

  • The message text is in a label control on the modal MessageBox dialog window. You have to get the window handle to the MessageBox dialog (win32 API FindWindow) then retrieve the window handle to the control (win32 API GetDlgItem) and then retrieve the text from that window win32 API GetWindowText).

    EDIT --

    TCHAR text[51] = {0};
    HWND msgBox = ::FindWindow(NULL, TEXT("MessageBoxCaption"));
    HWND label = ::GetDlgItem(msgBox, 0xFFFF);
    ::GetWindowText(label, text, sizeof(text)-1);