1

I am unable to figure out how to properly use the EM_SETHANDLE mechanism to set the text for an edit control. Get and Set window text will be too slow for my application.

From the documentation I understand that the allocated buffer will be sued by the control and it works partially for me.

When the text is entered in the control, it is seen in the buffer but when the buffer is updated using memcpy etc (no bug in the code), the updated text won't show properly. I even tried EM_SETHANDLE (SetHandle() ) after every update but it fails after a couple of attempts. There is some kind of heap allocation failure. RedrawWindow() won't work either.

I am unable to get any proper info on the net on the usage. Help!

My code, leaving the app specific details, looks like this.

// init
HANDLE m_hMem = HeapAlloc(...)
m_edit.SetHandle(m_hMem)

// on some event
char *pbuf = (char*)m_hMem;
memcpy(...)

thanks in advance

joseph
  • 11
  • 3
  • You are using a multi-line edit control, right? You didn't really post enough sample code for me to tell anything. Have you followed the instructions given in [this MSDN article](http://msdn.microsoft.com/en-us/library/bb775456.aspx)? Check specifically under the section titled "Allocating a Text Buffer". – Cody Gray - on strike Mar 31 '11 at 13:11
  • 2
    This is a message that was interesting to use in Windows version 2. – Hans Passant Mar 31 '11 at 13:42
  • @Cody: 1. Yes, I use multiline edit. 2.The sample I've given is what it actually looks like. 3. Yes I have followed what the article says. It is advised to use Heap functions instead of LocalAlloc so I've done it accordingly. – joseph Mar 31 '11 at 15:21
  • @Hans lol but how could I make the setting and getting of huge text from the control faster. This seems the only was as the memory is actually shared and theres no copying involved. – joseph Mar 31 '11 at 15:23
  • How much text are you trying to stuff into this control? – David Heffernan Mar 31 '11 at 15:29
  • 2
    It doesn't solve any real problems, the control still drags mud when the user uses it to edit. Use a real text editor control, Scintilla for example. – Hans Passant Mar 31 '11 at 15:29
  • 1
    Several KBs!!! SetWindowText will eat that for breakfast. That's absolutely nothing. – David Heffernan Mar 31 '11 at 15:46

2 Answers2

3

The docs for EM_GETHANDLE tells you that this memory has to be movable memory allocated by LocalAlloc.

I'm guess you can get away with something like this:

int cbCh = sizeof(TCHAR) > 1 ? sizeof(TCHAR) : IsUsingComCtlV6() ? sizeof(WCHAR) : sizeof(char);
HLOCAL hOrgMem = SendMessage(hEdit,EM_GETHANDLE,0,0);
HLOCAL hNewMem = LocalReAlloc(hOrgMem,cbCh * cchYourTextLength,LMEM_MOVEABLE);
if (hNewMem)
{
  //LocalLock, assign string, LocalUnlock
  SendMessage(hEdit,EM_SETHANDLE,(WPARAM)hNewMem,0);
}
Anders
  • 97,548
  • 12
  • 110
  • 164
1

Looks like you need to allocate the memory with LocalAlloc. See the companion message EM_GETHANDLE: http://msdn.microsoft.com/en-us/library/bb761576(v=vs.85).aspx

Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175