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
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
相关文章推荐
- 如何从 CString 转换为其他字符串类型
- 如何从 wchar_t * 转换为其他字符串类型
- 如何:在各种字符串类型之间进行转换--如何从 char * 转换为上面列出的其他字符串类型
- 如何从 basic_string 转换为其他字符串类型
- socket发送十六进制字符串 (十六进制字符串与其他类型的转换)
- 关于如何将含有Date类型属性的对象转换成符合实际应用的Json字符串
- Visual C++ 如何:在各种字符串类型之间进行转换
- js中如何将字符串转换成json类型
- C#中如何把byte[]数组转换成其他类型
- C++ 如何:在各种字符串类型之间进行转换
- 如何:在各种字符串类型之间进行转换
- C 和 JAVA 中字符串和int等其他类型互相转换
- 如何:在各种字符串类型之间进行转换
- _variant_t的类型解说以及如何转换成其它类型
- 将 _variant_t 类型的变量,转换成字符串
- Visual C++ 如何:在各种字符串类型之间进行转换
- Visual C++ 如何:在各种字符串类型之间进行转换
- 如何正确转换QSTring类型字符串和UNICODE
- C/C++关于字符串string、char*字符串数组与其他类型转换
- python------unicode字符串转换为其他类型