I am looking for ways to convert a PCHAR* variable to a TCHAR* without having any warnings in Visual Studio( this is a requirement)? Looking online I can't find a function or a method to do so without having warnings. Maybe somebody has come across something similar?
Thank you !
PCHARis atypedefthat resolves tochar*, soPCHAR*meanschar**.TCHARis a macro#define'd to either the "wide"wchar_tor the "narrow"char.In neither case can you (safely) convert between a
char **and a simple character pointer, so the following assumes the question is actually about converting aPCHARto aTCHAR*.PCHARis the sameTCHAR*in ANSI builds, and no conversion would be necessary in that case, so it can be further assumed that the question is about Unicode builds.The general question of converting between narrow and wide strings has been answered before, see for example Convert char * to LPWSTR or How to convert char* to LPCWSTR?. However, in this particular case, you could weigh the alternatives before choosing the general approaches.
Change your build settings to ANSI, instead of Unicode, then no conversion is necessary.
That's as easy as making sure neither
UNICODEnor_UNICODEmacros are defined when compiling, or changing in the IDE the project Configuration Properties / Advanced / Character Set from Use Unicode Character Set to either Not Set or Use Multi-Byte Character Set.Disclaimer: it is retrograde nowadays to compile against an 8-bit Windows codepage. I am not advising it, and doing that means many international characters cannot be represented literally. However, a chain is only as strong as its weakest link, and if you are forced to use narrow strings returned by an external function that you cannot change, then that's limiting the usefulness of going full Unicode elsewhere.
Keep the build as Unicode, but change just the concatenation code to use ANSI strings.
This can be done by explicitly calling the ANSI version
GetCurrentDirectoryAof the API, which returns a narrow string. Then you canstrcatthat directly with the otherPCHARstring.Keep it as is, but combine the narrow and wide strings using
[w]printfinstead of_tcscat_s.