?? 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 + -