Im trying to convert arguments LPWSTR to LPTSTR, it there any library I can user for easy string convertion?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If
_UNICODEis defined there’s no conversion to do – aTCHARis aWCHAR, andLPWSTRisLPTSTR; otherwise,TCHARis aCHAR, andLPTSTRisLPSTR. In this last case, you can use the WideCharToMultiByte API to convert aLPWSTR(i.e. a Unicode string) to a “narrow” string.Also, you can use the
wcstombsC library function, assuming that the Windows typesCHARandWCHARmap to the C typescharandwchar_t(which is surely the case in VC++), and that the current C locale is the one you intend to use for the conversion.See also Alf‘s comments for more insight on the matter – the most important point being that if you can you should avoid such conversion, since your output encoding in general cannot represent faithfully the input, and, depending from what your string represents this may mean garbled text as well as not finding the required file or, even worse, operating on the wrong path.
So, set your project to use Unicode (as in @prazuber‘s answer) and always try to use Unicode-aware APIs, so to avoid this kind of conversion.
Still, keep in mind that using only the Unicode version of the Windows APIs may mean losing compatibility with Windows 9x; this is not a problem for the vast majority of cases, but if it’s critical to support such OSes in your application check if the APIs you are using are supported in MSLU.