c++ ×Ö·ûÀàÐÍ×ܽá

1.Çø±ðwchar_t,char,WCHAR

?? ANSI£º¼´ char£¬¿ÉÓÃ×Ö·û´®´¦Àíº¯Êý£ºstrcat( ),strcpy( ), strlen( )µÈÒÔstr´òÍ·µÄº¯Êý¡£
?? UNICODE£ºwchar_tÊÇUnicode×Ö·ûµÄÊý¾ÝÀàÐÍ£¬Ëüʵ¼Ê¶¨ÒåÔÚÀ
??typedef unsigned short wchar_t;
??ÁíÍ⣬ÔÚÍ·ÎļþÖÐÓÐÕâÑùµÄ¶¨Ò壺typedef wchar_t WCHAR; ËùÒÔWCHARʵ¼Ê¾ÍÊÇwchar_t
??wchar_t ¿ÉÓÃ×Ö·û´®´¦Àíº¯Êý£ºwcscat(),wcscpy(),wcslen()µÈÒÔwcs´òÍ·µÄº¯Êý¡£ÎªÁËÈñàÒëÆ÷ʶ±ðUnicode×Ö·û´®£¬±ØÐëÒÔÔÚÇ°Ãæ¼ÓÒ»¸ö¡°L¡±,ÀýÈç: wchar_t *szTest=L"This is a Unicode string.";

2.TCHAR

ÔÚCÓïÑÔÀïÃæÌṩÁË _UNICODEºê£¨ÓÐÏ»®Ïߣ©£¬ÔÚWindowsÀïÃæÌṩÁËUNICODEºê£¨ÎÞÏ»®Ïߣ©£¬Ö»Òª¶¨ÁË_UNICODEºêºÍUNICODEºê£¬ÏµÍ³¾Í»á×Ô ¶¯Çл»µ½UNICODE°æ±¾£¬·ñÔò£¬ÏµÍ³°´ÕÕANSIµÄ·½Ê½½øÐбàÒëºÍÔËÐС£Ö»¶¨ÒåÁ˺겢²»ÄÜʵÏÖ×Ô¶¯µÄת»»£¬Ëû»¹ÐèҪһϵÁеÄ×Ö·û¶¨ÒåÖ§³Ö¡£
? ?1£® TCHAR
? ?Èç¹û¶¨ÒåÁËUNICODEºêÔòTCHAR±»¶¨ÒåΪwchar_t¡£
? ?typedef wchar_t TCHAR;
??·ñÔòTCHAR±»¶¨ÒåΪchar typedef char TCHAR;
? ?2£® LPTSTR
??Èç¹û¶¨ÒåÁËUNICODEºêÔòLPTSTR±»¶¨ÒåΪLPWSTR¡£
??typedef LPTSTR LPWSTR;
??·ñÔòTCHAR±»¶¨ÒåΪchar typedef LPTSTR LPSTR;
??˵Ã÷£ºÔÚʹÓÃ×Ö·û´®³£Á¿µÄʱºòÐèҪʹÓÃ_TEXT(¡°MyStr¡±)»òÕß_T("")À´Ö§³ÖϵͳµÄ×Ô¶¯×ª»»¡£

3.BSTR

??BSTRÊÇÒ»¸ö´ø³¤¶Èǰ׺µÄ×Ö·û´®,Ö÷ÒªÓɲÙ×÷ϵͳÀ´¹ÜÀíµÄ,ËùÒÔÒªÓÃapi.Ö÷ÒªÓÃÀ´ºÍVB´ò½»µÀµÄ(VBÀïµÄstring¾ÍÊÇÖ¸Ëü)Òª²Ù×÷ËüµÄAPIº¯ÊýÓкܶà.±ÈÈçSysAllocString,SysFreeStringµÈµÈ.
??vcÀï·â×°ËüµÄÀàÈç_bstr_t,¼°ATLÖеÄCComBSTRµÈ.
??Ò»¸ö BSTR ÓÉÍ·²¿ºÍ×Ö·û´®×é³É£¬Í·²¿°üº¬ÁË×Ö·û´®µÄ³¤¶ÈÐÅÏ¢£¬×Ö·û´®ÖпÉÒÔ°üº¬Ç¶ÈëµÄ null Öµ¡£
??BSTR ÊÇÒÔÖ¸ÕëµÄÐÎʽ½øÐд«µÝµÄ¡£(Ö¸ÕëÊÇÒ»¸ö±äÁ¿£¬°üº¬ÁíÍâÒ»¸ö±äÁ¿µÄÄÚ´æµØÖ·£¬¶ø²»ÊÇÊý¾Ý¡£) BSTR ÊÇ Unicode µÄ£¬¼´Ã¿¸ö×Ö·ûÐèÒªÁ½¸ö×Ö½Ú¡£ BSTR ͨ³£ÒÔÁ½×Ö½ÚµÄ null ×Ö·û½áÊø¡£ wstrÊÇ¿í×Ö·û£¬ÒÔË«×Ö½Ú±íʾһ¸ö×Ö·û bstrÊÇΪÁËÓëÔ­ÏȵÄbasic×Ö·û¼æÈÝ£¬ËüµÄ×îÇ°ÃæµÄ4¸ö×Ö½ÚΪÆ䳤¶È£¬ÒÔ'\0'½áÊø.

4.¸ü½øÒ»²½µÄ×Ö·û´®ÒÔ¼°ÆäÖ¸ÕëµÄÀàÐͶ¨Òå¡¡

ÓÉÓÚWin32 APIÎĵµµÄº¯ÊýÁбíʹÓú¯ÊýµÄ³£ÓÃÃû×Ö£¨ÀýÈ磬 "SetWindowText"£©£¬ËùÓеÄ×Ö·û´®¶¼ÊÇÓÃTCHARÀ´¶¨ÒåµÄ¡££¨³ýÁËXPÖÐÒýÈëµÄÖ»ÊÊÓÃÓÚUnicodeµÄAPI£©¡£ÏÂÃæÁгöһЩ³£ÓõÄtypedefs£¬Äã¿ÉÒÔÔÚmsdnÖп´µ½ËûÃÇ¡£

type Meaning in MBCS builds Meaning in Unicode builds
WCHAR wchar_t wchar_t
LPSTR char* char*
LPCSTR const char* const char*
LPWSTR wchar_t* wchar_t*
LPCWSTR wchar_t* wchar_t*
TCHAR TCHAR char wchar_t
LPTSTR TCHAR* TCHAR*
LPCTSTR const TCHAR* const TCHAR*


5.Ï໥ת»»

(1) char*ת»»³ÉCString
¡¡¡¡Èô½«char*ת»»³ÉCString£¬³ýÁËÖ±½Ó¸³ÖµÍ⣬»¹¿ÉʹÓÃCString::Format½øÐС£ÀýÈ磺
char chArray[] = "This is a test";
char * p = "This is a test";
¡¡¡¡»ò
LPSTR p = "This is a test";
¡¡¡¡»òÔÚÒѶ¨ÒåUnicodeÓ¦µÄÓóÌÐòÖÐ
TCHAR * p = _T("This is a test");
¡¡¡¡»ò
LPTSTR p = _T("This is a test");
CString theString = chArray;
theString.Format(_T("%s"), chArray);
theString = p;
¡¡¡¡(2) CStringת»»³Échar*
¡¡¡¡Èô½«CStringÀàת»»³Échar*(LPSTR)ÀàÐÍ£¬³£³£Ê¹ÓÃÏÂÁÐÈýÖÖ·½·¨£º
¡¡¡¡·½·¨Ò»£¬Ê¹ÓÃÇ¿ÖÆת»»¡£ÀýÈ磺
CString theString( "This is a test" );
LPTSTR lpsz =(LPTSTR)(LPCTSTR)theString;
¡¡¡¡·½·¨¶þ£¬Ê¹ÓÃstrcpy¡£ÀýÈ磺
CString theString( "This is a test" );
LPTSTR lpsz = new TCHAR[theString.GetLength()+1];
_tcscpy(lpsz, theString);
¡¡¡¡ÐèҪ˵Ã÷µÄÊÇ£¬strcpy(»ò¿ÉÒÆÖµUnicode/MBCSµÄ_tcscpy)µÄµÚ¶þ¸ö²ÎÊýÊÇ const wchar_t* (Unicode)»òconst char* (ANSI)£¬ÏµÍ³±àÒëÆ÷½«»á×Ô¶¯¶ÔÆä½øÐÐת»»¡£
¡¡¡¡·½·¨Èý£¬Ê¹ÓÃCString::GetBuffer¡£ÀýÈ磺
CString s(_T("This is a test "));
LPTSTR p = s.GetBuffer();
// ÔÚÕâÀïÌí¼ÓʹÓÃpµÄ´úÂë
if(p != NULL) *p = _T('\0');
s.ReleaseBuffer();
// ʹÓÃÍêºó¼°Ê±ÊÍ·Å£¬ÒÔ±ãÄÜʹÓÃÆäËüµÄCString³ÉÔ±º¯Êý
¡¡¡¡(3) BSTRת»»³Échar*
¡¡¡¡·½·¨Ò»£¬Ê¹ÓÃConvertBSTRToString¡£ÀýÈ磺
#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); // ÓÃÍêÊÍ·Å
delete[] lpszText2;
return 0;
}
¡¡¡¡·½·¨¶þ£¬Ê¹ÓÃ_bstr_tµÄ¸³ÖµÔËËã·ûÖØÔØ¡£ÀýÈ磺
_bstr_t b = bstrText;
char* lpszText2 = b;
¡¡¡¡(4) char*ת»»³ÉBSTR
¡¡¡¡·½·¨Ò»£¬Ê¹ÓÃSysAllocStringµÈAPIº¯Êý¡£ÀýÈ磺
BSTR bstrText = ::SysAllocString(L"Test");
BSTR bstrText = ::SysAllocStringLen(L"Test",4);
BSTR bstrText = ::SysAllocStringByteLen("Test",4);
¡¡¡¡·½·¨¶þ£¬Ê¹ÓÃCOleVariant»ò_variant_t¡£ÀýÈ磺
//COleVariant strVar("This is a test");
_variant_t strVar("This is a test");
BSTR bstrText = strVar.bstrVal;
¡¡¡¡·½·¨Èý£¬Ê¹ÓÃ_bstr_t£¬ÕâÊÇÒ»ÖÖ×î¼òµ¥µÄ·½·¨¡£ÀýÈ磺
BSTR bstrText = _bstr_t("This is a test");
¡¡¡¡·½·¨ËÄ£¬Ê¹ÓÃCComBSTR¡£ÀýÈ磺
BSTR bstrText = CComBSTR("This is a test");
¡¡¡¡»ò
CComBSTR bstr("This is a test");
BSTR bstrText = bstr.m_str;
¡¡¡¡·½·¨Î壬ʹÓÃConvertStringToBSTR¡£ÀýÈ磺
char* lpszText = "Test";
BSTR bstrText = _com_util::ConvertStringToBSTR(lpszText);
¡¡¡¡(5) CStringת»»³ÉBSTR
¡¡¡¡Í¨³£ÊÇͨ¹ýʹÓÃCStringT::AllocSysStringÀ´ÊµÏÖ¡£ÀýÈ磺
CString str("This is a test");
BSTR bstrText = str.AllocSysString();
¡­
SysFreeString(bstrText); // ÓÃÍêÊÍ·Å
¡¡¡¡(6) BSTRת»»³ÉCString
¡¡¡¡Ò»°ã¿É°´ÏÂÁз½·¨½øÐУº
BSTR bstrText = ::SysAllocString(L"Test");
CStringA str;
str.Empty();
str = bstrText;
¡¡¡¡»ò
CStringA str(bstrText);
¡¡¡¡(7) ANSI¡¢UnicodeºÍ¿í×Ö·ûÖ®¼äµÄת»»
¡¡¡¡·½·¨Ò»£¬Ê¹ÓÃMultiByteToWideChar½«ANSI×Ö·ûת»»³ÉUnicode×Ö·û£¬Ê¹ÓÃWideCharToMultiByte½«Unicode×Ö·ûת»»³ÉANSI×Ö·û¡£
¡¡¡¡·½·¨¶þ£¬Ê¹Óá°_T¡±½«ANSIת»»³É¡°Ò»°ã¡±ÀàÐÍ×Ö·û´®£¬Ê¹Óá°L¡±½«ANSIת»»³ÉUnicode£¬¶øÔÚÍйÜC++»·¾³Öл¹¿ÉʹÓÃS½«ANSI×Ö·û´®×ª»»³ÉString*¶ÔÏó¡£ÀýÈ磺
TCHAR tstr[] = _T("this is a test");
wchar_t wszStr[] = L"This is a test";
String* str = S¡±This is a test¡±;
¡¡¡¡·½·¨Èý£¬Ê¹ÓÃATL 7.0µÄת»»ºêºÍÀà¡£ATL7.0ÔÚÔ­ÓÐ3.0»ù´¡ÉÏÍêÉƺÍÔö¼ÓÁËÐí¶à×Ö·û´®×ª»»ºêÒÔ¼°ÌṩÏàÓ¦µÄÀ࣬Ëü¾ßÓÐÈçͼ3ËùʾµÄͳһÐÎʽ£º
¡¡¡¡ÆäÖУ¬µÚÒ»¸öC±íʾ¡°Àࡱ£¬ÒÔ±ãÓÚATL 3.0ºêÏàÇø±ð£¬µÚ¶þ¸öC±íʾ³£Á¿£¬2±íʾ¡°to¡±£¬EX±íʾҪ¿ª±ÙÒ»¶¨´óСµÄ»º³å¡£SourceTypeºÍDestinationType¿ÉÒÔÊÇA¡¢ T¡¢WºÍOLE£¬Æ京Òå·Ö±ðÊÇANSI¡¢Unicode¡¢¡°Ò»°ã¡±ÀàÐͺÍOLE×Ö·û´®¡£ÀýÈ磬CA2CT¾ÍÊǽ«ANSIת»»³ÉÒ»°ãÀàÐ͵Ä×Ö·û´®³£Á¿¡£ÏÂÃæ ÊÇһЩʾÀý´úÂ룺
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);

你可能感兴趣的:(C++,c,C#,XP,vb)