﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>IT博客-_skyfly-文章分类-ATL/WTL</title><link>http://www.cnitblog.com/_skyfly/category/366.html</link><description>我不要在IT中迷失方向</description><language>zh-cn</language><lastBuildDate>Mon, 26 Sep 2011 17:06:04 GMT</lastBuildDate><pubDate>Mon, 26 Sep 2011 17:06:04 GMT</pubDate><ttl>60</ttl><item><title>我最近正在将以前的项目转为UNICODE编码格式,看到下面的一篇文章,觉得很有用,现转贴在这里</title><link>http://www.cnitblog.com/_skyfly/articles/2657.html</link><dc:creator>龙井2008</dc:creator><author>龙井2008</author><pubDate>Wed, 14 Sep 2005 06:14:00 GMT</pubDate><guid>http://www.cnitblog.com/_skyfly/articles/2657.html</guid><wfw:comment>http://www.cnitblog.com/_skyfly/comments/2657.html</wfw:comment><comments>http://www.cnitblog.com/_skyfly/articles/2657.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.cnitblog.com/_skyfly/comments/commentRss/2657.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/_skyfly/services/trackbacks/2657.html</trackback:ping><description><![CDATA[<DIV class=postText>
<P><FONT face="Courier New">网上有一篇丁有和的《Visual C++.NET中 字符串转换方法》，对于我这号笨人来说，不是很清晰，所以重新写了一下。</FONT></P>
<P><FONT face="Courier New"><STRONG>一、TCHAR,LPTSTR,LPCTSTR</STRONG>:定义于WinNT.h</FONT></P>
<P><FONT face="Courier New">//<BR>// Neutral ANSI/UNICODE types and macros<BR>//<BR>#ifdef UNICODE // r_winnt<BR>#ifndef _TCHAR_DEFINED<BR>typedef WCHAR TCHAR, *PTCHAR;<BR>typedef WCHAR TBYTE , *PTBYTE ;<BR>#define _TCHAR_DEFINED<BR>#endif /* !_TCHAR_DEFINED */<BR>typedef LPWSTR LPTCH, PTCH;<BR>typedef LPWSTR PTSTR, LPTSTR;<BR>typedef LPCWSTR PCTSTR, LPCTSTR;<BR>typedef LPUWSTR PUTSTR, LPUTSTR;<BR>typedef LPCUWSTR PCUTSTR, LPCUTSTR;<BR>typedef LPWSTR LP;<BR>#define __TEXT(quote) L##quote&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // r_winnt<BR>#else&nbsp;&nbsp; /* UNICODE */&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // r_winnt<BR>#ifndef _TCHAR_DEFINED<BR>typedef char TCHAR, *PTCHAR;<BR>typedef unsigned char TBYTE , *PTBYTE ;<BR>#define _TCHAR_DEFINED<BR>#endif /* !_TCHAR_DEFINED */<BR>typedef LPSTR LPTCH, PTCH;<BR>typedef LPSTR PTSTR, LPTSTR, PUTSTR, LPUTSTR;<BR>typedef LPCSTR PCTSTR, LPCTSTR, PCUTSTR, LPCUTSTR;<BR>#define __TEXT(quote) quote&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // r_winnt<BR>#endif /* UNICODE */&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // r_winnt</FONT></P>
<P><FONT face="Courier New">类似的定义出现于Wtypes.h：</FONT></P>
<P><BR><FONT face="Courier New">typedef char CHAR;<BR>typedef /* [string] */ CHAR *LPSTR;<BR>typedef /* [string] */ const CHAR *LPCSTR;<BR>#ifndef _WCHAR_DEFINED<BR>#define _WCHAR_DEFINED<BR>typedef wchar_t WCHAR;<BR>typedef WCHAR TCHAR;<BR>#endif // !_WCHAR_DEFINED<BR>typedef /* [string] */ WCHAR *LPWSTR;<BR>typedef /* [string] */ TCHAR *LPTSTR;<BR>typedef /* [string] */ const WCHAR *LPCWSTR;<BR>typedef /* [string] */ const TCHAR *LPCTSTR;</FONT></P>
<P><BR><FONT face="Courier New">注意在TCHAR.h, WinNT.h中都定义了TCHAR，它们是靠#ifdef _TCHAR_DEFINED来判断的。在VC6.0中：</FONT></P>
<P><FONT face="Courier New">#include &lt;WINNT.h&gt;只在WINDEF.H中出现过。<BR>#include &lt;windef.h&gt;出现在 Windows.h中。<BR>#include &lt;windef.h&gt;、#include &lt;TCHAR.h&gt;出现在ATL/INCLUDE/ATLBASE.H中</FONT></P>
<P><FONT face="Courier New">TCHAR.h中重新定义了标准C库的很多字符串处理函数的宏，只要在工程中定义或取消UNICODE和＿UNICODE，就可以在多字节字符串和UNICODE间互相转换。按Win32编程要求的"Think In UNICODE"，TCHAR.h中的函数应该是经常用到的。</FONT></P>
<P><FONT face="Courier New"><STRONG>二、BSTR</STRONG>(BASIC String):定义于WTypes.h</FONT></P>
<P><FONT face="Courier New">#if defined(_WIN32) &amp;&amp; !defined(OLE2ANSI)<BR>&nbsp;&nbsp;&nbsp; typedef WCHAR OLECHAR;<BR>&nbsp;&nbsp;&nbsp; typedef /* [string] */ OLECHAR *LPOLESTR;<BR>&nbsp;&nbsp;&nbsp; typedef /* [string] */ const OLECHAR *LPCOLESTR;<BR>#&nbsp;&nbsp; define OLESTR(str) L##str<BR>#else<BR>&nbsp;&nbsp;&nbsp; typedef char&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OLECHAR;<BR>&nbsp;&nbsp;&nbsp; typedef LPSTR&nbsp;&nbsp;&nbsp;&nbsp; LPOLESTR;<BR>&nbsp;&nbsp;&nbsp; typedef LPCSTR&nbsp;&nbsp;&nbsp; LPCOLESTR;<BR>#&nbsp;&nbsp; define OLESTR(str) str<BR>#endif<BR>typedef /* [wire_marshal] */ OLECHAR *BSTR;</FONT></P>
<P><BR><FONT face="Courier New">可以看到，BSTR实际上还是一个C-style的宽字符串（如果定义了OLE2ANSI，就是C字符串了，但很少用到，因为在OLE接口中传递时都是宽字符串），只不过在使用BSTR时，得用到特殊的申请/释放函数组。因为BSTR指针位置前面有一个四字节的数据，存储的是BSTR的长度。就是这个原因，我们不能把OLECHAR*当作BSTR来用，而编译器在遇到这种方式时还不报错！</FONT></P>
<P><FONT face="Courier New">在MFC程序中不用BSTR时，可以在stdafx.h中所有#include之前加一行：<BR>#define _AFX_NO_BSTR_SUPPORT</FONT></P>
<P><FONT face="Courier New">在ATL对象的接口定义中，无论是双接口还是自定义接口，我们一般看不到TCHAR等定义，我们看到的是BSTR(双接口时的VARIANT，如果是BSTR类型，可以直接用BSTR的),这要求我们作一些处理。对于传入的参数，一般的做法是将参数赋给一个串处理类。对于传出的参数，一般的方法是内部先处理，最后将字符串复制一份传出去。</FONT></P>
<P><FONT face="Courier New"><STRONG>三、CComBSTR</STRONG></FONT></P>
<P><FONT face="Courier New">CComBSTR是ATL库中的一个BSTR封装类，提供了一些简单的函数,支持各类字符串到BSTR的转换,但没有格式化、搜索、子串处理等基本功能。功能太过简单使得这个类可以说是一个鸡胁，只能作一个转换工具用。奇怪的是在Visaul .NET 2003版本中还是没什么大的改进，我本以为M$会把CString的所有功能都搬进去的。</FONT></P>
<P><FONT face="Courier New">在M$的《用 CComBSTR 进行编程》提到了一些使用CComBSTR时要注意的一些问题。</FONT></P>
<P><FONT face="Courier New"><STRONG>四、_bstr_t</STRONG></FONT></P>
<P><FONT face="Courier New">＿bstr_t是一个与ATL无关的COM的支持类，这个类定义在comutil.h文件中（同时还定义有一个_variant_t类）。这个类也是一个功能与CComBSTR类似，主要用于转换是，也是一个功能不怎么强的类。</FONT></P>
<P><FONT face="Courier New">在comutil.h还定义了两个函数用于在标准C字符串与BSTR间的转换，可以说这是个底层函数。程序员对TCHAR作一次分解就可以写出TCHAR适用的程序了。</FONT></P>
<P><FONT face="Courier New">namespace _com_util { <BR>&nbsp;&nbsp;&nbsp; // Convert char * to BSTR<BR>&nbsp;&nbsp;&nbsp; //<BR>&nbsp;&nbsp;&nbsp; BSTR __stdcall ConvertStringToBSTR(const char* pSrc) ;<BR>&nbsp;&nbsp;&nbsp; // Convert BSTR to char *<BR>&nbsp;&nbsp; &nbsp;//<BR>&nbsp;&nbsp; &nbsp;char* __stdcall ConvertBSTRToString(BSTR pSrc) ;<BR>}</FONT></P>
<P><FONT face="Courier New"><STRONG>五、CString/CStringA/CStringW</STRONG></FONT></P>
<P><FONT face="Courier New">CString在VC6.0以前是MFC的一个类，要想在COM中使用，就得加上那个900K的MFC42.DLL。可能是MFC下的CString太好用了(:p,不要打我)，所以M$干脆把这个类搬到ATL中。在Visual C++.NET中CStringT(在CStringT.h中实现)成为ATL的一个模板方式的类，只是将以前在MFC中的方法搬了过来；而CString定义在afxstr.h中：</FONT></P>
<P><FONT face="Courier New">// MFC-enabled compilation. Use MFC memory management and exceptions;<BR>// also, use MFC module state.<BR>// Don't import when MFC dll is being built<BR>#if !defined(_WIN64) &amp;&amp; defined(_AFXDLL)<BR>#if defined(_MFC_DLL_BLD)<BR>template class ATL::CSimpleStringT&lt; char, true &gt;;<BR>template class ATL::CStringT&lt; char, StrTraitMFC_DLL&lt; char &gt; &gt;;<BR>template class ATL::CSimpleStringT&lt; wchar_t, true &gt;;<BR>template class ATL::CStringT&lt; wchar_t, StrTraitMFC_DLL&lt; wchar_t &gt; &gt;;<BR>#else<BR>template class __declspec(dllimport) ATL::CSimpleStringT&lt; char, true &gt;;<BR>template class __declspec(dllimport) ATL::CStringT&lt; char, StrTraitMFC_DLL&lt; char &gt; &gt;;<BR>template class __declspec(dllimport) ATL::CSimpleStringT&lt; wchar_t, true &gt;;<BR>template class __declspec(dllimport) ATL::CStringT&lt; wchar_t, StrTraitMFC_DLL&lt; wchar_t &gt; &gt;;<BR>#if defined(_NATIVE_WCHAR_T)<BR>template class __declspec(dllimport) ATL::CSimpleStringT&lt; unsigned short, true &gt;;<BR>template class __declspec(dllimport) ATL::CStringT&lt; unsigned short, StrTraitMFC_DLL&lt; unsigned short &gt; &gt;;<BR>#endif // _NATIVE_WCHAR_T<BR>#endif // _MFC_DLL_BLD<BR>typedef ATL::CStringT&lt; wchar_t, StrTraitMFC_DLL&lt; wchar_t &gt; &gt; CStringW;<BR>typedef ATL::CStringT&lt; char, StrTraitMFC_DLL&lt; char &gt; &gt; CStringA;<BR>typedef ATL::CStringT&lt; TCHAR, StrTraitMFC_DLL&lt; TCHAR &gt; &gt; CString;<BR>#else<BR>typedef ATL::CStringT&lt; wchar_t, StrTraitMFC&lt; wchar_t &gt; &gt; CStringW;<BR>typedef ATL::CStringT&lt; char, StrTraitMFC&lt; char &gt; &gt; CStringA;<BR>typedef ATL::CStringT&lt; TCHAR, StrTraitMFC&lt; TCHAR &gt; &gt; CString;<BR>#endif // !_WIN64 &amp;&amp; _AFXDLL</FONT></P>
<P><BR><FONT face="Courier New">关于ATL7.0中CString的用法，在M$的《自 Visual C++ 6.0 以来 ATL 7.0 和 MFC 7.0 中的重大更改》中有提到：<FONT color=#339966>从 BSTR 转换到 CString</FONT></FONT></P>
<P><FONT face="Courier New" color=#339966>在 Visual C++ 6.0 中，使用下面的代码是可以接受的：</FONT></P>
<P><FONT face="Courier New" color=#339966>BSTR bstr = SysAllocString(L"Hello");<BR>CString str = bstr;<BR>SysFreeString(bstr);对于 Visual C++ .NET 下的新项目，这将在 ANSI 版本下导致下面的错误error C2440: 'initializing' : cannot convert from 'BSTR' to <BR>'ATL::CStringT&lt;BaseType,StringTraits&gt;'<BR>现在有 CString 的 UNICODE 和 ANSI 版本（CStringW 和 CStringA）。若要标记任何由隐式转换导致的不必要的系统开销，采用反向类型（如带 UNICODE 参数的 CStringA，或者带 ANSI 参数的 CStringW）的构造函数现在在 stdafx.h 中被使用下面的项标记为显式的：</FONT></P>
<P><FONT face="Courier New" color=#339966>#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS</FONT></P>
<P><FONT face="Courier New" color=#339966>若要避免此错误，请执行下列操作之一： </FONT></P>
<P><FONT face="Courier New"><FONT color=#339966>使用 CStringW 以避免转换： <BR>&nbsp;&nbsp;&nbsp; </FONT></FONT><FONT face="Courier New"><FONT color=#339966>BSTR bstr = SysAllocString(L"Hello");<BR>&nbsp;&nbsp;&nbsp; CStringW str = bstr;<BR>&nbsp;&nbsp;&nbsp; SysFreeString(bstr);<BR><BR>显式调用该构造函数： <BR>&nbsp;&nbsp;&nbsp; BSTR bstr = SysAllocString(L"Hello");<BR>&nbsp;&nbsp;&nbsp; CString str = CString(bstr);<BR>&nbsp;&nbsp;&nbsp; SysFreeString(bstr);<BR><BR>从 stdafx.h 中移除 #define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS 行。</FONT> <BR><BR>在VC6.0及以前，很多程序员都做过将MFC中的CString，这种在网上很多。我看到的一个很完整的是gosh的gd::CString，网址是</FONT><A href="http://www.codeproject.com/soap/serialize_xml.asp"><FONT face="Courier New">http://www.codeproject.com/soap/serialize_xml.asp</FONT></A><FONT face="Courier New">。据这位老兄自己说，他自己还没有VC 6.0编译器。</FONT></P>
<P><FONT face="Courier New"><STRONG>六、std::string和std::wstring</STRONG></FONT></P>
<P><FONT face="Courier New">std::string在我看来是std::vector&lt;char&gt;。但这个类可以说是一个有史以来计算机程序员对字符串处理类的一个总结，它的算法应该是没得说的。</FONT></P>
<P><FONT face="Courier New"><STRONG>七、字符串(类)间的转换</STRONG></FONT></P>
<P><FONT face="Courier New">以上各类字符串间的转换从概念上有两种方式，第一种是已经有的转换接口，另一种是基于某一个中间格式作转换，其实第一种实现也是基于第二种的。</FONT></P>
<P><FONT face="Courier New"><STRONG>char 与 wchar_t </STRONG>间的转换：<BR>简单地说，是两个Win32函数：WideCharToMultiByte和MultiByteToWideChar。当然中间还涉及到代码页和线程代码页等参数。如果用标准C函数wctomb和mbtowc也可以。</FONT></P>
<P><FONT face="Courier New">在M$的《自Visual C++6.0以来ATL7.0和MFC7.0中的重大更改》中有提到：<FONT color=#339966>字符串转换</FONT></FONT></P>
<P><FONT face="Courier New" color=#339966>Visual C++ 6.0 中 ATL 3.0 和 ATL 3.0 以前的 ATL 版本中，使用 atlconv.h 中的宏的字符串转换始终是使用系统 (CP_ACP) 的 ANSI 代码页执行的。从 Visual C++ .NET 中的 ATL 7.0 开始，字符串转换将使用当前线程的默认 ANSI 代码页执行，除非定义了 _CONVERSION_DONT_USE_THREAD_LOCALE（此情况下，如以前一样使用系统的 ANSI 代码页）。</FONT></P>
<P><FONT face="Courier New" color=#339966>请注意，字符串转换类（如 CW2AEX）使您得以将用于转换的代码页传递给它们的构造函数。如果未指定代码页，这些类使用与宏相同的代码页。</FONT></P>
<P><FONT face="Courier New" color=#339966>有关更多信息，请参见 ATL 和 MFC 字符串转换宏。</FONT></P>
<P><FONT face="Courier New"><STRONG>COM程序</STRONG>中，接口一般都是BSTR，所以无论在编写COM组件时还是使用组件时，转为BSTR和从BSTR转出都可能遇到。如果全部采用UNICODE，麻烦要少一些，如果使用到了char或std::string，麻烦就不少了。但一般而言，我们有以下方法可以用：</FONT></P>
<P><FONT face="Courier New">在接口(组件)内部，全部用UNICODE处理。将传入的BSTR转换为std::wstring、gd:CString或是其它的什么类，在内部成员变量声明时用同样的类，内部处理时不用管外面，生成结果时用一个CComBST或是_bstr_t类作一次转换就可以了。</FONT></P>
<P><FONT face="Courier New">在接口外部，调用COM组件的接口函数前，将字符串用_bstr_t或者comutil.h的函数作一个转换，传入组件进行处理；传出时，作一个反向的转换。</FONT></P>
<P><FONT face="Courier New"><STRONG>非COM程序</STRONG>中，接口一般用TCHAR*，这种方式的代价最小。<BR>&nbsp;<BR></FONT></P><BR><BR>
<P id=TBPingURL>Trackback: <A href="http://tb.donews.net/TrackBack.aspx?PostId=245116">http://tb.donews.net/TrackBack.aspx?PostId=245116</A></P></DIV><img src ="http://www.cnitblog.com/_skyfly/aggbug/2657.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/_skyfly/" target="_blank">龙井2008</a> 2005-09-14 14:14 <a href="http://www.cnitblog.com/_skyfly/articles/2657.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>