Sigh
I am working with the Microsoft Visual Express C++ IDE. I have recently transitioned from DevC++ to this IDE.
I have a function that works perfectly in DevC++ but in MSVC++ it wont compile because of Unicode (I think?). What do I need to change to get my function to compile?
See the commented code line for the compiler error I get
map <string, string> GetEvironmentVariablesEx()
{
map <string, string> envVariables;
char* environVar = GetEnvironmentStrings(); // Compile error: error C2440: 'initializing' : cannot convert from 'LPWCH' to 'char *'
char* pos = strchr( environVar, '\0' );
// Skip over the "=::=::\0" of the environVar string
if ( pos != NULL ) { environVar = ++pos; pos = strchr( environVar, '\0' ); }
else return envVariables;
while ( true )
{
char* delim = strchr( environVar, '=' );
if ( delim == NULL )
break;
string variable = string( environVar, strlen(environVar)-strlen(delim) );
string value = string( ++delim );
envVariables.insert( pair<string, string>(variable, value) );
environVar = ++pos;
// find the "\0\0" that identifies the end of environVar
if ( pos != NULL && *pos == 0 ) { break; }
pos = strchr( environVar, '\0' );
}
FreeEnvironmentStrings( environVar );
return envVariables;
}
PS: Because this application is compiled in Unicode, does that mean that it will work on both ANSII computers & UNICODE computers – so my app will able to be run internationally?
your project is a UNICODE build and you using ANSI strings, first thing to do is replacing your char variables to TCHAR and try again.
And as @David Heffernan, aptly suggested, you will have to switch to wstring and aptly change the library functions from standard library which you use.