Click to See Complete Forum and Search --> : WM_GETTEXT - heap/stack buffer issue


danperrin
June 14th, 2005, 02:39 AM
Can anyone explain why WM_GETTEXT works when using a stack buffer and fails when using a heap buffer. See code samples - this code has been simplified to demonstrate the problem.


//-- this works
#define MYLEN 255
char szText[MYLEN];
LRESULT lResult = ::SendMessage(hWndMyRichedit20A,WM_GETTEXT,MYLEN,(LPARAM)szText);

//-- this fails
DWORD dwLen = 255;
char * pszText = new char[dwLen];
ZeroMemory(pszText,dwLen);
LRESULT lResult = ::SendMessage(hWndMyRichedit20A,WM_GETTEXT,dwLen,(LPARAM)pszText);
if (lResult == 0)
TRACE("Last Error = %d\n",GetLastError());
delete [] pszText;


Note - both cases above work with standard text controls - the 2nd portion only ever fails with RichEdit controls.
- GetLastError() returns zero
- the RichEdit controls are owned by another process (hence the WM_GETTEXT and not GetWindowText(...))



Your comments would be appreciated. Thanks.

NoHero
June 14th, 2005, 09:47 AM
What fails? A memory exception? Does it simply return error? Is the data in the buffer invalid?

danperrin
June 14th, 2005, 10:14 AM
Thanks for your reply. There is no exception or any other form of error. ::SendMessage simply returns 0 (zero) and the text buffer is empty. I have not checked GetLastError() yet - will do that - but I do not expect that there will be an error code.

Darka
June 14th, 2005, 10:43 AM
Try using memset to clear the buffer before you pass it to SendMessage().

ovidiucucu
June 14th, 2005, 10:48 AM
Thanks for your reply. There is no exception or any other form of error. ::SendMessage simply returns 0 (zero) and the text buffer is empty. I have not checked GetLastError() yet - will do that - but I do not expect that there will be an error code.
Never say "hop!" before jumping. ;) Call GetLastError, no matter what are you expecting. No sweat.
If your control (I presume it's a rich edit) is in the same application, call GetWindowText instead, and then for sure GetLastError will say something meaningful in case of failure.
See also remarks about rich edit in WM_GETTEXT documentation from MSDN.

Anyhow both your tries do not work because posted code do not compile... (is it a quiz?)

remove semicolon:
#define MYLEN 255;
add semicolon:
char szText[MYLEN]
cast to LPARAM the last parameter:
::SendMessage(hWndMyRichedit20A,WM_GETTEXT,MYLEN,szText);

danperrin
June 15th, 2005, 06:54 AM
Thanks to all that replied to this question. My apolagies for not being more specific with detail first time round. I am working round the problem at this stage - as there are other ways of extracting RichEdit content.

The exact same code works when passing a HWND to another type of control. It seems to be specific to RichEdit Controls in another process.

I have zero'd the mem buffer, checked last error, casted all parameters to their correct types and so on - so I suspect the problem is not in my code, but possibly some anomaly in Windows that has been documented deep in the MSDN abyss. Please do not spend more time on this - its time to move on.

Thanks Again.