您的位置:首页 > 其它

VARIANT类型如何与其他字符串类型转换?

2009-03-21 11:30 411 查看
//Create a BSTR and assign it to a Variant
BSTR x = SysAllocString(L"Hello");
VARIANT myVariant;
myVariant.vt = VT_BSTR;
myVariant.bstrVal = x;
SysFreeString(x);

//Create a CString and change it to a variant;
CString myCString(_T("My String"));
CString mySecondString;

//This is required to use the T2COLE macro.
USES_CONVERSION;

BSTR y = SysAllocString(T2COLE(myCString));
myVariant.bstrVal = y;
mySecondString = y;
SysFreeString(y);

//Create two BSTRs and add them.
BSTR a = SysAllocString(L"One two ");
BSTR b = SysAllocString(L"three four.");
_bstr_t my_bstr_t(a, TRUE);
my_bstr_t += b;
myVariant.bstrVal = my_bstr_t;
// or
myVariant.bstrVal = _bstr_t(a, FALSE) + b;

//Change a bstr to a CString.
CString ANewString(b);
//or if CString already exists.
myCString = b;

//Use of CComBSTR
CComBSTR myCComBSTR(L"Hello");
myCComBSTR.Append(L", how are you?");
VARIANT varFromCCom;
varFromCCom.vt = VT_BSTR;
varFromCCom.bstrVal = myCComBSTR
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: