aboutsummaryrefslogtreecommitdiffstats
path: root/src/native/windows/msofficecomm/DispatchImpl.h
blob: f7abcbe339db0ad926e8e44a7e8410c7e79e4d02 (plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
#ifndef _JMSOFFICECOMM_DISPATCHIMPL_H_
#define _JMSOFFICECOMM_DISPATCHIMPL_H_

#include <oaidl.h>
#include "UnknownImpl.h"

/**
 * Represents a base implementation of the <tt>IDispatch</tt> interface.
 *
 * @author Lyubomir Marinov
 */
template <class T, REFIID IID_T>
class DispatchImpl
    : public UnknownImpl<T, IID_T>
{
public:
    // IUnknown
    STDMETHODIMP QueryInterface(REFIID iid, PVOID *obj)
        {
            HRESULT ret;

            if (obj)
            {
                if (IID_IDispatch == iid)
                {
                    static_cast<LPUNKNOWN>(this)->AddRef();
                    *obj = static_cast<LPDISPATCH>(this);
                    ret = S_OK;
                }
                else
                    ret = UnknownImpl<T, IID_T>::QueryInterface(iid, obj);
            }
            else
                ret = E_POINTER;
            return ret;
        }

    // IDispatch
    STDMETHODIMP GetTypeInfoCount(UINT *pctinfo)
#ifdef DISPATCHIMPL_CREATESTDDISPATCH
        {
            LPDISPATCH iDispatch = getIDispatch();
            HRESULT hr;

            if (iDispatch)
                hr = iDispatch->GetTypeInfoCount(pctinfo);
            else if (pctinfo)
            {
                *pctinfo = 0;
                hr = S_OK;
            }
            else
                hr = E_INVALIDARG;
            return hr;
        }
#else
        STDMETHODIMP_E_NOTIMPL_STUB
#endif /* #ifdef DISPATCHIMPL_CREATESTDDISPATCH */

    STDMETHODIMP GetTypeInfo(UINT iTInfo, LCID lcid, LPTYPEINFO *ppTInfo)
#ifdef DISPATCHIMPL_CREATESTDDISPATCH
        {
            LPDISPATCH iDispatch = getIDispatch();
            HRESULT hr;

            if (iDispatch)
                hr = iDispatch->GetTypeInfo(iTInfo, lcid, ppTInfo);
            else if (ppTInfo)
            {
                *ppTInfo = NULL;
                hr = TYPE_E_ELEMENTNOTFOUND;
            }
            else
                hr = E_INVALIDARG;
            return hr;
        }
#else
        STDMETHODIMP_E_NOTIMPL_STUB
#endif /* #ifdef DISPATCHIMPL_CREATESTDDISPATCH */

    STDMETHODIMP GetIDsOfNames(REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
#ifdef DISPATCHIMPL_CREATESTDDISPATCH
        {
            LPDISPATCH iDispatch = getIDispatch();
            HRESULT hr;

            if (iDispatch)
                hr = iDispatch->GetIDsOfNames(riid, rgszNames, cNames, lcid, rgDispId);
            else
                hr = TYPE_E_ELEMENTNOTFOUND;
            return hr;
        }
#else
        STDMETHODIMP_E_NOTIMPL_STUB
#endif /* #ifdef DISPATCHIMPL_CREATESTDDISPATCH */

    STDMETHODIMP Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
#ifdef DISPATCHIMPL_CREATESTDDISPATCH
        {
            LPDISPATCH iDispatch = getIDispatch();
            HRESULT hr;

            if (iDispatch)
                hr = iDispatch->Invoke(dispIdMember, riid, lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
            else
                hr = TYPE_E_ELEMENTNOTFOUND;
            return hr;
        }
#else
        STDMETHODIMP_E_NOTIMPL_STUB
#endif /* #ifdef DISPATCHIMPL_CREATESTDDISPATCH */

protected:
    DispatchImpl() : _iDispatch(NULL) {};

    virtual ~DispatchImpl()
        {
            if (_iDispatch)
            {
                _iDispatch->Release();
                _iDispatch = NULL;
            }
        }

private:
#ifdef DISPATCHIMPL_CREATESTDDISPATCH
    LPDISPATCH getIDispatch()
        {
            if (!_iDispatch)
            {
                LPTYPEINFO iTypeInfo;

                if (SUCCEEDED(
                        OutOfProcessServer::getTypeInfoOfGuid(
                                IID_T,
                                &iTypeInfo)))
                {
                    LPUNKNOWN iUnknown;

                    if (SUCCEEDED(
                            ::CreateStdDispatch(
                                    this,
                                    this,
                                    iTypeInfo,
                                    &iUnknown)))
                    {
                        LPDISPATCH iDispatch;

                        if (SUCCEEDED(
                                iUnknown->QueryInterface(
                                        IID_IDispatch,
                                        (PVOID *) &iDispatch)))
                            _iDispatch = iDispatch;
                        iUnknown->Release();
                    }
                    iTypeInfo->Release();
                }
            }
            return _iDispatch;
        }
#endif /* #ifdef DISPATCHIMPL_CREATESTDDISPATCH */

    LPDISPATCH _iDispatch;
};

#endif /* #ifndef _JMSOFFICECOMM_DISPATCHIMPL_H_ */