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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
38 #include "winnls.h" /* for PRIMARYLANGID */
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(ole);
44 static IDispatch * WINAPI StdDispatch_Construct(IUnknown * punkOuter, void * pvThis, ITypeInfo * pTypeInfo);
46 /******************************************************************************
47 * DispInvoke (OLEAUT32.30)
49 * Call an object method using the information from its type library.
53 * Failure: Returns DISP_E_EXCEPTION and updates pexcepinfo if an exception occurs.
54 * DISP_E_BADPARAMCOUNT if the number of parameters is incorrect.
55 * DISP_E_MEMBERNOTFOUND if the method does not exist.
56 * puArgErr is updated if a parameter error (see notes) occurs.
57 * Otherwise, returns the result of calling ITypeInfo_Invoke().
60 * Parameter errors include the following:
62 *| E_INVALIDARG An argument was invalid
63 *| DISP_E_TYPEMISMATCH,
64 *| DISP_E_OVERFLOW An argument was valid but could not be coerced
65 *| DISP_E_PARAMNOTOPTIONAL A non optional parameter was not passed
66 *| DISP_E_PARAMNOTFOUND A parameter was passed that was not expected by the method
67 * This call defers to ITypeInfo_Invoke().
69 HRESULT WINAPI DispInvoke(
70 VOID *_this, /* [in] Object to call method on */
71 ITypeInfo *ptinfo, /* [in] Object type info */
72 DISPID dispidMember, /* [in] DISPID of the member (e.g. from GetIDsOfNames()) */
73 USHORT wFlags, /* [in] Kind of method call (DISPATCH_ flags from "oaidl.h") */
74 DISPPARAMS *pparams, /* [in] Array of method arguments */
75 VARIANT *pvarResult, /* [out] Destination for the result of the call */
76 EXCEPINFO *pexcepinfo, /* [out] Destination for exception information */
77 UINT *puArgErr) /* [out] Destination for bad argument */
81 return ITypeInfo_Invoke(ptinfo, _this, dispidMember, wFlags,
82 pparams, pvarResult, pexcepinfo, puArgErr);
85 /******************************************************************************
86 * DispGetIDsOfNames (OLEAUT32.29)
88 * Convert a set of parameter names to DISPID's for DispInvoke().
92 * Failure: An HRESULT error code.
95 * This call defers to ITypeInfo_GetIDsOfNames(). The ITypeInfo interface passed
96 * as ptinfo contains the information to map names to DISPID's.
98 HRESULT WINAPI DispGetIDsOfNames(
99 ITypeInfo *ptinfo, /* [in] Object's type info */
100 OLECHAR **rgszNames, /* [in] Array of names to get DISPID's for */
101 UINT cNames, /* [in] Number of names in rgszNames */
102 DISPID *rgdispid) /* [out] Destination for converted DISPID's */
104 return ITypeInfo_GetIDsOfNames(ptinfo, rgszNames, cNames, rgdispid);
107 /******************************************************************************
108 * DispGetParam (OLEAUT32.28)
110 * Retrive a parameter from a DISPPARAMS structure and coerce it to the
111 * specified variant type.
114 * Coercion is done using system (0) locale.
118 * Failure: DISP_E_PARAMNOTFOUND, if position is invalid. or
119 * DISP_E_TYPEMISMATCH, if the coercion failed. puArgErr is
120 * set to the index of the argument in pdispparams.
122 HRESULT WINAPI DispGetParam(
123 DISPPARAMS *pdispparams, /* [in] Parameter list */
124 UINT position, /* [in] Position of parameter to coerce in pdispparams */
125 VARTYPE vtTarg, /* [in] Type of value to coerce to */
126 VARIANT *pvarResult, /* [out] Destination for resulting variant */
127 UINT *puArgErr) /* [out] Destination for error code */
129 /* position is counted backwards */
133 TRACE("position=%d, cArgs=%d, cNamedArgs=%d\n",
134 position, pdispparams->cArgs, pdispparams->cNamedArgs);
135 if (position < pdispparams->cArgs) {
136 /* positional arg? */
137 pos = pdispparams->cArgs - position - 1;
139 /* FIXME: is this how to handle named args? */
140 for (pos=0; pos<pdispparams->cNamedArgs; pos++)
141 if (pdispparams->rgdispidNamedArgs[pos] == position) break;
143 if (pos==pdispparams->cNamedArgs)
144 return DISP_E_PARAMNOTFOUND;
146 hr = VariantChangeType(pvarResult,
147 &pdispparams->rgvarg[pos],
149 if (hr == DISP_E_TYPEMISMATCH) *puArgErr = pos;
153 /******************************************************************************
154 * CreateStdDispatch [OLEAUT32.32]
156 * Create and return a standard IDispatch object.
159 * Success: S_OK. ppunkStdDisp contains the new object.
160 * Failure: An HRESULT error code.
163 * Outer unknown appears to be completely ignored.
165 HRESULT WINAPI CreateStdDispatch(
169 IUnknown** ppunkStdDisp)
171 TRACE("(%p, %p, %p, %p)\n", punkOuter, pvThis, ptinfo, ppunkStdDisp);
173 *ppunkStdDisp = (LPUNKNOWN)StdDispatch_Construct(punkOuter, pvThis, ptinfo);
175 return E_OUTOFMEMORY;
180 /******************************************************************************
181 * IDispatch {OLEAUT32}
184 * The IDispatch interface provides a single interface to dispatch method calls,
185 * regardless of whether the object to be called is in or out of process,
186 * local or remote (e.g. being called over a network). This interface is late-bound
187 * (linked at run-time), as opposed to early-bound (linked at compile time).
189 * The interface is used by objects that wish to called by scripting
190 * languages such as VBA, in order to minimise the amount of COM and C/C++
191 * knowledge required, or by objects that wish to live out of process from code
192 * that will call their methods.
194 * Method, property and parameter names can be localised. The details required to
195 * map names to methods and parameters are collected in a type library, usually
196 * output by an IDL compiler using the objects IDL description. This information is
197 * accessible programatically through the ITypeLib interface (for a type library),
198 * and the ITypeInfo interface (for an object within the type library). Type information
199 * can also be created at run-time using CreateDispTypeInfo().
202 * Instead of using IDispatch directly, there are several wrapper functions available
203 * to simplify the process of calling an objects methods through IDispatch.
205 * A standard implementation of an IDispatch object is created by calling
206 * CreateStdDispatch(). Numeric Id values for the parameters and methods (DISPID's)
207 * of an object of interest are retrieved by calling DispGetIDsOfNames(). DispGetParam()
208 * retrieves information about a particular parameter. Finally the DispInvoke()
209 * function is responsable for actually calling methods on an object.
216 const IDispatchVtbl *lpVtbl;
218 ITypeInfo * pTypeInfo;
222 /******************************************************************************
223 * IDispatch_QueryInterface {OLEAUT32}
225 * See IUnknown_QueryInterface.
227 static HRESULT WINAPI StdDispatch_QueryInterface(
232 StdDispatch *This = (StdDispatch *)iface;
233 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppvObject);
235 if (IsEqualIID(riid, &IID_IDispatch) ||
236 IsEqualIID(riid, &IID_IUnknown))
238 *ppvObject = (LPVOID)This;
239 IUnknown_AddRef((LPUNKNOWN)*ppvObject);
242 return E_NOINTERFACE;
245 /******************************************************************************
246 * IDispatch_AddRef {OLEAUT32}
248 * See IUnknown_AddRef.
250 static ULONG WINAPI StdDispatch_AddRef(LPDISPATCH iface)
252 StdDispatch *This = (StdDispatch *)iface;
253 ULONG refCount = InterlockedIncrement(&This->ref);
255 TRACE("(%p)->(ref before=%lu)\n",This, refCount - 1);
260 /******************************************************************************
261 * IDispatch_Release {OLEAUT32}
263 * See IUnknown_Release.
265 static ULONG WINAPI StdDispatch_Release(LPDISPATCH iface)
267 StdDispatch *This = (StdDispatch *)iface;
268 ULONG refCount = InterlockedDecrement(&This->ref);
270 TRACE("(%p)->(ref before=%lu)\n", This, refCount + 1);
274 ITypeInfo_Release(This->pTypeInfo);
281 /******************************************************************************
282 * IDispatch_GetTypeInfoCount {OLEAUT32}
284 * Get the count of type information in an IDispatch interface.
287 * iface [I] IDispatch interface
288 * pctinfo [O] Destination for the count
291 * Success: S_OK. pctinfo is updated with the count. This is always 1 if
292 * the object provides type information, and 0 if it does not.
293 * Failure: E_NOTIMPL. The object does not provide type information.
296 * See IDispatch() and IDispatch_GetTypeInfo().
298 static HRESULT WINAPI StdDispatch_GetTypeInfoCount(LPDISPATCH iface, UINT * pctinfo)
300 StdDispatch *This = (StdDispatch *)iface;
301 TRACE("(%p)\n", pctinfo);
303 *pctinfo = This->pTypeInfo ? 1 : 0;
307 /******************************************************************************
308 * IDispatch_GetTypeInfo {OLEAUT32}
310 * Get type information from an IDispatch interface.
313 * iface [I] IDispatch interface
314 * iTInfo [I] Index of type information.
315 * lcid [I] Locale of the type information to get
316 * ppTInfo [O] Destination for the ITypeInfo object
319 * Success: S_OK. ppTInfo is updated with the objects type information
320 * Failure: DISP_E_BADINDEX, if iTInfo is any value other than 0.
325 static HRESULT WINAPI StdDispatch_GetTypeInfo(LPDISPATCH iface, UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
327 StdDispatch *This = (StdDispatch *)iface;
328 TRACE("(%d, %lx, %p)\n", iTInfo, lcid, ppTInfo);
332 return DISP_E_BADINDEX;
336 *ppTInfo = This->pTypeInfo;
337 ITypeInfo_AddRef(*ppTInfo);
342 /******************************************************************************
343 * IDispatch_GetIDsOfNames {OLEAUT32}
345 * Convert a methods name and an optional set of parameter names into DISPID's
346 * for passing to IDispatch_Invoke().
349 * iface [I] IDispatch interface
350 * riid [I] Reserved, set to IID_NULL
351 * rgszNames [I] Name to convert
352 * cNames [I] Number of names in rgszNames
353 * lcid [I] Locale of the type information to convert from
354 * rgDispId [O] Destination for converted DISPID's.
358 * Failure: DISP_E_UNKNOWNNAME, if any of the names is invalid.
359 * DISP_E_UNKNOWNLCID if lcid is invalid.
360 * Otherwise, an An HRESULT error code.
363 * This call defers to ITypeInfo_GetIDsOfNames(), using the ITypeInfo object
364 * contained within the IDispatch object.
365 * The first member of the names list must be a method name. The names following
366 * the method name are the parameters for that method.
368 static HRESULT WINAPI StdDispatch_GetIDsOfNames(LPDISPATCH iface, REFIID riid, LPOLESTR * rgszNames, UINT cNames, LCID lcid, DISPID * rgDispId)
370 StdDispatch *This = (StdDispatch *)iface;
371 TRACE("(%s, %p, %d, 0x%lx, %p)\n", debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
373 if (!IsEqualGUID(riid, &IID_NULL))
375 FIXME(" expected riid == IID_NULL\n");
378 return DispGetIDsOfNames(This->pTypeInfo, rgszNames, cNames, rgDispId);
381 /******************************************************************************
382 * IDispatch_Invoke {OLEAUT32}
384 * Call an object method.
387 * iface [I] IDispatch interface
388 * dispIdMember [I] DISPID of the method (from GetIDsOfNames())
389 * riid [I] Reserved, set to IID_NULL
390 * lcid [I] Locale of the type information to convert parameters with
391 * wFlags, [I] Kind of method call (DISPATCH_ flags from "oaidl.h")
392 * pDispParams [I] Array of method arguments
393 * pVarResult [O] Destination for the result of the call
394 * pExcepInfo [O] Destination for exception information
395 * puArgErr [O] Destination for bad argument
399 * Failure: See DispInvoke() for failure cases.
402 * See DispInvoke() and IDispatch().
404 static HRESULT WINAPI StdDispatch_Invoke(LPDISPATCH iface, DISPID dispIdMember, REFIID riid, LCID lcid,
405 WORD wFlags, DISPPARAMS * pDispParams, VARIANT * pVarResult,
406 EXCEPINFO * pExcepInfo, UINT * puArgErr)
408 StdDispatch *This = (StdDispatch *)iface;
409 TRACE("(%ld, %s, 0x%lx, 0x%x, %p, %p, %p, %p)\n", dispIdMember, debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
411 if (!IsEqualGUID(riid, &IID_NULL))
413 FIXME(" expected riid == IID_NULL\n");
416 return DispInvoke(This->pvThis, This->pTypeInfo, dispIdMember, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
419 static const IDispatchVtbl StdDispatch_VTable =
421 StdDispatch_QueryInterface,
424 StdDispatch_GetTypeInfoCount,
425 StdDispatch_GetTypeInfo,
426 StdDispatch_GetIDsOfNames,
430 static IDispatch * WINAPI StdDispatch_Construct(
431 IUnknown * punkOuter,
433 ITypeInfo * pTypeInfo)
435 StdDispatch * pStdDispatch;
437 pStdDispatch = CoTaskMemAlloc(sizeof(StdDispatch));
439 return (IDispatch *)pStdDispatch;
441 pStdDispatch->lpVtbl = &StdDispatch_VTable;
442 pStdDispatch->pvThis = pvThis;
443 pStdDispatch->pTypeInfo = pTypeInfo;
444 pStdDispatch->ref = 1;
446 /* we keep a reference to the type info so prevent it from
447 * being destroyed until we are done with it */
448 ITypeInfo_AddRef(pTypeInfo);
450 return (IDispatch *)pStdDispatch;