TCHAR
C++支持两种字符串,即常规的ANSI编码(使用""包裹)和Unicode编码(使用L""包裹)
#include <stdio.h>
int main(void)
{
_TCHAR* tchar_str="weihuanzhen";
char char_str[256];
sprintf(char_str, "%S", tchar_str);
return 0;
}
BSTR
它被描述成一个与自动化相兼容的类型,由于操作系统提供相应的API函数(如SysAllocString)来管理它以及一些默认的调度代码。因此BSTR实际上就是一个COM字符串,但它却在自动化技术以外的多种场合下得到广泛使用
方法1,使用COleVariant或_variant_t。例如://COleVariant strVar("This is a test");
_variant_t strVar("This is a test");
BSTR bstrText = strVar.bstrVal;
方法2,使用_bstr_t,这是一种最简单的方法。例如:
BSTR bstrText = _bstr_t("This is a test");
方法3,使用CComBSTR。例如:
BSTR bstrText = CComBSTR("This is a test");
或
CComBSTR bstr("This is a test");
BSTR bstrText = bstr.m_str;
本文介绍了TCHAR宏在C++中用于支持ANSI和Unicode编码字符串的方式,并详细阐述了BSTR作为COM字符串的三种创建方法及其应用场景。
4568





