📄 csdn_文档中心_understanding strings in com.htm
字号:
<P>The following table briefly describes the task of each routine:
</P>
<TABLE bgColor=#dfdfdf border=1 cellPadding=0 cellSpacing=3
style="BACKGROUND: #dfdfdf; WIDTH: 100%; mso-cellspacing: 1.5pt; mso-padding-alt: 3.75pt 3.75pt 3.75pt 3.75pt"
width="100%">
<TBODY>
<TR>
<TD
style="PADDING-BOTTOM: 3.75pt; PADDING-LEFT: 3.75pt; PADDING-RIGHT: 3.75pt; PADDING-TOP: 3.75pt">
<P class=MsoNormal><B><SPAN
style="FONT-FAMILY: Arial; FONT-SIZE: 9pt">Function
name</SPAN></B><SPAN
style="COLOR: black; FONT-FAMILY: Arial; FONT-SIZE: 9pt"><o:p></o:p></SPAN></P></TD>
<TD
style="PADDING-BOTTOM: 3.75pt; PADDING-LEFT: 3.75pt; PADDING-RIGHT: 3.75pt; PADDING-TOP: 3.75pt">
<P class=MsoNormal><B><SPAN
style="FONT-FAMILY: Arial; FONT-SIZE: 9pt">Description</SPAN></B><SPAN
style="COLOR: black; FONT-FAMILY: Arial; FONT-SIZE: 9pt"><o:p></o:p></SPAN></P></TD></TR>
<TR>
<TD
style="PADDING-BOTTOM: 3.75pt; PADDING-LEFT: 3.75pt; PADDING-RIGHT: 3.75pt; PADDING-TOP: 3.75pt">
<P class=MsoNormal><SPAN
style="FONT-FAMILY: Arial; FONT-SIZE: 9pt">SysAllocString()<SPAN
style="COLOR: black"><o:p></o:p></SPAN></SPAN></P></TD>
<TD
style="PADDING-BOTTOM: 3.75pt; PADDING-LEFT: 3.75pt; PADDING-RIGHT: 3.75pt; PADDING-TOP: 3.75pt">
<P class=MsoNormal><SPAN
style="FONT-FAMILY: Arial; FONT-SIZE: 9pt">Allocates a new
BSTR and initializes it with an OLECHAR*<SPAN
style="COLOR: black"><o:p></o:p></SPAN></SPAN></P></TD></TR>
<TR>
<TD
style="PADDING-BOTTOM: 3.75pt; PADDING-LEFT: 3.75pt; PADDING-RIGHT: 3.75pt; PADDING-TOP: 3.75pt">
<P class=MsoNormal><SPAN
style="FONT-FAMILY: Arial; FONT-SIZE: 9pt">SysReAllocString()<SPAN
style="COLOR: black"><o:p></o:p></SPAN></SPAN></P></TD>
<TD
style="PADDING-BOTTOM: 3.75pt; PADDING-LEFT: 3.75pt; PADDING-RIGHT: 3.75pt; PADDING-TOP: 3.75pt">
<P class=MsoNormal><SPAN
style="FONT-FAMILY: Arial; FONT-SIZE: 9pt">Reallocates an
existing BSTR and initializes it with an OLECHAR*<SPAN
style="COLOR: black"><o:p></o:p></SPAN></SPAN></P></TD></TR>
<TR>
<TD
style="PADDING-BOTTOM: 3.75pt; PADDING-LEFT: 3.75pt; PADDING-RIGHT: 3.75pt; PADDING-TOP: 3.75pt">
<P class=MsoNormal><SPAN
style="FONT-FAMILY: Arial; FONT-SIZE: 9pt">SysAllocStringLen()<SPAN
style="COLOR: black"><o:p></o:p></SPAN></SPAN></P></TD>
<TD
style="PADDING-BOTTOM: 3.75pt; PADDING-LEFT: 3.75pt; PADDING-RIGHT: 3.75pt; PADDING-TOP: 3.75pt">
<P class=MsoNormal><SPAN
style="FONT-FAMILY: Arial; FONT-SIZE: 9pt">Allocates a new
BSTR, copies a specified number of characters from the passed
OLECHAR* into it, and then appends a null character<SPAN
style="COLOR: black"><o:p></o:p></SPAN></SPAN></P></TD></TR>
<TR>
<TD
style="PADDING-BOTTOM: 3.75pt; PADDING-LEFT: 3.75pt; PADDING-RIGHT: 3.75pt; PADDING-TOP: 3.75pt">
<P class=MsoNormal><SPAN
style="FONT-FAMILY: Arial; FONT-SIZE: 9pt">SysReAllocStringLen()<SPAN
style="COLOR: black"><o:p></o:p></SPAN></SPAN></P></TD>
<TD
style="PADDING-BOTTOM: 3.75pt; PADDING-LEFT: 3.75pt; PADDING-RIGHT: 3.75pt; PADDING-TOP: 3.75pt">
<P class=MsoNormal><SPAN
style="FONT-FAMILY: Arial; FONT-SIZE: 9pt">Reallocates an
existing BSTR, copies a specified number of characters from
the passed OLECHAR* into it, and then appends a null
character<SPAN
style="COLOR: black"><o:p></o:p></SPAN></SPAN></P></TD></TR>
<TR>
<TD
style="PADDING-BOTTOM: 3.75pt; PADDING-LEFT: 3.75pt; PADDING-RIGHT: 3.75pt; PADDING-TOP: 3.75pt">
<P class=MsoNormal><SPAN
style="FONT-FAMILY: Arial; FONT-SIZE: 9pt">SysFreeString()<SPAN
style="COLOR: black"><o:p></o:p></SPAN></SPAN></P></TD>
<TD
style="PADDING-BOTTOM: 3.75pt; PADDING-LEFT: 3.75pt; PADDING-RIGHT: 3.75pt; PADDING-TOP: 3.75pt">
<P class=MsoNormal><SPAN
style="FONT-FAMILY: Arial; FONT-SIZE: 9pt">Deallocates a
BSTR<SPAN
style="COLOR: black"><o:p></o:p></SPAN></SPAN></P></TD></TR>
<TR>
<TD
style="PADDING-BOTTOM: 3.75pt; PADDING-LEFT: 3.75pt; PADDING-RIGHT: 3.75pt; PADDING-TOP: 3.75pt">
<P class=MsoNormal><SPAN
style="FONT-FAMILY: Arial; FONT-SIZE: 9pt">SysStringLen()<SPAN
style="COLOR: black"><o:p></o:p></SPAN></SPAN></P></TD>
<TD
style="PADDING-BOTTOM: 3.75pt; PADDING-LEFT: 3.75pt; PADDING-RIGHT: 3.75pt; PADDING-TOP: 3.75pt">
<P class=MsoNormal><SPAN
style="FONT-FAMILY: Arial; FONT-SIZE: 9pt">Returns the number
of characters in a BSTR<SPAN
style="COLOR: black"><o:p></o:p></SPAN></SPAN></P></TD></TR>
<TR>
<TD
style="PADDING-BOTTOM: 3.75pt; PADDING-LEFT: 3.75pt; PADDING-RIGHT: 3.75pt; PADDING-TOP: 3.75pt">
<P class=MsoNormal><SPAN
style="FONT-FAMILY: Arial; FONT-SIZE: 9pt">SysStringByteLen()<SPAN
style="COLOR: black"><o:p></o:p></SPAN></SPAN></P></TD>
<TD
style="PADDING-BOTTOM: 3.75pt; PADDING-LEFT: 3.75pt; PADDING-RIGHT: 3.75pt; PADDING-TOP: 3.75pt">
<P class=MsoNormal><SPAN
style="FONT-FAMILY: Arial; FONT-SIZE: 9pt">Returns the length
in bytes of a BSTR (Win32 only)<SPAN
style="COLOR: black"><o:p></o:p></SPAN></SPAN></P></TD></TR>
<TR>
<TD
style="PADDING-BOTTOM: 3.75pt; PADDING-LEFT: 3.75pt; PADDING-RIGHT: 3.75pt; PADDING-TOP: 3.75pt">
<P class=MsoNormal><SPAN
style="FONT-FAMILY: Arial; FONT-SIZE: 9pt">SysAllocStringByteLen()<SPAN
style="COLOR: black"><o:p></o:p></SPAN></SPAN></P></TD>
<TD
style="PADDING-BOTTOM: 3.75pt; PADDING-LEFT: 3.75pt; PADDING-RIGHT: 3.75pt; PADDING-TOP: 3.75pt">
<P class=MsoNormal><SPAN
style="FONT-FAMILY: Arial; FONT-SIZE: 9pt">Allocates a BSTR
that contains the ANSI string passed as a parameter. Does not
perform any ANSI-to-Unicode translation (Win32 only)<SPAN
style="COLOR: black"><o:p></o:p></SPAN></SPAN></P></TD></TR></TBODY></TABLE>
<P>The succinct description provided above, in conjunction with the
official documentation, should be everything you will ever need to
know to deal with BSTRs. Note that the expected usage pattern is the
preventive allocation of an array of OLECHARs, which is later copied
into the system string. </P>
<P><B><SPAN style="COLOR: windowtext">Basic strings must be
allocated and freed manually. But who has the responsibility of
doing so when function calls are involved? This is a general COM
question and so the answer does not apply solely to strings. If the
parameter is input-only (IDL attribute [in]) the caller is
responsible for both the creation and the destruction of the
variable. If the parameter is output-only (IDL attribute [out]) then
the callee is responsible for the allocation of the string, but the
caller is expected to free it after use. If the parameter is both
input and output (IDL attribute [in, out]) then the caller allocates
the string and after the method invocation frees the memory. The
callee though is allowed to reallocate the string if necessary to do
so before returning it to the caller. <o:p></o:p></SPAN></B></P>
<P>Obviously these details interest C/C++ developers only, as Visual
Basic will continue to treat strings as usual without any special
consideration. </P>
<P><B><SPAN style="COLOR: #0e3092; FONT-SIZE: 10pt">BSTR wrappers
</SPAN></B></P>
<P>Both ATL and MFC offer particular support for simplified BSTR
management. ATL does it by means of a specialized wrapper class,
<I>CComBSTR,</I> whose declaration in atlbase.h looks like the
following (stripped down as usual for clarity and space
constraints): </P>
<TABLE border=0 cellPadding=0 cellSpacing=3
style="WIDTH: 100%; mso-cellspacing: 1.5pt; mso-padding-alt: 3.75pt 3.75pt 3.75pt 3.75pt"
width="100%">
<TBODY>
<TR>
<TD
style="BACKGROUND: #dfdfdf; PADDING-BOTTOM: 3.75pt; PADDING-LEFT: 3.75pt; PADDING-RIGHT: 3.75pt; PADDING-TOP: 3.75pt"><PRE>class CComBSTR</PRE><PRE>{</PRE><PRE>public:</PRE><PRE><SPAN style="mso-tab-count: 1"> </SPAN>BSTR m_str;</PRE><PRE><SPAN style="mso-tab-count: 1"> </SPAN>CComBSTR();</PRE><PRE><SPAN style="mso-tab-count: 1"> </SPAN>CComBSTR(int nSize, LPCOLESTR sz = NULL);</PRE><PRE><SPAN style="mso-tab-count: 1"> </SPAN>CComBSTR(LPCOLESTR pSrc);</PRE><PRE><SPAN style="mso-tab-count: 1"> </SPAN>CComBSTR(const CComBSTR& src);</PRE><PRE><SPAN style="mso-tab-count: 1"> </SPAN>CComBSTR& operator=(const CComBSTR& src);</PRE><PRE><SPAN style="mso-tab-count: 1"> </SPAN>CComBSTR& operator=(LPCOLESTR pSrc);</PRE><PRE><SPAN style="mso-tab-count: 1"> </SPAN>~CComBSTR();</PRE><PRE><SPAN style="mso-tab-count: 1"> </SPAN>unsigned int Length() const;</PRE><PRE><SPAN style="mso-tab-count: 1"> </SPAN>operator BSTR() const;</PRE><PRE><SPAN style="mso-tab-count: 1"> </SPAN>BSTR* operator&();</PRE><PRE><SPAN style="mso-tab-count: 1"> </SPAN>BSTR Copy() const;</PRE><PRE><SPAN style="mso-tab-count: 1"> </SPAN>void Attach(BSTR src);</PRE><PRE><SPAN style="mso-tab-count: 1"> </SPAN>BSTR Detach();</PRE><PRE><SPAN style="mso-tab-count: 1"> </SPAN>void Empty();</PRE><PRE>#if _MSC_VER>1020</PRE><PRE><SPAN style="mso-tab-count: 1"> </SPAN>bool operator!();</PRE><PRE>#else</PRE><PRE><SPAN style="mso-tab-count: 1"> </SPAN>BOOL operator!();</PRE><PRE>#endif</PRE><PRE><SPAN style="mso-tab-count: 1"> </SPAN>void Append(const CComBSTR& bstrSrc);</PRE><PRE><SPAN style="mso-tab-count: 1"> </SPAN>void Append(LPCOLESTR lpsz);</PRE><PRE><SPAN style="mso-tab-count: 1"> </SPAN>void AppendBSTR(BSTR p);</PRE><PRE><SPAN style="mso-tab-count: 1"> </SPAN>void Append(LPCOLESTR lpsz, int nLen);</PRE><PRE><SPAN style="mso-tab-count: 1"> </SPAN>CComBSTR& operator+=(const CComBSTR& bstrSrc);</PRE><PRE>#ifndef OLE2ANSI</PRE><PRE><SPAN style="mso-tab-count: 1"> </SPAN>CComBSTR(LPCSTR pSrc);</PRE><PRE><SPAN style="mso-tab-count: 1"> </SPAN>CComBSTR(int nSize, LPCSTR sz = NULL);</PRE><PRE><SPAN style="mso-tab-count: 1"> </SPAN>CComBSTR& operator=(LPCSTR pSrc);</PRE><PRE><SPAN style="mso-tab-count: 1"> </SPAN>void Append(LPCSTR);</PRE><PRE>#endif</PRE><PRE><SPAN style="mso-tab-count: 1"> </SPAN>HRESULT WriteToStream(IStream* pStream);</PRE><PRE><SPAN style="mso-tab-count: 1"> </SPAN>HRESULT ReadFromStream(IStream* pStream);</PRE><PRE>};</PRE></TD></TR></TBODY></TABLE>
<P>The utilization of the class is very straightforward even for the
non-ATL experts. Basically the features offered are: </P>
<UL type=disc>
<LI class=MsoNormal
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -