Posted on 2002-03-21
I'm using VC++ 6.0/SP4 on Windows NT 2000/SP2. The problem I've discovered is inconsistencies in what ::GetWindowText() returns when it is passed a handle to a RICHEDIT control.
In the example below IDC_EDIT is a RICHEDIT control and there has been over 255 characters entered in the field.
int nResult = ::GetWindowText(GetDlgItem(IDC_EDIT)->m_hWnd, str.GetBuffer(256), 256);
strResult.Format("::GetWindowText() returned: %d Value: \n%s", nResult, (LPCTSTR) str);
On my machine (NT2000) ::GetWindowText() returns 0 and str is empty. Calling ::GetLastError() returns 122 (ERROR_INSUFFICIENT_BUFFER). A true statement, but the documentation says:
HWND hWnd, // handle to window or control
LPTSTR lpString, // text buffer
int nMaxCount // maximum number of characters to copy
[in] Handle to the window or control containing the text.
[out] Pointer to the buffer that will receive the text.
[in] Specifies the maximum number of characters to copy to the buffer, including the NULL character. If the text exceeds this limit, it is truncated.
On NT4/SP5 and on Windows 95 the function returns 255 and str is equal to the first 255 characters.
I'd have expected XP to act the same as NT 2000, and it does sort of. It returns 0, but str is set to the 1st 256 of data. This is alarming, because I first discovered this problem in the DDX_Text() processing. The AfxSetWindowText() utility function located in WINUTIL.CPP defines a buffer like so:
Then calls the API like so:
::GetWindowText(hWndCtrl, szOld, _countof(szOld))
I wonder if it returned the NULL also? If so, then the stack is messed up.
Has anyone seen this behavior before?