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