2011-11-08 95 views
5

额外的信息,以LPCWSTR IM建筑物使用WinHttpOpenRequest阿比使用Visual Studio 2008如何从LPCSTR转换成C++

+0

为什么你不在整个应用程序中使用宽字符串? – tenfour

+0

[LPCWSTR代表什么以及它应该如何处理?]的可能重复(http://stackoverflow.com/questions/2230758/what-does-lpcwstr-stand-for-and-how-should-it-被处理) –

回答

11

最简单的方法是使用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; 
} 

的A2W分配(ANSI到广角端)的任何内存将被释放函数退出时。

+2

这些ATL 3.0宏已被版本7.0 [ATL和MFC字符串转换宏](http://msdn.microsoft.com/zh-cn/library/87zae4a3.aspx)取代超过十年前。其中,ATL 7.0宏不再需要使用“USES_CONVERSION”宏。另外,您可以使用const-correct变体,例如在你的例子中:'CA2W(a)'。 – IInspectable

4

Converting from char *需要LPCWSTR用于对象名称 和IM应用程序有一个很好的样本

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; 

但就像提到的那样。使用generic text mapping如果可能的话