dogger20011
dogger20011

Reputation: 255

C++ - ExpandEnvironmentStrings Giving Conversion Error

I have been using this:

char appdata[250];
    ExpandEnvironmentStrings("%AppData%\\\MCPCModLocator\\", appdata, 250);

in C++ Win32 to get the AppData folder on one of my projects. Its worked fine, no issues. Now on my newest project (same PC, still in Visual Studio 2013) when I try to do that, i get an error on the first string saying "const char* is incompatible with type LPCWSTR" and on the second parameter it says "char * is incompatible with type LPWSTR". I have no idea why it work on the first project, but not the second. I assume its a setting change, but looking through each projects settings, I see nothing. Any help is appreciated! Thanks!

Upvotes: 0

Views: 1032

Answers (2)

Sorayuki
Sorayuki

Reputation: 266

By default, newly created project in VS2013 has been set to use Unicode APIs, those use LPWSTR (or, wchar_t*) instead of LPSTR(or, char*).

You can call the old ANSI version APIs by add "A" at the end of function name explicitly e.g. ExpandEnvironmentStringsA or change the project configuration to use Multibyte character set(Project Property pages -> configuration properties -> general -> character set)

Upvotes: 4

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145269

ExpandEnvironmentStrings is a macro that expands to ExpandEnvironmentStringsA or ExpandEnvironmentStringsW depending on whether UNICODE was defined when you included <windows.h>.

In a Visual Studio project UNICODE is defined by default, but this is not so for command line use of the compiler.

Since modern Windows programming is better based on Unicode, the best fix is not to remove the definition of UNICODE but to add an L prefix to your literals, like L"Hello", which makes it a “wide” wchar_t based literal, and correspondingly change the type of appdata.

Upvotes: 5

Related Questions