whoa2k Posted February 13, 2014 Posted February 13, 2014 (edited) 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 February 13, 2014 by whoa2k
JohnOne Posted February 13, 2014 Posted February 13, 2014 (edited) GetWindowText cannot retrieve the text of a control in another application. EDIT: Maybe you want to send WM_GETTEXT message to the controls. Edited February 13, 2014 by JohnOne AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans.
jaberwacky Posted February 13, 2014 Posted February 13, 2014 When you say some of the text is missing, do you mean that it's truncated or random sections are missing from the middle? Does it affect every line? Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum?
whoa2k Posted February 13, 2014 Author Posted February 13, 2014 (edited) 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: expandcollapse popup#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 February 13, 2014 by whoa2k
funkey Posted February 13, 2014 Posted February 13, 2014 Here is the code for a WinGetText function in C. Maybe useful for somebody. expandcollapse popup#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 tobuild bigger and better idiot-proof programs, and the Universetrying to produce bigger and better idiots.So far, the Universe is winning.
whoa2k Posted February 16, 2014 Author Posted February 16, 2014 Thank you, the code is good and working. But any idea why it's not working to read text from Chrome/Firefox ? Im trying to get the "visible text" from Chrome and Firefox.. and it's not working
Richard Robertson Posted February 17, 2014 Posted February 17, 2014 Web browsers don't use standard windows controls. You need to use specialized tools for each browser to pull web page text.
junkew Posted February 20, 2014 Posted February 20, 2014 I would suggest to use interfaces like iaccessible, iaccessible2, isimpledom, iuiautomation search for MSAA and accessibility in C++ see from AutoIT some of this logic in '?do=embed' frameborder='0' data-embedContent>> FAQ 31 How to click some elements, FAQ 40 Test automation with AutoIt, Multithreading CLR .NET Powershell CMDLets
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now