views:

30

answers:

2

I have 2 CRichEditCtrls. One is part of a dialog template, created automatically. When I call GetSelText on it, the bytes returned are one byte per char, i.e I'll get back char *str={'a','n','d'}. The 2nd control is created dynamically using the Create method, and the data returned calling GetSelText is returned in 2-byte characters: char *str={'a',0,'n',0,'d',0}.

This is making things a real pain... see this topic. One way works with one control, one way works with the other.

I can't even see how two controls (on the same dialog) can have different behavior like this. I don't see a way to tell the one created dynamically what way to work.

How can this be going on? The control created dynamically is the odd one out in our application, so that's the one that needs to be changed...

Here is the code I'm using:

ASSERT(::IsWindow(m_hWnd));
CHARRANGE cr;
cr.cpMin = cr.cpMax = 0;
::SendMessage(m_hWnd, EM_EXGETSEL, 0, (LPARAM)&cr);
CString strText;
LPTSTR lpsz=strText.GetBufferSetLength((cr.cpMax - cr.cpMin + 1) * 2);
lpsz[0] = NULL;
long nLen = ::SendMessage(m_hWnd, EM_GETSELTEXT, 0, (LPARAM)lpsz);
lpsz[nLen] = NULL;

for(long i=0;i<nLen;++i)
{
    TRACE("lpsz[%d] (%d bytes) = %d {",i,sizeof(lpsz[i]),lpsz[i]);
    char *pc = (char *)&lpsz[i];
    for(int j=0;j<sizeof(lpsz[i]);++j)
    {
        TRACE(" %d(%c)",pc[j],pc[j] ? pc[j] : '#');
    }
    TRACE("}\n");
}
strText.ReleaseBuffer();
return CString(strText);

The output from my dialog-template control:

lpsz[0] (2 bytes) = 28257 { 97(a) 110(n)}
lpsz[1] (2 bytes) = 100 { 100(d) 0(#)}
lpsz[2] (2 bytes) = 52685 { -51(Í) -51(Í)}

And from my dynamically created control:

lpsz[0] (2 bytes) = 97 { 97(a) 0(#)}
lpsz[1] (2 bytes) = 110 { 110(n) 0(#)}
lpsz[2] (2 bytes) = 100 { 100(d) 0(#)}
+3  A: 

Your first richedit ctrl is of class "RichEdit20A" second one is "RichEdit20W" - wide char

One thing you can do is change the class of your first richeditctrl in RC file to RichEdit20W So, both gives the value in wide_char's.

See this KB article. This will help http://support.microsoft.com/kb/261171

SysAdmin
1)Like I said, the whole app uses dialogs, this is the only place a control is dynamically created. So how can I make the 2nd version match the dialog version?
John
2)Also, my app is set to be unicode. I wonder _why_ MSVC is creating richtext controls as RichEdit20A by default, is this wrong? I confirmed that it is doing this in the .rc file. But I can't really update all my dialogs in many apps to match that one manually-created control.
John
You can use CreateWindow API with class "RichEdit20A" to dynamically create it rather than using CRichEditCtrl
SysAdmin
Awesome, it works fine now. I'd _never_ have thought of this!
John
+1  A: 

Use the IsWindowUnicode() API to find out whether the control is ANSI or Unicode. Ansi controls return single-byte strings, unicode controls return double-byte strings.

And if you change your resourcefile to use RichEdit20W instead of RichEdit20A, you have to check from time to time to make sure it stays as RichEdit20W! See here for a detailed recipe why this is necessary. Note: VS2010 seems to have this bug finally fixed.

Stefan
Neat. Thanks for mentioning it.
John