VC TCHAR转char ~ BSTR转换成char*

来源:互联网 发布:变声器男变女软件 编辑:程序博客网 时间:2024/06/05 09:14

TCHAR

TCHAR是通过define定义的字符串宏

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;