Reputation: 157
How would I take...
string modelPath = "blah/blah.obj"
and concatenate it with...
L" not found."
While passing it in as LPCWSTR. I tried to do
(LPCWSTR)(modelPath + " was not found.").c_str()
However that did not work. Here is a larger example of what it looks like now.
if(!fin)
{
MessageBox(0, L"Models/WheelFinal.txt not found.", 0, 0); //
return;
}
Upvotes: 3
Views: 4845
Reputation: 15153
LPCWSTR
is a L ong P ointer to a C onstant W ide STR ing. Wide strings, at least in Win32, are 16 bits, whereas (const) char
strings (i.e. (C)STR
or their pointer-counterparts LP(C)STR
) are 8 bits.
Think of them on Win32 as typedef const char* LPCSTR
and typedef const wchar_t* LPCWSTR
.
std::string
is an 8-bit string (using the underlying type char
by default) whereas std::wstring
is a wider character string (i.e. 16-bits on win32, using wchar_t
by default).
If you can, use std::wstring
to concatenate a L"string"
as a drop-in replacement.
MessageBox()
Windows has a funny habit of defining macros for API calls that switch out underlying calls given the program's multibyte configuration. For almost every API call that uses strings, there is a FunctionA
and FunctionW
call that takes an LPCSTR
or LPWCSTR
respectively.
MessageBox
is one of them. In Visual Studio, you can go into project settings and change your Multi-Byte (wide/narrow) setting or you can simply call MessageBoxA/W
directly in order to pass in different encodings of strings.
For example:
LPWCSTR wideString = L"Hello, ";
MessageBoxW(NULL, (std::wstring(wideString) + L"world!").c_str(), L"Hello!", MB_OK);
LPCSTR narrowString = "Hello, ";
MessageBoxA(NULL, (std::string(narrowString) + "world!").c_str(), "Hello!", MB_OK);
Upvotes: 4
Reputation: 61970
If you can change modelPath
to std::wstring
, it becomes easy:
MessageBox(nullptr, (modelPath + L" not found.").c_str(), nullptr, 0);
I changed your 0
pointer values into nullptr
as well.
Since std::string
represents a narrow string, std::wstring
represents a wide string, and the two are wildly different, casting from one representation to the other does not work, while starting with the appropriate one does. On the other hand, one can properly convert between representations using the new <codecvt>
header in C++11.
Upvotes: 3