CString::GetBufferSetLength
https://msdn.microsoft.com/en-us/library/aa516250(v=msdn.10)
This method retrieves a pointer to the internal character buffer for the CString object, truncating or growing its length, if necessary, to exactly match the length specified in nNewLength. The returned LPTSTR pointer is not const and thus allows direct modification of CString contents.
LPTSTR GetBufferSetLength(
int nNewLength );
Parameters
- nNewLength
Specifies the exact size of the CString character buffer in characters.
Return Value
This is an LPTSTR pointer to the character buffer of the null-terminated object.
Remarks
If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other CString methods.
The address returned by GetBufferSetLength may not be valid after the call to ReleaseBuffer since additional CString operations may cause the CString buffer to be reallocated. If you do not change the length of the Cstring, the buffer will not be reassigned.
The buffer memory will be freed automatically when the CString object is destroyed.
Note that, if you keep track of the string length yourself, you should not append the terminating null character. You must, however, specify the final string length when you release the buffer with ReleaseBuffer. If you do append a terminating null character when you call ReleaseBuffer, you should pass –1 (the default) for the length. ReleaseBuffer will perform a on the buffer to determine its length.
Example
CString str; LPTSTR pstr = str.GetBufferSetLength(3); pstr[0] = 'I'; pstr[1] = 'c'; pstr[2] = 'e'; // No need for trailing zero or call to ReleaseBuffer() // because GetBufferSetLength() set it for us! str += _T(" hockey is best!");
Requirements
Windows CE versions: 1.0 and later
Header file: Declared in Afx.h
Platform: H/PC Pro, Palm-size PC, Pocket PC