Re: GetWindowText() issue
- From: "Ivo Beltchev" <ivo _@_ adelphia.net>
- Date: Tue, 25 Sep 2007 08:12:35 -0700
Seems like there are 2 things wrong:
1) Sometimes the function returns 6 characters+NUL, sometimes 7 characters. In both cases though it doesn't write past the 7th char, so no buffer overflow here
2) The return value is wrong
If the above is true, it's probably a bug in Windows. Won't be the first one. So here's a possible workaround:
TCHAR Buffer[10];
GetWindowText(hWndCombo, Buffer, 8); // gets either 7 character+NUL, or 8 characters, ignore the return value
Buffer[7]=0; // force-terminate the string
Ivo
"Leslie Milburn" <CDB4W@xxxxxxxxxxxxxxxxxx> wrote in message news:e2cEjfw$HHA.4496@xxxxxxxxxxxxxxxxxxxxxxx
"Kellie Fitton" <KELLIEFITTON@xxxxxxxxx> wrote in message.
news:1190656131.438765.106910@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Sep 24, 8:57 am, "Leslie Milburn" <CD...@xxxxxxxxxxxxxxxxxx> wrote:
GetWindowTextLength()
Also,
IsTextUnicode()
http://msdn2.microsoft.com/en-us/library/ms633521.aspx
http://msdn2.microsoft.com/en-us/library/ms776445.aspx
Kellie.
Hi Kellie,
Yep already thought about that. The GetWindowTextLength is returning the
length of the currently selected item in the Combobox and so it returns 22
which is correct. Remember that I only wanted the first 7 bytes and so I set
the buffer to 8 to allow for 7 bytes plus the NULL, always has worked on
Win9x.
Anyway, for the record I have found the problem, and oh boy I cannot believe
it. I would be grateful if someone else can confirm what I am seeing here.
Here is the minimum code to repeat the problem..... (remember no UNICODE)
{
char Buffer[10];
Buffer[0] = '0';
Buffer[1] = '1';
Buffer[2] = '2';
Buffer[3] = '3';
Buffer[4] = '4';
Buffer[5] = '5';
Buffer[6] = '6';
Buffer[7] = '7';
Buffer[8] = '8';
Buffer[9] = '9';
Index = SendMessage(hWndCombo, CB_ADDSTRING, 0, "ABCDEFGHIJKLMNOP");
SendMessage(hWndCombo, CB_SETCURSEL, Index, 0);
GetWindowText(hWndCombo, Buffer, 7);
}
Running this on Win9x: Buffer contains "ABCDEF" as expected (6 chars plus
the NULL).
Running this on WinXP SP2: Buffer contains "ABCDEFG789" (not expected by me,
7 chars total).
This means no terminating NULL is being stored in the buffer and so we have
a buffer overrun.
Can anyone else confirm this behaviour ?
Thanks
Leslie.
- References:
- GetWindowText() issue
- From: Leslie Milburn
- Re: GetWindowText() issue
- From: Leslie Milburn
- GetWindowText() issue
- Prev by Date: Re: GetWindowText() issue - I have Identified the cause
- Next by Date: Re: Trapping 'application' keys
- Previous by thread: Re: GetWindowText() issue
- Next by thread: Re: GetWindowText() issue
- Index(es):
Relevant Pages
|
Loading