10

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

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

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

How do I fix this, without changing the SomeFileCall prototype?

3

6 Answers 6

13

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.

3
  • I would say most string-based WinAPI functions have both A and W. There are even some functions that are exclusively wide, such as ReadDirectoryChangesW.
    – dreamlax
    May 23, 2012 at 22:44
  • @dreamlax: Yep, CommandLineToArgvW is another one. Also, apparently Windows CE only has the W versions of most (all?) functions to reduce code size, though granted not too many people target Windows CE these days. May 24, 2012 at 4:25
  • Calling the -A version may be simpler, but it uses the "ANSI" code page. So, if your code uses UTF-8 for strings, you'll have to convert anyway. And you won't be able to open files with non-ANSI characters in their names.
    – dan04
    May 25, 2012 at 12:37
5

{project properties->advanced->character set->use multi byte character set} İf you do these step you problem is solved

4

Configure your project to use ANSI character set. (General -> Character Set)

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

typedef const wchar_t* LPCWSTR;

1

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

1

Am able to solve this error by setting the Character set to "Use Multi-Byte Character set" [Project Properties-> Configuration Properties -> General -> Character Set ->"Use Multi-Byte Character set"

0

not sure what compiler you are using but in visual studio you can specify the default char type, whether it be UNICODE or multibyte. In your case it sounds as if UNICODE is default so the simplest solution is to check for the switch on your particular compiler that determines default char type because it would save you some work, otherwise you would end up adding code to convert back and forth from UNICODE which may add unnecessary overhead plus could be an additional source of error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.