NativeEvent使用

微软官方网站:

http://msdn.microsoft.com/zh-cn/library/ee2k0a7d(v=VS.80).aspx

根据工程需要,对相关方法进行了封装

 

///事件源
[event_source(native)]
class __declspec(dllexport)  CEventSource
{
public:

 CEventSource();
 ~CEventSource();

public:
 //生成事件
 bool DoEvent(void* pPack,int nLength);
 //事件
 __event bool Event(void* pEventData,int nLength);
};

///事件接收
[event_receiver(native)]
class __declspec(dllexport)  CEventReceiver
{
public:
 CEventReceiver();
 ~CEventReceiver();

public:
   
 void HookEvent(CEventSource* pSource,pReceiveEventCallbackFun pReceiveEventCallbackFun,

         void* pReceiveEventCallbackFunUser);

 void UnhookEvent(CEventSource* pSource);
 
private:
 bool AccepEvent(void* pEventData,int nLength);

private:
 //事件回调
 pReceiveEventCallbackFun m_pEventDataCallbackFun;
 void* m_pEventDataCallbackFunUser;
 

};
示例:
//提供应用层使用的回调
static void ReceiveEventCallbackFun(void* pBuffer,int nBufferLen,void* pUser); 
//调用过程
CEventSource* source = new CEventSource(); 
CEventReceiver* receiver = new CEventReceiver(); 
receiver->HookEvent(source,ReceiveEventCallbackFun,this); 
//构造测试数据 
STestParam* temp = (STestParam*)Malloc(STestParamSize); 
temp->m_nType = 256; SafePrintf(temp->m_szInfo,256,"DoEvent: %d",temp->m_nType); 
//发送事件 
source->DoEvent((void*)temp,STestParamSize); 
// Do something
receiver->UnhookEvent(source); 
delete(receiver);
receiver = NULL;
delete(source); 
source = NULL;

你可能感兴趣的:(开发)