How do I convert a CString to a char*

來源:互聯網
上載者:User

<P>First, be sure you actually need a <CODE>char*</CODE> (non-constant pointer, or
<CODE>LPTSTR</CODE>). If you need a <CODE>const
char*</CODE> (or <CODE>LPCTSTR</CODE>), then
<CODE>CString</CODE> has a conversion function that will be called automatically
if you pass a <CODE>CString</CODE> to a function expecting an
<CODE>LPCTSTR</CODE>. For example:</P><PRE>void f ( LPCTSTR somestring )
{
  cout << somestring << endl;
}

main()
{
CString str = "bonjour";

  f ( str );  // OK - calls CString::operator LPCTSTR() to convert
}</PRE>
<P>The remainder of this FAQ deals with obtaining a non-constant pointer to the
string.</P>
<P>Because a <CODE>CString</CODE> object manages the character array, you must
explicitly tell the <CODE>CString</CODE> that you want to get a non-constant
pointer to the string. Call <CODE>GetBuffer()</CODE> to get a <CODE>char*</CODE> to the string, and then call
<CODE>ReleaseBuffer()</CODE> when you no longer need that pointer. Calling
<CODE>ReleaseBuffer()</CODE> tells the <CODE>CString</CODE> that it can resume
managing the character array.</P><PRE>CString str = "some string";
LPTSTR  pch;

  pch = str.GetBuffer(0);

  // use pch here...

  // When you're done using pch, give the CString control
  // of the buffer again.
  str.ReleaseBuffer();</PRE>
<P>After calling <CODE>GetBuffer()</CODE>, you may modify the contents of the
string through <CODE>pch</CODE>, although you can't make the string longer since
that would overrun the array. If you do modify the string, you must not call any
<CODE>CString</CODE> methods before the call to <CODE>ReleaseBuffer()</CODE>,
since <CODE>CString</CODE> methods may reallocate and move the array, which
would render <CODE>pch</CODE> invalid. After you call
<CODE>ReleaseBuffer()</CODE>, you must not use <CODE>pch</CODE> any more, again
because the <CODE>CString</CODE> may reallocate and move the character
array.</P>
<P>If you want to create a larger buffer for the string, for example if you are
going to pass it to an API that returns a filename, you can do so by passing the
desired length to <CODE>GetBuffer()</CODE>:</P><PRE>CString sFilename;
LPTSTR  pch;

  // Get a non-const pointer and set the buffer size.
  pch = sFilename.GetBuffer ( MAX_PATH );

  // Pass the buffer to an API that writes to it.
  GetModuleFileName ( NULL, pch, MAX_PATH );

  // Return control of the array to the CString object.
  sFilename.RelaseBuffer();</PRE>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.