Cannot Convert Parameter 1 from 'Char' to 'Lpcwstr'

cannot convert parameter 1 from 'char' to 'LPCWSTR'

It should be

int main(int argc, char* argv[]) 

And

HANDLE h = CreateFileA(argv[1],GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);

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

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.

cannot convert parameter 1 from 'const char *' to 'LPCWSTR'

Most of the Windows APIs that take strings have two versions: one that takes char * and one that takes WCHAR * (that latter is equivalent to wchar_t *).

SetWindowText, for example, is actually a macro that expands to either SetWindowTextA (which takes char *) or SetWindowTextW (which takes WCHAR *).

In your project, it sounds like all of these macros are referencing the -W versions. This is controlled by the UNICODE preprocessor macro (which is defined if you choose the "Use Unicode Character Set" project option in Visual Studio). (Some of Microsoft's C and C++ run time library functions also have ANSI and wide versions. Which one you get is selected by the similarly-named _UNICODE macro that is also defined by that Visual Studio project setting.)

Typically, both of the -A and -W functions exist in the libraries and are available, even if your application is compiled for Unicode. (There are exceptions; some newer functions are available only in "wide" versions.)

If you have a char * that contains text in the proper ANSI code page, you can call the -A version explicitly (e.g., SetWindowTextA). The -A versions are typically wrappers that make wide character copies of the string parameters and pass control to the -W versions.

An alternative is to make your own wide character copies of the strings. You can do this with MultiByteToWideChar. Calling it can be tricky, because you have to manage the buffers. If you can get away with calling the -A version directly, that's generally simpler and already tested. But if your char * string is using UTF-8 or any encoding other than the user's current ANSI code page, you should do the conversion yourself.

Bonus Info

The -A suffix stands for "ANSI", which was the common Windows term for a single-byte code-page character set.

The -W suffix stands for "Wide" (meaning the encoding units are wider than a single byte). Specifically, Windows uses little-endian UTF-16 for wide strings. The MSDN documentation simply calls this "Unicode", which is a little bit of a misnomer.

CreateDirectoryW' : cannot convert parameter 1 from 'const char *' to 'LPCWSTR' in OpenCV 2.4.5 and VS 2010

You have code that calls CreateDirectory. When UNICODE is defined, that symbol is actually a macro for CreateDirectoryW; the intention is for you to use "ambiguous" function names when you're also using TCHAR instead of char or wchar_t, so you can switch between compiling for Unicode or Ansi programs.

However, std::string doesn't change according to UNICODE; it's always Ansi, so its c_str method always returns a char*, never wchar_t*. When you have a parameter that's always Ansi, you should explicitly call functions that are always Ansi, too. In this case, call CreateDirectoryA.

You could also consider using std::basic_string<TCHAR>, but that's probably heading in a direction you don't wish to go.

A quick fix would be to adjust your project settings so that UNICODE is no longer defined. Consult the documentation for your tool set to find out how to do that, or explore your IDE's project options.

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

  1. Put a 'L' in front of the "example.bmp" string, e.g:

    file = CreateFile(L"exmple.bmp",GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL, NULL);
  2. This transforms "example.bmp" into a wide-character string.

How to convert char* to LPCWSTR?

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.

Cannot convert parameter from 'const char[20]' to 'LPCWSTR'

You have UNICODE defined, so MessageBox is expecting a wide string.



Related Topics



Leave a reply



Submit