Error C2440 when compiling a library with meson - c++

I have code C++ that I want to compile as a library using meson where I get 2 kinds of errors
error C2440: 'initializing': cannot convert from 'const wchar_t [19]'
to 'const PWCHAR'
-note: Conversion from string literal loses const qualifier (see /Zc:strictStrings)
error C2664: '... cannot convert argument 2 from 'const wchar_t [6]'
to 'PWSTR note: Conversion from string literal loses const qualifier (see /Zc:strictStrings)
winnt.h uses typedef for wchar_t:
typedef wchar_t WCHAR;
typedef WCHAR *PWCHAR;
If I do this in my code I get Error C2440:
const PWCHAR Tokens[] = { L"A", L"B", L"C", L"D" };
If I change my code that error disappears:
const wchar_t * Tokens[] = { L"A", L"B", L"C", L"D" };
I know in C, the type of a string literal is array of char, but in C++, it's array of const char which causes this error.
I also know it is possible to change Zc:strictStrings in VStudio. But since I compile my code with meson how would I get rid of that error using meson?

With the definition
const PWCHAR Tokens[] = { ... };
you declare that Tokens is an array of constant pointers. The data that those pointers are pointing to are not constant.
And as literal strings are constant you can't use them to initialize the array.
The working definition:
const wchar_t * Tokens[] = { ... };
Or the alternative
wchar_t const * Tokens[] = { ... };
Here you declare that Tokens is an array of (non-constant) pointers to constant wchar_t data. Which is correct for literal strings.
If you want to be portable and follow the C++ standard, using PWCHAR is simply not possible.
If you want the pointers themselves to be constant, you need to add another const qualifier for them:
wchar_t const * const Tokens[] = { ... };
// ^^^^^
// makes the pointers constant
// ^^^^^
// makes the data constant
Or as mentioned in a comment: Use std::wstring (and other standard C++ containers):
// Or std::array if the size if known and fixed at compile-time
std::vector<std::wstring> Tokens = { ... };

Related

Cannot construct CString from a char

In VS2008, I try to construct a CString from a char, as follows:
CString str = CString(_T('A') + 5);
But will get the following compile error:
error C2440: '' : cannot convert from 'int' to 'CString'
I belive TCHAR and int is exchangable in C++, so why it refuses to accept the TCHAR type parameter to construct a CString?
The purpose of the line is to construct a CString with only one character _T('F'). Not to get "A5".
I try to understand how the compiler processes the code, as follows:
It will first promote _T('A') to an integer.
It will add the integer value of _T('A') by 5.
It will get a new integer and cannot find a way to convert it back to TCHAR, which can be used as input parameter for CString constructor.
The CstringT constructors that accept a char or a wchar_t are explicit. See the docs.
The explicit specifier in C++ means that the compiler is not allowed to do an implicit conversion of the parameter(s). See also explicit specifier.
Thus the compiler cannot (is not allowed to) implicitly convert the int value to char or wchar_t.
(Note that this depends on the flag _ATL_CSTRING_EXPLICIT_CONSTRUCTORS being set. Without the flag being set, the constructors are not explicit, but then it's ambiguous which constructor to chose.)
So you need to explicitly cast the parameter to char or wchar_t, depending on what you want:
CStringA str = CStringA(static_cast<char>('A' + 5));
or:
CStringW str = CStringW(static_cast<wchar_t>(L'A' + 5));
CString will not implicitly convert an int to CString. However you can try like so
CString str;
char c = 'A';
int i = 5;
str.Format(L"%c%d",c, i); // unicode assumed

How to construct WebURL from std::wstring?

I am writing a Win32 application with Awesomium. According to the tutorial section I can load a local file inside my view:
WebURL url(WSLit("file:///C:/dev/project/util/ui/index.html"));
view_->web_view()->LoadURL(url);
This works as expected.
When I try to pass an std::wstring to WSList function:
std::wstring ui_path = L"file:///" + install_path + L"/util/ui/index.html";
WebURL url(WSLit(ui_path));
view_->web_view()->LoadURL(url);
I get a compiler error:
src/main.cc(52) : error C2664: 'Awesomium::WSLit' : cannot convert parameter 1 from
'std::wstring' to 'const char *' No user-defined-conversion operator available that can
perform this conversion, or the operator cannot be called
When I try to pass WSLit( ui_path.c_str() ) the compiler throws another error:
src/main.cc(52) : error C2664: 'Awesomium::WSLit' : cannot convert parameter 1 from
'const wchar_t *' to 'const char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or
function-style cast
What is the right way to convert it to const char * type?
Edit:
From Awesomium documentation :
WSLit() is a special helper function that lets you declare WebString literals. Most of our API uses UTF-16 strings (wrapped with WebString) but we added WSLit() so you can declare ASCII C-strings with minimal fuss.
WSLit is meant to construct a WebString object from an ASCII string. Since you don't want to construct a WebString from ASCII, but rather have a UNICODE string from the beginning, you simply don't need to use WSLit at all.
The following line of code constructs a WebURL from a std::wstring:
WebURL url(WebString(ui_path.c_str()));
As pointed out by Remy Lebeau this may not compile for any given compiler or compiler settings. WebString has an explicit constructor taking a const wchar16*. Platform.h defines wchar16 as
typedef unsigned short wchar16;
Depending on your compiler and compiler settings, this may or may not be the same as wchar_t. When compiling with the Microsoft compiler using the command line option /Zc:wchar_t, wchar_t is interpreted as a native data type. This is a different type from unsigned short, and the explicit constructor of WebString requires an additional cast:
WebURL url(WebString(reinterpret_cast<const wchar16*>(ui_path.c_str())));
Implicitly invoking the conversion constructor of WebString using the following syntax is not possible, since it is declared explicit:
WebURL url(reinterpret_cast<const wchar16*>(ui_path.c_str()));
If the question is "convert std::wstring to const char*", I usually use this function and it works fine:
std::string wstringToString(const std::wstring& in){
std::string result(in.begin(), in.end());
return result;
}
And then, you can get char* by calling result.c_str();
Try this one:
std::wstring ui_path = L"file:///" + install_path + L"/util/ui/index.html";
std::string cui_path( ui_path.begin(), ui_path.end() );
WebURL url(WSLit(cui_path));
view_->web_view()->LoadURL(url);
However, IMHO, if Awesomium is intended to run under Windows, the WSLit constructor should support either a std::wstring or wchar_t * argument.

How to convert char* to LPCWSTR?

I know this has already been discussed in several questions on SO, but none of those solutions have worked for me.
I start with a char* because this is for a DLL that will be called from VBA, and char* is necessary for VBA to pass a string to the DLL.
I need to return a LPCWSTR because that's the input parameter for the API function I'm trying to call, and I can't enable casting by switching from Unicode to multi-byte character set in the Properties window, because the API has this code:
#if !defined(UNICODE) && !defined(NOUNICODE)
#error UNICODE is not defined. UNICODE must be defined for correct API arguments.
#endif
I tried this:
LPCWSTR convertCharArrayToLPCWSTR(char* charArray)
{
const char* cs=charArray;
wchar_t filename[4096] = {0};
MultiByteToWideChar(0, 0, cs[1], strlen(cs[1]), filename, strlen(cs[1]));
}
which gave these errors:
error C2664: 'strlen' : cannot convert parameter 1 from 'const char' to 'const char *'
error C2664: 'MultiByteToWideChar' : cannot convert parameter 3 from 'const char' to 'LPCCH'
I tried this (same function header), loosely adapted from this post:
size_t retVal;
const char * cs = charArray;
size_t length=strlen(cs);
wchar_t * buf = new wchar_t[length](); // value-initialize to 0 (see below)
size_t wn = mbsrtowcs_s(&retVal,buf,20, &cs, length + 1, NULL);
return buf;
This compiled ok, but when I passed it an example string of "xyz.xlsx", mbsrtowcs_s() set buf to an empty string: L""
So, how do I make this conversion?
Following Hans Passant's advice regarding pointers to local variables, I worked out this approach, which seems to work well:
wchar_t *convertCharArrayToLPCWSTR(const char* charArray)
{
wchar_t* wString=new wchar_t[4096];
MultiByteToWideChar(CP_ACP, 0, charArray, -1, wString, 4096);
return wString;
}
I'm aware that the use of new requires memory management, which I perform in the function that calls this one.
Since cs is a const char*, cs[1] is a const char. C++ won't convert it to a pointer for you, because in most cases that doesn't make sense.
You could instead say &cs[1] or cs+1 if the intent is to skip the first char. (That's what you're doing when you pass a pointer to the 1th element; in C++, indexes start at 0.) If the intent is to pass the whole string, then just pass cs.

const WCHAR * myVar vs const char * myVar

I've developed a small bmp to jpg conveter.
Following code is working and providing exact results as I need
BOOLEAN convertBMPtoJPG(const WCHAR *src_bmp_path,const WCHAR *dest_jpeg_path);
then calling function as,
const WCHAR *src_bmp_path = L"test.bmp";
const WCHAR *dest_jpeg_path= L"test.jpg";
convertBMPtoJPG(src_bmp_path,dest_jpeg_path);
However I need the function to be changed as following (as per requirements I've been given), but doing so results as a compilation error.
BOOLEAN convertBMPtoJPG(char *src_bmp_path,char *dest_jpeg_path);
then function would be called as, (though I need to follow just the prototype as above),
char *src_bmp_path = "test.bmp";
char *dest_jpeg_path= "test.jpg";
convertBMPtoJPG(src_bmp_path,dest_jpeg_path);
Another question on stackover provided too much information about Win32 types, however I yet couldn't resolve the issue.
I'm not that great in Win32 API, please guide me what's going wrong in later approach.
Edit:
Error Message:
error C2664: 'Gdiplus::Status Gdiplus::Image::Save(const WCHAR *,const CLSID *,const Gdiplus::EncoderParameters *)' : cannot convert parameter 1 from 'char *' to 'const WCHAR *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Image::Save() only accepts WCHAR* values, so your char* wrapper will have to convert to WCHAR*, such as with MultiByteToWideChar() (just like Win32 API Ansi functions do when they call Win32 API Unicode functions internally), eg:
std::wstring towstring(const char *src)
{
std::wstring output;
int src_len = strlen(src);
if (src_len > 0)
{
int out_len = MultiByteToWideChar(CP_ACP, 0, src, src_len, NULL, 0);
if (out_len > 0)
{
output.resize(out_len);
MultiByteToWideChar(CP_ACP, 0, src, src_len, &output[0], out_len);
}
}
return output;
}
BOOLEAN convertBMPtoJPG(char *src_bmp_path,char *dest_jpeg_path)
{
return convertBMPtoJPG(towstring(src_bmp_path).c_str(), towstring(dest_jpeg_path).c_str());
}
BOOLEAN convertBMPtoJPG(const WCHAR *src_bmp_path, const WCHAR *dest_jpeg_path)
{
// your existing conversion logic here...
}
Well, looks like your are compiling for Unicode support. The list of Win32 data types can be found here
WCHAR is defined as -
A 16-bit Unicode character. For more information, see Character Sets Used By Fonts.
This type is declared in WinNT.h as follows:
typedef wchar_t WCHAR;
Here is a link showing how to convert between various string types String Conversion Samples.

C code in C++ compiler

I have following code, it's code from tomcrypto's manual and it won't work on MS VC++ 2008 EE. Any help? Also can I ask replace char* by std::string object?
int main(void)
{
hash_state md;
unsigned char *in = "hello world", out[16];
/* setup the hash */
md5_init(&md);
/* add the message */
md5_process(&md, in, strlen(in));
/* get the hash in out[0..15] */
md5_done(&md, out);
return 0;
}
Errors:
\main.cpp(7) : error C2440: 'initializing' : cannot convert from 'const char [12]' to 'unsigned char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
.\main.cpp(11) : error C2664: 'strlen' : cannot convert parameter 1 from 'unsigned char *' to 'const char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
EDIT: Now code looks like:
int main(void)
{
register_hash(&md5_desc);
hash_state md;
char* p = "hello wordl";
unsigned char *in = reinterpret_cast<unsigned char*>(p);
char* out[16];
/* setup the hash */
md5_init(&md);
/* add the message */
md5_process(&md, const_cast<char*>(in), strlen(in));
/* get the hash in out[0..15] */
md5_done(&md, out);
return 0;
}
Errors:
\main.cpp(21) : error C2440: 'const_cast' : cannot convert from 'unsigned char *' to 'char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
.\main.cpp(21) : error C2664: 'strlen' : cannot convert parameter 1 from 'unsigned char *' to 'const char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
.\main.cpp(23) : error C2664: 'md5_done' : cannot convert parameter 2 from 'char *[16]' to 'unsigned char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
unsigned char *in = "hello world"
This is incorrect in C++: "hello world" is a string literal and is of type const char[12]. In C it is of type char[12], but the const here doesn't matter because in C++ there is an implicit (but deprecated) conversion that allows a string literal to be converted to a char*.
The problem is that char and unsigned char are different types. It doesn't matter whether char is unsigned; the three char types (char, unsigned char, and signed char) are all distinct and in C++ you cannot convert between pointers to those three types without a cast.
This works in C because in C you can convert any pointer-to-object type to any other pointer-to-object type without a cast. That isn't the case in C++.
In C++ you would need to use:
// use the implicit conversion to 'char*' to cast away constness:
char* p = "hello world";
// explicitly cast to 'unsigned char*'
unsigned char* in = reinterpret_cast<unsigned char*>(p);
The removal of constness is usually a bad idea since string literals are not modifiable, but sometimes it is necessary when dealing with legacy libraries that are not const-correct.
The conversion from char* to unsigned char* is safe because all objects can be treated as an array of char, unsigned char, or signed char in C++.
char is a different type to signed char or unsigned char; string literals are always of type (const) char *; so you cannot assign them to a (const) signed char * or a (const) unsigned char *. To fix this, remove the unsigned from line 4.
If your md5_process() function explicitly takes an unsigned char * as an argument, then you should perform a cast at that point:
md5_process(&md, reinterpret_cast<unsigned char*>(in), strlen(in));
[As others have said, you should really define in as const char *in as it's pointing to a string literal, but that is not the issue here.]
Let's try again:
int main(void)
{
register_hash(&md5_desc);
hash_state md;
const char* p = "hello wordl";
const unsigned char* in = reinterpret_cast<const unsigned char*>(p);
unsigned char out[16];
/* setup the hash */
md5_init(&md);
/* add the message */
md5_process(&md, in, strlen(p));
/* get the hash in out[0..15] */
md5_done(&md, out);
return 0;
}
Does this work?
This is because litteral strings are const in C++, while you initialize it with a non-const pointer:
const char* in = "hello world";
char * out[16];
However it might cause a problem if md5_process takes a non-const char*, in this case you'll have to cast to a non-const:
md5_process(&md, const_cast<char*>(in), strlen(in));