subject_21305.htm
来自「一些关于vc的问答」· HTM 代码 · 共 24 行
HTM
24 行
<p>
序号:21305 发表者:李春宁 发表日期:2002-11-15 18:31:03
<br>主题:如何将CString对象转换为char *?
<br>内容:如何实现CString和char *的转换<BR><BR>
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:linney 回复日期:2002-11-15 18:46:28
<br>内容:使用CString::GetBuffer()方法可以拿到串的指针,拿到这个指针,呵呵,你想怎么都行:)
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:李春宁 回复日期:2002-11-15 19:04:03
<br>内容:char cChar[512];<BR><BR>// CString转换为char*<BR>strcpy(cChar, strString.GetBuffer(0));<BR>strString.ReleaseBuffer();<BR><BR>为什么GetBuffer(0)和GetBuffer(3)结果都一样<BR><BR>GetBuffer的参数作用是什么?
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:TheCold 回复日期:2002-11-15 19:11:51
<br>内容:You will need this mostly to pass a CString to a function that expects a char*. <BR> // Prototype of a function expecting a char*:<BR> void Foo(char *c);<BR> //...<BR> CString csMyString = "Hello World";<BR> //...now call Foo():<BR> char *str = csMyString.GetBuffer(csMyString.GetLength());<BR> Foo(str);<BR> // or directly:<BR> Foo(csMyString.GetBuffer(csMyString.GetLength()));<BR> // if Foo modifies the passed char*, you must call<BR> csMyString.ReleaseBuffer(-1);<BR><BR>Notes:<BR>a) CString::GetBuffer() will return a char* only in non-UNICODE builds. <BR>b) CString has an implicit 208 operator to LPCTSTR. In non-UNICODE builds, that is a const char*. Do not use a cast hack like this: <BR> Foo((char *)((LPCSTR)csMyString)); // BAD!!!!<BR><BR>c) Do not call any other CString member function on csMyString between GetBuffer() and ReleaseBuffer().<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?