cannot convert 'const char*' to 'LPCWSTR {aka const wchar_t*}'

sci-guy

I'm getting an error in my C++ code that I can't quite make sense of. The stripped down code bits are here:

RS232Handle=OpenRS232("COM1", 9600);

HANDLE OpenRS232(const char* ComName, DWORD BaudRate)
{
    ComHandle=CreateFile(ComName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
}

I get the following error:

error: cannot convert 'const char*' to 'LPCWSTR {aka const wchar_t*}' for argument '1' to 'void* CreateFileW(LPCWSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE)'
     ComHandle=CreateFile(ComName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

The code was taken from VS code and I am now using Qt creator.

How can I fix this issue? Thanks!

dan04

The Windows CreateFile function is actually a macro that expands to one of:

  • CreateFileA, which takes a file path of type const char*
  • CreateFileW, which takes a file path of type const wchar_t*.

(The same is true for most of the functions in the Windows API that take a string.)

You're declaring the parameter const char* ComName, but apparently compiling with UNICODE defined, so it's calling the W version of the function. There's no automatic conversion from const wchar_t* to const char*, hence the error.

Your options are to:

  1. Change the function parameter to a UTF-16 (const wchar_t*) string.
  2. Keep the char* parameter, but have your function explicitly convert it to a UTF-16 string with a function like MultiByteToWideChar.
  3. Explicitly call CreateFileA instead of CreateFile.
  4. Compile your program without UNICODE, so that the macros expand to the A versions by default.
  5. Kidnap a prominent Microsoft developer and force him to read UTF-8 Everywhere until he agrees to have Windows fully support UTF-8 as an “ANSI” code page, thus freeing Windows developers everywhere from this wide-character stuff.

Edit: I don't know if a kidnapping was involved, but Windows 10 1903 finally added support for UTF-8 as an ANSI code page.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

"cannot convert 'wchar_t*' to 'LPCSTR' {aka 'const char*'}gcc" when trying to use WriteConsoleOutputCharacter()

Convert const char* to const wchar_t*

cannot convert argument 1 from 'ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t>>>' to 'const char *'

error C2440: '=': cannot convert from 'const char *' to 'LPCWSTR'

Cannot convert argument 1 from 'const char [11]' to 'LPCWSTR'

Convert const wchar_t* into a WCHAR*

GETTING THE ERROR IN VISUAL STUDIO: error C2664: '_chmod' : cannot convert parameter 1 from 'wchar_t [260]' to 'const char *'

Cast (const) char * to LPCWSTR

Cannot convert 'const char*' to 'WCHAR*' in argument passing

Convert LOWORD(wParam) to const wchar_t*

Convert const wchar_t* to LPWSTR

Error C2664: MessageBoxW cannot convert argument 2 from 'const char ' to 'LPCWSTR'

cannot convert from const char* to const char *&

String parameter const char* and const wchar_t*

Cannot convert parameter 1 from 'const wchar_t *' to 'LPCTSTR' in MFC / C++ project

cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' to 'LPCSTR {aka const char*}'

error: cannot convert const string to const char* for argument 1 to size_t strlen(const char*)

Cannot convert 'TCHAR*' to 'const char*'

How to use regex_iterator with const char/wchar_t*?

win32 atoi issue which can't convert argument 1 from 'wchar_t [5]' to 'const char *

How to convert int variable and append to const wchar_t*

How convert LPCWSTR to wchar_t*?

Convert a Vcl::Controls::TCaption aka (System::UnicodeString) to a const char *

error: cannot convert 'const char**' to 'const char (*)[64]'

Is " const wchar_t* " a pointer?

C++ wifstream: Incompatible type char const*, wchar_t const*

C++ Builder 10.3 can not assign to const wchar_t* from const char[18]

Cannot convert const char to string constructing string

Getting "error: cannot convert 'std::__cxx11::string* {aka std::__cxx11::basic_string<char>*}' to 'const char*' error while passing a path as argument