ATL 提供的转换宏

A2BSTROLE2AT2AW2A
A2COLEOLE2BSTRT2BSTRW2BSTR
A2CTOLE2CAT2CAW2CA
A2CWOLE2CTT2COLEW2COLE
A2OLEOLE2CWT2CWW2CT
A2TOLE2TT2OLEW2OLE
A2WOLE2WT2WW2T



2好搞笑的缩写,to 的发音和 2 一样,所以借用来表示“转换为、转换到”的含义。
AANSI 字符串,也就是 MBCS。
W、OLE宽字符串,也就是 UNICODE。
T中间类型T。如果定义了 _UNICODE,则T表示W;如果定义了 _MBCS,则T表示A
Cconst 的缩写


使用范例:

      #include <atlconv.h>
      
      void fun()
      {
          USES_CONVERSION;  // 只需要调用一次,就可以在函数中进行多次转换
          
          LPCTSTR lp = OLE2CT( L"Hello,你好") );
          ... ... ... ...
          // 不用显式释放 lp 的内存,因为
          // 由于 ATL 转换宏使用栈作为临时空间,函数结束后会自动释放栈空间。
      }
  使用 ATL 转换宏,由于不用释放临时空间,所以使用起来非常方便。但是考虑到栈空间的尺寸(VC 默认2M),使用时要注意几点:
    1、只适合于进行短字符串的转换;
    2、不要试图在一个次数比较多的循环体内进行转换;
    3、不要试图对字符型文件内容进行转换,因为文件尺寸一般情况下是比较大的;

    4、对情况 2 和 3,要使用 MultiByteToWideChar() 和 WideCharToMultiByte();




_variant_t是一个用于COM的VARIANT类,它的功能与COleVariant相似。不过在VisualC++.NET的MFC应用程序中使用时需要在代码文件前面添加下列两句:

  #include "comutil.h"

  #pragma comment( lib, "comsupp.lib" )

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是TCHAR和std::string之间的转换方法: 1.将TCHAR转换为std::string: ```c++ #include <string> #include <tchar.h> TCHAR tcharStr[] = _T("TCHAR string"); std::string str = std::string(tcharStr, tcharStr + _tcslen(tcharStr)); ``` 2.将std::string转换为TCHAR: ```c++ #include <string> #include <tchar.h> std::string str = "std::string"; TCHAR tcharStr[100]; _tcscpy_s(tcharStr, CA2T(str.c_str())); ``` 其中,CA2T是ATL/MFC库中的一个,用于将const char*转换为TCHAR*。 以下是std::string和LPCWSTR之间的转换方法: 1.将std::string转换为LPCWSTR: ```c++ #include <string> #include <Windows.h> std::string str = "std::string"; LPCWSTR lpcwstr = CA2W(str.c_str()); ``` 其中,CA2W是ATL/MFC库中的一个,用于将const char*转换为LPCWSTR。 2.将LPCWSTR转换为std::string: ```c++ #include <string> #include <Windows.h> LPCWSTR lpcwstr = L"LPCWSTR string"; std::wstring wstr(lpcwstr); std::string str(wstr.begin(), wstr.end()); ``` 以下是完整的代码示例: ```c++ #include <iostream> #include <string> #include <tchar.h> #include <Windows.h> using namespace std; int main() { // TCHAR to std::string TCHAR tcharStr[] = _T("TCHAR string"); std::string str1 = std::string(tcharStr, tcharStr + _tcslen(tcharStr)); cout << "TCHAR to std::string: " << str1 << endl; // std::string to TCHAR std::string str2 = "std::string"; TCHAR tcharStr2[100]; _tcscpy_s(tcharStr2, CA2T(str2.c_str())); wcout << "std::string to TCHAR: " << tcharStr2 << endl; // std::string to LPCWSTR std::string str3 = "std::string"; LPCWSTR lpcwstr = CA2W(str3.c_str()); wcout << "std::string to LPCWSTR: " << lpcwstr << endl; // LPCWSTR to std::string LPCWSTR lpcwstr2 = L"LPCWSTR string"; std::wstring wstr(lpcwstr2); std::string str4(wstr.begin(), wstr.end()); cout << "LPCWSTR to std::string: " << str4 << endl; return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值