How to convert a CStringW into LPCWSTR? [duplicate] - c++

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.

Related

How to convert string to wchar_t when string is stored in a variable? [duplicate]

This question already has answers here:
C++ Convert string (or char*) to wstring (or wchar_t*)
(19 answers)
Closed 2 years ago.
To convert a string literal to wchar_t we can use L like:
wchar_t variable[10] = L"some text";`
But if the string is stored inside a variable then how do I convert it to wchar_t?
Suppose the string is in a variable
string varString="someText";
I want to store it in a variable of type wchar_t, for example wchar_t var;
How do I type cast and store it?
I want to place the variables inside a loop where their values will change with each cycle:
for(i=0;i<10;i++)
{
var=(*some kind of casting*)varString;
}
Two different ways...depending on simplicity... This is in Visual C++ group...
So first I would try using CStringW. Depending on #defines your regular CString might be a CStringA or CStringW. But, you can say CStringW.
CStringW sWide = "abcdef"; // uses current thread code page
const wchar_t* pWide = sWide.GetString(); // pointer only valid for scope of sWide
Or you can use MultiByteToWideChar() API.
wchar_t wszBuf[512];
MultiByteToWideChar(CP_ACP, 0, "abcdef", 6, wszBuf, _countof(wszBuf)); // substitute "abcdef" and the 6 (length) for your usage...

Conversion of wstring* to PWSTR [duplicate]

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.

Display a Variable in MessageBoxW c++ [duplicate]

This question already has answers here:
Convert int to LPCWSTR by using wsprintf
(1 answer)
Display a Variable in MessageBox c++
(5 answers)
Closed 8 years ago.
I have a proplem , like :
Ex :
MessageBoxW(0,L"Đây là ABC (This is ABC)",L"Lỗi (Error)",0);
All ok !
But i want to replace ABC to variable , like it :
char buff[500];
char author[] = "ABC";
sprintf_s(buff,"Đây là %s (This is %s)",author);
MessageBoxW(0, WHAT WILL BE HERE,L"Lỗi (Error)",0);
I hope someone may help !
You can certainly display a variable, but it has to be of the correct type. MessageBoxW takes a LPCWSTR (wide), and a char[] provides a LPCSTR (narrow) instead. So swap out the types accordingly:
WCHAR buff[500]; // WCHAR not char
WCHAR author[] = L"ABC"; // WCHAR not char
swprintf_s(buff, L"Đây là %s (This is %s)", author); // swprintf_s not sprintf_s
MessageBoxW(0, buff, L"Lỗi (Error)", 0);
It's also a good idea to avoid the raw buffers and use a wrapper class such as ATL::CStringW or std::wstring.
(I had some trouble deciding whether to answer this. The related question Why can't I display this string on MessageBox? seems like a duplicate, but it's closed as a duplicate of Cannot convert parameter from 'const char[20]' to 'LPCWSTR' which does not answer this question. In fact its answer is included in this question.)

How to put a CString variable in a message box? [duplicate]

This question already has answers here:
How to concatenate multiple CString
(2 answers)
Closed 8 years ago.
In my program, I need put a CString variable in a MessageBox. I use the following code:
messagebox("hi" + txt);
But I get the following error message:
error C2678: binary '+' : no operator found which takes a
left-hand operand of type 'const char [3]' (or there is no acceptable
conversion)
you can use a CString variable to format, then pass it to MessageBox.
_T() is a macro for Unicode or MBCS.
you should make sure txt is same encode as str,
CString str ;
str.Format(_T("Hi %s"), txt);
Use format method of the CString. The Format method do thing like how printf, sprintf works.
Example
CString str ;
str.Format("Hi %s", txt);
Use the _T macro to wrap the string literal into a CString:
messagebox(_T("hi") + txt);

Convert from ATL::CString to string in C++ [duplicate]

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.