作者:朱金灿 来源:http://blog.csdn.net/clever101 以前看《Window核心编程》,感觉多字节和宽字节之间还比较麻烦的,至少MultiByteToWideChar函数和WideCharToMultiByte函数有足够多的参数的意义让我们去理解。近日接触了ATL的一个很好的字符串的转换宏:A2W和W2A。 用法很简单,A2W的用法: view plaincopy to clipboardprint? #include <atlconv.h> DoSomething(LPWSTR str); // 函数声明 USES_CONVERSION; DoSomething(A2W("SomeString")); #include <atlconv.h> DoSomething(LPWSTR str); // 函数声明 USES_CONVERSION; DoSomething(A2W("SomeString")); W2A的用法: view plaincopy to clipboardprint? #include <atlconv.h> DoSomething(LPCSTR str); // 函数声明 USES_CONVERSION; DoSomething(W2A(L"SomeString")); #include <atlconv.h> DoSomething(LPCSTR str); // 函数声明 USES_CONVERSION; DoSomething(W2A(L"SomeString")); 另外使用这两个宏时最好把它们单独放入一个函数实现,具体原因见: 谨慎使用A2W等字符转换宏 另外这儿一篇详细介绍这两个宏的原理的文章:VC中一个关于宏的使用问题。

评论