다음을 통해 공유


CSimpleStringT::GetBuffer

Returns a pointer to the internal character buffer for the CSimpleStringT object.

PXSTR GetBuffer(
   int nMinBufferLength
);
PXSTR GetBuffer( );

Parameters

  • nMinBufferLength
    The minimum size of the character buffer in characters. This value does not include space for a null terminator.

Return Value

An PXSTR pointer to the object's (null-terminated) character buffer.

Remarks

Call this method to return the buffer contents of the CSimpleStringT object. The returned PXSTR is not const and thus allows direct modification of CSimpleStringT contents.

참고

If nMinBufferLength is greater than the length of the current buffer, the call to GetBuffer destroys the current buffer, replacing it with a buffer of the requested size and resetting the reference count to zero. If you have previously called LockBuffer on this buffer, you will lose the buffer's lock.

If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other CSimpleStringT member methods.

The address returned by GetBuffer may not be valid after the call to ReleaseBuffer because additional CSimpleStringT operations can cause the CSimpleStringT buffer to be reallocated. The buffer is not reallocated if you do not change the length of the CSimpleStringT.

The buffer memory is automatically freed when the CSimpleStringT object is destroyed.

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, you should pass –1 (the default) for the length to ReleaseBuffer, and ReleaseBuffer will perform a strlen on the buffer to determine its length.

If there is insufficient memory to satisfy the GetBuffer request, the method will throw an exception.

Example

CSimpleString s(_T("abcd"), pMgr);

LPTSTR pBuffer = s.GetBuffer(10);
int    sizeOfBuffer = s.GetAllocLength();

// Directly access CSimpleString buffer
_tcscpy_s(pBuffer, sizeOfBuffer, _T("Hello")); 
ASSERT(_tcscmp(s, _T("Hello")) == 0);

s.ReleaseBuffer();   

Requirements

Header: atlsimpstr.h

See Also

Concepts

CSimpleStringT Class

CSimpleStringT Members

CSimpleStringT::GetBufferSetLength

CSimpleStringT::ReleaseBuffer