Display a Variable in MessageBoxW c++ [duplicate] - c++

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.)

Related

how to convert char to wchar_t in C++ [duplicate]

This question already has answers here:
How to convert string to wstring in C++
(1 answer)
How to convert char* to wchar_t*?
(9 answers)
How to convert UTF-8 std::string to UTF-16 std::wstring?
(6 answers)
Closed 2 months ago.
This post was edited and submitted for review 2 months ago and failed to reopen the post:
Original close reason(s) were not resolved
If I have this below code -
string str;
wstring wstr;
for (char x : str)
{
wstr += x;
}
Is this line wstr += x wrong? Do I need some conversion function to convert char x to wchar_t to be stored in wstr? If yes, which conversion function do I need?
Edit - I have gone through the linked answers, it mentions about converting the array of wchar_t and char -> for that for sure conversion functions are needed but my question specifically asks if a char -> wchar_t would need conversion function

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...

how to convert LPCWSTR and char? [duplicate]

This question already has answers here:
Convert char[] to LPCWSTR
(5 answers)
Closed 6 years ago.
SetTextColor(hdc, RGB(0, 0, 0));
char str[20]="";
sprintf(str, "sorce: %d", sorce);
TextOut(hdc, 930, 810, str,strlen(str));
It showed the error that char* cannot be converted to LPCWSTR. How can I solve it?
The W in LPCWSTR means wide characters. You have a few options.
You can change from using char types to wchar_t
You can convert using codecvt or Win32 API
Use TextOutA to keep using char types.

ERROR: argument of type "" is incompatible with parameter of type "LPWSTR" [duplicate]

This question already has answers here:
Argument of type "char *" is incompatible with parameter of type "LPWSTR"
(2 answers)
Closed 6 years ago.
I know this has probably been asked before, but I am in need of a fix. I have tried to alter it, but yet again this error keeps popping up.
// Get the dll's full path name
char buf[MAX_PATH] = { 0 };
GetFullPathName(L"Project1.dll", MAX_PATH, buf, NULL);
printf(buf);
printf("\n");
If any of you could help me, I would appreciate it heavily.
Thanks in advance.
Replace
char buf[MAX_PATH] = { 0 };
With
WCHAR buf[MAX_PATH] = { 0 };
You're obviously calling a wide string version of GetFullPathName here, and your first argument is wchar_t*, so the buffer should be, too.
Or better yet, use TCHAR and _T("Project1.dll") instead of L"Project1.dll".

How to convert a CStringW into LPCWSTR? [duplicate]

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.