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
20 * TODO: Type coercion is implemented in variant.c but not called yet.
34 #include "winreg.h" /* for HKEY_LOCAL_MACHINE */
35 #include "winnls.h" /* for PRIMARYLANGID */
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
40 WINE_DECLARE_DEBUG_CHANNEL(typelib
);
42 static IDispatch
* WINAPI
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 * For each param, call DispGetParam to perform type coercion
81 FIXME("Coercion of arguments not implemented\n");
83 return ITypeInfo_Invoke(ptinfo
, _this
, dispidMember
, wFlags
,
84 pparams
, pvarResult
, pexcepinfo
, puArgErr
);
87 /******************************************************************************
88 * DispGetIDsOfNames (OLEAUT32.29)
90 * Convert a set of parameter names to DISPID's for DispInvoke().
94 * Failure: An HRESULT error code.
97 * This call defers to ITypeInfo_GetIDsOfNames(). The ITypeInfo interface passed
98 * as ptinfo contains the information to map names to DISPID's.
100 HRESULT WINAPI
DispGetIDsOfNames(
101 ITypeInfo
*ptinfo
, /* [in] Object's type info */
102 OLECHAR
**rgszNames
, /* [in] Array of names to get DISPID's for */
103 UINT cNames
, /* [in] Number of names in rgszNames */
104 DISPID
*rgdispid
) /* [out] Destination for converted DISPID's */
106 return ITypeInfo_GetIDsOfNames(ptinfo
, rgszNames
, cNames
, rgdispid
);
109 /******************************************************************************
110 * DispGetParam (OLEAUT32.28)
112 * Retrive a parameter from a DISPPARAMS structure and coerce it to the
113 * specified variant type.
116 * Coercion is done using system (0) locale.
120 * Failure: DISP_E_PARAMNOTFOUND, if position is invalid. or
121 * DISP_E_TYPEMISMATCH, if the coercion failed. puArgErr is
122 * set to the index of the argument in pdispparams.
124 HRESULT WINAPI
DispGetParam(
125 DISPPARAMS
*pdispparams
, /* [in] Parameter list */
126 UINT position
, /* [in] Position of parameter to coerce in pdispparams */
127 VARTYPE vtTarg
, /* [in] Type of value to coerce to */
128 VARIANT
*pvarResult
, /* [out] Destination for resulting variant */
129 UINT
*puArgErr
) /* [out] Destination for error code */
131 /* position is counted backwards */
135 TRACE("position=%d, cArgs=%d, cNamedArgs=%d\n",
136 position
, pdispparams
->cArgs
, pdispparams
->cNamedArgs
);
137 if (position
< pdispparams
->cArgs
) {
138 /* positional arg? */
139 pos
= pdispparams
->cArgs
- position
- 1;
141 /* FIXME: is this how to handle named args? */
142 for (pos
=0; pos
<pdispparams
->cNamedArgs
; pos
++)
143 if (pdispparams
->rgdispidNamedArgs
[pos
] == position
) break;
145 if (pos
==pdispparams
->cNamedArgs
)
146 return DISP_E_PARAMNOTFOUND
;
148 hr
= VariantChangeType(pvarResult
,
149 &pdispparams
->rgvarg
[pos
],
151 if (hr
== DISP_E_TYPEMISMATCH
) *puArgErr
= pos
;
155 /******************************************************************************
156 * CreateStdDispatch [OLEAUT32.32]
158 * Create and return a standard IDispatch object.
161 * Success: S_OK. ppunkStdDisp contains the new object.
162 * Failure: An HRESULT error code.
164 HRESULT WINAPI
CreateStdDispatch(
168 IUnknown
** ppunkStdDisp
)
170 TRACE("(%p, %p, %p, %p)\n", punkOuter
, pvThis
, ptinfo
, ppunkStdDisp
);
172 *ppunkStdDisp
= (LPUNKNOWN
)StdDispatch_Construct(punkOuter
, pvThis
, ptinfo
);
174 return E_OUTOFMEMORY
;
178 /******************************************************************************
179 * CreateDispTypeInfo [OLEAUT32.31]
181 * Build type information for an object so it can be called through an
182 * IDispatch interface.
185 * Success: S_OK. pptinfo contains the created ITypeInfo object.
186 * Failure: E_INVALIDARG, if one or more arguments is invalid.
189 * This call allows an objects methods to be accessed through IDispatch, by
190 * building an ITypeInfo object that IDispatch can use to call through.
192 HRESULT WINAPI
CreateDispTypeInfo(
193 INTERFACEDATA
*pidata
, /* [I] Description of the interface to build type info for */
194 LCID lcid
, /* [I] Locale Id */
195 ITypeInfo
**pptinfo
) /* [O] Destination for created ITypeInfo object */
197 FIXME("(%p,%ld,%p),stub\n",pidata
,lcid
,pptinfo
);
201 /******************************************************************************
202 * IDispatch {OLEAUT32}
205 * The IDispatch interface provides a single interface to dispatch method calls,
206 * regardless of whether the object to be called is in or out of process,
207 * local or remote (e.g. being called over a network). This interface is late-bound
208 * (linked at run-time), as opposed to early-bound (linked at compile time).
210 * The interface is used by objects that wish to called by scripting
211 * languages such as VBA, in order to minimise the amount of COM and C/C++
212 * knowledge required, or by objects that wish to live out of process from code
213 * that will call their methods.
215 * Method, property and parameter names can be localised. The details required to
216 * map names to methods and parameters are collected in a type library, usually
217 * output by an IDL compiler using the objects IDL description. This information is
218 * accessable programatically through the ITypeLib interface (for a type library),
219 * and the ITypeInfo interface (for an object within the type library). Type information
220 * can also be created at run-time using CreateDispTypeInfo().
223 * Instead of using IDispatch directly, there are several wrapper functions available
224 * to simplify the process of calling an objects methods through IDispatch.
226 * A standard implementation of an IDispatch object is created by calling
227 * CreateStdDispatch(). Numeric Id values for the parameters and methods (DISPID's)
228 * of an object of interest are retrieved by calling DispGetIDsOfNames(). DispGetParam()
229 * retrieves information about a particular parameter. Finally the DispInvoke()
230 * function is responsable for actually calling methods on an object.
237 ICOM_VFIELD(IDispatch
);
238 IUnknown
* outerUnknown
;
240 ITypeInfo
* pTypeInfo
;
244 /******************************************************************************
245 * IDispatch_QueryInterface {OLEAUT32}
247 * See IUnknown_QueryInterface.
249 static HRESULT WINAPI
StdDispatch_QueryInterface(
254 ICOM_THIS(StdDispatch
, iface
);
255 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_guid(riid
), ppvObject
);
257 if (This
->outerUnknown
)
258 return IUnknown_QueryInterface(This
->outerUnknown
, riid
, ppvObject
);
260 if (IsEqualIID(riid
, &IID_IDispatch
) ||
261 IsEqualIID(riid
, &IID_IUnknown
))
263 *ppvObject
= (LPVOID
)This
;
264 IUnknown_AddRef((LPUNKNOWN
)*ppvObject
);
267 return E_NOINTERFACE
;
270 /******************************************************************************
271 * IDispatch_AddRef {OLEAUT32}
273 * See IUnknown_AddRef.
275 static ULONG WINAPI
StdDispatch_AddRef(LPDISPATCH iface
)
277 ICOM_THIS(StdDispatch
, iface
);
280 if (This
->outerUnknown
)
281 return IUnknown_AddRef(This
->outerUnknown
);
286 /******************************************************************************
287 * IDispatch_Release {OLEAUT32}
289 * See IUnknown_Release.
291 static ULONG WINAPI
StdDispatch_Release(LPDISPATCH iface
)
293 ICOM_THIS(StdDispatch
, iface
);
295 TRACE("(%p)->()\n", This
);
299 if (This
->outerUnknown
)
300 ret
= IUnknown_Release(This
->outerUnknown
);
310 /******************************************************************************
311 * IDispatch_GetTypeInfoCount {OLEAUT32}
313 * Get the count of type information in an IDispatch interface.
316 * iface [I] IDispatch interface
317 * pctinfo [O] Destination for the count
320 * Success: S_OK. pctinfo is updated with the count. This is always 1 if
321 * the object provides type information, and 0 if it does not.
322 * Failure: E_NOTIMPL. The object does not provide type information.
325 * See IDispatch() and IDispatch_GetTypeInfo().
327 static HRESULT WINAPI
StdDispatch_GetTypeInfoCount(LPDISPATCH iface
, UINT
* pctinfo
)
329 ICOM_THIS(StdDispatch
, iface
);
330 TRACE("(%p)\n", pctinfo
);
332 *pctinfo
= This
->pTypeInfo
? 1 : 0;
336 /******************************************************************************
337 * IDispatch_GetTypeInfo {OLEAUT32}
339 * Get type information from an IDispatch interface.
342 * iface [I] IDispatch interface
343 * iTInfo [I] Index of type information.
344 * lcid [I] Locale of the type information to get
345 * ppTInfo [O] Destination for the ITypeInfo object
348 * Success: S_OK. ppTInfo is updated with the objects type information
349 * Failure: DISP_E_BADINDEX, if iTInfo is any value other than 0.
354 static HRESULT WINAPI
StdDispatch_GetTypeInfo(LPDISPATCH iface
, UINT iTInfo
, LCID lcid
, ITypeInfo
** ppTInfo
)
356 ICOM_THIS(StdDispatch
, iface
);
357 TRACE("(%d, %lx, %p)\n", iTInfo
, lcid
, ppTInfo
);
361 return DISP_E_BADINDEX
;
365 *ppTInfo
= This
->pTypeInfo
;
366 ITypeInfo_AddRef(*ppTInfo
);
371 /******************************************************************************
372 * IDispatch_GetIDsOfNames {OLEAUT32}
374 * Convert a methods name and an optional set of parameter names into DISPID's
375 * for passing to IDispatch_Invoke().
378 * iface [I] IDispatch interface
379 * riid [I] Reserved, set to IID_NULL
380 * rgszNames [I] Name to convert
381 * cNames [I] Number of names in rgszNames
382 * lcid [I] Locale of the type information to convert from
383 * rgDispId [O] Destination for converted DISPID's.
387 * Failure: DISP_E_UNKNOWNNAME, if any of the names is invalid.
388 * DISP_E_UNKNOWNLCID if lcid is invalid.
389 * Otherwise, an An HRESULT error code.
392 * This call defers to ITypeInfo_GetIDsOfNames(), using the ITypeInfo object
393 * contained within the IDispatch object.
394 * The first member of the names list must be a method name. The names following
395 * the method name are the parameters for that method.
397 static HRESULT WINAPI
StdDispatch_GetIDsOfNames(LPDISPATCH iface
, REFIID riid
, LPOLESTR
* rgszNames
, UINT cNames
, LCID lcid
, DISPID
* rgDispId
)
399 ICOM_THIS(StdDispatch
, iface
);
400 TRACE("(%s, %p, %d, 0x%lx, %p)\n", debugstr_guid(riid
), rgszNames
, cNames
, lcid
, rgDispId
);
402 if (!IsEqualGUID(riid
, &IID_NULL
))
404 FIXME(" expected riid == IID_NULL\n");
407 return DispGetIDsOfNames(This
->pTypeInfo
, rgszNames
, cNames
, rgDispId
);
410 /******************************************************************************
411 * IDispatch_Invoke {OLEAUT32}
413 * Call an object method.
416 * iface [I] IDispatch interface
417 * dispIdMember [I] DISPID of the method (from GetIDsOfNames())
418 * riid [I] Reserved, set to IID_NULL
419 * lcid [I] Locale of the type information to convert parameters with
420 * wFlags, [I] Kind of method call (DISPATCH_ flags from "oaidl.h")
421 * pDispParams [I] Array of method arguments
422 * pVarResult [O] Destination for the result of the call
423 * pExcepInfo [O] Destination for exception information
424 * puArgErr [O] Destination for bad argument
428 * Failure: See DispInvoke() for failure cases.
431 * See DispInvoke() and IDispatch().
433 static HRESULT WINAPI
StdDispatch_Invoke(LPDISPATCH iface
, DISPID dispIdMember
, REFIID riid
, LCID lcid
,
434 WORD wFlags
, DISPPARAMS
* pDispParams
, VARIANT
* pVarResult
,
435 EXCEPINFO
* pExcepInfo
, UINT
* puArgErr
)
437 ICOM_THIS(StdDispatch
, iface
);
438 TRACE("(%ld, %s, 0x%lx, 0x%x, %p, %p, %p, %p)\n", dispIdMember
, debugstr_guid(riid
), lcid
, wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
440 if (!IsEqualGUID(riid
, &IID_NULL
))
442 FIXME(" expected riid == IID_NULL\n");
445 return DispInvoke(This
->pvThis
, This
->pTypeInfo
, dispIdMember
, wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
448 static ICOM_VTABLE(IDispatch
) StdDispatch_VTable
=
450 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
451 StdDispatch_QueryInterface
,
454 StdDispatch_GetTypeInfoCount
,
455 StdDispatch_GetTypeInfo
,
456 StdDispatch_GetIDsOfNames
,
460 static IDispatch
* WINAPI
StdDispatch_Construct(
461 IUnknown
* punkOuter
,
463 ITypeInfo
* pTypeInfo
)
465 StdDispatch
* pStdDispatch
;
467 pStdDispatch
= CoTaskMemAlloc(sizeof(StdDispatch
));
469 return (IDispatch
*)pStdDispatch
;
471 pStdDispatch
->lpVtbl
= &StdDispatch_VTable
;
472 pStdDispatch
->outerUnknown
= punkOuter
;
473 pStdDispatch
->pvThis
= pvThis
;
474 pStdDispatch
->pTypeInfo
= pTypeInfo
;
475 pStdDispatch
->ref
= 1;
477 return (IDispatch
*)pStdDispatch
;