Jump to content

How can WinGetText be coded in C/C++ ?


whoa2k
 Share

Recommended Posts

Im trying to code WinGetText in C/C++.

I've read this post before:  '?do=embed' frameborder='0' data-embedContent>>

so i've coded this:

#include <Windows.h>
#include <stdio.h>

BOOL CALLBACK EnumChildProc(
    _In_  HWND hwnd,
    _In_  LPARAM lParam
    )
{
    char strData[10000] = "";
    
    GetWindowText(hwnd, strData, 10000);

    printf("%s\n", strData);

    return TRUE;
}

void main(int argc, char *argv[])
{
    HWND hWnd = (HWND)INVALID_HANDLE_VALUE;

    hWnd = FindWindow("Notepad", NULL);

    EnumChildWindows(hWnd, EnumChildProc, NULL);
    
    system("Pause");
}

but the result is different than using WinGetText, some of the text is missing.

Any ideas how WinGetText / "Visible Text" really works ? ^^

Edited by whoa2k
Link to comment
Share on other sites

Thank you :)

For other people who are interested, this is the new working code that searches a string within the "Visible Strings" of a window:

#include <Windows.h>
#include <stdio.h>

#define _CRT_SECURE_NO_WARNINGS

#pragma warning (disable: 4996)

BOOL CALLBACK EnumChildProc(
    _In_  HWND hwnd,
    _In_  LPARAM lParam
    )
{

    char  strSRCH[]      = "Search this string within the Visible Strings";
    char  strData[10000] = "";
    char* pstrData       = NULL;
    int   len            = 0;

    len = SendMessage(hwnd, WM_GETTEXTLENGTH, 0, 0);
    
    SendMessage(hwnd, WM_GETTEXT, (WPARAM)(len + 1), (LPARAM)strData);

    pstrData = strstr(strData, strSRCH);
    if (NULL != pstrData)
    {
        strcpy((char*)lParam, pstrData);
    }

    return TRUE;
}

void main(int argc, char *argv[])
{
    char strResult[10000] = "";

    HWND hWnd = (HWND)INVALID_HANDLE_VALUE;

    hWnd = FindWindow("Notepad", NULL);

    EnumChildWindows(hWnd, EnumChildProc, (LPARAM)strResult);

    printf("%s\n", strResult);
    
    system("Pause");
}
Edited by whoa2k
Link to comment
Share on other sites

Here is the code for a WinGetText function in C. Maybe useful for somebody.
 

#include <Windows.h>
#include <tchar.h>

HWND WinGetText(const TCHAR* WinTitle, TCHAR* buffer);
BOOL CALLBACK WinGetText_EnumChildProc(_In_ HWND hwnd, _In_ LPARAM lParam);


HWND WinGetText(const TCHAR* WinTitle, TCHAR* buffer)
{
    TCHAR sResult[65535] = { 0 };

    HWND hWnd = FindWindow(NULL, WinTitle);
    if (hWnd == NULL) return NULL;

    EnumChildWindows(hWnd, WinGetText_EnumChildProc, (LPARAM)sResult);
    _tcscpy_s(buffer, sizeof(TCHAR)* _tcslen(sResult), sResult);

    return hWnd;
}

BOOL CALLBACK WinGetText_EnumChildProc(_In_ HWND hwnd, _In_ LPARAM lParam)
{
    static int iOffset = 0;
    TCHAR* Test = (TCHAR*)lParam;

    int len = SendMessage(hwnd, WM_GETTEXTLENGTH, 0, 0);
    TCHAR* strData = (TCHAR*)calloc(len + 3, sizeof(TCHAR));

    SendMessage(hwnd, WM_GETTEXT, (WPARAM)(len + 1), (LPARAM)strData);

    (TCHAR)strData[len] = 0x0D;
    (TCHAR)strData[len + 1] = 0x0A;

    if (Test[0] == 0) iOffset = 0;

    _tcscpy_s((TCHAR*)(lParam + iOffset), sizeof(TCHAR)* (len + 2), strData);
    free(strData);
    iOffset += sizeof(TCHAR)* (len + 2);

    return TRUE;
}


int _tmain(int argc, TCHAR* argv[])
{
    TCHAR strResult[65535] = {0};

    HWND hWnd = WinGetText(_T("Window title 1"), strResult);
    if(hWnd == NULL)
        _tprintf(_T("Error window not found!\n"));
    else
        _tprintf(_T("%s\n"), strResult);

    hWnd = WinGetText(_T("Window title 2"), strResult);
    if (hWnd == NULL)
        _tprintf(_T("Error window not found!\n"));
    else
        _tprintf(_T("%s\n"), strResult);

    return 0;
}

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...