__event/__raise/__hook用法

来源:互联网 发布:sql服务无法启动 编辑:程序博客网 时间:2024/06/16 10:04

#include <stdio.h>

 

class CSource
{
public:
   __event void MyEvent(int nValue);
};

 

class CReceiver

{
public:
   void MyHandler(int nValue)
   {
      printf_s("MyHandler1 was called with value %d./n", nValue);
   }

 

   void hookEvent(CSource* pSource)

   {
      __hook(&CSource::MyEvent, pSource, &CReceiver::MyHandler);
   }

   void unhookEvent(CSource* pSource)

  {
      __unhook(&CSource::MyEvent, pSource, &CReceiver::MyHandler);
   }
};

int main() {
   CSource source;
   CReceiver receiver;

 

   receiver.hookEvent(&source);
   __raise source.MyEvent(123);
   receiver.unhookEvent(&source);
}

 

当程序执行到__raise source.MyEvent(123)时,程序就会跳到   void MyHandler(int nValue) 
 执行printf_s("MyHandler1 was called with value %d./n", nValue)。简单的用法!

 

 

 

原创粉丝点击