`
pleasetojava
  • 浏览: 701344 次
  • 性别: Icon_minigender_2
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

Win编程笔记:字符串小结

 
阅读更多

<!-- [if gte mso 9]><xml> <o:OfficeDocumentSettings> <o:RelyOnVML/> <o:AllowPNG/> </o:OfficeDocumentSettings> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:TrackMoves/> <w:TrackFormatting/> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:DoNotPromoteQF/> <w:LidThemeOther>EN-US</w:LidThemeOther> <w:LidThemeAsian>ZH-CN</w:LidThemeAsian> <w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:SplitPgBreakAndParaMark/> <w:DontVertAlignCellWithSp/> <w:DontBreakConstrainedForcedTables/> <w:DontVertAlignInTxbx/> <w:Word11KerningPairs/> <w:CachedColBalance/> <w:UseFELayout/> </w:Compatibility> <m:mathPr> <m:mathFont m:val="Cambria Math"/> <m:brkBin m:val="before"/> <m:brkBinSub m:val="&#45;-"/> <m:smallFrac m:val="off"/> <m:dispDef/> <m:lMargin m:val="0"/> <m:rMargin m:val="0"/> <m:defJc m:val="centerGroup"/> <m:wrapIndent m:val="1440"/> <m:intLim m:val="subSup"/> <m:naryLim m:val="undOvr"/> </m:mathPr></w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" DefUnhideWhenUsed="true" DefSemiHidden="true" DefQFormat="false" DefPriority="99" LatentStyleCount="267"> <w:LsdException Locked="false" Priority="0" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Normal"/> <w:LsdException Locked="false" Priority="9" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="heading 1"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 2"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 3"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 4"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 5"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 6"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 7"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 8"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 9"/> <w:LsdException Locked="false" Priority="39" Name="toc 1"/> <w:LsdException Locked="false" Priority="39" Name="toc 2"/> <w:LsdException Locked="false" Priority="39" Name="toc 3"/> <w:LsdException Locked="false" Priority="39" Name="toc 4"/> <w:LsdException Locked="false" Priority="39" Name="toc 5"/> <w:LsdException Locked="false" Priority="39" Name="toc 6"/> <w:LsdException Locked="false" Priority="39" Name="toc 7"/> <w:LsdException Locked="false" Priority="39" Name="toc 8"/> <w:LsdException Locked="false" Priority="39" Name="toc 9"/> <w:LsdException Locked="false" Priority="35" QFormat="true" Name="caption"/> <w:LsdException Locked="false" Priority="10" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Title"/> <w:LsdException Locked="false" Priority="1" Name="Default Paragraph Font"/> <w:LsdException Locked="false" Priority="11" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Subtitle"/> <w:LsdException Locked="false" Priority="22" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Strong"/> <w:LsdException Locked="false" Priority="20" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Emphasis"/> <w:LsdException Locked="false" Priority="59" SemiHidden="false" UnhideWhenUsed="false" Name="Table Grid"/> <w:LsdException Locked="false" UnhideWhenUsed="false" Name="Placeholder Text"/> <w:LsdException Locked="false" Priority="1" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="No Spacing"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 1"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 1"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 1"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 1"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 1"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 1"/> <w:LsdException Locked="false" UnhideWhenUsed="false" Name="Revision"/> <w:LsdException Locked="false" Priority="34" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="List Paragraph"/> <w:LsdException Locked="false" Priority="29" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Quote"/> <w:LsdException Locked="false" Priority="30" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Intense Quote"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 1"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 1"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 1"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 1"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 1"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 1"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 1"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 1"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 2"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 2"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 2"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 2"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 2"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 2"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 2"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 2"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 2"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 2"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 2"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 2"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 2"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 2"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 3"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 3"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 3"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 3"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 3"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 3"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 3"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 3"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 3"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 3"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 3"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 3"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 3"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 3"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 4"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 4"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 4"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 4"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 4"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 4"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 4"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 4"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 4"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 4"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 4"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 4"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 4"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 4"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 5"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 5"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 5"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 5"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 5"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 5"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 5"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 5"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 5"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 5"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 5"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 5"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 5"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 5"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 6"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 6"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 6"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 6"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 6"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 6"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 6"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 6"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 6"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 6"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 6"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 6"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 6"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 6"/> <w:LsdException Locked="false" Priority="19" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Subtle Emphasis"/> <w:LsdException Locked="false" Priority="21" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Intense Emphasis"/> <w:LsdException Locked="false" Priority="31" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Subtle Reference"/> <w:LsdException Locked="false" Priority="32" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Intense Reference"/> <w:LsdException Locked="false" Priority="33" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Book Title"/> <w:LsdException Locked="false" Priority="37" Name="Bibliography"/> <w:LsdException Locked="false" Priority="39" QFormat="true" Name="TOC Heading"/> </w:LatentStyles> </xml><![endif]--><!-- [if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.5pt; mso-bidi-font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-font-kerning:1.0pt;} </style> <![endif]-->

Win 编程笔记:字符串小结

JasonLee @ http://blog.csdn.net/jasonblog

Basics

ANSI Unicode

前者是单字节字符编码,用来表示英语以及一些西欧语言的所有字符;后者是双字节字符编码,可以表示现代计算机涉及的所有字符(包括中文、日文等)。

显然,使用 Unicode 更有利于国际化和本地化。除此之外, Windows 所有核心函数也都需要 Unicode 字符串。在调用这些函数时,如果传入 ANSI 字符串,那么这些函数会将其转换成 Unicode 再执行;反之,如果函数返回 ANSI 字符串,那么操作系统也是先将其从 Unicode 字符串转换为 ANSI 字符串再返回。这样的话,会增加开销,而且埋下一些目前已知的转换函数中存在的 bug

使用 Unicode

ANSI 字符是由 char 类型来表示的,而在 win 编程中, Unicode 字符由 wchar_t wide char type )来表示的。在使用 wchar_t 类型时,需要在字符 / 字符串前加上大写字母 L 来告诉编译器这是 Unicode 字符 / 字符串,如下:

wchar_t c = L 'a' ;

wchar_t str [10] = L "hello" ;

为了更有 Windows 特色,微软做了如下类型定义( WinNT.h ):

typedef char CHAR ;

typedef short SHORT ;

typedef long LONG ;

……

typedef wchar_t WCHAR ;

在前面加 P LP 或者 C 分别表示指针类型和 Const 限定,如:

typedef __nullterminated WCHAR *NWPSTR , *LPWSTR , *PWSTR ;

……

typedef __nullterminated CONST WCHAR *LPCWSTR , *PCWSTR ;

……

typedef __nullterminated CHAR *NPSTR , *LPSTR , *PSTR ;

……

typedef __nullterminated CONST CHAR *LPCSTR , *PCSTR ;

此外,为了更通用一点,微软还做了如下定义:

#ifdef UNICODE // r_winnt

#ifndef _TCHAR_DEFINED

typedef WCHAR TCHAR , *PTCHAR ;

……

#define __TEXT (quote ) L # #quote // r_winnt

#else /* UNICODE */ // r_winnt

#ifndef _TCHAR_DEFINED

typedef char TCHAR, *PTCHAR;

……

#define __TEXT(quote) quote // r_winnt

#endif /* UNICODE */ // r_winnt

#define TEXT (quote ) __TEXT (quote ) // r_winnt

即,使用 TCHAR (以及 PTSTR ),无论字符集是 ANSI 还是 Unicode 都可以通过编译,与之相应的通用字符串函数也出现了,比如 _tcslen tchar.h ):

#ifdef _UNICODE

#define _tcslen wcslen

#else

#define _tcslen strlen

#endif

除了 _tcslen ,还有一堆其他的字符串处理函数,并且与 _tcslen (计算字符串长度)不同,其它很多函数需要考虑安全性,比如 _tcscpy 或者 _tcscat

#ifdef _UNICODE

#define _tcscat wcscat

#define _tcscat_s wcscat_s

#define _tcscpy wcscpy

#define _tcscpy_s wcscpy_s

#else

#define _tcscat strcat

#define _tcscat_s strcat_s

#define _tcscpy strcpy

#define _tcscpy_s strcpy_s

#endif

紧接着处理函数的都是一个安全版本,这些安全性函数都是添加一个 _s 后缀,它们检验参数的有效性以及缓冲区是否足以容纳结果。

最后, WinNT.h 中还定义了宏 TEXT __TEXT tchar.h 中定义了宏 _T __T _TEXT ,这些宏的功能都是将字符 / 字符串转换为相应的通用类型,比如:

LPTSTR str1 = _T ("hello,world" );

LPTSTR str2 = TEXT ("hello" );

ATL 字符串

COM 字符类型

OLECHAR :不同操作系统上, OLECHAR 对应不同字符类型,比如 win32 上对应 wchar_t

BSTR :一种带长度前缀、含许多特殊语义的 OLECHAR 字符数组。

ATL 字符串转换类

所有类名称都采用“ C< 源格式缩写 >2< 目标格式缩写 > ”的形式,第一个 C 表示类。缩写中 A 代表指向 char 的字符指针( LPSTR )、 W 代表指向 wchar_t 的字符指针( LPWSTR )、 T 代表指向 TCHAR 的字符指针( LPTSTR )、 OLE 代表指向 OLECHAR 的字符指针( LPOLESTR )、 C 表示 const 限定符。

以下是字符串转换类:

CA2W CA2WEX CA2T CA2TEX CA2CT CA2CTEX

COLE2T COLE2TEX COLE2CT COLE2CTEX CT2A

CT2AEX CT2CA CT2CAEX CT2OLE CT2OLEEX CT2COLE

CT2COLEEX CT2W CT2WEX CT2CW CT2CWEX CW2A

CW2AEX CW2T CW2TEX CW2CT CW2CTEX

BSTR CComBSTR

BSTR 是指向复合数据类型的指针,该复合数据类型由长度前缀、数据字符串和结束符组成,并且要使用 COM 内存管理函数来管理 BSTR ,其专用语义繁多,用法繁琐。

CComBSTR BSTR 的一个封装类。

CComBSTR

CComBSTR 类维护一个 BSTR 类型的 public 成员 m_str

使用小结如下:

//CComBSTR 使用小结

//1. 构造

//CComBSTR() { m_str = NULL; }

CComBSTR str1 ;

//CComBSTR(LPCOLESTR pSrc);

CComBSTR str2 (LPCOLESTR ("hello,world" ));

//CComBSTR(int nSize, LPCOLESTR sz);

CComBSTR str3 (5, (LPCOLESTR )NULL );

//CComBSTR(int nSize);

CComBSTR str4 (5);

//CComBSTR(LPCSTR pSrc);

CComBSTR str5 ("hello,world" );

//CComBSTR(int nSize, LPCSTR sz);

CComBSTR str6 (5, "hello,world" );

//CComBSTR(REFGUID src);

static const GUID id_sample =

{ 0x3d8b3644, 0xf90e, 0x4058, { 0x95, 0xaf, 0xda, 0x5a, 0xe8, 0x81, 0x8, 0x98 } };

CComBSTR str7 (id_sample );

//2. 复制构造函数

//CComBSTR(const CComBSTR& src);

CComBSTR str8 (str2 );

//3. 析构

//~CComBSTR() {::SysFreeString(m_str); }

//4. 赋值

//CComBSTR& operator=(const CComBSTR& src){ ... m_str = src.Copy(); ...}

str3 = str2 ;

//CComBSTR& operator=(LPCOLESTR pSrc){ ... m_str = ::SysAllocString(pSrc); ... }

str8 = LPCOLESTR ("hello,world" );

//CComBSTR& operator=(LPCSTR pSrc){ ... m_str = A2WBSTR(pSrc); ... }

str1 = "hello,world" ;

//HRESULT AssignBSTR(const BSTR bstrSrc){ ... }

BSTR bstrTemp = ::SysAllocString (LPCOLESTR ("hello,world" ));

CComBSTR str9 ;

str9 .AssignBSTR (bstrTemp );

//bool LoadString(HINSTANCE hInst, UINT nID);

// 从指定模块hInst 加载指定的字符串资源nID

//bool LoadString(UINT nID);

// 使用全局变量_AtlBaseModule 从当前模块加载指定的字符串资源nID

//5. 字符串连接

//HRESULT Append(LPCOLESTR lpsz, int nLen);

//HRESULT Append(LPCOLESTR lpsz);

//HRESULT Append(LPCSTR);

//HRESULT Append(char ch);

//HRESULT Append(wchar_t ch);

//HRESULT Append(const CComBSTR& bstrSrc);

//HRESULT AppendBSTR(BSTR p);

//HRESULT AppendBytes(const char * lpsz, int nLen);

//CComBSTR& operator+=(const CComBSTR& bstrSrc);

//6. 字符串比较

//bool operator !() const { return (m_str == NULL); }

//operator<() 一共有四个重载版本,后面个都是调用第一个,第一个使用VarBstrCmp 进行比较

//bool operator<(const CComBSTR& bstrSrc) const{ ... }

//bool operator<(LPCSTR pszSrc) const{ ... }

//bool operator<(LPCOLESTR pszSrc) const{ ... }

//bool operator<(LPOLESTR pszSrc) const{ ... }

//operator>() operator<() 类似

//operator!=() operator==() 则多重载了一个与NULL 比较的版本

//7. 其它操作

// 返回长度

//unsigned int Length() const { return ::SysStringLen(m_str); }

unsigned int len = str2 .Length ();

// 复制操作

//BSTR Copy() { ... }

//HRESULT CopyTo(BSTR* pbstr);

// 转换操作

str2 .ToLower ();

str2 .ToUpper ();

//HRESULT BSTRToArray(LPSAFEARRAY * ppArray){ ... }

//HRESULT ArrayToBSTR(const SAFEARRAY * pSrc){ ... }

// 清空操作

str1 .Empty ();

// 附加和分离BSTR

//BSTR Detach() { BSTR s = m_str; m_str = NULL; return s; }

//void Attach(BSTR src){

// if(m_str != src){

// ::SysFreeString(m_str);

// m_str = src;

// }

//}

CString

从简,使用 CString ,本质是使用 CStringT 模板类。

typedef CStringT < wchar_t , StrTraitATL < wchar_t , ChTraitsCRT < wchar_t > > > CAtlStringW ;

typedef CStringT < char , StrTraitATL < char , ChTraitsCRT < char > > > CAtlStringA ;

typedef CStringT < TCHAR , StrTraitATL < TCHAR , ChTraitsCRT < TCHAR > > > CAtlString ;

……

typedef CAtlStringW CStringW ;

typedef CAtlStringA CStringA ;

typedef CAtlString CString ;

……

template < typename BaseType , class StringTraits >

class CStringT :

public CSimpleStringT ……

CStringT 继承 CSimpleStringT ,该基类提供了很多基本字符串功能; BaseType 模板参数用来确定字符类型,数据保存在基类私有成员 m_pszData 中; StringTraits 参数确定资源字符串加载的模块、字符串管理器以及提供低级的字符操作。 ATL 提供了默认的字符串管理器,该管理器是一个实现了 IAtlStringMgr 的具体类 CAtlStringMgr

CStringT 采用了类型定义来处理一些语法分支, XCHAR PXSTR PCXSTR 代表与模板参数一致的字符类型,而 YCHAR PYSTR PCYSTR 相反。比如如果 BaseType char ,则 XCHAR 代表 char YCHAR 代表 wchar_t ,相应的 PXSTR LPSTR PYSTR LPWSTR

基本使用小结如下:

//CStringT 基本使用小结

//1. 构造函数

//CStringT();

//explicit CStringT( IAtlStringMgr* pStringMgr );

//CStringT( const CStringT& strSrc );

//CStringT( const CThisSimpleString& strSrc );

//CStringT( const XCHAR* pszSrc );

//CStringT( const XCHAR* pch, int nLength );

//CStringT( const XCHAR* pch, int nLength, IAtlStringMgr* pStringMgr );

//CStringT( PXSTR pszSrc, IAtlStringMgr* pStringMgr );

// 以上例,与之相反的YCHARPYSTR 也可

CStringA caStr1 (L "hello,world" );

CStringW cwStr1 ("hello,world" );

CString cStr1 ;

CString cStr2 (cStr1 );

CString cStr3 ("hello,world" );

CString cStr4 ('a' , 5);

//2. 赋值

//CStringT& operator=( const CStringT& strSrc );

//CStringT& operator=( const CThisSimpleString& strSrc );

//CStringT& operator=( PCXSTR pszSrc );

//CStringT& operator=( XCHAR ch );

// 以上两例,YCHARPCYSTR 也可

caStr1 = L 'b' ;

cwStr1 = 'a' ;

cStr1 = "hello,world" ;

cStr1 = L "hello,world" ;

//3. 字符串连接

//CStringT& operator+=( const CThisSimpleString& str );

//CStringT& operator+=( const PCXSTR pszSrc );

//CStringT& operator+=( const XCHAR ch );

// 以上两例,PCYSTRYCHAR 也可

caStr1 += L 'a' ;

cwStr1 += L "new" ;

cStr1 += cStr2 ;

//+ 运算

cStr1 + cStr2 ;

cStr1 + L 'a' ;

cwStr1 + 'a' ;

//Append

//void Append( PCXSTR pszSrc );

//void Append( PCXSTR pszSrc, int nLength );

//void Append( const CSimpleStringT& strSrc );

//void AppendChar( XCHAR ch );

//4. 转换

cStr1 .MakeLower ();

cStr1 .MakeUpper ();

cStr1 .MakeReverse ();

//5. 比较

//== != < >

caStr1 == L "hello,world" ;

cwStr1 != "hello,world" ;

cStr1 < cStr2 ;

//6. 其它操作

//

//int Insert( int iIndex, PCXSTR psz )

//int Insert( int iIndex, XCHAR ch )

cStr1 .Insert (0, 'a' );

//

// iIndex 开始删除nCount 个字符

//int Delete( int iIndex, int nCount = 1 )

// 删除所有chRemove 字符

//int Remove( XCHAR chRemove )

//

//int Replace( XCHAR chOld, XCHAR chNew )

//int Replace( PCXSTR pszOld, PCXSTR pszNew )

//

//int Find( XCHAR ch, int iStart = 0 ) const

//int Find( PCXSTR pszSub, int iStart = 0 ) const

//int FindOneOf( PCXSTR pszCharSet ) const

//int ReverseFind( XCHAR ch ) const

//CStringT Left( int nCount ) const

//CStringT Mid( int iFirst ) const

//CStringT Mid( int iFirst, int nCount ) const

//CStringT Right( int nCount ) const

//XCHAR operator[]( int iChar ) const

str1 [3];

//Trim 方法

//CStringT& Trim()

//CStringT& Trim( XCHAR chTarget )

//CStringT& Trim( PCXSTR pszTargets )

//CStringT& TrimLeft()

//CStringT& TrimLeft( XCHAR chTarget )

//CStringT& TrimLeft( PCXSTR pszTargets )

//CStringT& TrimRight()

//CStringT& TrimRight( XCHAR chTarget )

//CStringT& TrimRight( PCXSTR pszTargets )

// 按指定字符分割

//CStringT Tokenize( PCXSTR pszTokens, int & iStart ) const

//CStringT 格式化

//void __cdecl Format( PCXSTR pszFormat, ... );

//void __cdecl Format( UINT nFormatID, .. );

参考资料:

Windows 核心编程》

《深入解析 ATL

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics