Get Events from an COM Server

Hi All,

now I get connected in my main.cpp file to the server. I hope this will be correct. But how can I get now an event from the server? The server sends some events. I can see that because an Message Dialog will be opened from the server.

Now my questions are the following:

a)
Is the code until now correct? Is it possible to make a test if I get the correct connection or anything else?

b)
How can I get in the main.cpp an event (OnShowMessageDlg) or any message from the server? Do I need to add something to the sink class?

c)
Must I add something in the Sink class to get the an event in the invoke function? Or how should this work?


Let me know If you need any more information.

Once more, thanks for any help.

Juergen

main.cpp File
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
CComPtr<ICWOLE2> tCWOLE;
HRESULT hr;
hr = CoInitialize(0);

hr = CoCreateInstance(CLSID_CWOLEv2, 0, CLSCTX_LOCAL_SERVER, IID_ICWOLE2, (void**)&tCWOLE);
IConnectionPointContainer   * pConnPtContainer;
hr = tCWOLE->QueryInterface(IID_IConnectionPointContainer,(void**)&pConnPtContainer);
if(FAILED(hr)){
	CoUninitialize();
	return false;
}
CComPtr<IConnectionPoint> ICPoint;
hr = pConnPtContainer->FindConnectionPoint(DIID_ICWOLEEvents,&m_pConnectionPoint);
if(FAILED(hr)){
	CoUninitialize();
	return false;
}

m_sink = new CSink;
LPUNKNOWN pUnk = NULL;
m_sink->QueryInterface(IID_IUnknown,(void**)&pUnk);
hr = m_pConnectionPoint->Advise(pUnk,&m_sink->cookie);
if(FAILED(hr)){
	CoUninitialize();
	return false;
}
pConnPtContainer->Release();
.
.
.
// Here I must get the event from the COM Server, but how can this be done???
// How can I test if I get the information from the correct class??
.
.
.

m_pConnectionPoint->Unadvise(m_sink->cookie);
m_pConnectionPoint->Release();
m_sink->Release();



CSink.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class CSink : public ICWOLEEvents
{
	public:
		
		CSink::CSink() {m_refCount = 1, cookie = 0;}
		CSink::~CSink()	{}
		
		STDMETHODIMP QueryInterface(REFIID riid, void ** ppvObj); //HRESULT _stdcall
		STDMETHODIMP_(ULONG) AddRef();  // Note the underscore
		STDMETHODIMP_(ULONG) Release();
	
		
		STDMETHODIMP GetTypeInfoCount(UINT *iTInfo);
		STDMETHODIMP GetTypeInfo(UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo);
		STDMETHODIMP GetIDsOfNames(REFIID riid, OLECHAR **rgszNames, UINT cNames,  LCID lcid, DISPID *rgDispId);
		STDMETHODIMP Invoke(DISPID dispIdMember, 
							REFIID riid, 
							LCID lcid,
							WORD wFlags, DISPPARAMS* pDispParams,
							VARIANT* pVarResult, EXCEPINFO* pExcepInfo,
							UINT* puArgErr);
		
		
		
    public:
		int m_refCount;
		DWORD cookie;
		//void OnConfigurationApply(VARIANT_BOOL *pAccept);
		void OnShowMessageDlg(IMsgDlg *pIMsg, VARIANT_BOOL *Result);

};


CSink.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
HRESULT CSink::QueryInterface(REFIID riid, void ** ppvObj)
{

	if (riid == IID_IUnknown){
		*ppvObj = static_cast<void*>(this);
	}
	
	else if (riid == DIID_ICWOLEEvents){
		*ppvObj = static_cast<ICWOLEEvents*>(this);
	}
	else if (riid == IID_IDispatch){
		*ppvObj = static_cast<IDispatch*>(this);
	}
	
	else
	{
	    char clsidStr[256];
	    WCHAR wClsidStr[256];
	    char txt[512];
		
	    StringFromGUID2(riid, (LPOLESTR)&wClsidStr, 256);
		
	    // Convert down to ANSI
	    WideCharToMultiByte(CP_ACP, 0, wClsidStr, -1, clsidStr, 256, NULL, NULL);
		
	    sprintf(txt, "riid is : %s: Unsupported Interface", clsidStr);
		
		*ppvObj = NULL;
		return E_NOINTERFACE;
	}
	
	static_cast<IUnknown*>(*ppvObj)->AddRef();
	return S_OK;
}
/******************************************************************************/
STDMETHODIMP_(ULONG) CSink::AddRef()
{
	return ++m_refCount;
}
/******************************************************************************/
STDMETHODIMP_(ULONG) CSink::AddRef()
{
	return ++m_refCount;
}
/******************************************************************************/
STDMETHODIMP_(ULONG) CSink::Release()
{
	if (--m_refCount == 0)
	{
		delete this;
		return 0;
	}
	return m_refCount;
}
/******************************************************************************/
STDMETHODIMP CSink::GetTypeInfoCount(UINT *iTInfo)
{
	return E_NOTIMPL;
}
/******************************************************************************/
STDMETHODIMP CSink::GetTypeInfo(UINT iTInfo, LCID lcid, 
                                       ITypeInfo **ppTInfo)
{
	
	return E_NOTIMPL;
}
/******************************************************************************/
STDMETHODIMP CSink::GetIDsOfNames(REFIID riid,  
                                         OLECHAR **rgszNames, 
                                         UINT cNames,  LCID lcid,
                                         DISPID *rgDispId)
{
	HRESULT hr = E_FAIL;
	return hr;
}
/******************************************************************************/
STDMETHODIMP CSink::Invoke(DISPID dispIdMember, REFIID riid, LCID lcid,
                                  WORD wFlags, DISPPARAMS* pDispParams,
                                  VARIANT* pVarResult, EXCEPINFO* pExcepInfo,
                                  UINT* puArgErr)
{

	if ((riid != IID_NULL))
		return E_INVALIDARG;
	
	HRESULT hr = S_OK;  // Initialize
	
	
	return hr;
}
/******************************************************************************/
void CSink::OnShowMessageDlg(IMsgDlg *pIMsg, VARIANT_BOOL *Result){
	// Is this correct???
}


Test.idl
// Generated .IDL file (by the OLE/COM Object Viewer)
//
// typelib filename: test_programm.exe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
[
  uuid(90B4344B-90CD-4527-BC00-4F4D45C793D6),
  version(2.13),
  helpstring("CW  CWOLE")
]
library CW_CWOLE
{
    // TLib :     // TLib : OLE Automation : {00020430-0000-0000-C000-000000000046}
    importlib("stdole2.tlb");

    // Forward declare all types defined in this typelib
    interface ICWOLE2;
    dispinterface ICWOLEEvents;
.
.
.
.
.

    [
      uuid(6775FB91-B5BE-11D6-A996-0050BA24C7B9),
      version(1.1),
      helpstring("Event dispatch interface for CW CWOLE")
    ]
    dispinterface ICWOLEEvents {
        properties:
        methods:
            [id(0x00000001)]
            void OnConfigurationApply([in, out] VARIANT_BOOL* pAccept);
            [id(0x00000002)]
            void OnShowMessageDlg(
                            [in] IMsgDlg* pIMsg, 
                            [out, retval] VARIANT_BOOL* Result);
    };

    typedef [uuid(5CBBA151-1C47-4D6C-B14C-C527E333F812), version(1.2)]
    enum {
        ECS_DEFAULT = 0,
        ECS_FRONT = 1,
        ECS_LEFT = 2,
        ECS_RIGHT = 3,
        ECS_LEFTUP = 4,
        ECS_RIGHTUP = 5,
        ECS_TOP = 6,
        ECS_BACK = 7,
        ECS_BACKLEFTUP = 8,
        ECS_BACKRIGHTUP = 9,
        ECS_BOTTOM = 10
    } ECamSetup;

    typedef [uuid(9545E3CA-6401-4418-A040-DA3A89E2C792), version(1.3)]
    enum {
        EPM_PERSP = 0,
        EPM_ORTHO = 1,
        EPM_2D = 2
    } EProjectionMode;

    [
      uuid(F560F763-2948-11D7-A9BF-0050BA24C7B9),
      version(2.13),
      helpstring("CWOLE2")
    ]
    coclass CWOLEv2 {
        [default] interface ICWOLE2;
        [default, source] dispinterface ICWOLEEvents;
    };


Topic archived. No new replies allowed.