about BSTR, _bstr_t details"_bstr_t and _variant_t"
.
CString turn BSTR
BSTR BSTR;
CString strSQL; BSTR = Strsql.allocsysstring ();
... SysFreeString (Bstrtext); Use out of release
Note: After use, you must use SysFreeString to release ...
BSTR turn CString
BSTR BSTR =:: SysAllocString (L "Test");
CString strSQL; Strsql.empty (); strSQL = (LPCSTR) BSTR;
Or
BSTR BSTR =:: SysAllocString (L "Test");
CString Str (BSTR); CString turn _bstr_t
_bstr_t BSTR;
CString strSQL; BSTR = (_bstr_t) strSQL;
_bstr_t Turn CString
_bstr_t BSTR;
CString strSQL; strSQL = (LPCSTR) BSTR;
BSTR converted into char*
method One, using Convertbstrtostring
BSTR Bstrtext =:: SysAllocString (L "Test"); char * lpszText2 = _com_util:: convertbstrtostring (Bstrtext); SysFreeString (Bstrtext); Use out of release
delete [] lpszText2;
method Two, using the _bstr_t assignment operator overload
_bstr_t B = bstrtext; char * lpszText2 = b;
char* converted into BSTR
method One, using API functions such as SysAllocString
BSTR Bstrtext =:: SysAllocString (L "Test"); BSTR Bstrtext =:: SysAllocStringLen (L "Test", 4); BSTR Bstrtext =:: SysAllocStringByteLen ("Test", 4);
method Two, using COleVariant or _variant_t
COleVariant Strvar ("This is a Test"); _variant_t Strvar ("This is a Test"); BSTR bstrtext = Strvar.bstrval;
method Three, using _bstr_t, this is one of the easiest ways
BSTR Bstrtext = _bstr_t ("This is a Test"); Method four, using CComBSTR
BSTR Bstrtext = CComBSTR ("This is a Test"); or
CComBSTR BSTR ("This is a Test"); BSTR Bstrtext = BSTR. M_STR;
Method Five, using Convertstringtobstr
char * lpsztext = "Test"; BSTR Bstrtext = _com_util:: Convertstringtobstr (Lpsztext);