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