Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4137636
  • 博文数量: 447
  • 博客积分: 1241
  • 博客等级: 中尉
  • 技术积分: 5786
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-27 06:48
个人简介

读好书,交益友

文章分类

全部博文(447)

文章存档

2023年(6)

2022年(29)

2021年(49)

2020年(16)

2019年(15)

2018年(23)

2017年(67)

2016年(42)

2015年(51)

2014年(57)

2013年(52)

2012年(35)

2011年(5)

分类: WINDOWS

2012-04-19 16:36:11

07年在启明做个专题的讲座,现在提炼升华一下。
vc工程中考虑到UNICODE的设置问题,尽可能使用三种字符串,ATL的CString,不是mfc的CString,使用com 就使用CComBSTR,使用api的话,尽可能用TCHAR,字符串操作尽可能用_tcs函数。
除非考虑跨平台,否则不要使用stl的string,应该使用CString,虽然可以使用如下设置
#include
using namespace std;
#ifndef UNICODE
typedef string TSTRING;
#else
typedef wstring TSTRING;
#endif
使用stl的string 以后的字符串操作会很复杂,
例如得到程序当前的路径
TCHAR buffer[MAX_PATH+1];

DWORD iNombreChars = GetCurrentDirectory(MAX_PATH, buffer);

string strPath;

strPath.assign(&buffer[0], &buffer[iNombreChars]);
这是闲的蛋疼
也有简单的,
std::string str(MAX_PATH+1, 0);
GetCurrentDirectory(MAX_PATH, &str[0]);

将string转为TCHAR
TCHAR *param=new TCHAR[tsDir.size()+1];
param[tsDir.size()]=_T('\0');
//As much as we'd love to, we can't use memcpy() because
//sizeof(TCHAR)==sizeof(char) may not be true:
std::copy(tsDir.begin(),tsDir.end(),param);
如果经常转换,最后会抓狂的。

阅读(3065) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~