CString and other type conversions

Source: Internet
Author: User
Tags ole

1, char* converted into CString

If the char* is converted into CString, the Cstring::format can be used in addition to the direct assignment. For example:

Char charray[] = "This is a test";

char * p = "This is a test";

Or

LPSTR p = "This is a test";

Or in a program that has a Unicode application defined

TCHAR * p = _t ("This is a test";

Or

LPTSTR p = _t ("This is a test";

CString thestring = Charray;

Thestring.format (_t ("%s", Charray);

TheString = p;

2, CString converted into char*

If you convert a CString class to a char* (LPSTR) type, you often use the following three methods:

Method one, using the cast. For example:

CString TheString ("This is a test";

LPTSTR lpsz = (LPTSTR) (LPCTSTR) thestring;

Method two, using strcpy. For example:

CString TheString ("This is a test";

LPTSTR lpsz = new Tchar[thestring.getlength () +1];

_tcscpy (Lpsz, thestring);

It is necessary to note that the second parameter of the strcpy (or Unicode/mbcs _tcscpy) is either const wchar_t* (Unicode) or const char* (ANSI), which will be converted automatically by the system compiler.

Method three, using Cstring::getbuffer. For example:

CString s (_t ("This is a Test");

LPTSTR p = s.getbuffer ();

Add the code to use p here

if (P!= NULL) *p = _t (' ");

S.releasebuffer ();

Released in time after use so that other CString member functions can be used

3, BSTR converted into char*

Method one, using Convertbstrtostring. For example:

#include

#pragma comment (lib, "Comsupp.lib")

int _tmain (int argc, _tchar* argv[])

{

BSTR Bstrtext =:: SysAllocString (L "Test";

char* lpszText2 = _com_util::convertbstrtostring (Bstrtext);

SysFreeString (Bstrtext); Use out of release

Delete[] LPSZTEXT2;

return 0;

}

Method two, using the _bstr_t assignment operator overload. For example:

_bstr_t B = bstrtext;

char* lpszText2 = b;

4, char* converted into BSTR

Method one, using API functions such as SysAllocString. For example:

BSTR Bstrtext =:: SysAllocString (L "Test";

BSTR Bstrtext =:: SysAllocStringLen (L "Test", 4);

BSTR Bstrtext =:: SysAllocStringByteLen ("Test", 4);

Method two, using COleVariant or _variant_t. For example:

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 simplest methods. For example:

BSTR Bstrtext = _bstr_t ("This is a test";

Method four, using CComBSTR. For example:

BSTR Bstrtext = CComBSTR ("This is a test";

Or

CComBSTR BSTR ("This is a test";

BSTR bstrtext = bstr.m_str;

Method five, using CONVERTSTRINGTOBSTR. For example:

char* lpsztext = "Test";

BSTR Bstrtext = _com_util::convertstringtobstr (lpsztext);

5, CString converted into BSTR

This is usually done by using cstringt::allocsysstring. For example:

CString Str ("This is a test";

BSTR bstrtext = str. AllocSysString ();

...

SysFreeString (Bstrtext); Use out of release

6, BSTR converted into CString

Generally, the following methods are available:

BSTR Bstrtext =:: SysAllocString (L "Test";

CStringA str;

Str. Empty ();

str = Bstrtext;

Or

CStringA str (bstrtext);

7. Conversion between ANSI, Unicode, and wide characters

Method One, use MultiByteToWideChar to convert ANSI characters to Unicode characters, and use WideCharToMultiByte to convert Unicode characters to ANSI characters.

Method Two, use "_t" to convert ANSI to a "generic" type string, convert ANSI to Unicode using "L", and use S to convert an ANSI string to a string* object in a managed C + + environment. For example:

TCHAR tstr[] = _t ("This is a test";

wchar_t wszstr[] = L "This is a test";

string* str = S "This is a test";

Method Three, using the conversion macros and classes of ATL 7.0. ATL7.0 has refined and added a number of string conversion macros based on the original 3.0 and provided the corresponding classes, which have the uniform form shown in Figure 3:

Where the first C represents "class" for the purpose of distinguishing between ATL 3.0 macros, the second C represents a constant, and 2 is "to", and the ex indicates a buffer of a certain size. SourceType and destinationtype can be a, T, W, and OLE, meaning ANSI, Unicode, general type, and OLE strings, respectively. For example, CA2CT is a string constant that converts ANSI to a generic type. Here are some sample code:

LPTSTR tstr= ca2tex<16> ("This is a test";

LPCTSTR tcstr= ca2ct ("This is a test";

wchar_t wszstr[] = L "This is a test";

char* chstr = cw2a (WSZSTR);

8. CString Convert to char *

Cstring strFileName;

int length = Strfilename.getlength ();

Char *cfilename;

Cfilename= (char *) strfilename.getbuffer (length);

9. Conversion of CString to LPSTR

Cstring str;

LPSTR LPSTR = (LPSTR) str;

CString converted to LPCSTR

int length = Strfilename.getlength ();

size_t aLen = length + 1;

LPCSTR lpstrfilename = (LPCSTR) strfilename.getbuffer (length);

int length = Strfilename.getlength ();

size_t aLen = length + 1;

CString converted to LPOLESTR/LPCOLESTR.

int length = Strfilename.getlength ();

size_t aLen = length + 1;

LPCSTR lpstrfilename = (LPCSTR) strfilename.getbuffer (length);

int wlen = MultiByteToWideChar (CP_ACP, 0,lpstrfilename,alen, null,0);

Lpolestr lpfilename = new WCHAR [Wlen];

MultiByteToWideChar (CP_ACP, 0,lpstrfilename,alen,lpfilename,wlen);

LPOLESTR can be passed into functions as LPCOLESTR, such as HRESULT stdmethodcalltype setoutputfilename (const GUID *ptype, Lpcolestr lpstrfile, IBase Filter **PPF, Ifilesinkfilter **ppsink) can pass lpFileName (LPOLESTR) into functions

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.