BSTR, _bstr_t and CString, char * conversion

Source: Internet
Author: User
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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.