Understanding Strings In COM[7]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 edyang 的 blog

w

lpwstr, wchar_t*

bstr

bstr

c

const - associated to another type

the macros operate intelligently: if for some reason the source and destination types coincide, the code does not waste time in a useless process. internally most of the macros call the _alloca() run-time library function and allocate the storage for the new data on the stack, as this simplifies the deallocation policy by delegating it to the rules of the variables scope. for this reason, a uses_conversion macro must be put just before the conversion operation in each function or class method that contains the macros. the following sample code, taken from the downloadable sample pack available on the web, will clarify the process:

// conversions through mfc/atl's macros
void sample2()
{
                    uses_conversion;
 
                    // ansi
                    lpstr ansistr = "this is a sample message";
                    printf("before the string contains: %s\n", ansistr);
 
                    // ansi -> const tchar
                    const tchar* ptchar = a2ct(ansistr);
                    _tprintf(_t("midway the string contains: %s\n"), ptchar);
 
                    // const tchar -> unicode
                    lpwstr wstr = t2w(ptchar);
                    wprintf(l"after the string contains: %s\n", wstr);
}

as i stated earlier, the conversion macros are part of mfc and atl, but surprisingly the header files are not directly shared by the two frameworks. atl programmers should include atlconv.h, while mfc developers are supposed to include afxconv.h in their projects. after digging into the sources i found that in the latest versions of mfc, afxconv.h does little more than include atlconv.h itself, so in practice the string conversion code exposed by the two frameworks is the same.

the com compiler support offers good bstr conversion code, too. the actual conversion functions are the cast operators that convert a _bstr_t to either an ansi or a unicode string, either constant or not, plus the omnipresent class constructors. the following code snippet, taken from the downloadable sample pack available on the web, shows some common usage patterns of bstr conversions:

// bstr conversions
void sample3()
{
                    uses_conversion;
 
                    lpwstr wstr = l"this is a sample message";
                    wprintf(l"before the string contains: %s\n", wstr);
 
                    bstr bstr1 = w2bstr(wstr);
 
                    cstring mfcstr = bstr1;
                    printf("midway the string contains: %s\n", (lpcstr)mfcstr);
 
                    bstr bstr2 = mfcstr.allocsysstring();
                    _bstr_t bstr3 = bstr2;
                    verify(bstr3 == (_bstr_t)bstr1);
                    
                    wchar* wstr2 = bstr3;
                    wprintf(l"after the string contains: %s\n", wstr2);
 
                    ::sysfreestring(bstr1);
                    ::sysfreestring(bstr2);
}

本文关键:Understanding Strings In COM
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top