This question already has answers here:
What exactly is a PWSTR and why use this naming compared to char*, std::string, or CString in C++?
(8 answers)
Closed 7 years ago.
I am trying to convert wstring* to PWSTR*.
As per http://devsolvd.com/questions/how-to-convert-std-string-to-lpcstr PWSTR is wchar_t*. So PWSTR* is wchar_t**
As per document http://en.cppreference.com/w/cpp/string/basic_string, wstring is wchar_t. So wstring* is whar_t*
to convert wstring to PWSTR, i am trying following expression:
std::wstring *currentMetric =...
PWSTR *metric=...
*metric=currentMetric
It is getting error "cannot convert from 'std::wstring *' to 'PWSTR'"
I don't want to use c_str as it return PCWSTR and const_cast can give PWSTR but reassignment of the variable might lead to data corruption.
I want to know any other method to do so.
Thanks
Edit: answer suggests to use c_str(), but I wanted to use something other than c_str()
I found a way to do this:
*metric = &(*currentMetric)[0];
PS.I didn't want to use c_str as it returns PCWSTR which can be cons_cast to PWSTR. But reassignment of hthe variable might lead to data corruption
This part is not correct:
As per document http://en.cppreference.com/w/cpp/string/basic_string, wstring is wchar_t. So wstring* is whar_t*
wstring is a wrapper around wchar_t*, not wchar_t. To access the underlying wchar_t*, use the c_str member function.
Related
As I clould not pass LPCSTR from one function to another (Data get changed) I tried passing it as a string.
But later I need to again convert it back to LPSTR. While trying the conversion I am getting the above error:
cannot convert from 'std::string' to 'LPSTR'
How can I resolve this?
That's just because you should use std::string::c_str() method.
But this involves const_cast in given case because const char * returned by c_str() can not be assigned to a non-constant LPSTR.
std::string str = "something";
LPSTR s = const_cast<char *>(str.c_str());
But you must be sure that lifetime of str will be longer that that of LPTSTR variable.
Another mention, if code compiles as Unicode-conformant, then types LPTSTR and std::string are incompatible. You should use std::wstring instead.
Important note: If you pass the resulting pointer s from above to a function which tries to modify the data it is pointing to this will result in undefined behaviour. The only way to properly deal with it is to duplicate the string into a non-const buffer (e.g. via strdup)
If you need an LPSTR, that means the string will/may be modified. std::string::c_str() returns a const pointer, and you can't just const_cast it away and hope all is good in the world, because it isn't. The string may be changed in all sorts of nasty ways, and your original std::string will be oblivious to all of them.
Try this instead:
// myFunction takes an LPSTR
std::string cppString = "something";
LPSTR cString = strdup( cppString.c_str() );
try {
myFunction( cString );
cppString = cString;
} catch(...) {
free( cString );
}
Wrap the string in a smart pointer and get rid of the try...catch for bonus points (don't forget the custom deleter).
There is a function on std::string c_str() . However I doubt that you could not use a std::string in your case.
Are you running somestringvariablename.c_str()?
That should work.
An LPSTR can be substituted with by using a TCHAR (i.e. found in tchar.h). So if you have a std::string, you can use the method std::string::c_str().
If the function, you are calling does not write to string, but only reads it, then you can simply use string::c_str method. If it is going to write something, then you probably should ensure that your string has enough space by calling string::reserve().
This question already has answers here:
vc++ - How to convert a CString into LPCWSTR
(6 answers)
Closed 6 years ago.
I have a function which retrieves resources from .rc file using CStringW.
I want use this returned value in sprintf_s.Is there any way?
//Snippet
sprintf_s(szMsgBoxText, LoadFromResource(IDS_INSTALLATION_COMPLETE), g_szProductName);
CStringW LoadFromResource(int ID)
{
CStringW m_resoucestring(MAKEINTRESOURCE(ID));
return m_resoucestring;
}
sprintf_s giving me an error.Is there any alternative for this?
Use CStringW::Format which does exactly the same as sprintf_s. So your code will become:
CStringW sText;
sText.Format(LoadFromResource(IDS_INSTALLATION_COMPLETE), g_szProductName);
or even
CStringW sText;
sText.Format(IDS_INSTALLATION_COMPLETE, g_szProductName);
You can pass CStringW directly to any function that accepts LPCWSTR type of parameter as it has corresponding cast operator.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to convert CString and ::std::string ::std::wstring to each other?
Ok, I know my question may be trivial, but I have not yet found how I can convert an ATL::Cstring to a basic string. I have tried the "converting from CString example" of the page http://msdn.microsoft.com/en-us/library/ms235631%28v=vs.80%29.aspx but it does not work. Any due indication?
This should work -
CStringA cstr1("Hello");
std::string str1(cstr1);
OR
CStringW cstr2(L"Hello");
std::wstring str2(cstr2);
CString is a macro that may be converted to CStringA or CStringW depending on whether UNICODE is defined or not.
Assigning CStringA to std::wstring and CStringW to std::string will not work, which is probably what is happening for you.
I'm writing a custom text-to-speech program that uses SAPI 5, and one problem I'm facing is that enumerating voices with SpEnumTokens and iterating over them produces CSpDynamicString objects.
My question is, how do I convert CSpDynamicString to char * so I could printf them?
It looks like I've to use some kind of text-conversion macro from ATL. I found an example that does this (given dstrDesc is CSpDynamicString):
CSpDynamicString dstrDesc;
SpGetDescription(voiceToken, &dstrDesc);
USES_CONVERSION;
printf("%s\n", W2T(dstrDesc));
However this only prints the first character of the voice name!
Any ideas?
CSpDynamicString implements an operator to convert to WCHAR* and also manages the LPWSTR pointer internally. So, W2T gets you LPTSTR pointer as printf argument. If you have a Unicode build, this is results still in a WCHAR* pointer and printf("%s"... expects CHAR* argument - this is where you can have the problem you are describing.
Try it this way:
CSpDynamicString dstrDesc;
SpGetDescription(voiceToken, &dstrDesc);
printf("%ls\n", (WCHAR*) dstrDesc);
As I clould not pass LPCSTR from one function to another (Data get changed) I tried passing it as a string.
But later I need to again convert it back to LPSTR. While trying the conversion I am getting the above error:
cannot convert from 'std::string' to 'LPSTR'
How can I resolve this?
That's just because you should use std::string::c_str() method.
But this involves const_cast in given case because const char * returned by c_str() can not be assigned to a non-constant LPSTR.
std::string str = "something";
LPSTR s = const_cast<char *>(str.c_str());
But you must be sure that lifetime of str will be longer that that of LPTSTR variable.
Another mention, if code compiles as Unicode-conformant, then types LPTSTR and std::string are incompatible. You should use std::wstring instead.
Important note: If you pass the resulting pointer s from above to a function which tries to modify the data it is pointing to this will result in undefined behaviour. The only way to properly deal with it is to duplicate the string into a non-const buffer (e.g. via strdup)
If you need an LPSTR, that means the string will/may be modified. std::string::c_str() returns a const pointer, and you can't just const_cast it away and hope all is good in the world, because it isn't. The string may be changed in all sorts of nasty ways, and your original std::string will be oblivious to all of them.
Try this instead:
// myFunction takes an LPSTR
std::string cppString = "something";
LPSTR cString = strdup( cppString.c_str() );
try {
myFunction( cString );
cppString = cString;
} catch(...) {
free( cString );
}
Wrap the string in a smart pointer and get rid of the try...catch for bonus points (don't forget the custom deleter).
There is a function on std::string c_str() . However I doubt that you could not use a std::string in your case.
Are you running somestringvariablename.c_str()?
That should work.
An LPSTR can be substituted with by using a TCHAR (i.e. found in tchar.h). So if you have a std::string, you can use the method std::string::c_str().
If the function, you are calling does not write to string, but only reads it, then you can simply use string::c_str method. If it is going to write something, then you probably should ensure that your string has enough space by calling string::reserve().