﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-sanxcoo</title><link>http://www.cppblog.com/sanxcoo/</link><description>做一个耐得住寂寞的人</description><language>zh-cn</language><lastBuildDate>Tue, 21 Apr 2026 18:21:03 GMT</lastBuildDate><pubDate>Tue, 21 Apr 2026 18:21:03 GMT</pubDate><ttl>60</ttl><item><title>将成员函数作为回调函数</title><link>http://www.cppblog.com/sanxcoo/archive/2010/04/01/111271.html</link><dc:creator>Sanxcoo</dc:creator><author>Sanxcoo</author><pubDate>Thu, 01 Apr 2010 06:25:00 GMT</pubDate><guid>http://www.cppblog.com/sanxcoo/archive/2010/04/01/111271.html</guid><wfw:comment>http://www.cppblog.com/sanxcoo/comments/111271.html</wfw:comment><comments>http://www.cppblog.com/sanxcoo/archive/2010/04/01/111271.html#Feedback</comments><slash:comments>8</slash:comments><wfw:commentRss>http://www.cppblog.com/sanxcoo/comments/commentRss/111271.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sanxcoo/services/trackbacks/111271.html</trackback:ping><description><![CDATA[<p>在网上查了一些资料，做了一个Thunk模板，能够正确调用成员函数。但是在做取成员函数地址操作时比较麻烦，需要用到汇编。<br>//取成员函数地址<br>DWORD_PTR off = 0;<br>_asm<br>{<br>&nbsp;&nbsp;&nbsp;mov eax, Class::MemFunc<br>&nbsp;&nbsp;&nbsp;mov DWORD PTR [off], eax<br>}<br>每指定一个成员函数作为一个回调函数就要做如上操作。本想将Class和MemFunc作为两个参数定义一个宏包含汇编语言部分,<br>但不知道怎样编写汇编部分。如果有知道怎么编写的，希望能不吝赐教^_^<br><br>// Thunk 具体实现<br>#pragma pack( push, 1 )<br>struct Thunk_struct<br>{<br>&nbsp;BYTE&nbsp;&nbsp;op_movecx;&nbsp;&nbsp;// as operation "mov" in asm<br>&nbsp;DWORD_PTR&nbsp;val_ecx;<br>&nbsp;BYTE&nbsp;&nbsp;op_call;&nbsp;&nbsp;// as operation "jmp" in asm<br>&nbsp;DWORD_PTR&nbsp;val_address;<br>};<br>#pragma pack( pop )<br><br>template &lt; class TCallback, class TClass &gt;<br>class Thunk<br>{<br>public:<br>&nbsp;TCallback MemFuncToCallback( TClass* pObject, DWORD_PTR pMemFuncAddress )<br>&nbsp;{<br>&nbsp;&nbsp;// 0xB9是&#8220;mov ecx, 数值&#8221;的机器码<br>&nbsp;&nbsp;m_thunk.op_movecx = 0xB9;<br>&nbsp;&nbsp;// 将对象指针pObject赋值给ecx<br>&nbsp;&nbsp;m_thunk.val_ecx = (DWORD_PTR)pObject;<br>&nbsp;&nbsp;// 0xE9是&#8220;jmp 相对地址&#8221;的机器码<br>&nbsp;&nbsp;m_thunk.op_call = 0xE9;<br>&nbsp;&nbsp;// 利用成员函数的具体地址pMemFuncAddress计算jmp的相对地址<br>&nbsp;&nbsp;m_thunk.val_address = pMemFuncAddress - ((DWORD_PTR)(&amp;m_thunk.val_address) + sizeof(DWORD_PTR));</p>
<p>&nbsp;&nbsp;return (TCallback)&amp;m_thunk;<br>&nbsp;};</p>
<p>protected:<br>&nbsp;Thunk_struct m_thunk;<br>};</p>
<img src ="http://www.cppblog.com/sanxcoo/aggbug/111271.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sanxcoo/" target="_blank">Sanxcoo</a> 2010-04-01 14:25 <a href="http://www.cppblog.com/sanxcoo/archive/2010/04/01/111271.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>