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

Basically, I have simple code that does some things for files, and I'm trying to port it to windows. I have something similar to this:

int SomeFileCall(const char * filename){ #ifndef __unix__ SomeWindowsFileCall(filename); #endif #ifdef __unix__ /**** Some unix only stat code here! ****/ #endif } 

string SomeWindowsFileCall(filename); causes a compiler error: cannot convert parameter 1 from 'const char *' to 'LPCWSTR'

How can I fix this without changing the prototype of SomeFileCall ?

+5
source share
5 answers

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

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

In your project, it looks like all of these macros are referencing versions of -W. This is controlled by the UNICODE preprocessor macro (which is determined if you select the "Use Unicode Character Set" project option in Visual Studio). (Some of the Microsoft C and C ++ runtime library functions also have ANSI and wider versions. Which you get is selected by a macro with the same name _UNICODE , which is also determined by this setting of the Visual Studio project.)

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

If you have a char * containing the text on the corresponding ANSI code page, you can explicitly invoke the -A version (e.g. SetWindowTextA ). Versions of -A are typically wrappers that create wide copies of characters in string parameters and transfer control to the -W version.

An alternative is to create your own custom character characters. You can do this with MultiByteToWideChar . Calling this can be tricky because you need to manage buffers. If you can get away with a call to the -A version directly, which is usually simpler and already verified. But if your char * string uses UTF-8 or any encoding other than the current ANSI code page, you must do the conversion yourself.

Bonus Information

The suffix -A means "ANSI", which is a general Windows term for a single-byte character set for a code page.

The suffix -W means "Wide" (which means that the coding units are wider than one byte). In particular, Windows uses small-end UTF-16 for wide strings. The MSDN documentation just calls this Unicode, which is a bit wrong.

+9
source

Configure the project to use the ANSI character set. (General → Character Set)

What are TCHAR, WCHAR, LPSTR, LPWSTR, LPCTSTR, etc.

typedef const wchar_t * LPCWSTR;

+2
source

You create WinApi in Unicode mode, so all string parameters are allowed for wide strings. The simplest solution would be to change WinApi to ANSI, otherwise you need to create wchar_t* with the contents from filename and use this as an argument.

+1
source

not sure which compiler you are using, but in visual studio you can specify the default char type, be it UNICODE or multibyte. In your case, it sounds as if UNICODE was by default, so the easiest solution is to check the switch on your specific compiler, which determines the default type of char, because it will save you some work, otherwise you will end up adding code for conversions to and from UNICODE, which can add unnecessary overhead plus, can be an additional source of errors.

0
source

I can solve this error by setting the character set "Use multibyte character set" [Project Properties-> Configuration Properties → General → Character Set → "Use Multibyte Character Set"

0
source

Source: https://habr.com/ru/post/1414026/


All Articles