Visual Studio 2013 Community Edition on windows 7 Ultimate.
Dialog app with two rich edit boxes. The boxes are set to auto vertical scroll true, horizontal auto scroll false.
I am attempting to read from one rich edit box one line at a time and print these lines out to the second rich edit box, one line at a time.
I am using the following code to read the line:
The problem is that LineLength() includes some word wrap characters that are put into the text when it is typed into the first rich edit box. When this line is printed out in the second box, these characters show up. The characters are '\uCDCD' and I tried to use CString::Find to trim the line length that way but Find couldn't locate the characters.
Anyone have any idea how to read or print the line without showing these characters?
According to https://msdn.microsoft.com/en-us/library/z0z83tcz.aspx
" nMaxLength
Maximum number of characters that can be copied into lpszBuffer. The second form of GetLine places this value into the first word of the buffer specified by lpszBuffer."
Also, why do you think that '\uCDCD' is "some word wrap characters"?
Last edited by VladimirF; June 30th, 2015 at 04:47 PM.
Reason: Fixed the link
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio: FeinWindows - replacement windows manager for Visual Studio, and more...
I have no difficulty in copying the line, its just that the extra "control" characters show up when I write the line to the second richedit box.
I copied one of the characters from the richedit box into the code to use CString.Find() and thats what the compiler identified it as.
I've fixed the link, please try it again.
How do you place that line into the second box?
Please note that Visual Studio writes 0xCD bytes into uninitialized memory - could it be that you are seeing those bytes?
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio: FeinWindows - replacement windows manager for Visual Studio, and more...
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
Yes I am terminating the string and popping the buffer, I just didn't show it, my bad.
For what its worth, its a unicode project if maybe that has something to do with it.
Here is the code I am using:
Code:
CString Line1, Line2;
int nLineLength = 0;
//Get the length of the second line
nLineLength = m_RichEditVar.LineLength(1);
//Get the text from the second line
m_RichEditVar.GetLine(1, Line2.GetBufferSetLength(nLineLength + 1), nLineLength);
//Terminate the string
Line2.SetAt(nLineLength, _T('\0'));
Line2.ReleaseBuffer(nLineLength + 1);
//Print out line to second box
m_RichEdit2Var.SetWindowTextW(Line2);
Yes I am terminating the string and popping the buffer, I just didn't show it, my bad.
For what its worth, its a unicode project if maybe that has something to do with it.
Here is the code I am using:
Is that the EXACT code that you are using?
1. CString's GetBufferSetLength() *will* place 0xCDCD into allocated buffer in DEBUG build.
2. Your code works for me in UNICODE project.
3. Your manipulation with CString's internal buffer are not as suggested by MSDN:
- If you pass the string's length to ReleaseBuffer() - you don't need to place the terminating 0 yourself;
- if you *do* add that 0 - you should pass -1 as a length.
4. Your symptoms indicate that the terminating 0 was placed too far (passed the end of the string). Could you do additional debugging?
- what is the string length returned by m_RichEditVar.LineLength(1);
- if you look at the CString's internal buffer in the debugger's Memory window at the time you put that terminating 0 and release the buffer - do you see the 0 being written? In the correct place?
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio: FeinWindows - replacement windows manager for Visual Studio, and more...
3. Your manipulation with CString's internal buffer are not as suggested by MSDN:
- If you pass the string's length to ReleaseBuffer() - you don't need to place the terminating 0 yourself;
- if you *do* add that 0 - you should pass -1 as a length.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
So what does it say about MS documentation?
Haven't you been a part of MS Doc discussion here today already?
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio: FeinWindows - replacement windows manager for Visual Studio, and more...
I ran it thru the debugger and the line length came in at 44 and the CString Line2 was showing the text AND the weird characters. I noticed though that the return value coming from GetLine was showing the correct value of 40.
So as a work around I used that value to terminate the string and that got rid of the weird characters.
@Vlad
Yes that is the exact code I am using.
I am modifying the font when the dialog initiates, that could be the reason I am getting the strange results?
I ran it thru the debugger and the line length came in at 44 and the CString Line2 was showing the text AND the weird characters. I noticed though that the return value coming from GetLine was showing the correct value of 40.
So as a work around I used that value to terminate the string and that got rid of the weird characters.
@Vlad
Yes that is the exact code I am using.
Wait! Who shows 40 and who - 44? And when?
In your code:
Code:
nLineLength = m_RichEditVar.LineLength(1);
is nLineLength 40 or 44? (I've counted 40 characters in your second line, including trailing space).
The GetBufferSetLength() will allocate *AT LEAST* nLineLength + 1 bytes (may be more), filling them with 0xCD. So right after
you would see those "strage" characters after your text. BUT they should be trimmed by your next line of code:
Code:
Line2.SetAt(nLineLength, _T('\0'));
Is that where you still see them?
Originally Posted by zapper222
I am modifying the font when the dialog initiates, that could be the reason I am getting the strange results?
Font should make no difference, except for the number of characters that fit in the line.
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio: FeinWindows - replacement windows manager for Visual Studio, and more...
Specifies the character index of a character in the line whose length is to be retrieved.
The character # 1 is in the first line, and its length is 44.
Later on that page:
Remarks
Use the LineIndex member function to retrieve a character index for a given line number within this CRichEditCtrl object.
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio: FeinWindows - replacement windows manager for Visual Studio, and more...
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.