Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.


Disables reference counting and protects the string in the buffer.

PXSTR LockBuffer( );

A pointer to a CSimpleStringT object or a null-terminated string.

Call this method to lock the buffer of the CSimpleStringT object. By calling LockBuffer, you create a copy of the string, with a –1 for the reference count. When the reference count value is -1, the string in the buffer is considered to be in a "locked" state. While in a locked state, the string is protected in two ways:

  • No other string can get a reference to the data in the locked string, even if that string is assigned to the locked string.

  • The locked string will never reference another string, even if that other string is copied to the locked string.

By locking the string in the buffer, you ensure that the string's exclusive hold on the buffer will remain intact.

After you have finished with LockBuffer, call UnlockBuffer to reset the reference count to 1.


If you call GetBuffer on a locked buffer and you set the GetBuffer parameter nMinBufferLength to greater than the length of the current buffer, you will lose the buffer lock. Such a call to GetBuffer destroys the current buffer, replaces it with a buffer of the requested size, and resets the reference count to zero.

For more information about reference counting, see the following articles:

The following example demonstrates the use of CSimpleStringT::LockBuffer.

CSimpleString str(_T("Hello"), pMgr);

ch = str.GetAt(2);
_tprintf_s(_T("%c"), ch);

Header: atlsimpstr.h

Community Additions

© 2015 Microsoft