<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://interop123.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>C++ Interop</title><link>http://interop123.com/forums/12/ShowForum.aspx</link><description>有关Managed C++, C++/CLI相关的互操作技术资源和工具</description><dc:language>zh-CHS</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61120.2)</generator><item><title>【推荐】一篇用C++/CLI讲述在托管委托（delegate）和非托管函数指针之间相互转化的文章</title><link>http://interop123.com/forums/thread/40.aspx</link><pubDate>Wed, 27 May 2009 06:47:46 GMT</pubDate><guid isPermaLink="false">b1280fd9-26a1-4865-a2de-26bd23f581dd:40</guid><dc:creator>小崔爱自由</dc:creator><slash:comments>0</slash:comments><comments>http://interop123.com/forums/thread/40.aspx</comments><wfw:commentRss>http://interop123.com/forums/commentrss.aspx?SectionID=12&amp;PostID=40</wfw:commentRss><description>&lt;P&gt;我们的图书&lt;A href="http://www.china-pub.com/45607" target=_blank&gt;《精通.NET互操作》&lt;/A&gt;出版有一个月的时间了，有一些朋友会想我请教一些问题。并反馈说，没有专门详细地介绍有关用委托实现函数指针、回调函数的章节。我觉得大家的建议很有道理。现在这里做个记号，挖个坑。&lt;/P&gt;
&lt;P&gt;之所以推荐这篇文章是有两个原因：&lt;/P&gt;
&lt;P&gt;1. 它介绍了在托管委托和非托管函数指针，回调函数之间相互转换的方法&lt;/P&gt;
&lt;P&gt;2. 它使用C++/CLI实现了对整个问题的描述。因为网络上有关C++ Interop的相关资料很少。所以值得一看&lt;/P&gt;
&lt;P&gt;&lt;A class=TitleLinkStyle href="http://www.heege.net/blog/PermaLink,guid,94167d73-7954-4a5c-a745-dc60d352cdef.aspx"&gt;&lt;FONT color=#cc0033 size=3 face=verdana,geneva&gt;Marshalling native function pointers&lt;/FONT&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;我读后，发现作者主要强调了以下几点：&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;1.在定义&lt;/FONT&gt;托管委托（delegate）的时候，可以使用&lt;FONT face="Courier New"&gt;&lt;FONT color=#0033ff&gt;&lt;STRONG&gt;UnmanagedFunctionPointer&lt;/STRONG&gt;&lt;/FONT&gt;属性（Attribute）指定CallingConvention，以使其托管签名与非托管函数定义相匹配。&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;2.使用&lt;FONT color=#0033ff&gt;&lt;STRONG&gt;System.Runtime.InteropServices.Marshal&lt;/STRONG&gt;&lt;/FONT&gt;类的方法实现托管委托和非托管函数指针之间的封送。&lt;/FONT&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;FONT color=#0033ff face="courier new,courier"&gt;&lt;STRONG&gt;Marshal.GetFunctionPointerForDelegate&lt;/STRONG&gt;&lt;/FONT&gt;方法，用于将托管委托转化为非托管代码可以调用的方法。&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN id=nsrTitle&gt;&lt;STRONG&gt;&lt;FONT color=#0033ff face="courier new,courier"&gt;Marshal&lt;SPAN class=cs&gt;.&lt;/SPAN&gt;GetDelegateForFunctionPointer&lt;/FONT&gt;&lt;/STRONG&gt;方法，用于将非托管函数转化为托管委托。&lt;/SPAN&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;FONT face="courier new,courier"&gt;3.&lt;/FONT&gt;垃圾回收器（GC）对封送委托到非托管代码时的影响。由于委托（delegate）和由委托转化成的函数指针处于不同的堆上。所以一定要保证非托管代码执行过程中，托管对象没有被回收。&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;希望大家觉得有用。&lt;/SPAN&gt;&lt;/P&gt;</description></item></channel></rss>