jscript: Get rid of no longer used jsdisp_propput_const.
[wine.git] / dlls / oleaut32 / dispatch.c
bloba666d3dba1ced239a70ce624a842261328dab7e9
1 /**
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
22 #include "config.h"
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <ctype.h>
30 #define COBJMACROS
32 #include "windef.h"
33 #include "winbase.h"
34 #include "objbase.h"
35 #include "oleauto.h"
36 #include "winerror.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(ole);
42 /******************************************************************************
43 * DispInvoke (OLEAUT32.30)
45 * Call an object method using the information from its type library.
47 * RETURNS
48 * Success: S_OK.
49 * Failure: Returns DISP_E_EXCEPTION and updates pexcepinfo if an exception occurs.
50 * DISP_E_BADPARAMCOUNT if the number of parameters is incorrect.
51 * DISP_E_MEMBERNOTFOUND if the method does not exist.
52 * puArgErr is updated if a parameter error (see notes) occurs.
53 * Otherwise, returns the result of calling ITypeInfo_Invoke().
55 * NOTES
56 * Parameter errors include the following:
57 *| DISP_E_BADVARTYPE
58 *| E_INVALIDARG An argument was invalid
59 *| DISP_E_TYPEMISMATCH,
60 *| DISP_E_OVERFLOW An argument was valid but could not be coerced
61 *| DISP_E_PARAMNOTOPTIONAL A non optional parameter was not passed
62 *| DISP_E_PARAMNOTFOUND A parameter was passed that was not expected by the method
63 * This call defers to ITypeInfo_Invoke().
65 HRESULT WINAPI DispInvoke(
66 VOID *_this, /* [in] Object to call method on */
67 ITypeInfo *ptinfo, /* [in] Object type info */
68 DISPID dispidMember, /* [in] DISPID of the member (e.g. from GetIDsOfNames()) */
69 USHORT wFlags, /* [in] Kind of method call (DISPATCH_ flags from "oaidl.h") */
70 DISPPARAMS *pparams, /* [in] Array of method arguments */
71 VARIANT *pvarResult, /* [out] Destination for the result of the call */
72 EXCEPINFO *pexcepinfo, /* [out] Destination for exception information */
73 UINT *puArgErr) /* [out] Destination for bad argument */
75 TRACE("\n");
77 return ITypeInfo_Invoke(ptinfo, _this, dispidMember, wFlags,
78 pparams, pvarResult, pexcepinfo, puArgErr);
81 /******************************************************************************
82 * DispGetIDsOfNames (OLEAUT32.29)
84 * Convert a set of parameter names to DISPIDs for DispInvoke().
86 * RETURNS
87 * Success: S_OK.
88 * Failure: An HRESULT error code.
90 * NOTES
91 * This call defers to ITypeInfo_GetIDsOfNames(). The ITypeInfo interface passed
92 * as ptinfo contains the information to map names to DISPIDs.
94 HRESULT WINAPI DispGetIDsOfNames(
95 ITypeInfo *ptinfo, /* [in] Object's type info */
96 OLECHAR **rgszNames, /* [in] Array of names to get DISPIDs for */
97 UINT cNames, /* [in] Number of names in rgszNames */
98 DISPID *rgdispid) /* [out] Destination for converted DISPIDs */
100 return ITypeInfo_GetIDsOfNames(ptinfo, rgszNames, cNames, rgdispid);
103 /******************************************************************************
104 * DispGetParam (OLEAUT32.28)
106 * Retrieve a parameter from a DISPPARAMS structure and coerce it to the
107 * specified variant type.
109 * NOTES
110 * Coercion is done using system (0) locale.
112 * RETURNS
113 * Success: S_OK.
114 * Failure: DISP_E_PARAMNOTFOUND, if position is invalid. or
115 * DISP_E_TYPEMISMATCH, if the coercion failed. puArgErr is
116 * set to the index of the argument in pdispparams.
118 HRESULT WINAPI DispGetParam(
119 DISPPARAMS *pdispparams, /* [in] Parameter list */
120 UINT position, /* [in] Position of parameter to coerce in pdispparams */
121 VARTYPE vtTarg, /* [in] Type of value to coerce to */
122 VARIANT *pvarResult, /* [out] Destination for resulting variant */
123 UINT *puArgErr) /* [out] Destination for error code */
125 /* position is counted backwards */
126 UINT pos;
127 HRESULT hr;
129 TRACE("position=%d, cArgs=%d, cNamedArgs=%d\n",
130 position, pdispparams->cArgs, pdispparams->cNamedArgs);
132 if (position < pdispparams->cArgs)
134 /* positional arg? */
135 pos = pdispparams->cArgs - position - 1;
137 else
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;
147 if (pdispparams->cArgs > 0 && !pdispparams->rgvarg)
149 hr = E_INVALIDARG;
150 goto done;
153 if (!pvarResult)
155 hr = E_INVALIDARG;
156 goto done;
159 hr = VariantChangeType(pvarResult,
160 &pdispparams->rgvarg[pos],
161 0, vtTarg);
163 done:
164 if (FAILED(hr))
165 *puArgErr = pos;
167 return hr;
171 /******************************************************************************
172 * IDispatch {OLEAUT32}
174 * NOTES
175 * The IDispatch interface provides a single interface to dispatch method calls,
176 * regardless of whether the object to be called is in or out of process,
177 * local or remote (e.g. being called over a network). This interface is late-bound
178 * (linked at run-time), as opposed to early-bound (linked at compile time).
180 * The interface is used by objects that wish to called by scripting
181 * languages such as VBA, in order to minimise the amount of COM and C/C++
182 * knowledge required, or by objects that wish to live out of process from code
183 * that will call their methods.
185 * Method, property and parameter names can be localised. The details required to
186 * map names to methods and parameters are collected in a type library, usually
187 * output by an IDL compiler using the objects IDL description. This information is
188 * accessible programmatically through the ITypeLib interface (for a type library),
189 * and the ITypeInfo interface (for an object within the type library). Type information
190 * can also be created at run-time using CreateDispTypeInfo().
192 * WRAPPERS
193 * Instead of using IDispatch directly, there are several wrapper functions available
194 * to simplify the process of calling an objects methods through IDispatch.
196 * A standard implementation of an IDispatch object is created by calling
197 * CreateStdDispatch(). Numeric Id values for the parameters and methods (DISPIDs)
198 * of an object of interest are retrieved by calling DispGetIDsOfNames(). DispGetParam()
199 * retrieves information about a particular parameter. Finally the DispInvoke()
200 * function is responsible for actually calling methods on an object.
202 * METHODS
205 typedef struct
207 IDispatch IDispatch_iface;
208 void * pvThis;
209 ITypeInfo * pTypeInfo;
210 LONG ref;
211 } StdDispatch;
213 static inline StdDispatch *impl_from_IDispatch(IDispatch *iface)
215 return CONTAINING_RECORD(iface, StdDispatch, IDispatch_iface);
218 /******************************************************************************
219 * IDispatch_QueryInterface {OLEAUT32}
221 * See IUnknown_QueryInterface.
223 static HRESULT WINAPI StdDispatch_QueryInterface(
224 LPDISPATCH iface,
225 REFIID riid,
226 void** ppvObject)
228 StdDispatch *This = impl_from_IDispatch(iface);
229 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppvObject);
231 *ppvObject = NULL;
233 if (IsEqualIID(riid, &IID_IDispatch) ||
234 IsEqualIID(riid, &IID_IUnknown))
236 *ppvObject = iface;
237 IDispatch_AddRef(iface);
238 return S_OK;
240 return E_NOINTERFACE;
243 /******************************************************************************
244 * IDispatch_AddRef {OLEAUT32}
246 * See IUnknown_AddRef.
248 static ULONG WINAPI StdDispatch_AddRef(LPDISPATCH iface)
250 StdDispatch *This = impl_from_IDispatch(iface);
251 ULONG refCount = InterlockedIncrement(&This->ref);
253 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
255 return refCount;
258 /******************************************************************************
259 * IDispatch_Release {OLEAUT32}
261 * See IUnknown_Release.
263 static ULONG WINAPI StdDispatch_Release(LPDISPATCH iface)
265 StdDispatch *This = impl_from_IDispatch(iface);
266 ULONG refCount = InterlockedDecrement(&This->ref);
268 TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
270 if (!refCount)
272 ITypeInfo_Release(This->pTypeInfo);
273 CoTaskMemFree(This);
276 return refCount;
279 /******************************************************************************
280 * IDispatch_GetTypeInfoCount {OLEAUT32}
282 * Get the count of type information in an IDispatch interface.
284 * PARAMS
285 * iface [I] IDispatch interface
286 * pctinfo [O] Destination for the count
288 * RETURNS
289 * Success: S_OK. pctinfo is updated with the count. This is always 1 if
290 * the object provides type information, and 0 if it does not.
291 * Failure: E_NOTIMPL. The object does not provide type information.
293 * NOTES
294 * See IDispatch() and IDispatch_GetTypeInfo().
296 static HRESULT WINAPI StdDispatch_GetTypeInfoCount(LPDISPATCH iface, UINT * pctinfo)
298 TRACE("(%p)\n", pctinfo);
299 *pctinfo = 1;
300 return S_OK;
303 /******************************************************************************
304 * IDispatch_GetTypeInfo {OLEAUT32}
306 * Get type information from an IDispatch interface.
308 * PARAMS
309 * iface [I] IDispatch interface
310 * iTInfo [I] Index of type information.
311 * lcid [I] Locale of the type information to get
312 * ppTInfo [O] Destination for the ITypeInfo object
314 * RETURNS
315 * Success: S_OK. ppTInfo is updated with the objects type information
316 * Failure: DISP_E_BADINDEX, if iTInfo is any value other than 0.
318 * NOTES
319 * See IDispatch.
321 static HRESULT WINAPI StdDispatch_GetTypeInfo(LPDISPATCH iface, UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
323 StdDispatch *This = impl_from_IDispatch(iface);
324 TRACE("(%d, %x, %p)\n", iTInfo, lcid, ppTInfo);
326 *ppTInfo = NULL;
327 if (iTInfo != 0)
328 return DISP_E_BADINDEX;
330 *ppTInfo = This->pTypeInfo;
331 ITypeInfo_AddRef(*ppTInfo);
333 return S_OK;
336 /******************************************************************************
337 * IDispatch_GetIDsOfNames {OLEAUT32}
339 * Convert a methods name and an optional set of parameter names into DISPIDs
340 * for passing to IDispatch_Invoke().
342 * PARAMS
343 * iface [I] IDispatch interface
344 * riid [I] Reserved, set to IID_NULL
345 * rgszNames [I] Name to convert
346 * cNames [I] Number of names in rgszNames
347 * lcid [I] Locale of the type information to convert from
348 * rgDispId [O] Destination for converted DISPIDs.
350 * RETURNS
351 * Success: S_OK.
352 * Failure: DISP_E_UNKNOWNNAME, if any of the names is invalid.
353 * DISP_E_UNKNOWNLCID if lcid is invalid.
354 * Otherwise, an HRESULT error code.
356 * NOTES
357 * This call defers to ITypeInfo_GetIDsOfNames(), using the ITypeInfo object
358 * contained within the IDispatch object.
359 * The first member of the names list must be a method name. The names following
360 * the method name are the parameters for that method.
362 static HRESULT WINAPI StdDispatch_GetIDsOfNames(LPDISPATCH iface, REFIID riid, LPOLESTR * rgszNames, UINT cNames, LCID lcid, DISPID * rgDispId)
364 StdDispatch *This = impl_from_IDispatch(iface);
365 TRACE("(%s, %p, %d, 0x%x, %p)\n", debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
367 if (!IsEqualGUID(riid, &IID_NULL))
369 FIXME(" expected riid == IID_NULL\n");
370 return E_INVALIDARG;
372 return DispGetIDsOfNames(This->pTypeInfo, rgszNames, cNames, rgDispId);
375 /******************************************************************************
376 * IDispatch_Invoke {OLEAUT32}
378 * Call an object method.
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
391 * RETURNS
392 * Success: S_OK.
393 * Failure: See DispInvoke() for failure cases.
395 * NOTES
396 * See DispInvoke() and IDispatch().
398 static HRESULT WINAPI StdDispatch_Invoke(LPDISPATCH iface, DISPID dispIdMember, REFIID riid, LCID lcid,
399 WORD wFlags, DISPPARAMS * pDispParams, VARIANT * pVarResult,
400 EXCEPINFO * pExcepInfo, UINT * puArgErr)
402 StdDispatch *This = impl_from_IDispatch(iface);
403 TRACE("(%d, %s, 0x%x, 0x%x, %p, %p, %p, %p)\n", dispIdMember, debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
405 if (!IsEqualGUID(riid, &IID_NULL))
407 FIXME(" expected riid == IID_NULL\n");
408 return E_INVALIDARG;
410 return DispInvoke(This->pvThis, This->pTypeInfo, dispIdMember, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
413 static const IDispatchVtbl StdDispatch_VTable =
415 StdDispatch_QueryInterface,
416 StdDispatch_AddRef,
417 StdDispatch_Release,
418 StdDispatch_GetTypeInfoCount,
419 StdDispatch_GetTypeInfo,
420 StdDispatch_GetIDsOfNames,
421 StdDispatch_Invoke
424 /******************************************************************************
425 * CreateStdDispatch [OLEAUT32.32]
427 * Create and return a standard IDispatch object.
429 * RETURNS
430 * Success: S_OK. ppunkStdDisp contains the new object.
431 * Failure: An HRESULT error code.
433 * NOTES
434 * Outer unknown appears to be completely ignored.
436 HRESULT WINAPI CreateStdDispatch(
437 IUnknown* punkOuter,
438 void* pvThis,
439 ITypeInfo* ptinfo,
440 IUnknown** stddisp)
442 StdDispatch *pStdDispatch;
444 TRACE("(%p, %p, %p, %p)\n", punkOuter, pvThis, ptinfo, stddisp);
446 if (!pvThis || !ptinfo || !stddisp)
447 return E_INVALIDARG;
449 pStdDispatch = CoTaskMemAlloc(sizeof(StdDispatch));
450 if (!pStdDispatch)
451 return E_OUTOFMEMORY;
453 pStdDispatch->IDispatch_iface.lpVtbl = &StdDispatch_VTable;
454 pStdDispatch->pvThis = pvThis;
455 pStdDispatch->pTypeInfo = ptinfo;
456 pStdDispatch->ref = 1;
458 /* we keep a reference to the type info so prevent it from
459 * being destroyed until we are done with it */
460 ITypeInfo_AddRef(ptinfo);
461 *stddisp = (IUnknown*)&pStdDispatch->IDispatch_iface;
463 return S_OK;