additional info im building an application which use the WinHttpOpenRequest Api which requires LPCWSTR for the object name and im using visual studio 2008
Asked
Active
Viewed 2.8k times
10
-
Why aren't you using wide char strings throughout your app? – tenfour Nov 08 '11 at 00:20
-
possible duplicate of [What does LPCWSTR stand for and how should it be handled with?](http://stackoverflow.com/questions/2230758/what-does-lpcwstr-stand-for-and-how-should-it-be-handled-with) – Greg Hewgill Nov 08 '11 at 00:33
2 Answers
14
The simplest way is to use ATL:
#include <Windows.h>
#include <atlbase.h>
#include <iostream>
int main(int argc, char *argv[]) {
USES_CONVERSION;
LPCSTR a = "hello";
LPCWSTR w = A2W(a);
std::wcout << w << std::endl;
return 0;
}
Any memory allocated by A2W (ANSI to Wide) will be freed when the function exits.

Ferruccio
- 98,941
- 38
- 226
- 299
-
3Those ATL 3.0 macros have been superseded by version 7.0 [ATL and MFC String Conversion Macros](http://msdn.microsoft.com/en-us/library/87zae4a3.aspx) more than a decade ago. Among others, the ATL 7.0 macros no longer require the use of the `USES_CONVERSION` macro. Plus, you can use the const-correct variants, e.g. in your example: `CA2W(a)`. – IInspectable Oct 21 '14 at 12:39
-
However, in present 2021, there doesn't seem to be `ATL` library anymore. Any other ideas? – IneoSenigupta Nov 03 '21 at 09:48
-
@IneoSenigupta - I haven't been actively involved in Windows/C++ work in a long time but, as far as I can tell ATL is in VS2019. See https://learn.microsoft.com/en-us/cpp/atl/active-template-library-atl-concepts?view=msvc-160 – Ferruccio Nov 03 '21 at 11:12
4
Converting from char * has a nice sample
char *orig = "Hello, World!";
cout << orig << " (char *)" << endl;
// Convert to a wchar_t*
size_t origsize = strlen(orig) + 1;
const size_t newsize = 100;
size_t convertedChars = 0;
wchar_t wcstring[newsize];
mbstowcs_s(&convertedChars, wcstring, origsize, orig, _TRUNCATE);
wcscat_s(wcstring, L" (wchar_t *)");
wcout << wcstring << endl;
But like tenfour mentioned. Use generic text mapping if possible

parapura rajkumar
- 24,045
- 1
- 55
- 85