I have following code, which can load text from all child windows of a specified parent window. It works fine, but sometimes, there are some parent windows (such as opened Notepad with a very long C++ source file) those have large amount of text and cause a buffer overflow.
BOOL CALLBACK EnumChildProc(__in HWND hWnd, __in LPARAM lParam) {
LRESULT TEXT_LENGTH = NULL;
WCHAR szText[32767];
LPWSTR szWindowText;
UINT nBuffer = NULL, nText = NULL;
szWindowText = reinterpret_cast<LPWSTR>(lParam); szText[0] = L'\0';
nBuffer = (UINT)wcslen(szWindowText);
TEXT_LENGTH = SendMessage(hWnd, WM_GETTEXTLENGTH, NULL, NULL);
if (TEXT_LENGTH > NULL)
{
SendMessage(hWnd, WM_GETTEXT, (WPARAM)32767, reinterpret_cast<LPARAM>(&szText));
szText[TEXT_LENGTH] = L'\n'; szText[TEXT_LENGTH + 1] = L'\0';
while ((nBuffer < 32766) && (szText[nText] != L'\0'))
{ szWindowText[nBuffer++] = szText[nText++]; }
szWindowText[nBuffer] = L'\0';
}
return TRUE;
}
The line SendMessage(hWnd, WM_GETTEXT, (WPARAM)32767, reinterpret_cast<LPARAM>(&szText));
sometimes causes a buffer overflow and my application crashes.
I know how to detect this overflow like if (TEXT_LENGTH > 32767)
, but I cannot dynamically increase the size of the buffer szText
.
As the question title mentions, I don't want to increase its size, I just want to truncate and null terminate return text to maximum buffer size of 32767
(if TEXT_LENGTH
is over 32767
) and assign it to szWindowText
to be used for other purposes.
Any help is greatly appreciated.
A few things:
DWORD l = SendMessage(hWnd, WM_GETTEXTLENGTH, NULL, NULL);
if (l > 0){
TCHAR *szText = new TCHAR[l + 1];
SendMessage(hWnd, WM_GETTEXT, (WPARAM)l + 1, reinterpret_cast<LPARAM>(szText));
// use szText
delete[] szText;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With