0

In visual studio 2015, When I am trying to write less than 4 characters in Rich Text Box, it gives exception (below is the attachment)

After doing Debugging, we came to know that Ensure() is causing the Exception:

int CRichEditCtrl::GetLine(_In_ int nIndex, _Out_writes_to_(nMaxLength, return) LPTSTR lpszBuffer, _In_ int nMaxLength) const
    {
        ASSERT(::IsWindow(m_hWnd));
        ENSURE(sizeof(nMaxLength)<=nMaxLength*sizeof(TCHAR)&&nMaxLength>0);
        *(LPINT)lpszBuffer = nMaxLength;
        return (int)::SendMessage(m_hWnd, EM_GETLINE, nIndex, (LPARAM)lpszBuffer);
    }

When we are giving less that 4 characters in Rich Text Box,

sizeof(nMaxLength)<=nMaxLength*sizeof(TCHAR)

in this case sizeof(nMaxLength) = 4 and nMaxLength*sizeof(TCHAR) = 3 So, 3<4 is causing the Exception.

Now, I need help in which way Shall I give less than 4 charcters in a Rich Text Box, so that this function works and doesnt gives an Exception.

  • On a similar [question](https://stackoverflow.com/q/46371932/7571258) I've come to the [conclusion](https://stackoverflow.com/a/46374521/7571258) that `CRichEditCtrl::GetLine()` overload with 3 parameters is buggy. – zett42 Sep 23 '17 at 13:45

1 Answers1

0

Sure it works.

This function GETs a line. You have to offer a buffer, large enough to fir the contents. The smallest buffer you are allowed to pass to the message is the size of an integer (4 bytes).

The size of the that you receive is returned by the function.

This code always work for any length

CString strTemp;
nMinLength = min(nLineLength,sizeof(int));
int iLen = m_ItemTextCtrl.GetLine(k, strtemp.GetBuffer(nMinLength), nLineLength); 
strTetmp.ReleaseBuffer(iLen);

Passing data to the RTF control is done by streaming in data or using WM_SETTEXT or SetWindowText

xMRi
  • 14,982
  • 3
  • 26
  • 59
  • Thanks for the Reply. Do you mean minimum 4 characters i have to provide in the Rich Text Box? But I want to pass less than 4 characters as well in the Rich Text Box. So How can I pass less than 4 characters? Please help me on the same. – Sonam Agrawal Sep 22 '17 at 07:00
  • But, I want to call CRichEditCtrl::GetLine() function after giving characters < 4, and it is not accepting characters <4 and throwing assert fail error. Can you suggest work around so that CRichEditCtrl::GetLine() will accept characters < 4? – Sonam Agrawal Sep 22 '17 at 08:09
  • @SonamAgrawal You are propably using the function incorrectly. Please edit your question to show the code where you are calling `GetLine()`. – zett42 Sep 22 '17 at 08:21
  • m_ItemTextCtrl.GetLine(k, strtemp, nLineLength); and nLineLength < 4 throws exception. – Sonam Agrawal Sep 22 '17 at 08:34
  • 1
    Again: You need to supply a buffer. You are forced to specify a buffer equal larger than 4 chars. If the line has less 4 chars, the buffer is only used up to that point. The length you get is returned. I already answered the question. – xMRi Sep 22 '17 at 08:35
  • we are giving strtemp buffer whose size is > 4 as below: char strtemp[kEclMaxDuText]; (kEclMaxDuText = 54), but still getting the exception. – Sonam Agrawal Sep 22 '17 at 08:39
  • 1
    Than specify nMinLength with this size! – xMRi Sep 22 '17 at 08:41
  • CString strttemp; int nMinLength = min(nLineLength, sizeof(int)); int iLen = m_ItemTextCtrl.GetLine(k, strttemp.GetBuffer(nMinLength), nLineLength); I added the above code and executed but still exception is coming. – Sonam Agrawal Sep 22 '17 at 09:01
  • 1
    @xMRi Error in your sample code. It should be `nMinLength = max(nLineLength,sizeof(int));`. Currently your `nMinLength` is always 4. And last argument to `GetLine()` should be `nMinLength`. – zett42 Sep 22 '17 at 09:02
  • I aslo Specified nMinLength = 54, then also it throws exception. – Sonam Agrawal Sep 22 '17 at 09:04
  • m_ItemTextCtrl.GetLine(k, strtemp.GetBuffer(nMinLength), nLineLength);Yes it will work for nLineLength = 4 or > 4 But I want to pass nLineLength = 1, 2 or 3 , because my code wants to give character <4 in rich edit text box, in that scenario what to do is my question? – Sonam Agrawal Sep 22 '17 at 09:09
  • How m_ItemTextCtrl.GetLine(k, strttemp.GetBuffer(nMinLength), nLineLength); will work if I am passing nLineLength = 1, 2 or 3?is my question. because i want to give "s", "so" or "son" as the values in rich text box – Sonam Agrawal Sep 22 '17 at 09:11
  • Than get the minium number of 4 chars and truncate afterwards. Its a function to get a whole line! You say give, do you mean get? – xMRi Sep 22 '17 at 09:17
  • My whole line has only 3 characters then how to handle? no give means not get, it means my rich text box has only 3 characters and nLineLength = 1, 2 is the length of rich text box line that has either 1, 2 or 3 chars – Sonam Agrawal Sep 22 '17 at 09:23
  • And were is the problem? Again, set a length larger than this at least 4. The real length of the line is returned by GetLine. Did you check my code? – xMRi Sep 22 '17 at 09:55
  • @Mr.C64 I came to the same conclusion a few comments before. – zett42 Sep 22 '17 at 20:18