ReactOS 0.4.17-dev-577-ge3e58ac
dispatch.c
Go to the documentation of this file.
1
22#include <stdlib.h>
23#include <string.h>
24#include <stdarg.h>
25#include <stdio.h>
26#include <ctype.h>
27
28#define COBJMACROS
29
30#include "windef.h"
31#include "winbase.h"
32#include "objbase.h"
33#include "oleauto.h"
34#include "winerror.h"
35
36#include "wine/debug.h"
37
39
40/******************************************************************************
41 * DispInvoke (OLEAUT32.30)
42 *
43 * Call an object method using the information from its type library.
44 *
45 * RETURNS
46 * Success: S_OK.
47 * Failure: Returns DISP_E_EXCEPTION and updates pexcepinfo if an exception occurs.
48 * DISP_E_BADPARAMCOUNT if the number of parameters is incorrect.
49 * DISP_E_MEMBERNOTFOUND if the method does not exist.
50 * puArgErr is updated if a parameter error (see notes) occurs.
51 * Otherwise, returns the result of calling ITypeInfo_Invoke().
52 *
53 * NOTES
54 * Parameter errors include the following:
55 *| DISP_E_BADVARTYPE
56 *| E_INVALIDARG An argument was invalid
57 *| DISP_E_TYPEMISMATCH,
58 *| DISP_E_OVERFLOW An argument was valid but could not be coerced
59 *| DISP_E_PARAMNOTOPTIONAL A non optional parameter was not passed
60 *| DISP_E_PARAMNOTFOUND A parameter was passed that was not expected by the method
61 * This call defers to ITypeInfo_Invoke().
62 */
64 VOID *_this, /* [in] Object to call method on */
65 ITypeInfo *ptinfo, /* [in] Object type info */
66 DISPID dispidMember, /* [in] DISPID of the member (e.g. from GetIDsOfNames()) */
67 USHORT wFlags, /* [in] Kind of method call (DISPATCH_ flags from "oaidl.h") */
68 DISPPARAMS *pparams, /* [in] Array of method arguments */
69 VARIANT *pvarResult, /* [out] Destination for the result of the call */
70 EXCEPINFO *pexcepinfo, /* [out] Destination for exception information */
71 UINT *puArgErr) /* [out] Destination for bad argument */
72{
73 TRACE("\n");
74
75 return ITypeInfo_Invoke(ptinfo, _this, dispidMember, wFlags,
76 pparams, pvarResult, pexcepinfo, puArgErr);
77}
78
79/******************************************************************************
80 * DispGetIDsOfNames (OLEAUT32.29)
81 *
82 * Convert a set of parameter names to DISPIDs for DispInvoke().
83 *
84 * RETURNS
85 * Success: S_OK.
86 * Failure: An HRESULT error code.
87 *
88 * NOTES
89 * This call defers to ITypeInfo_GetIDsOfNames(). The ITypeInfo interface passed
90 * as ptinfo contains the information to map names to DISPIDs.
91 */
93 ITypeInfo *ptinfo, /* [in] Object's type info */
94 OLECHAR **rgszNames, /* [in] Array of names to get DISPIDs for */
95 UINT cNames, /* [in] Number of names in rgszNames */
96 DISPID *rgdispid) /* [out] Destination for converted DISPIDs */
97{
98 return ITypeInfo_GetIDsOfNames(ptinfo, rgszNames, cNames, rgdispid);
99}
100
101/******************************************************************************
102 * DispGetParam (OLEAUT32.28)
103 *
104 * Retrieve a parameter from a DISPPARAMS structure and coerce it to the
105 * specified variant type.
106 *
107 * NOTES
108 * Coercion is done using system (0) locale.
109 *
110 * RETURNS
111 * Success: S_OK.
112 * Failure: DISP_E_PARAMNOTFOUND, if position is invalid. or
113 * DISP_E_TYPEMISMATCH, if the coercion failed. puArgErr is
114 * set to the index of the argument in pdispparams.
115 */
117 DISPPARAMS *pdispparams, /* [in] Parameter list */
118 UINT position, /* [in] Position of parameter to coerce in pdispparams */
119 VARTYPE vtTarg, /* [in] Type of value to coerce to */
120 VARIANT *pvarResult, /* [out] Destination for resulting variant */
121 UINT *puArgErr) /* [out] Destination for error code */
122{
123 /* position is counted backwards */
124 UINT pos;
125 HRESULT hr;
126
127 TRACE("position=%d, cArgs=%d, cNamedArgs=%d\n",
128 position, pdispparams->cArgs, pdispparams->cNamedArgs);
129
130 if (position < pdispparams->cArgs)
131 {
132 /* positional arg? */
133 pos = pdispparams->cArgs - position - 1;
134 }
135 else
136 {
137 /* FIXME: is this how to handle named args? */
138 for (pos=0; pos<pdispparams->cNamedArgs; pos++)
139 if (pdispparams->rgdispidNamedArgs[pos] == position) break;
140
141 if (pos==pdispparams->cNamedArgs)
143 }
144
145 if (pdispparams->cArgs > 0 && !pdispparams->rgvarg)
146 {
148 goto done;
149 }
150
151 if (!pvarResult)
152 {
154 goto done;
155 }
156
157 hr = VariantChangeType(pvarResult,
158 &pdispparams->rgvarg[pos],
159 0, vtTarg);
160
161done:
162 if (FAILED(hr))
163 *puArgErr = pos;
164
165 return hr;
166}
167
168
169/******************************************************************************
170 * IDispatch {OLEAUT32}
171 *
172 * NOTES
173 * The IDispatch interface provides a single interface to dispatch method calls,
174 * regardless of whether the object to be called is in or out of process,
175 * local or remote (e.g. being called over a network). This interface is late-bound
176 * (linked at run-time), as opposed to early-bound (linked at compile time).
177 *
178 * The interface is used by objects that wish to called by scripting
179 * languages such as VBA, in order to minimize the amount of COM and C/C++
180 * knowledge required, or by objects that wish to live out of process from code
181 * that will call their methods.
182 *
183 * Method, property and parameter names can be localised. The details required to
184 * map names to methods and parameters are collected in a type library, usually
185 * output by an IDL compiler using the objects IDL description. This information is
186 * accessible programmatically through the ITypeLib interface (for a type library),
187 * and the ITypeInfo interface (for an object within the type library). Type information
188 * can also be created at run-time using CreateDispTypeInfo().
189 *
190 * WRAPPERS
191 * Instead of using IDispatch directly, there are several wrapper functions available
192 * to simplify the process of calling an objects methods through IDispatch.
193 *
194 * A standard implementation of an IDispatch object is created by calling
195 * CreateStdDispatch(). Numeric Id values for the parameters and methods (DISPIDs)
196 * of an object of interest are retrieved by calling DispGetIDsOfNames(). DispGetParam()
197 * retrieves information about a particular parameter. Finally the DispInvoke()
198 * function is responsible for actually calling methods on an object.
199 *
200 * METHODS
201 */
202
203typedef struct
204{
206 void * pvThis;
210
212{
213 return CONTAINING_RECORD(iface, StdDispatch, IDispatch_iface);
214}
215
216/******************************************************************************
217 * IDispatch_QueryInterface {OLEAUT32}
218 *
219 * See IUnknown_QueryInterface.
220 */
222 LPDISPATCH iface,
223 REFIID riid,
224 void** ppvObject)
225{
227 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppvObject);
228
229 *ppvObject = NULL;
230
233 {
234 *ppvObject = iface;
235 IDispatch_AddRef(iface);
236 return S_OK;
237 }
238 return E_NOINTERFACE;
239}
240
241/******************************************************************************
242 * IDispatch_AddRef {OLEAUT32}
243 *
244 * See IUnknown_AddRef.
245 */
246static ULONG WINAPI StdDispatch_AddRef(LPDISPATCH iface)
247{
249 ULONG refCount = InterlockedIncrement(&This->ref);
250
251 TRACE("%p, refcount %lu.\n", iface, refCount);
252
253 return refCount;
254}
255
256/******************************************************************************
257 * IDispatch_Release {OLEAUT32}
258 *
259 * See IUnknown_Release.
260 */
261static ULONG WINAPI StdDispatch_Release(LPDISPATCH iface)
262{
264 ULONG refCount = InterlockedDecrement(&This->ref);
265
266 TRACE("%p, refcount %lu.\n", iface, refCount);
267
268 if (!refCount)
269 {
270 ITypeInfo_Release(This->pTypeInfo);
272 }
273
274 return refCount;
275}
276
277/******************************************************************************
278 * IDispatch_GetTypeInfoCount {OLEAUT32}
279 *
280 * Get the count of type information in an IDispatch interface.
281 *
282 * PARAMS
283 * iface [I] IDispatch interface
284 * pctinfo [O] Destination for the count
285 *
286 * RETURNS
287 * Success: S_OK. pctinfo is updated with the count. This is always 1 if
288 * the object provides type information, and 0 if it does not.
289 * Failure: E_NOTIMPL. The object does not provide type information.
290 *
291 * NOTES
292 * See IDispatch() and IDispatch_GetTypeInfo().
293 */
294static HRESULT WINAPI StdDispatch_GetTypeInfoCount(LPDISPATCH iface, UINT * pctinfo)
295{
296 TRACE("(%p)\n", pctinfo);
297 *pctinfo = 1;
298 return S_OK;
299}
300
301/******************************************************************************
302 * IDispatch_GetTypeInfo {OLEAUT32}
303 *
304 * Get type information from an IDispatch interface.
305 *
306 * PARAMS
307 * iface [I] IDispatch interface
308 * iTInfo [I] Index of type information.
309 * lcid [I] Locale of the type information to get
310 * ppTInfo [O] Destination for the ITypeInfo object
311 *
312 * RETURNS
313 * Success: S_OK. ppTInfo is updated with the objects type information
314 * Failure: DISP_E_BADINDEX, if iTInfo is any value other than 0.
315 *
316 * NOTES
317 * See IDispatch.
318 */
319static HRESULT WINAPI StdDispatch_GetTypeInfo(LPDISPATCH iface, UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
320{
322
323 TRACE("%d, %#lx, %p.\n", iTInfo, lcid, ppTInfo);
324
325 *ppTInfo = NULL;
326 if (iTInfo != 0)
327 return DISP_E_BADINDEX;
328
329 *ppTInfo = This->pTypeInfo;
330 ITypeInfo_AddRef(*ppTInfo);
331
332 return S_OK;
333}
334
335/******************************************************************************
336 * IDispatch_GetIDsOfNames {OLEAUT32}
337 *
338 * Convert a methods name and an optional set of parameter names into DISPIDs
339 * for passing to IDispatch_Invoke().
340 *
341 * PARAMS
342 * iface [I] IDispatch interface
343 * riid [I] Reserved, set to IID_NULL
344 * rgszNames [I] Name to convert
345 * cNames [I] Number of names in rgszNames
346 * lcid [I] Locale of the type information to convert from
347 * rgDispId [O] Destination for converted DISPIDs.
348 *
349 * RETURNS
350 * Success: S_OK.
351 * Failure: DISP_E_UNKNOWNNAME, if any of the names is invalid.
352 * DISP_E_UNKNOWNLCID if lcid is invalid.
353 * Otherwise, an HRESULT error code.
354 *
355 * NOTES
356 * This call defers to ITypeInfo_GetIDsOfNames(), using the ITypeInfo object
357 * contained within the IDispatch object.
358 * The first member of the names list must be a method name. The names following
359 * the method name are the parameters for that method.
360 */
361static HRESULT WINAPI StdDispatch_GetIDsOfNames(LPDISPATCH iface, REFIID riid, LPOLESTR * rgszNames, UINT cNames, LCID lcid, DISPID * rgDispId)
362{
364
365 TRACE("%s, %p, %d, %#lx, %p.\n", debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
366
367 if (!IsEqualGUID(riid, &IID_NULL))
368 {
369 FIXME(" expected riid == IID_NULL\n");
370 return E_INVALIDARG;
371 }
372 return DispGetIDsOfNames(This->pTypeInfo, rgszNames, cNames, rgDispId);
373}
374
375/******************************************************************************
376 * IDispatch_Invoke {OLEAUT32}
377 *
378 * Call an object method.
379 *
380 * PARAMS
381 * iface [I] IDispatch interface
382 * dispIdMember [I] DISPID of the method (from GetIDsOfNames())
383 * riid [I] Reserved, set to IID_NULL
384 * lcid [I] Locale of the type information to convert parameters with
385 * wFlags, [I] Kind of method call (DISPATCH_ flags from "oaidl.h")
386 * pDispParams [I] Array of method arguments
387 * pVarResult [O] Destination for the result of the call
388 * pExcepInfo [O] Destination for exception information
389 * puArgErr [O] Destination for bad argument
390 *
391 * RETURNS
392 * Success: S_OK.
393 * Failure: See DispInvoke() for failure cases.
394 *
395 * NOTES
396 * See DispInvoke() and IDispatch().
397 */
398static HRESULT WINAPI StdDispatch_Invoke(LPDISPATCH iface, DISPID dispIdMember, REFIID riid, LCID lcid,
399 WORD wFlags, DISPPARAMS * pDispParams, VARIANT * pVarResult,
400 EXCEPINFO * pExcepInfo, UINT * puArgErr)
401{
403
404 TRACE("%ld, %s, %#lx, 0x%x, %p, %p, %p, %p.\n", dispIdMember, debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
405
406 if (!IsEqualGUID(riid, &IID_NULL))
407 {
408 FIXME(" expected riid == IID_NULL\n");
409 return E_INVALIDARG;
410 }
411 return DispInvoke(This->pvThis, This->pTypeInfo, dispIdMember, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
412}
413
414static const IDispatchVtbl StdDispatch_VTable =
415{
423};
424
425/******************************************************************************
426 * CreateStdDispatch [OLEAUT32.32]
427 *
428 * Create and return a standard IDispatch object.
429 *
430 * RETURNS
431 * Success: S_OK. ppunkStdDisp contains the new object.
432 * Failure: An HRESULT error code.
433 *
434 * NOTES
435 * Outer unknown appears to be completely ignored.
436 */
438 IUnknown* punkOuter,
439 void* pvThis,
440 ITypeInfo* ptinfo,
441 IUnknown** stddisp)
442{
443 StdDispatch *pStdDispatch;
444
445 TRACE("(%p, %p, %p, %p)\n", punkOuter, pvThis, ptinfo, stddisp);
446
447 if (!pvThis || !ptinfo || !stddisp)
448 return E_INVALIDARG;
449
450 pStdDispatch = CoTaskMemAlloc(sizeof(StdDispatch));
451 if (!pStdDispatch)
452 return E_OUTOFMEMORY;
453
454 pStdDispatch->IDispatch_iface.lpVtbl = &StdDispatch_VTable;
455 pStdDispatch->pvThis = pvThis;
456 pStdDispatch->pTypeInfo = ptinfo;
457 pStdDispatch->ref = 1;
458
459 /* we keep a reference to the type info so prevent it from
460 * being destroyed until we are done with it */
461 ITypeInfo_AddRef(ptinfo);
462 *stddisp = (IUnknown*)&pStdDispatch->IDispatch_iface;
463
464 return S_OK;
465}
#define InterlockedIncrement
Definition: armddk.h:53
#define InterlockedDecrement
Definition: armddk.h:52
#define WINE_DEFAULT_DEBUG_CHANNEL(t)
Definition: precomp.h:23
#define FIXME(fmt,...)
Definition: precomp.h:53
const GUID IID_IUnknown
#define E_OUTOFMEMORY
Definition: ddrawi.h:100
#define E_INVALIDARG
Definition: ddrawi.h:101
HRESULT hr
Definition: delayimp.cpp:582
#define NULL
Definition: types.h:112
void *WINAPI CoTaskMemAlloc(SIZE_T size)
Definition: malloc.c:381
void WINAPI CoTaskMemFree(void *ptr)
Definition: malloc.c:389
WCHAR OLECHAR
Definition: compat.h:2292
unsigned short VARTYPE
Definition: compat.h:2254
LCID lcid
Definition: locale.c:5660
HRESULT WINAPI DispInvoke(VOID *_this, ITypeInfo *ptinfo, DISPID dispidMember, USHORT wFlags, DISPPARAMS *pparams, VARIANT *pvarResult, EXCEPINFO *pexcepinfo, UINT *puArgErr)
Definition: dispatch.c:63
static HRESULT WINAPI StdDispatch_GetTypeInfoCount(LPDISPATCH iface, UINT *pctinfo)
Definition: dispatch.c:294
static ULONG WINAPI StdDispatch_AddRef(LPDISPATCH iface)
Definition: dispatch.c:246
static const IDispatchVtbl StdDispatch_VTable
Definition: dispatch.c:414
static HRESULT WINAPI StdDispatch_Invoke(LPDISPATCH iface, DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
Definition: dispatch.c:398
static StdDispatch * impl_from_IDispatch(IDispatch *iface)
Definition: dispatch.c:211
HRESULT WINAPI DispGetIDsOfNames(ITypeInfo *ptinfo, OLECHAR **rgszNames, UINT cNames, DISPID *rgdispid)
Definition: dispatch.c:92
static HRESULT WINAPI StdDispatch_QueryInterface(LPDISPATCH iface, REFIID riid, void **ppvObject)
Definition: dispatch.c:221
static ULONG WINAPI StdDispatch_Release(LPDISPATCH iface)
Definition: dispatch.c:261
static HRESULT WINAPI StdDispatch_GetIDsOfNames(LPDISPATCH iface, REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
Definition: dispatch.c:361
HRESULT WINAPI DispGetParam(DISPPARAMS *pdispparams, UINT position, VARTYPE vtTarg, VARIANT *pvarResult, UINT *puArgErr)
Definition: dispatch.c:116
static HRESULT WINAPI StdDispatch_GetTypeInfo(LPDISPATCH iface, UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
Definition: dispatch.c:319
HRESULT WINAPI CreateStdDispatch(IUnknown *punkOuter, void *pvThis, ITypeInfo *ptinfo, IUnknown **stddisp)
Definition: dispatch.c:437
unsigned short WORD
Definition: ntddk_ex.h:93
unsigned int UINT
Definition: sysinfo.c:13
REFIID riid
Definition: atlbase.h:39
#define S_OK
Definition: intsafe.h:52
#define FAILED(hr)
Definition: intsafe.h:51
#define debugstr_guid
Definition: kernel32.h:35
static VARIANTARG static DISPID
Definition: ordinal.c:49
const GUID IID_IDispatch
long LONG
Definition: pedump.c:60
unsigned short USHORT
Definition: pedump.c:61
#define IsEqualGUID(rguid1, rguid2)
Definition: guiddef.h:147
#define IsEqualIID(riid1, riid2)
Definition: guiddef.h:95
#define REFIID
Definition: guiddef.h:118
#define IID_NULL
Definition: guiddef.h:98
DWORD LCID
Definition: nls.h:13
#define TRACE(s)
Definition: solgame.cpp:4
LONG ref
Definition: dispatch.c:208
IDispatch IDispatch_iface
Definition: dispatch.c:205
void * pvThis
Definition: dispatch.c:206
ITypeInfo * pTypeInfo
Definition: dispatch.c:207
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260
uint32_t ULONG
Definition: typedefs.h:59
HRESULT WINAPI DECLSPEC_HOTPATCH VariantChangeType(VARIANTARG *pvargDest, const VARIANTARG *pvargSrc, USHORT wFlags, VARTYPE vt)
Definition: variant.c:939
WINBASEAPI _In_ DWORD _Out_ _In_ WORD wFlags
Definition: wincon_undoc.h:337
#define WINAPI
Definition: msvc.h:6
#define DISP_E_PARAMNOTFOUND
Definition: winerror.h:3616
#define E_NOINTERFACE
Definition: winerror.h:3479
#define DISP_E_BADINDEX
Definition: winerror.h:3623