2 * Dispatch API functions
4 * Copyright 2000 Francois Jacques, Macadamian Technologies Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(ole);
42 static IDispatch * StdDispatch_Construct(IUnknown * punkOuter, void * pvThis, ITypeInfo * pTypeInfo);
44 /******************************************************************************
45 * DispInvoke (OLEAUT32.30)
47 * Call an object method using the information from its type library.
51 * Failure: Returns DISP_E_EXCEPTION and updates pexcepinfo if an exception occurs.
52 * DISP_E_BADPARAMCOUNT if the number of parameters is incorrect.
53 * DISP_E_MEMBERNOTFOUND if the method does not exist.
54 * puArgErr is updated if a parameter error (see notes) occurs.
55 * Otherwise, returns the result of calling ITypeInfo_Invoke().
58 * Parameter errors include the following:
60 *| E_INVALIDARG An argument was invalid
61 *| DISP_E_TYPEMISMATCH,
62 *| DISP_E_OVERFLOW An argument was valid but could not be coerced
63 *| DISP_E_PARAMNOTOPTIONAL A non optional parameter was not passed
64 *| DISP_E_PARAMNOTFOUND A parameter was passed that was not expected by the method
65 * This call defers to ITypeInfo_Invoke().
67 HRESULT WINAPI DispInvoke(
68 VOID *_this, /* [in] Object to call method on */
69 ITypeInfo *ptinfo, /* [in] Object type info */
70 DISPID dispidMember, /* [in] DISPID of the member (e.g. from GetIDsOfNames()) */
71 USHORT wFlags, /* [in] Kind of method call (DISPATCH_ flags from "oaidl.h") */
72 DISPPARAMS *pparams, /* [in] Array of method arguments */
73 VARIANT *pvarResult, /* [out] Destination for the result of the call */
74 EXCEPINFO *pexcepinfo, /* [out] Destination for exception information */
75 UINT *puArgErr) /* [out] Destination for bad argument */
79 return ITypeInfo_Invoke(ptinfo, _this, dispidMember, wFlags,
80 pparams, pvarResult, pexcepinfo, puArgErr);
83 /******************************************************************************
84 * DispGetIDsOfNames (OLEAUT32.29)
86 * Convert a set of parameter names to DISPIDs for DispInvoke().
90 * Failure: An HRESULT error code.
93 * This call defers to ITypeInfo_GetIDsOfNames(). The ITypeInfo interface passed
94 * as ptinfo contains the information to map names to DISPIDs.
96 HRESULT WINAPI DispGetIDsOfNames(
97 ITypeInfo *ptinfo, /* [in] Object's type info */
98 OLECHAR **rgszNames, /* [in] Array of names to get DISPIDs for */
99 UINT cNames, /* [in] Number of names in rgszNames */
100 DISPID *rgdispid) /* [out] Destination for converted DISPIDs */
102 return ITypeInfo_GetIDsOfNames(ptinfo, rgszNames, cNames, rgdispid);
105 /******************************************************************************
106 * DispGetParam (OLEAUT32.28)
108 * Retrieve a parameter from a DISPPARAMS structure and coerce it to the
109 * specified variant type.
112 * Coercion is done using system (0) locale.
116 * Failure: DISP_E_PARAMNOTFOUND, if position is invalid. or
117 * DISP_E_TYPEMISMATCH, if the coercion failed. puArgErr is
118 * set to the index of the argument in pdispparams.
120 HRESULT WINAPI DispGetParam(
121 DISPPARAMS *pdispparams, /* [in] Parameter list */
122 UINT position, /* [in] Position of parameter to coerce in pdispparams */
123 VARTYPE vtTarg, /* [in] Type of value to coerce to */
124 VARIANT *pvarResult, /* [out] Destination for resulting variant */
125 UINT *puArgErr) /* [out] Destination for error code */
127 /* position is counted backwards */
131 TRACE("position=%d, cArgs=%d, cNamedArgs=%d\n",
132 position, pdispparams->cArgs, pdispparams->cNamedArgs);
134 if (pdispparams->cArgs > 0 && !pdispparams->rgvarg)
140 if (position < pdispparams->cArgs) {
141 /* positional arg? */
142 pos = pdispparams->cArgs - position - 1;
144 /* FIXME: is this how to handle named args? */
145 for (pos=0; pos<pdispparams->cNamedArgs; pos++)
146 if (pdispparams->rgdispidNamedArgs[pos] == position) break;
148 if (pos==pdispparams->cNamedArgs)
149 return DISP_E_PARAMNOTFOUND;
151 hr = VariantChangeType(pvarResult,
152 &pdispparams->rgvarg[pos],
154 if (hr == DISP_E_TYPEMISMATCH) *puArgErr = pos;
158 /******************************************************************************
159 * CreateStdDispatch [OLEAUT32.32]
161 * Create and return a standard IDispatch object.
164 * Success: S_OK. ppunkStdDisp contains the new object.
165 * Failure: An HRESULT error code.
168 * Outer unknown appears to be completely ignored.
170 HRESULT WINAPI CreateStdDispatch(
174 IUnknown** ppunkStdDisp)
176 TRACE("(%p, %p, %p, %p)\n", punkOuter, pvThis, ptinfo, ppunkStdDisp);
178 *ppunkStdDisp = (LPUNKNOWN)StdDispatch_Construct(punkOuter, pvThis, ptinfo);
180 return E_OUTOFMEMORY;
185 /******************************************************************************
186 * IDispatch {OLEAUT32}
189 * The IDispatch interface provides a single interface to dispatch method calls,
190 * regardless of whether the object to be called is in or out of process,
191 * local or remote (e.g. being called over a network). This interface is late-bound
192 * (linked at run-time), as opposed to early-bound (linked at compile time).
194 * The interface is used by objects that wish to called by scripting
195 * languages such as VBA, in order to minimise the amount of COM and C/C++
196 * knowledge required, or by objects that wish to live out of process from code
197 * that will call their methods.
199 * Method, property and parameter names can be localised. The details required to
200 * map names to methods and parameters are collected in a type library, usually
201 * output by an IDL compiler using the objects IDL description. This information is
202 * accessible programmatically through the ITypeLib interface (for a type library),
203 * and the ITypeInfo interface (for an object within the type library). Type information
204 * can also be created at run-time using CreateDispTypeInfo().
207 * Instead of using IDispatch directly, there are several wrapper functions available
208 * to simplify the process of calling an objects methods through IDispatch.
210 * A standard implementation of an IDispatch object is created by calling
211 * CreateStdDispatch(). Numeric Id values for the parameters and methods (DISPIDs)
212 * of an object of interest are retrieved by calling DispGetIDsOfNames(). DispGetParam()
213 * retrieves information about a particular parameter. Finally the DispInvoke()
214 * function is responsible for actually calling methods on an object.
221 const IDispatchVtbl *lpVtbl;
223 ITypeInfo * pTypeInfo;
227 /******************************************************************************
228 * IDispatch_QueryInterface {OLEAUT32}
230 * See IUnknown_QueryInterface.
232 static HRESULT WINAPI StdDispatch_QueryInterface(
237 StdDispatch *This = (StdDispatch *)iface;
238 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppvObject);
240 if (IsEqualIID(riid, &IID_IDispatch) ||
241 IsEqualIID(riid, &IID_IUnknown))
244 IUnknown_AddRef((LPUNKNOWN)*ppvObject);
247 return E_NOINTERFACE;
250 /******************************************************************************
251 * IDispatch_AddRef {OLEAUT32}
253 * See IUnknown_AddRef.
255 static ULONG WINAPI StdDispatch_AddRef(LPDISPATCH iface)
257 StdDispatch *This = (StdDispatch *)iface;
258 ULONG refCount = InterlockedIncrement(&This->ref);
260 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
265 /******************************************************************************
266 * IDispatch_Release {OLEAUT32}
268 * See IUnknown_Release.
270 static ULONG WINAPI StdDispatch_Release(LPDISPATCH iface)
272 StdDispatch *This = (StdDispatch *)iface;
273 ULONG refCount = InterlockedDecrement(&This->ref);
275 TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
279 ITypeInfo_Release(This->pTypeInfo);
286 /******************************************************************************
287 * IDispatch_GetTypeInfoCount {OLEAUT32}
289 * Get the count of type information in an IDispatch interface.
292 * iface [I] IDispatch interface
293 * pctinfo [O] Destination for the count
296 * Success: S_OK. pctinfo is updated with the count. This is always 1 if
297 * the object provides type information, and 0 if it does not.
298 * Failure: E_NOTIMPL. The object does not provide type information.
301 * See IDispatch() and IDispatch_GetTypeInfo().
303 static HRESULT WINAPI StdDispatch_GetTypeInfoCount(LPDISPATCH iface, UINT * pctinfo)
305 StdDispatch *This = (StdDispatch *)iface;
306 TRACE("(%p)\n", pctinfo);
308 *pctinfo = This->pTypeInfo ? 1 : 0;
312 /******************************************************************************
313 * IDispatch_GetTypeInfo {OLEAUT32}
315 * Get type information from an IDispatch interface.
318 * iface [I] IDispatch interface
319 * iTInfo [I] Index of type information.
320 * lcid [I] Locale of the type information to get
321 * ppTInfo [O] Destination for the ITypeInfo object
324 * Success: S_OK. ppTInfo is updated with the objects type information
325 * Failure: DISP_E_BADINDEX, if iTInfo is any value other than 0.
330 static HRESULT WINAPI StdDispatch_GetTypeInfo(LPDISPATCH iface, UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
332 StdDispatch *This = (StdDispatch *)iface;
333 TRACE("(%d, %x, %p)\n", iTInfo, lcid, ppTInfo);
337 return DISP_E_BADINDEX;
341 *ppTInfo = This->pTypeInfo;
342 ITypeInfo_AddRef(*ppTInfo);
347 /******************************************************************************
348 * IDispatch_GetIDsOfNames {OLEAUT32}
350 * Convert a methods name and an optional set of parameter names into DISPIDs
351 * for passing to IDispatch_Invoke().
354 * iface [I] IDispatch interface
355 * riid [I] Reserved, set to IID_NULL
356 * rgszNames [I] Name to convert
357 * cNames [I] Number of names in rgszNames
358 * lcid [I] Locale of the type information to convert from
359 * rgDispId [O] Destination for converted DISPIDs.
363 * Failure: DISP_E_UNKNOWNNAME, if any of the names is invalid.
364 * DISP_E_UNKNOWNLCID if lcid is invalid.
365 * Otherwise, an HRESULT error code.
368 * This call defers to ITypeInfo_GetIDsOfNames(), using the ITypeInfo object
369 * contained within the IDispatch object.
370 * The first member of the names list must be a method name. The names following
371 * the method name are the parameters for that method.
373 static HRESULT WINAPI StdDispatch_GetIDsOfNames(LPDISPATCH iface, REFIID riid, LPOLESTR * rgszNames, UINT cNames, LCID lcid, DISPID * rgDispId)
375 StdDispatch *This = (StdDispatch *)iface;
376 TRACE("(%s, %p, %d, 0x%x, %p)\n", debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
378 if (!IsEqualGUID(riid, &IID_NULL))
380 FIXME(" expected riid == IID_NULL\n");
383 return DispGetIDsOfNames(This->pTypeInfo, rgszNames, cNames, rgDispId);
386 /******************************************************************************
387 * IDispatch_Invoke {OLEAUT32}
389 * Call an object method.
392 * iface [I] IDispatch interface
393 * dispIdMember [I] DISPID of the method (from GetIDsOfNames())
394 * riid [I] Reserved, set to IID_NULL
395 * lcid [I] Locale of the type information to convert parameters with
396 * wFlags, [I] Kind of method call (DISPATCH_ flags from "oaidl.h")
397 * pDispParams [I] Array of method arguments
398 * pVarResult [O] Destination for the result of the call
399 * pExcepInfo [O] Destination for exception information
400 * puArgErr [O] Destination for bad argument
404 * Failure: See DispInvoke() for failure cases.
407 * See DispInvoke() and IDispatch().
409 static HRESULT WINAPI StdDispatch_Invoke(LPDISPATCH iface, DISPID dispIdMember, REFIID riid, LCID lcid,
410 WORD wFlags, DISPPARAMS * pDispParams, VARIANT * pVarResult,
411 EXCEPINFO * pExcepInfo, UINT * puArgErr)
413 StdDispatch *This = (StdDispatch *)iface;
414 TRACE("(%d, %s, 0x%x, 0x%x, %p, %p, %p, %p)\n", dispIdMember, debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
416 if (!IsEqualGUID(riid, &IID_NULL))
418 FIXME(" expected riid == IID_NULL\n");
421 return DispInvoke(This->pvThis, This->pTypeInfo, dispIdMember, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
424 static const IDispatchVtbl StdDispatch_VTable =
426 StdDispatch_QueryInterface,
429 StdDispatch_GetTypeInfoCount,
430 StdDispatch_GetTypeInfo,
431 StdDispatch_GetIDsOfNames,
435 static IDispatch * StdDispatch_Construct(
436 IUnknown * punkOuter,
438 ITypeInfo * pTypeInfo)
440 StdDispatch * pStdDispatch;
442 pStdDispatch = CoTaskMemAlloc(sizeof(StdDispatch));
444 return (IDispatch *)pStdDispatch;
446 pStdDispatch->lpVtbl = &StdDispatch_VTable;
447 pStdDispatch->pvThis = pvThis;
448 pStdDispatch->pTypeInfo = pTypeInfo;
449 pStdDispatch->ref = 1;
451 /* we keep a reference to the type info so prevent it from
452 * being destroyed until we are done with it */
453 ITypeInfo_AddRef(pTypeInfo);
455 return (IDispatch *)pStdDispatch;