Re: Invalid pointer
"Brian Muth" <bmuth@mvps.org> ha scritto nel messaggio
news:OrDWYkXUIHA.3676@TK2MSFTNGP06.phx.gbl...
"thejackofall" <thejackofall@discussions.microsoft.com> wrote in message
news:05886C58-A0A6-4D0D-AF7D-06247DE0FBEF@microsoft.com...
Really? I've used the cast from an example. I want to use std::string
because it is more flexible than CString. When I parse and extract data
from a XML file, it gives me _bstr_t type. From _bstr_t, how can I
safely convert to string?
#include <atlconv.h>
const int bufsize = 1024; // make sure buffer size is large enough
CW2AEX<bufsize> c = mybstring;
I beg to differ with Brian on this point.
You can just use:
CW2A c( mybstring );
You don't need to specify the buffer length. That template parameter is just
used by the CW2AEX template class to choose if allocating memory on the
heap, or allocate on the stack (typically for small strings, to avoid heap
fragmentation).
Personally, I prefer using std::wstring since I never compile in ANSII,
always UNICODE.
One of the good things about CString is that it is Unicode aware, and
compiles in both ANSI and UNICODE.
(Moreover, it implements a LPCTSTR casting conversion operator, so it
produces IMHO better code when doing Win32 or MFC programming, because we
can use e.g. .SetWindowText( myCString ) instead of .SetWindowText(
myStdString.c_str() ) ).
However, the conversion from UNICODE to ANSI can be lossy: if your method
returns a _bstr_t, why don't you use a UNICODE string, like CStringW or
std::wstring, as Brian suggested?
Giovanni