source: trunk/Utilities/Miscellaneous/ComEventAttribute/ComEventAttribute.cpp @ 937

Last change on this file since 937 was 318, checked in by roman, 8 years ago
File size: 1.8 KB
Line 
1#include <SDKDDKVer.h>
2#include <stdio.h>
3#include <tchar.h>
4#define _ATL_ATTRIBUTES 1
5#include <atlbase.h>
6#include <atlcom.h>
7#include <atlstr.h>
8
9[dual, uuid("00000000-0000-0000-0000-000000000001")]
10__interface IEvents
11{
12        [id(1)] HRESULT MyEvent([in] int value);
13};
14
15[dual, uuid("00000000-0000-0000-0000-000000000002")]
16__interface IEventSource
17{
18        [id(1)] HRESULT FireEvent();
19};
20
21class DECLSPEC_UUID("530DF3AD-6936-3214-A83B-27B63C7997C4") CSource;
22
23[coclass, event_source(com), uuid("530DF3AD-6936-3214-A83B-27B63C7997C4")]
24class CSource : public IEventSource
25{
26public:
27        __event __interface IEvents; 
28
29        HRESULT FireEvent() 
30        {
31                __raise MyEvent(123);
32                return S_OK;
33        }
34};
35
36[event_receiver(com)]
37class CReceiver 
38{
39public:
40        HRESULT MyHandler1(int nValue) 
41        {
42                printf_s("MyHandler1 was called with value %d.\n", nValue);
43                return S_OK;
44        }
45        HRESULT MyHandler2(int nValue) 
46        {
47                printf_s("MyHandler2 was called with value %d.\n", nValue);
48                return S_OK;
49        }
50        void HookEvent(IEventSource* pSource) 
51        {
52                __hook(&IEvents::MyEvent, pSource, &CReceiver::MyHandler1);
53                __hook(&IEvents::MyEvent, pSource, &CReceiver::MyHandler2);
54        }
55        void UnhookEvent(IEventSource* pSource) 
56        {
57                __unhook(&IEvents::MyEvent, pSource, &CReceiver::MyHandler1);
58                __unhook(&IEvents::MyEvent, pSource, &CReceiver::MyHandler2);
59        }
60};
61
62[ module(type=exe, name="Event") ];
63
64int _tmain(int argc, _TCHAR* argv[])
65{
66        // Create COM object
67        CoInitialize(NULL);
68        {
69                CComObject<CSource>* pSource = NULL;
70                ATLVERIFY(SUCCEEDED(CComObject<CSource>::CreateInstance(&pSource)));
71                CComPtr<IEventSource> pEventSource = pSource;
72                CReceiver Receiver;
73                Receiver.HookEvent(pEventSource);
74                pSource->FireEvent();
75                Receiver.UnhookEvent(pEventSource);
76        }
77        CoUninitialize();
78        return 0;
79}
80
Note: See TracBrowser for help on using the repository browser.