Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

[C++] Win32 API - Modifying the text of a child window

Hello.

Someone had a problem earlier. Despite the simplicity of the problem: It's slightly off-key for what one may expect.
There are a few solutions that would ideally fit the problem, but this is the one I've chosen:

Code
#define _WIN32_WINNT 0x0500

#include <windows.h>
#include <string>

int main() {

    // The console's window handle (used for MessageBox dialogs)
    HWND hwnd = GetConsoleWindow();

    // The window title we're searching for (the Notepad)
    std::string windowTitle("HackForums.txt - Notepad");

    std::string textToSend("HackForums example\r\nSendMessage() successfully edited the text!\r\n- Irritiable@HackForums");

    // The window handle of the parent handle to the Notepad
    HWND hwndParent = FindWindow(nullptr, windowTitle.c_str());

    // Error is thrown if the value returned is NULL
    if (hwndParent == nullptr) {
        std::string errorBuffer("The parent window's handle returned: NULL");
        MessageBox(hwnd, errorBuffer.c_str(), "HackForums - ERROR", MB_ICONERROR);
        return -1;
    }

    // The window handle of the child handle to the Notepad's internal edit box window
    HWND childHwnd = FindWindowEx(hwndParent, nullptr, "Edit", nullptr);

    // Error is thrown if the value returned is NULL
    if (childHwnd == nullptr) {
        std::string errorBuffer("The child window's handle returned: NULL");
        MessageBox(hwnd, errorBuffer.c_str(), "HackForums - ERROR", MB_ICONERROR);
        return -1;
    }

    // SendMessage dispatches the official message (WM_SETTEXT) to the child window handle with the character data
    if (SendMessage(childHwnd, WM_SETTEXT, 0, reinterpret_cast<LPARAM>(textToSend.c_str())) == false) {
        std::string errorBuffer("WM_SETTEXT returned FALSE (data not sent)");
        MessageBox(hwnd, errorBuffer.c_str(), "HackForums - ERROR", MB_ICONERROR);
        return -1;
    }

    return 0;
}

We use FindWindow() to grab the parent window's handle then FindWindowEx() to grab the child window (edit box, push button, rich text edit, static text, etc) handle.
After this: We call on SendMessage() and pass it the WM_SETTEXT message (macro) along with the desired data that we'd like to re-assign to the child window.

We could amend text to the child window, but that would require that we save the current text of the child control, then concatenate our new text with the old text and then finally call on WM_SETTEXT.

Happy coding, y'all! 👍
Is there any such thing as a woman of honor, and sincerity?

https://www.PutItOnMute.com/

4K HDR, WDR time-lapsed truck driving videos!


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by notahackermanApr 26, 2020 11:32 PM 17 784 Apr 26, 2020 11:32 PM
Last Post: Shadow_Cloud
[C] by SIM-3991524Mar 11, 2020 02:03 PM 11 347 Mar 11, 2020 02:03 PM
Last Post: some1
[Shell] by Emre_Sep 5, 2019 02:55 PM 1 177 Sep 5, 2019 02:55 PM
Last Post: Zedx