Fixed a race condition on RPC worker thread creation, and a typo.
[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 <stdio.h>
28 #include <ctype.h>
29
30 #include "windef.h"
31 #include "objbase.h"
32 #include "oleauto.h"
33 #include "winerror.h"
34 #include "winreg.h"         /* for HKEY_LOCAL_MACHINE */
35 #include "winnls.h"         /* for PRIMARYLANGID */
36
37 #include "wine/debug.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(ole);
40 WINE_DECLARE_DEBUG_CHANNEL(typelib);
41
42 static IDispatch * WINAPI 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     /**
78      * TODO:
79      * For each param, call DispGetParam to perform type coercion
80      */
81     FIXME("Coercion of arguments not implemented\n");
82
83     return ITypeInfo_Invoke(ptinfo, _this, dispidMember, wFlags,
84                             pparams, pvarResult, pexcepinfo, puArgErr);
85 }
86
87 /******************************************************************************
88  *              DispGetIDsOfNames (OLEAUT32.29)
89  *
90  * Convert a set of parameter names to DISPID's for DispInvoke().
91  *
92  * RETURNS
93  *  Success: S_OK.
94  *  Failure: An HRESULT error code.
95  *
96  * NOTES
97  *  This call defers to ITypeInfo_GetIDsOfNames(). The ITypeInfo interface passed
98  *  as ptinfo contains the information to map names to DISPID's.
99  */
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 */
105 {
106     return ITypeInfo_GetIDsOfNames(ptinfo, rgszNames, cNames, rgdispid);
107 }
108
109 /******************************************************************************
110  *              DispGetParam (OLEAUT32.28)
111  *
112  * Retrive a parameter from a DISPPARAMS structure and coerce it to the
113  * specified variant type.
114  *
115  * NOTES
116  *  Coercion is done using system (0) locale.
117  *
118  * RETURNS
119  *  Success: S_OK.
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.
123  */
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 */
130 {
131     /* position is counted backwards */
132     UINT pos;
133     HRESULT hr;
134
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;
140     } else {
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     hr = VariantChangeType(pvarResult,
149                            &pdispparams->rgvarg[pos],
150                            0, vtTarg);
151     if (hr == DISP_E_TYPEMISMATCH) *puArgErr = pos;
152     return hr;
153 }
154
155 /******************************************************************************
156  * CreateStdDispatch [OLEAUT32.32]
157  *
158  * Create and return a standard IDispatch object.
159  *
160  * RETURNS
161  *  Success: S_OK. ppunkStdDisp contains the new object.
162  *  Failure: An HRESULT error code.
163  */
164 HRESULT WINAPI CreateStdDispatch(
165         IUnknown* punkOuter,
166         void* pvThis,
167         ITypeInfo* ptinfo,
168         IUnknown** ppunkStdDisp)
169 {
170     TRACE("(%p, %p, %p, %p)\n", punkOuter, pvThis, ptinfo, ppunkStdDisp);
171
172     *ppunkStdDisp = (LPUNKNOWN)StdDispatch_Construct(punkOuter, pvThis, ptinfo);
173     if (!*ppunkStdDisp)
174         return E_OUTOFMEMORY;
175     return S_OK;
176 }
177
178 /******************************************************************************
179  * CreateDispTypeInfo [OLEAUT32.31]
180  *
181  * Build type information for an object so it can be called through an
182  * IDispatch interface.
183  *
184  * RETURNS
185  *  Success: S_OK. pptinfo contains the created ITypeInfo object.
186  *  Failure: E_INVALIDARG, if one or more arguments is invalid.
187  *
188  * NOTES
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.
191  */
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 */
196 {
197         FIXME("(%p,%ld,%p),stub\n",pidata,lcid,pptinfo);
198         return 0;
199 }
200
201 /******************************************************************************
202  * IDispatch {OLEAUT32}
203  *
204  * NOTES
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).
209  *
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.
214  *
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().
221  *
222  * WRAPPERS
223  *  Instead of using IDispatch directly, there are several wrapper functions available
224  *  to simplify the process of calling an objects methods through IDispatch.
225  *
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.
231  *
232  * METHODS
233  */
234
235 typedef struct
236 {
237     ICOM_VFIELD(IDispatch);
238     IUnknown * outerUnknown;
239     void * pvThis;
240     ITypeInfo * pTypeInfo;
241     ULONG ref;
242 } StdDispatch;
243
244 /******************************************************************************
245  * IDispatch_QueryInterface {OLEAUT32}
246  *
247  * See IUnknown_QueryInterface.
248  */
249 static HRESULT WINAPI StdDispatch_QueryInterface(
250   LPDISPATCH iface,
251   REFIID riid,
252   void** ppvObject)
253 {
254     ICOM_THIS(StdDispatch, iface);
255     TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppvObject);
256
257     if (This->outerUnknown)
258         return IUnknown_QueryInterface(This->outerUnknown, riid, ppvObject);
259
260     if (IsEqualIID(riid, &IID_IDispatch) ||
261         IsEqualIID(riid, &IID_IUnknown))
262     {
263         *ppvObject = (LPVOID)This;
264         IUnknown_AddRef((LPUNKNOWN)*ppvObject);
265         return S_OK;
266     }
267     return E_NOINTERFACE;
268 }
269
270 /******************************************************************************
271  * IDispatch_AddRef {OLEAUT32}
272  *
273  * See IUnknown_AddRef.
274  */
275 static ULONG WINAPI StdDispatch_AddRef(LPDISPATCH iface)
276 {
277     ICOM_THIS(StdDispatch, iface);
278     TRACE("()\n");
279     This->ref++;
280     if (This->outerUnknown)
281         return IUnknown_AddRef(This->outerUnknown);
282     else
283         return This->ref;
284 }
285
286 /******************************************************************************
287  * IDispatch_Release {OLEAUT32}
288  *
289  * See IUnknown_Release.
290  */
291 static ULONG WINAPI StdDispatch_Release(LPDISPATCH iface)
292 {
293     ICOM_THIS(StdDispatch, iface);
294     ULONG ret;
295     TRACE("(%p)->()\n", This);
296
297     This->ref--;
298
299     if (This->outerUnknown)
300         ret = IUnknown_Release(This->outerUnknown);
301     else
302         ret = This->ref;
303
304     if (This->ref <= 0)
305         CoTaskMemFree(This);
306
307     return ret;
308 }
309
310 /******************************************************************************
311  * IDispatch_GetTypeInfoCount {OLEAUT32}
312  *
313  * Get the count of type information in an IDispatch interface.
314  *
315  * PARAMS
316  *  iface   [I] IDispatch interface
317  *  pctinfo [O] Destination for the count
318  *
319  * RETURNS
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.
323  *
324  * NOTES
325  *  See IDispatch() and IDispatch_GetTypeInfo().
326  */
327 static HRESULT WINAPI StdDispatch_GetTypeInfoCount(LPDISPATCH iface, UINT * pctinfo)
328 {
329     ICOM_THIS(StdDispatch, iface);
330     TRACE("(%p)\n", pctinfo);
331
332     *pctinfo = This->pTypeInfo ? 1 : 0;
333     return S_OK;
334 }
335
336 /******************************************************************************
337  * IDispatch_GetTypeInfo {OLEAUT32}
338  *
339  * Get type information from an IDispatch interface.
340  *
341  * PARAMS
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
346  *
347  * RETURNS
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.
350  *
351  * NOTES
352  *  See IDispatch.
353  */
354 static HRESULT WINAPI StdDispatch_GetTypeInfo(LPDISPATCH iface, UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
355 {
356     ICOM_THIS(StdDispatch, iface);
357     TRACE("(%d, %lx, %p)\n", iTInfo, lcid, ppTInfo);
358
359     *ppTInfo = NULL;
360     if (iTInfo != 0)
361         return DISP_E_BADINDEX;
362
363     if (This->pTypeInfo)
364     {
365       *ppTInfo = This->pTypeInfo;
366       ITypeInfo_AddRef(*ppTInfo);
367     }
368     return S_OK;
369 }
370
371 /******************************************************************************
372  * IDispatch_GetIDsOfNames {OLEAUT32}
373  *
374  * Convert a methods name and an optional set of parameter names into DISPID's
375  * for passing to IDispatch_Invoke().
376  *
377  * PARAMS
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.
384  *
385  * RETURNS
386  *  Success: S_OK.
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.
390  *
391  * NOTES
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.
396  */
397 static HRESULT WINAPI StdDispatch_GetIDsOfNames(LPDISPATCH iface, REFIID riid, LPOLESTR * rgszNames, UINT cNames, LCID lcid, DISPID * rgDispId)
398 {
399     ICOM_THIS(StdDispatch, iface);
400     TRACE("(%s, %p, %d, 0x%lx, %p)\n", debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
401
402     if (!IsEqualGUID(riid, &IID_NULL))
403     {
404         FIXME(" expected riid == IID_NULL\n");
405         return E_INVALIDARG;
406     }
407     return DispGetIDsOfNames(This->pTypeInfo, rgszNames, cNames, rgDispId);
408 }
409
410 /******************************************************************************
411  * IDispatch_Invoke {OLEAUT32}
412  *
413  * Call an object method.
414  *
415  * PARAMS
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
425  *
426  * RETURNS
427  *  Success: S_OK.
428  *  Failure: See DispInvoke() for failure cases.
429  *
430  * NOTES
431  *  See DispInvoke() and IDispatch().
432  */
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)
436 {
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);
439
440     if (!IsEqualGUID(riid, &IID_NULL))
441     {
442         FIXME(" expected riid == IID_NULL\n");
443         return E_INVALIDARG;
444     }
445     return DispInvoke(This->pvThis, This->pTypeInfo, dispIdMember, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
446 }
447
448 static ICOM_VTABLE(IDispatch) StdDispatch_VTable =
449 {
450   ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
451   StdDispatch_QueryInterface,
452   StdDispatch_AddRef,
453   StdDispatch_Release,
454   StdDispatch_GetTypeInfoCount,
455   StdDispatch_GetTypeInfo,
456   StdDispatch_GetIDsOfNames,
457   StdDispatch_Invoke
458 };
459
460 static IDispatch * WINAPI StdDispatch_Construct(
461   IUnknown * punkOuter,
462   void * pvThis,
463   ITypeInfo * pTypeInfo)
464 {
465     StdDispatch * pStdDispatch;
466
467     pStdDispatch = CoTaskMemAlloc(sizeof(StdDispatch));
468     if (!pStdDispatch)
469         return (IDispatch *)pStdDispatch;
470
471     pStdDispatch->lpVtbl = &StdDispatch_VTable;
472     pStdDispatch->outerUnknown = punkOuter;
473     pStdDispatch->pvThis = pvThis;
474     pStdDispatch->pTypeInfo = pTypeInfo;
475     pStdDispatch->ref = 1;
476
477     return (IDispatch *)pStdDispatch;
478 }