Re: GetWindowText() issue



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.


.



Relevant Pages

  • Re: Replacing fgets
    ... One more pointer is that if I use mmap it will solve my problem that is ... on SCO UNIX and on windows. ... char *buffer = 0; ... //Total Buffer Length ...
    (comp.lang.c)
  • Re: Replacing fgets
    ... One more pointer is that if I use mmap it will solve my problem that is ... on SCO UNIX and on windows. ... char *buffer = 0; ... //Total Buffer Length ...
    (comp.lang.c)
  • Re: Cannot return values of char variable
    ... - buffer = ... Since you seem to be trying to return a char pointer ... int id = random; ... content is interpreted as a string. ...
    (comp.lang.c)
  • Re: peer code review/advice needed for noob programmer
    ... And what is with the bizarre use of char **? ... Or using an obsolete data type like 'char'? ... to allocate a buffer here. ... BUt if you pass in a CString & String then the correct loop is ...
    (microsoft.public.vc.mfc)
  • Re: why I can not write to the file after initialize the MFC in a service program
    ... you don't use char, an obsolete data type ... Why do you need an intermedate buffer to write literal strings anyway? ... For example, if AfxWinInit fails, you copy a 45-character string into a ... So you are going to try to initialize MFC EACH TIME THROUGH THE LOOP? ...
    (microsoft.public.vc.mfc)

Loading