Welcome to MSDN Blogs Sign in | Join | Help

CString and GetLength

I ran into this problem recently when debugging some native code and thought that it will be good to share this with everyone.

 

CString sampleString = CString(_T("Sample\0String"), 14);

int len = sampleString.GetLength();          // len is 14

CString trimmedString = sampleString.Trim(); // trimmedString = "Sample"

CString newstring = CString(sampleString);   // newString = "Sample"

len = newstring.GetLength();                 // len = 14

 

CString GetLength returns the length that you passed to it in the constructor and not the length of the string. This can be confusing if you copy the string and loop through the length. It can also be the cause of bugs is you get the length and use CString.GetBuffer() and loop through the buffer for the length.

It looks like CString.GetLength() is the size of the internal buffer and nothing more.

 

MSDN documentation for CString - http://msdn.microsoft.com/en-us/library/aa300471(VS.60).aspx

Published Friday, February 06, 2009 7:52 PM by thottams@microsoft.com

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# CString and GetLength - Click & Solve

Saturday, February 07, 2009 12:23 AM by CString and GetLength - Click & Solve

# re: CString and GetLength

Nice post. Where is CString.Trim() in MSDN? Is it typo of CString.TrimRight()?

Saturday, February 07, 2009 12:37 AM by Bali

# re: CString and GetLength

Interesting, I could't find it either. But it compiles and works. It is supposed to remove leading and trailing white spaces I would think.

Saturday, February 07, 2009 2:46 PM by Thottam Sriram

# re: CString and GetLength

Good one !!! Bro, do you remember me ?

Thursday, February 26, 2009 5:53 PM by Tremonk

# re: CString and GetLength

Am I correct to assume that this is a heavy bug?

Just tried this code in Visual Studio 2005 and it really behaved as described.

I was sure that CString::GetLength() would always return the number of charactes up to the first null terminator (without including it in the count). All my code relies on that assumption...

Thursday, March 05, 2009 8:51 AM by Patrik Ehringer

# re: CString and GetLength

I would agree. I did not realize this until I ran into this issue recently. The assumption that CString makes may be is that the data in the buffer is not binary and does not contain null terminators in it. But that assumption is not evident from the documentation.

Friday, March 06, 2009 6:26 PM by Thottam Sriram

# re: CString and GetLength

Do you have any plans to officially report this bug to Microsoft?

If not, do you know what would be the place to do so on my own? And also in this case, would I be allowed to copy/paste your code from above into this bug report?

Thanks,

Patrik

Wednesday, March 11, 2009 12:42 PM by Patrik Ehringer

# re: CString and GetLength

Let me try to contact the right owners and see what they have to say. In the mean time feel free to use the code above.

Friday, March 13, 2009 9:59 PM by Thottam Sriram

# re: CString and GetLength

use strlen()

I'm pretty sure GetLength can only bu used with assert, like an if statment.

Sunday, September 27, 2009 1:17 PM by NMGod

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker