📄 csdn_文档中心_com深入理解(下)——方法参数类型为cruntimeclass、void等.htm
字号:
<SPAN style="COLOR: blue">unsigned</SPAN> <SPAN
style="COLOR: blue">long</SPAN>
StartingSize,<BR>
PA* <SPAN style="COLOR: green">/* ppA */</SPAN> )<BR>{<BR><SPAN
style="COLOR: green">//
之所以有StartingSize,因为此参数可能并不是第一个被列集的参数,</SPAN><BR><SPAN
style="COLOR: green">// 如:HRESULT SetA( [in] long tem1, [in] char
tem2, [in] PA a );</SPAN><BR><SPAN style="COLOR: green">//
此时的StartingSize就为sizeof( long ) + sizeof( char )</SPAN><BR><SPAN
style="COLOR: green">// 而之所以还要再将其传进来是为了对齐需要</SPAN><BR><BR><SPAN
style="COLOR: green">// 此处没有进行对齐处理,因为结构_SA是只有两个unsigned
long的简单</SPAN><BR><SPAN style="COLOR: green">//
结构,无须再刻意对齐。</SPAN><BR> <SPAN
style="COLOR: blue">return</SPAN> StartingSize + <SPAN
style="COLOR: blue">sizeof</SPAN>( _SA );<BR>}<BR><SPAN
style="COLOR: blue">unsigned</SPAN> <SPAN
style="COLOR: blue">char</SPAN>* __RPC_USER PA_UserMarshal( <SPAN
style="COLOR: blue">unsigned</SPAN> <SPAN
style="COLOR: blue">long</SPAN>
*pFlags,<BR>
<SPAN style="COLOR: blue">unsigned</SPAN> <SPAN
style="COLOR: blue">char</SPAN>
*Buffer,<BR>
PA *ppA )<BR>{<BR><SPAN style="COLOR: green">//
按线种类(即结构_SA)的定义填冲缓冲区,注意必须按照NDR传输格式</SPAN><BR><SPAN
style="COLOR: green">//
进行填充,这里由于_SA简单,所以只是简单地复制,没有什么对齐及一</SPAN><BR><SPAN
style="COLOR: green">// 致性数据的问题。关于NDR传输格式的详细内容,请参考</SPAN><BR><SPAN
style="COLOR: green">// <A
href="http://www.opengroup.org/onlinepubs/9629399/chap14.htm">http://www.opengroup.org/onlinepubs/9629399/chap14.htm</A></SPAN><BR>
<SPAN style="COLOR: blue">if</SPAN>( *pFlags & MSHCTX_INPROC
)<BR> {<BR> <SPAN
style="COLOR: green">//
是进程内调用,直接将CA*进行传递,而不进行拷贝</SPAN><BR>
*<SPAN style="COLOR: blue">reinterpret_cast</SPAN>< <SPAN
style="COLOR: blue">void</SPAN>** >( Buffer ) =
*ppA;<BR> }<BR> <SPAN
style="COLOR: blue">else</SPAN><BR>
{<BR> CA *pA = <SPAN
style="COLOR: blue">reinterpret_cast</SPAN>< CA* >( *ppA
);<BR> PSA pSA = <SPAN
style="COLOR: blue">reinterpret_cast</SPAN>< PSA >( Buffer
);<BR> pSA->a =
pA->m_a;<BR> pSA->b
= pA->m_b;<BR> }<BR><BR><SPAN
style="COLOR: green">// 返回缓冲区的有效位置,当前位置后的sizeof( _SA
)个字节</SPAN><BR> <SPAN
style="COLOR: blue">return</SPAN> Buffer + <SPAN
style="COLOR: blue">sizeof</SPAN>( _SA );<BR>}<BR><SPAN
style="COLOR: blue">unsigned</SPAN> <SPAN
style="COLOR: blue">char</SPAN>* __RPC_USER PA_UserUnmarshal( <SPAN
style="COLOR: blue">unsigned</SPAN> <SPAN
style="COLOR: blue">long</SPAN>
*pFlags,<BR>
<SPAN style="COLOR: blue">unsigned</SPAN> <SPAN
style="COLOR: blue">char</SPAN>
*Buffer,<BR>
PA *ppA )<BR>{<BR> <SPAN
style="COLOR: blue">if</SPAN>( *pFlags & MSHCTX_INPROC
)<BR> {<BR> <SPAN
style="COLOR: green">//
是进程内调用,直接将CA*进行传递,而不进行拷贝</SPAN><BR>
*ppA = *<SPAN style="COLOR: blue">reinterpret_cast</SPAN>< <SPAN
style="COLOR: blue">void</SPAN>** >( Buffer
);<BR> }<BR> <SPAN
style="COLOR: blue">else</SPAN><BR>
{<BR> <SPAN
style="COLOR: blue">void</SPAN> *p = CoTaskMemAlloc( <SPAN
style="COLOR: blue">sizeof</SPAN>( CA )
);<BR> <SPAN
style="COLOR: blue">if</SPAN>( !p
)<BR>
<SPAN style="COLOR: blue">return</SPAN> Buffer + <SPAN
style="COLOR: blue">sizeof</SPAN>( _SA
);<BR> CA *pAA = <SPAN
style="COLOR: blue">new</SPAN>( p ) CA; <SPAN
style="COLOR: green">//
生成一个类对象</SPAN><BR><BR> PSA
pSA = <SPAN style="COLOR: blue">reinterpret_cast</SPAN>< PSA
>( Buffer );<BR>
pAA->m_a =
pSA->a;<BR> pAA->m_b
= pSA->b;<BR><BR> *ppA
= p;<BR> }<BR><BR><SPAN style="COLOR: green">//
返回缓冲区的有效位置,当前位置后的sizeof( _SA )个字节</SPAN><BR> <SPAN
style="COLOR: blue">return</SPAN> Buffer + <SPAN
style="COLOR: blue">sizeof</SPAN>( _SA );<BR>}<BR><SPAN
style="COLOR: blue">void</SPAN> __RPC_USER PA_UserFree( <SPAN
style="COLOR: blue">unsigned</SPAN> <SPAN
style="COLOR: blue">long</SPAN>
*pFlags,<BR>
PA *ppA )<BR>{<BR> <SPAN
style="COLOR: blue">if</SPAN>( !( *pFlags & MSHCTX_INPROC
) )<BR> {<BR> <SPAN
style="COLOR: green">//
不是进程内汇集,分配了内存,释放资源</SPAN><BR>
CA *pAA = <SPAN style="COLOR: blue">reinterpret_cast</SPAN>< CA*
>( *ppA );<BR>
pAA->~CA();<BR>
CoTaskMemFree( pAA );<BR>
}<BR>}<BR> 使用中,则:<BR>IAbc *pA; <SPAN
style="COLOR: green">// 假设已初始化</SPAN><BR>CA a;<BR>a.SetA( 654
);<BR>PA pAA = &a;<BR>pA->SetA( pAA ); <SPAN
style="COLOR: green">// 或者直接pA->SetA( &a
);</SPAN><BR>pA->GetA( &a );
<P></P>
<P>
非常明显,MIDL提供的可用于自定义类型传递的属性很正常地不止上面几个,如:[transmit_as()]、[handle]等,在此仅起抛砖引玉的作用,关于MIDL提供的其他属性,还请参考MSDN。上面的实现方法中,都不仅仅提供了汇集自定义数据类型的渠道,还提供了优化的途径(如上面的pFlags标志参数)。因此在编写代理/占位组件时,应考虑在关键地方应用类似的属性进行生成代码的优化。</P></FONT><BR></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR>
<TABLE align=center bgColor=#006699 border=0 cellPadding=0 cellSpacing=0
width=770>
<TBODY>
<TR bgColor=#006699>
<TD align=middle bgColor=#006699 id=white><FONT
color=#ffffff>对该文的评论</FONT></TD>
<TD align=middle>
<SCRIPT
src="CSDN_文档中心_COM深入理解(下)——方法参数类型为CRuntimeClass、void等.files/readnum.htm"></SCRIPT>
</TD></TR></TBODY></TABLE><BR>
<DIV align=center>
<TABLE align=center bgColor=#cccccc border=0 cellPadding=2 cellSpacing=1
width=770>
<TBODY>
<TR>
<TH bgColor=#006699 id=white><FONT
color=#ffffff>我要评论</FONT></TH></TR></TBODY></TABLE></DIV>
<DIV align=center>
<TABLE border=0 width=770>
<TBODY>
<TR>
<TD>你没有登陆,无法发表评论。 请先<A
href="http://www.csdn.net/member/login.asp?from=/Develop/read_article.asp?id=27401">登陆</A>
<A
href="http://www.csdn.net/expert/zc.asp">我要注册</A><BR></TD></TR></TBODY></TABLE></DIV><BR>
<HR noShade SIZE=1 width=770>
<TABLE border=0 cellPadding=0 cellSpacing=0 width=500>
<TBODY>
<TR align=middle>
<TD height=10 vAlign=bottom><A
href="http://www.csdn.net/intro/intro.asp?id=2">网站简介</A> - <A
href="http://www.csdn.net/intro/intro.asp?id=5">广告服务</A> - <A
href="http://www.csdn.net/map/map.shtm">网站地图</A> - <A
href="http://www.csdn.net/help/help.asp">帮助信息</A> - <A
href="http://www.csdn.net/intro/intro.asp?id=2">联系方式</A> - <A
href="http://www.csdn.net/english">English</A> </TD>
<TD align=middle rowSpan=3><A
href="http://www.hd315.gov.cn/beian/view.asp?bianhao=010202001032100010"><IMG
border=0 height=48
src="CSDN_文档中心_COM深入理解(下)——方法参数类型为CRuntimeClass、void等.files/biaoshi.gif"
width=40></A></TD></TR>
<TR align=middle>
<TD vAlign=top>百联美达美公司 版权所有 京ICP证020026号</TD></TR>
<TR align=middle>
<TD vAlign=top><FONT face=Verdana>Copyright © CSDN.net, Inc. All rights
reserved</FONT></TD></TR>
<TR>
<TD height=15></TD>
<TD></TD></TR></TBODY></TABLE></DIV>
<DIV></DIV><!--内容结束//--><!--结束//--></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -