From: Barnabé on
I forgot to said that I am working with VS.NET.

On Dec 14, 9:50 am, "Barnabé" <eric.fr...(a)gmail.com> wrote:
> I tried that function GetSeltext without any success
> I found the solution , in atl there is no CString GetSelText() but BOOL
> GetSelTextBSTR(BSTR& bstrText) const
>
> Here is the GetSelTextBSTR code
>
> BOOL GetSelTextBSTR(BSTR& bstrText) const
> {
> ATLENSURE(::IsWindow(m_hWnd));
> ATLENSURE(bstrText == NULL);
>
> CHARRANGE cr;
> cr.cpMin = cr.cpMax = 0;
> ::SendMessage(m_hWnd, EM_EXGETSEL, 0, (LPARAM)&cr);
> LPSTR lpstrText = (char*)_alloca((cr.cpMax - cr.cpMin + 1) * 2);
> lpstrText[0] = 0;
> if(::SendMessage(m_hWnd, EM_GETSELTEXT, 0, (LPARAM)lpstrText) == 0)
> return FALSE;
>
> bstrText = ::SysAllocString(CA2W(lpstrText));
> return (bstrText != NULL) ? TRUE : FALSE;
> }
>
> They have done almost the same as me. about the "2", I agree that
> sizeof(TCHAR) is way more better.
>
> about the following code you gave, I have tried similar and it failed:
>
> CString s;
> LPTSTR p = s.GetBuffer(cr.cpMax - cr.cpMin + 1);
> ::SendMessage(m_hWnd, EM_GETSELTEXT, 0, (LPARAM)p);
> s.ReleaseBuffer();
> return s;
>
> EM_GETSELTEXT return incorrect characters. LPARAM return LPSTR not
> LPTSTR.
>
> Thanks for your precision.
>
> On Dec 13, 11:23 pm, Joseph M. Newcomer <newco...(a)flounder.com> wrote:
>
> > See below...
> > On 12 Dec 2006 23:08:28 -0800, "Barnabé" <eric.fr...(a)gmail.com> wrote:
>
> > >Thanks for the help,
>
> > >I have checked more in details what was wrong with my code and it
> > >appeared that it's not the clipboard whose making the leak
> > >but the rich editGetSelText function.
>
> > >Here is the code I use:
>
> > >CString CTest::GetSelText()
> > >{
> > > CHARRANGE cr;
> > > cr.cpMin = cr.cpMax = 0;
> > > ::SendMessage(m_hWnd, EM_EXGETSEL, 0, (LPARAM)&cr);****
> > And why doesn't
> > GetSel(cr);
> > do the job? This seems a clumsy way to implement what is already in MFC.
> > ****> LPSTR lpsz = (char*)_alloca((cr.cpMax - cr.cpMin + 1)*2);****
> > This is unbelievably clumsy! Why do something this complex? What's the *2 doing there?
> > Did you mean sizeof(TCHAR)? Why are you using an LPSTR buffer (which is for ASCII
> > characters) and casting to char* if you are in Unicode? Why not
> > LPTSTR lpsz = (LPTSTR)_alloca((cr.cpMax - cpMin + 1) * sizeof(TCHAR));
> > but that's still an awfully complicated way to solve a simple problem. But the REAL
> > solution, if you have to do this (you haven't said if you are working in VS6 or VS.NET,
> > and to do this in VS6 you need to implement it yourself) is
>
> > CString s;
> > LPTSTR p = s.GetBuffer(cr.cpMax - cr.cpMin + 1);
> > ::SendMessage(m_hWnd, EM_GETSELTEXT, 0, (LPARAM)p);
> > s.ReleaseBuffer();
> > return s;
> > and no ugly obsolete _alloca calls are required! No sizeof(TCHAR)! It is about as
> > trivial as you can get. And it works correctly in both Unicode and ANSI versions.
> > ****> lpsz[0] = NULL;****
> > NULL is a pointer value. _T('\0') is a character value. Why are you writing a pointer
> > into a character value in a buffer that is about to be overwritten anyway?
> > ****> ::SendMessage(m_hWnd, EM_GETSELTEXT, 0, (LPARAM)lpsz);****
> > Again, a remarkably clumsy way to handle this! If you're using anything beyond VS6, you
> > can just write
> > return GetSelText();
> > **** > return CString(lpsz);
> > >}****
> > In anything beyond VS6 this whole piece of code could be replaced simply by the GetSelText
> > call on the CRichEditCtrl!
> > ****
>
> > >I have also tried the following one, I think this one would be more
> > >logical as I use unicode.
> > >but for some reason it does not work at all.
>
> > >CString CTest::GetSelection()
> > >{
> > > CHARRANGE cr;
> > > GetRichEditCtrl().GetSel(cr);
>
> > > // Allocate memory for the buffer
> > > LPTSTR lptbuf = new TCHAR[cr.cpMax - cr.cpMin + 1];
>
> > > // Get the selected text from the Rich Edit
> > > GetRichEditCtrl().SendMessage(EM_GETSELTEXT, 0, (LPARAM) lptbuf);
>
> > > CString strSelect(lptbuf);
>
> > > // Release allocated memory
> > > delete [] lptbuf;
>
> > > return strSelect;
> > >}Joseph M. Newcomer [MVP]
> > email: newco...(a)flounder.com
> > Web:http://www.flounder.com
> > MVP Tips:http://www.flounder.com/mvp_tips.htm

From: Joseph M. Newcomer on
You may have the wrong rich edit control installed. Rich edit comes in two versions,
RICHED20A and RICHED20W, and you need the W version to support Unicode.
joe

On 13 Dec 2006 23:50:43 -0800, "Barnab�" <eric.franc(a)gmail.com> wrote:

>I tried that function GetSeltext without any success
>I found the solution , in atl there is no CString GetSelText() but BOOL
>GetSelTextBSTR(BSTR& bstrText) const
>
>Here is the GetSelTextBSTR code
>
> BOOL GetSelTextBSTR(BSTR& bstrText) const
> {
> ATLENSURE(::IsWindow(m_hWnd));
> ATLENSURE(bstrText == NULL);
>
> CHARRANGE cr;
> cr.cpMin = cr.cpMax = 0;
> ::SendMessage(m_hWnd, EM_EXGETSEL, 0, (LPARAM)&cr);
> LPSTR lpstrText = (char*)_alloca((cr.cpMax - cr.cpMin + 1) * 2);
> lpstrText[0] = 0;
> if(::SendMessage(m_hWnd, EM_GETSELTEXT, 0, (LPARAM)lpstrText) == 0)
> return FALSE;
>
> bstrText = ::SysAllocString(CA2W(lpstrText));
> return (bstrText != NULL) ? TRUE : FALSE;
> }
>
>They have done almost the same as me. about the "2", I agree that
>sizeof(TCHAR) is way more better.
>
>about the following code you gave, I have tried similar and it failed:
>
> CString s;
> LPTSTR p = s.GetBuffer(cr.cpMax - cr.cpMin + 1);
> ::SendMessage(m_hWnd, EM_GETSELTEXT, 0, (LPARAM)p);
> s.ReleaseBuffer();
> return s;
>
>EM_GETSELTEXT return incorrect characters. LPARAM return LPSTR not
>LPTSTR.
>
>Thanks for your precision.
>
>On Dec 13, 11:23 pm, Joseph M. Newcomer <newco...(a)flounder.com> wrote:
>> See below...
>> On 12 Dec 2006 23:08:28 -0800, "Barnab�" <eric.fr...(a)gmail.com> wrote:
>>
>> >Thanks for the help,
>>
>> >I have checked more in details what was wrong with my code and it
>> >appeared that it's not the clipboard whose making the leak
>> >but the rich editGetSelText function.
>>
>> >Here is the code I use:
>>
>> >CString CTest::GetSelText()
>> >{
>> > CHARRANGE cr;
>> > cr.cpMin = cr.cpMax = 0;
>> > ::SendMessage(m_hWnd, EM_EXGETSEL, 0, (LPARAM)&cr);****
>> And why doesn't
>> GetSel(cr);
>> do the job? This seems a clumsy way to implement what is already in MFC.
>> ****> LPSTR lpsz = (char*)_alloca((cr.cpMax - cr.cpMin + 1)*2);****
>> This is unbelievably clumsy! Why do something this complex? What's the *2 doing there?
>> Did you mean sizeof(TCHAR)? Why are you using an LPSTR buffer (which is for ASCII
>> characters) and casting to char* if you are in Unicode? Why not
>> LPTSTR lpsz = (LPTSTR)_alloca((cr.cpMax - cpMin + 1) * sizeof(TCHAR));
>> but that's still an awfully complicated way to solve a simple problem. But the REAL
>> solution, if you have to do this (you haven't said if you are working in VS6 or VS.NET,
>> and to do this in VS6 you need to implement it yourself) is
>>
>> CString s;
>> LPTSTR p = s.GetBuffer(cr.cpMax - cr.cpMin + 1);
>> ::SendMessage(m_hWnd, EM_GETSELTEXT, 0, (LPARAM)p);
>> s.ReleaseBuffer();
>> return s;
>> and no ugly obsolete _alloca calls are required! No sizeof(TCHAR)! It is about as
>> trivial as you can get. And it works correctly in both Unicode and ANSI versions.
>> ****> lpsz[0] = NULL;****
>> NULL is a pointer value. _T('\0') is a character value. Why are you writing a pointer
>> into a character value in a buffer that is about to be overwritten anyway?
>> ****> ::SendMessage(m_hWnd, EM_GETSELTEXT, 0, (LPARAM)lpsz);****
>> Again, a remarkably clumsy way to handle this! If you're using anything beyond VS6, you
>> can just write
>> return GetSelText();
>> **** > return CString(lpsz);
>> >}****
>> In anything beyond VS6 this whole piece of code could be replaced simply by the GetSelText
>> call on the CRichEditCtrl!
>> ****
>>
>>
>>
>> >I have also tried the following one, I think this one would be more
>> >logical as I use unicode.
>> >but for some reason it does not work at all.
>>
>> >CString CTest::GetSelection()
>> >{
>> > CHARRANGE cr;
>> > GetRichEditCtrl().GetSel(cr);
>>
>> > // Allocate memory for the buffer
>> > LPTSTR lptbuf = new TCHAR[cr.cpMax - cr.cpMin + 1];
>>
>> > // Get the selected text from the Rich Edit
>> > GetRichEditCtrl().SendMessage(EM_GETSELTEXT, 0, (LPARAM) lptbuf);
>>
>> > CString strSelect(lptbuf);
>>
>> > // Release allocated memory
>> > delete [] lptbuf;
>>
>> > return strSelect;
>> >}Joseph M. Newcomer [MVP]
>> email: newco...(a)flounder.com
>> Web:http://www.flounder.com
>> MVP Tips:http://www.flounder.com/mvp_tips.htm
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Barnabé on
Thanks the problem was there.

I just updated the RICHED20A to RICHED20W in the .rc file and I could
use GetSelText().


Joseph M. Newcomer wrote:

> You may have the wrong rich edit control installed. Rich edit comes in two versions,
> RICHED20A and RICHED20W, and you need the W version to support Unicode.
> joe
>
> On 13 Dec 2006 23:50:43 -0800, "Barnabé" <eric.franc(a)gmail.com> wrote:
>
> >I tried that function GetSeltext without any success
> >I found the solution , in atl there is no CString GetSelText() but BOOL
> >GetSelTextBSTR(BSTR& bstrText) const
> >
> >Here is the GetSelTextBSTR code
> >
> > BOOL GetSelTextBSTR(BSTR& bstrText) const
> > {
> > ATLENSURE(::IsWindow(m_hWnd));
> > ATLENSURE(bstrText == NULL);
> >
> > CHARRANGE cr;
> > cr.cpMin = cr.cpMax = 0;
> > ::SendMessage(m_hWnd, EM_EXGETSEL, 0, (LPARAM)&cr);
> > LPSTR lpstrText = (char*)_alloca((cr.cpMax - cr.cpMin + 1) * 2);
> > lpstrText[0] = 0;
> > if(::SendMessage(m_hWnd, EM_GETSELTEXT, 0, (LPARAM)lpstrText) == 0)
> > return FALSE;
> >
> > bstrText = ::SysAllocString(CA2W(lpstrText));
> > return (bstrText != NULL) ? TRUE : FALSE;
> > }
> >
> >They have done almost the same as me. about the "2", I agree that
> >sizeof(TCHAR) is way more better.
> >
> >about the following code you gave, I have tried similar and it failed:
> >
> > CString s;
> > LPTSTR p = s.GetBuffer(cr.cpMax - cr.cpMin + 1);
> > ::SendMessage(m_hWnd, EM_GETSELTEXT, 0, (LPARAM)p);
> > s.ReleaseBuffer();
> > return s;
> >
> >EM_GETSELTEXT return incorrect characters. LPARAM return LPSTR not
> >LPTSTR.
> >
> >Thanks for your precision.
> >
> >On Dec 13, 11:23 pm, Joseph M. Newcomer <newco...(a)flounder.com> wrote:
> >> See below...
> >> On 12 Dec 2006 23:08:28 -0800, "Barnabé" <eric.fr...(a)gmail.com> wrote:
> >>
> >> >Thanks for the help,
> >>
> >> >I have checked more in details what was wrong with my code and it
> >> >appeared that it's not the clipboard whose making the leak
> >> >but the rich editGetSelText function.
> >>
> >> >Here is the code I use:
> >>
> >> >CString CTest::GetSelText()
> >> >{
> >> > CHARRANGE cr;
> >> > cr.cpMin = cr.cpMax = 0;
> >> > ::SendMessage(m_hWnd, EM_EXGETSEL, 0, (LPARAM)&cr);****
> >> And why doesn't
> >> GetSel(cr);
> >> do the job? This seems a clumsy way to implement what is already in MFC.
> >> ****> LPSTR lpsz = (char*)_alloca((cr.cpMax - cr.cpMin + 1)*2);****
> >> This is unbelievably clumsy! Why do something this complex? What's the *2 doing there?
> >> Did you mean sizeof(TCHAR)? Why are you using an LPSTR buffer (which is for ASCII
> >> characters) and casting to char* if you are in Unicode? Why not
> >> LPTSTR lpsz = (LPTSTR)_alloca((cr.cpMax - cpMin + 1) * sizeof(TCHAR));
> >> but that's still an awfully complicated way to solve a simple problem. But the REAL
> >> solution, if you have to do this (you haven't said if you are working in VS6 or VS.NET,
> >> and to do this in VS6 you need to implement it yourself) is
> >>
> >> CString s;
> >> LPTSTR p = s.GetBuffer(cr.cpMax - cr.cpMin + 1);
> >> ::SendMessage(m_hWnd, EM_GETSELTEXT, 0, (LPARAM)p);
> >> s.ReleaseBuffer();
> >> return s;
> >> and no ugly obsolete _alloca calls are required! No sizeof(TCHAR)! It is about as
> >> trivial as you can get. And it works correctly in both Unicode and ANSI versions.
> >> ****> lpsz[0] = NULL;****
> >> NULL is a pointer value. _T('\0') is a character value. Why are you writing a pointer
> >> into a character value in a buffer that is about to be overwritten anyway?
> >> ****> ::SendMessage(m_hWnd, EM_GETSELTEXT, 0, (LPARAM)lpsz);****
> >> Again, a remarkably clumsy way to handle this! If you're using anything beyond VS6, you
> >> can just write
> >> return GetSelText();
> >> **** > return CString(lpsz);
> >> >}****
> >> In anything beyond VS6 this whole piece of code could be replaced simply by the GetSelText
> >> call on the CRichEditCtrl!
> >> ****
> >>
> >>
> >>
> >> >I have also tried the following one, I think this one would be more
> >> >logical as I use unicode.
> >> >but for some reason it does not work at all.
> >>
> >> >CString CTest::GetSelection()
> >> >{
> >> > CHARRANGE cr;
> >> > GetRichEditCtrl().GetSel(cr);
> >>
> >> > // Allocate memory for the buffer
> >> > LPTSTR lptbuf = new TCHAR[cr.cpMax - cr.cpMin + 1];
> >>
> >> > // Get the selected text from the Rich Edit
> >> > GetRichEditCtrl().SendMessage(EM_GETSELTEXT, 0, (LPARAM) lptbuf);
> >>
> >> > CString strSelect(lptbuf);
> >>
> >> > // Release allocated memory
> >> > delete [] lptbuf;
> >>
> >> > return strSelect;
> >> >}Joseph M. Newcomer [MVP]
> >> email: newco...(a)flounder.com
> >> Web:http://www.flounder.com
> >> MVP Tips:http://www.flounder.com/mvp_tips.htm
> Joseph M. Newcomer [MVP]
> email: newcomer(a)flounder.com
> Web: http://www.flounder.com
> MVP Tips: http://www.flounder.com/mvp_tips.htm