> To convert std::wstring to wchar* or LPWSTR the function is
> wstring.c-str() which returns wchar*
Actually, wstring::c_str() returns a "const wchar_t *". You can use it
as a LPCWSTR, but not as a LPWSTR.
> This works fine in VC6
> But in visual studio 2008(vc9) it returns garbage value.
You should post a compilable example where you think that happens.
It returns a const wchar_t *
> This works fine in VC6
> But in visual studio 2008(vc9) it returns garbage value.
What does your code look like?
This works for me:
wstring wstr = L"Test wstring";
const wchar_t *pwstr = wstr.c_str();
Mark
--
Mark Salsbery
Microsoft MVP - Visual C++
As others already wrote, std::wstring::c_str() returns a '*const* wchar_t
*'.
If you want to modify the buffer (i.e. if you want a non-const wchar_t *),
you may try CStringW::GetBuffer().
Giovanni
std::wstring wst;
void Fillwst(std::wstring st) {wst = st;}
std::wstring Getwst(){return wst;}
Fillwst(L"WideString");
WCHAR* outstr= (WCHAR*) Getwst().c_str();
Please check what's in outstr.
It contains garbage value.
If we define Fillwst like
void Fillwst(const std::wstring& st) {wst = st;}
Then no impact means above code will return garbage value
> Please try following code
>
> std::wstring wst;
> void Fillwst(std::wstring st) {wst = st;}
> std::wstring Getwst(){return wst;}
>
> Fillwst(L"WideString");
> WCHAR* outstr= (WCHAR*) Getwst().c_str();
This example doesn't compile by any stretch of the imagination. I beg
you, please post compilable code. Don't let us guess over the actual
problems you're having.
In any event, two problems are obvious:
- You're brutally casting a const pointer into a non-const pointer.
Any use of the resulting pointer has undefined behaviour.
- You're storing a pointer that is only valid as long as the
underlying wstring object is not modified (in particular: not
destroyed). Since Getwst returns just a temporary wstring object, the
pointer becomes invalid after the line is finished.
#include<stdio.h>
#include <iostream>
#include <sstream>
#include <ComDef.h>
using std::wstring;
using std::cout;
class usewstring
{
public:
std::wstring wst;
void Fillwst(const std::wstring& st) {wst = st;}
std::wstring Getwst() const throw(){return wst;}
};
void main()
{
usewstring Obj;
Obj.Fillwst(L"WideString");
WCHAR* outstr= (WCHAR*) Obj.Getwst().c_str();
_putws(outstr);
}
The code exhibits undefined behavior (in other words, it's buggy). The
bug just happens to manifest differently under different
implementations. "Appears to work" is just one possible manifestation of
undefined behavior.
> WCHAR* outstr= (WCHAR*) Obj.Getwst().c_str();
Again, Getwst returns a temporary wstring object. This object goes out
of scope and is destroyed at the end of the statement above. When it's
destroyed, it deallocates the memory it has allocated for the string
data - precisely the memory that c_str() returns a pointer to. So outstr
becomes invalid - it ends up pointing to a block of deallocated memory
(also known as a "dangling pointer").
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
> class usewstring
> {
> public:
> std::wstring wst;
> void Fillwst(const std::wstring& st) {wst = st;}
> std::wstring Getwst() const throw(){return wst;}
I don't understand why you give 'wst' data member public visibility and you
offer also a getter (Getwst()).
I would just make 'wst' data member private, and expose two public
getter/setter (Getwst and Fillwst).
> void main()
> {
> usewstring Obj;
> Obj.Fillwst(L"WideString");
> WCHAR* outstr= (WCHAR*) Obj.Getwst().c_str();
> _putws(outstr);
_putws parameter is a const wchar_t *, so I think that you could just do
(without removing const qualifier):
_putws( Obj.Getwst().c_str() );
http://msdn.microsoft.com/en-us/library/tf52y4t1.aspx
or you could modify the Getwst() prototype like this:
const wchar_t * Getwst() const
{
return wst.c_str();
}
and just do:
_putws( Obj.Getwst() );
HTH,
Giovanni
.c_str() returns const wchar_t*, but you can copy that into a vector,
which in turn can be modified:
std::wstring text(L"hello");
const wchar_t* text_ptr = text.c_str();
std::vector<wchar_t> buffer(text_ptr, text_ptr + text.size() + 1);
wchar_t* buffer_ptr = &buffer[0];
Tom