Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

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.

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

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.

NoteNote

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.

// CSimpleStringT_GetBuffer.cpp
#include <atlmem.h>
#include <atlstr.h>

// Declare a thread-safe, growable, private heap with initial size 0:
CWin32Heap g_stringHeap( 0, 0, 0 );

// Declare a string manager that uses the private heap:
CAtlStringMgr g_stringMgr( &g_stringHeap ); 

int main() {
   CSimpleString s( "abcd", &g_stringMgr );
   LPTSTR pBuffer = s.GetBuffer( 10 );

   printf("%s\n", pBuffer);
   // Directly access CSimpleString buffer
   strcpy_s( pBuffer, 10, "Hello" );
   printf("%s\n", pBuffer);
   s.ReleaseBuffer( );
   _ASSERT(strcmp(s, "Hello") == 0);
}
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.