Using mfc mbcs/Unicode conversion macros

Source: Internet
Author: User

Tn059: Using mfc mbcs/Unicode conversion macros

The following technical note has not been updated since it was first defined ded in the online documentation. as a result, some procedures and topics might be out of date or incorrect. for the latest information, it is recommended that you search for the topic of interest in the online documentation index.

This note describes how to use the macros for MBCS/Unicode conversion, which are defined in afxpriv. h. these macros are most useful if your application deals directly with the ole api or for some reason, often needs to convert between Unicode and MBCS.

Overview

Generic conversion macros

The generic conversion macros form the underlying mechanic. the macro example and implementation shown in the previous section, a2w, is one such "generic" macro. it is not related to Ole specifically. the set of generic macros is listed below:

A2CW      (LPCSTR) -> (LPCWSTR)A2W      (LPCSTR) -> (LPWSTR)W2CA      (LPCWSTR) -> (LPCSTR)W2A      (LPCWSTR) -> (LPSTR)

Besides doing text conversions, there are also macros and helper functions for convertingTextmetric,Devmode,BSTR, And Ole allocated strings. These macros are beyond the scope of this discussion-refer to afxpriv. h for more information on those macros.

Ole conversion macros

The OLE conversion macros are designed specifically for handling functions that has CTOlestrCharacters. If you examine the OLE headers, you will see your referencesLpcolestrAndOlechar. These types are used to refer to the type of characters used in OLE interfaces in a way that is not specific to the platform.OlecharMaps to char in Win16 and Macintosh platforms andWcharIn win32.

In order to keep the number# IfdefDirectives in the MFC code to a minimum we have a similar macro for each conversion that where Ole strings are involved. The following macros are the most commonly used:

T2COLE   (LPCTSTR) -> (LPCOLESTR)T2OLE   (LPCTSTR) -> (LPOLESTR)OLE2CT   (LPCOLESTR) -> (LPCTSTR)OLE2T   (LPCOLESTR) -> (LPCSTR)

Again, there are similar macros for doingTextmetric,Devmode,BSTR, And Ole allocated strings. Refer to afxpriv. h for more information.

Other considerations

Do not use the Macros in a tight loop. For example, you do not want to write the following kind of code:

void BadIterateCode(LPCTSTR lpsz){   USES_CONVERSION;   for (int ii = 0; ii SomeMethod(ii, T2COLE(lpsz));}

The code above cocould result in allocating megabytes of memory on the stack depending on what the contents of the stringlpszis! It also takes time to convert the string for each iteration of the loop. Instead, move such constant conversions out of the loop:

void MuchBetterIterateCode(LPCTSTR lpsz){   USES_CONVERSION;   LPCOLESTR lpszT = T2COLE(lpsz);   for (int ii = 0; ii SomeMethod(ii, lpszT);}

If the string is not constant, then encapsulate the method call into a function. This will allow the conversion buffer to be freed each time. For example:

void CallSomeMethod(int ii, LPCTSTR lpsz){   USES_CONVERSION;   pI->SomeMethod(ii, T2COLE(lpsz));}void MuchBetterIterateCode2(LPCTSTR* lpszArray){   for (int ii = 0; ii 

Never return the result of one of the macros, unless the return value implies making a copy of the data before the return. For example, this code is bad:

LPTSTR BadConvert(ISomeInterface* pI){   USES_CONVERSION;   LPOLESTR lpsz = NULL;   pI->GetFileName(&lpsz);   LPTSTR lpszT = OLE2T(lpsz);   CoMemFree(lpsz);   return lpszT; // bad!  returning alloca memory}  

The code above could be fixed by changing the return value to something that copies the value:

CString BetterConvert(ISomeInterface* pI){   USES_CONVERSION;   LPOLESTR lpsz = NULL;   pI->GetFileName(&lpsz);   LPTSTR lpszT = OLE2T(lpsz);   CoMemFree(lpsz);   return lpszT; // CString makes copy}

The macros are easy to use and easy to insert into your code, but as you can tell from the caveats above, you need to be careful when using them.

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.