从char/wchar_t到TCHAR(2)

五.MFC动态字符串类CString

// /Microsoft Visual Studio 8/VC/atlmfc/include/afx.h

一个CString对象由可变长度的一队字符组成。CString使用类似于Basic的语法提供函数和操作符。连接和比较操作符以及简化的内存管理使CString对象比普通字符串数组容易使用。

CString是基于TCHAR数据类型的对象。如果在你的程序中定义了符号_UNICODE,则TCHAR被定义为类型wchar_t,即16位字符类型;否则,TCHAR被定义为char,即8位字符类型。在UNICODE方式下,CString对象由16位字符组成。非UNICODE方式下,CString对象由8位字符组成。 而VS2005默认TCHARwchar而不是char.

当不使用_UNICODE时,CString是多字节字符集(MBCS,也被认为是双字节字符集,DBCS)。注意,对于MBCS字符串,CString仍然基于8位字符来计算,返回,以及处理字符串,并且你的应用程序必须自己解释MBCS的开始和结束字节。

CString 提供 operator LPCTSTR 来在 CString  LPCTSTR 之间进行转换。

有关CString的操作请参考MSDN MFC类库。

六.更安全的C语言字符串处理函数 Strsafe.h

// …/Microsoft Visual Studio 8/VC/PlatformSDK/Include/strsafe.h

注意:使用StringCchCopy /StringCchPrintf时要#include  "strsafe.h".

STRSAFEAPI是为了解决现有的 C 语言运行时函数的代码太容易产生的内存溢出问题。当我们引用 strsafe 系列函数时,原有的 C 语言字符串处理函数都将被自动进行 #undef 处理。调试过程中的警告或出错信息将会告诉我们哪些函数哪些不安全,哪些已经被相应的 strsafe 系列函数取代了。 

//1.不赞成使用不安全的函数,以避免产生编译错误

//2.如果你不要安全处理,你可以在包含strsafe.h头文件之前,

#define STRSAFE_NO_DEPRECATE

#ifdef DEPRECATE_SUPPORTED

// First all the names that are a/w variants (or shouldn't be #defined by now anyway).

#pragma deprecated(strcpy)

#pragma deprecated(wcscpy)

#pragma deprecated(lstrcpy)

#pragma deprecated(StrCpy)

类似的Strcat/wcscat/lstrcat/StrCatsprintf/wsprintf

以下是D3D中预编译头文件dxstdafx.h

#pragma warning( disable : 4996 ) //将报警置为无效

#include <strsafe.h>

#pragma warning( default : 4996 ) //将报警置为默认

有关#pragma warning请参考:http://hi.baidu.com/iceland9/blog/item/5af9c0bfd334de0a18d81f33.html

以下是D3DVS2003移植到VS2005时遇到的安全警告:

warning C4996: 'wcscpy' was declared deprecated

see declaration of 'wcscpy'

Message: 'This function or variable may be unsafe.

Consider using wcscpy_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'

warning C4995: 'lstrcpy': name was marked as #pragma deprecated

warning C4995: 'wsprintf': name was marked as #pragma deprecated

推荐使用新的安全可靠的TRSAFEAPI

STRSAFEAPI

StringCchCopyA(

    __out_ecount(cchDest) STRSAFE_LPSTR pszDest,

    __in size_t cchDest,

    __in STRSAFE_LPCSTR pszSrc);

STRSAFEAPI

StringCchCopyW(

    __out_ecount(cchDest) STRSAFE_LPWSTR pszDest,

    __in size_t cchDest,

    __in STRSAFE_LPCWSTR pszSrc);

#ifdef UNICODE

#define StringCchCopy  StringCchCopyW (WWide Unicode)

#else

#define StringCchCopy  StringCchCopyA (AANSI)

#endif // !UNICODE

#undef strcpy

#define strcpy      strcpy_instead_use_StringCbCopyA_or_StringCchCopyA;

#undef wcscpy

#define wcscpy      wcscpy_instead_use_StringCbCopyW_or_StringCchCopyW;

#undef wsprintf

#define wsprintf    wsprintf_instead_use_StringCbPrintf_or_StringCchPrintf;

// Then all the windows.h names - we need to undef and redef based on UNICODE setting

#undef lstrcpy //取消已定义的宏

#pragma deprecated(lstrcpy) //安全警告

#ifdef UNICODE //使用UNICODE编程

#define lstrcpy    lstrcpyW //重定义

#else

#define lstrcpy    lstrcpyA //重定义

#endif

类似的有对lstrcat/wsprintf/wvsprintf#undef#pragma deprecated#define

推荐使用新的安全可靠的TRSAFEAPI

#undef lstrcpy

#define lstrcpy     lstrcpy_instead_use_StringCbCopy_or_StringCchCopy;

// Then the shlwapi names - they key off UNICODE also.

#undef  StrCpy

#pragma deprecated(StrCpy)

#ifdef UNICODE

#define StrCpy  StrCpyW

#else

#define StrCpy  lstrcpyA

#endif

类似的有#undef StrCpyA /StrCpy /StrCatA /StrCat /StrNCat /StrCatN

以及对StrCpy/StrCat/StrNCat#undef#pragma deprecated#define

推荐使用新的安全可靠的TRSAFEAPI

#undef StrCpy

#define StrCpy      StrCpy_instead_use_StringCbCopy_or_StringCchCopy;

// Then all the CRT names - we need to undef/redef based on _UNICODE value.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值