CString::GetBuffer()与CString::ReleaseBuffer到底有什么用?

siemun2002 2006-03-13 10:50:56
原型是:
LPTSTR GetBuffer( int nMinBufLength );
nMinBufLength这个参数取什么?
...全文
10554 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhoujiamurong 2006-05-23
  • 打赏
  • 举报
回复
mark
dnliophsam 2006-03-14
  • 打赏
  • 举报
回复
GetBuffer可以获得一个字符数组让你能够方便的对其进行操作,调用GetBuffer以后必须调用ReleaseBuffer释放,否则不能对该CString对象进行操作譬如
CString str = "abcd"
char * lpBuf = str.GetBuffer(0);
lpBuf[1] = 'e';
int nLen = str.GetLength(); // 出错,ReleaseBuffer前不能进行类似的操作
str.ReleaseBuffer(); // 此时str="aecd"
一个傻冒 2006-03-14
  • 打赏
  • 举报
回复
getbuffer是为了让你使用CString类中,保存字符串缓冲区的那块指针.
至于releasebuffer,在MSDN中有这样一句话.
If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other CString member functions.
在对GetBuffer返回的指针使用之后需要调用ReleaseBuffer,这样才能使用其他Cstring的operations。否则会发生错误.


siemun2002 2006-03-14
  • 打赏
  • 举报
回复
to 流云等:
我这样不也是可以达到目的吗?
CString s( "abcd" );
s="Hello";
为什么还搞个中间变量和getbuffer releasebuffer呢?
Jimmy_Xia 2006-03-14
  • 打赏
  • 举报
回复
nMinBufferLength
The minimum size of the character buffer in characters. This value does not include space for a null terminator.
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.
Jimmy_Xia 2006-03-14
  • 打赏
  • 举报
回复
The GetBuffer and ReleaseBuffer member functions allow you to gain access to the internal character buffer of a CString object and modify it directly. The following steps show how to use these functions for this purpose:

Call GetBuffer for a CString object and specify the length of the buffer you require.
Use the pointer returned by GetBuffer to write characters directly into the CString object.
Call ReleaseBuffer for the CString object to update all the internal CString state information, such as the length of the string. After modifying a CString object's contents directly, you must call ReleaseBuffer before calling any other CString member functions.
fisker0303 2006-03-14
  • 打赏
  • 举报
回复
LPTSTR CString::GetBuffer(int nMinBufLength)
{
ASSERT(nMinBufLength >= 0);

if (GetData()->nRefs > 1 || nMinBufLength > GetData()->nAllocLength)
{
#ifdef _DEBUG
// give a warning in case locked string becomes unlocked
if (GetData() != _afxDataNil && GetData()->nRefs < 0)
TRACE0("Warning: GetBuffer on locked CString creates unlocked CString!\n");
#endif
// we have to grow the buffer
CStringData* pOldData = GetData();
int nOldLen = GetData()->nDataLength; // AllocBuffer will tromp it
if (nMinBufLength < nOldLen)
nMinBufLength = nOldLen;
AllocBuffer(nMinBufLength);
memcpy(m_pchData, pOldData->data(), (nOldLen+1)*sizeof(TCHAR));
GetData()->nDataLength = nOldLen;
CString::Release(pOldData);
}
ASSERT(GetData()->nRefs <= 1);

// return a pointer to the character storage for this string
ASSERT(m_pchData != NULL);
return m_pchData;
}

void CString::ReleaseBuffer(int nNewLength)
{
CopyBeforeWrite(); // just in case GetBuffer was not called

if (nNewLength == -1)
nNewLength = lstrlen(m_pchData); // zero terminated

ASSERT(nNewLength <= GetData()->nAllocLength);
GetData()->nDataLength = nNewLength;
m_pchData[nNewLength] = '\0';
}
wawaku 2006-03-14
  • 打赏
  • 举报
回复
对一个CString变量,你可以使用的唯一合法转换符是LPCTSTR。转换成LPTSTR(非常量指针)是错误的。养成把一个CString变量转换成LPTSTR的习惯将会给你带来伤害,因为当你的程序后来崩溃时,你可能不知道为什么,因为你到处都使用同样的代码而那时它们都恰巧正常工作。正确的得到一个指向缓冲区的非常量指针的方法是调用GetBuffer()方法。下面是正确的用法的一个例子,这段代码是给一个列表控件中的项设定文字: CString str = _T("new text");
LVITEM item = {0};
item.mask = LVIF_TEXT;
item.iItem = 1;
item.pszText = (LPTSTR)(LPCTSTR) str; // WRONG!
item.pszText = str.GetBuffer(0); // correct

ListView_SetItem ( &item );
str.ReleaseBuffer(); // return control of the buffer to str
  pszText成员是一个LPTSTR变量,一个非常量指针,因此你需要对str调用GetBuffer()。GetBuffer()的参数是你需要CString为缓冲区分配的最小长度。如果因为某些原因,你需要一个可修改的缓冲区来存放1K TCHARs,你需要调用GetBuffer(1024)。把0作为参数时,GetBuffer()返回的是指向字符串当前内容的指针。
(摘)
Kudeet 2006-03-14
  • 打赏
  • 举报
回复
http://blog.csdn.net/laiyiling/archive/2004/10/05/125216.aspx
vcmute 2006-03-13
  • 打赏
  • 举报
回复
GetBuffer暴露pData让用户输入
ReleaseBuffer做些扫尾工作,如设置Length等
DentistryDoctor 2006-03-13
  • 打赏
  • 举报
回复
CString::ReleseBuffer
Releases control of the buffer allocated by GetBuffer.
nNewLength
The new length of the string in characters, not counting a null terminator. If the string is null terminated, the -1 default value sets the CSimpleStringT size to the current length of the string.

DentistryDoctor 2006-03-13
  • 打赏
  • 举报
回复
CString::GetBuffer
Returns a pointer to the internal character buffer for the CSimpleStringT object.

nMinBufferLength
The minimum size of the character buffer in characters. This value does not include space for a null terminator.
只见烟火飞扬 2006-03-13
  • 打赏
  • 举报
回复
CString s( "abcd" );
int len=s.GetLength();
LPTSTR p = s.GetBuffer( 5 );
strcpy( p, "Hello" );
//如果要修改的话,就要保证修改后的长度不大于len和nMinBufLength中的最大值
只见烟火飞扬 2006-03-13
  • 打赏
  • 举报
回复
一般置0就可以了
随笔 - 764 文章 - 3 评论 - 196 CString,string,char*之间的转换(转) 这三种类型各有各的优点,比如CString比较灵活,是基于MFC常用的类型,安全性也最高,但可移植性最差。string是使用STL时必不可少的类型,所以是做工程时必须熟练掌握的;char*是从学习C语言开始就已经和我们形影不离的了,有许多API都是以char*作为参数输入的。所以熟练掌握三者之间的转换十分必要。 以下我用简单的图示指出三者之间的关系,并以标号对应转换的方法。 1 string to CString CString.format("%s",string.c_str()); 2 CString to string string str(CString.GetBuffer(str.GetLength())); 3 string to char * char *p=string.c_str(); 4 char * to string string str(char*); 5 CString to char * strcpy(char,CString,sizeof(char)); 6 char * to CString CString.format("%s",char*); CString的format方法是非常好用的。string的c_str()也是非常常用的,但要注意和char *转换时,要把char定义成为const char*,这样是最安全的。 以上函数UNICODE编码也没问题:unicode下照用,加个_T()宏就行了,像这样子_T("%s") 补充: CString 可能是 CStringW/CStringA,在与 string 转换时,如果是 CStringW,还涉及编码转换问题。下面以 CStringA 来说明。 1 string to CString CString.format("%s",string.c_str()); CStringA = string.c_str() 就可以了 2 CString to string string str(CString.GetBuffer(str.GetLength())); GetBuffer 有参数的话,可能导致内部的分配空间动作,要进行后续 ReleaseBuffer 操作。 string = CStringA string = CStringA.GetBuffer(); 3 string to char * char *p=string.c_str(); 4 char * to string string str(char*); 5 CString to char * strcpy(char *,CString,sizeof(char)); 按照 3 风格,这里应该 char * = CStringA; 或者 char *p = CStringA.GetBuffer(); 6 char * to CString CStringA = char * 就可以了

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧