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!
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:
const wchar_t*) string.char* parameter, but have your function explicitly convert it to a UTF-16 string with a function like MultiByteToWideChar.CreateFileA instead of CreateFile.UNICODE, so that the macros expand to the A versions by default.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With