//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
VARIANT类型如何与其他字符串类型转换?
最新推荐文章于 2022-04-12 10:23:35 发布