Fixed a couple of races with exiting threads in suspend_for_ptrace().
[wine/gsoc_dplay.git] / dlls / oleaut32 / dispatch.c
bloba3ff490e791ae5efe89b4c057637f9b13ca558b6
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * TODO: Type coercion is implemented in variant.c but not called yet.
23 #include "config.h"
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <ctype.h>
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 */
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.
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().
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().
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 */
77 /**
78 * TODO:
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().
92 * RETURNS
93 * Success: S_OK.
94 * Failure: An HRESULT error code.
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.
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.
115 * NOTES
116 * Coercion is done using system (0) locale.
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.
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 */
132 UINT pos;
133 HRESULT hr;
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;
145 if (pos==pdispparams->cNamedArgs)
146 return DISP_E_PARAMNOTFOUND;
148 hr = VariantChangeType(pvarResult,
149 &pdispparams->rgvarg[pos],
150 0, vtTarg);
151 if (hr == DISP_E_TYPEMISMATCH) *puArgErr = pos;
152 return hr;
155 /******************************************************************************
156 * CreateStdDispatch [OLEAUT32.32]
158 * Create and return a standard IDispatch object.
160 * RETURNS
161 * Success: S_OK. ppunkStdDisp contains the new object.
162 * Failure: An HRESULT error code.
164 HRESULT WINAPI CreateStdDispatch(
165 IUnknown* punkOuter,
166 void* pvThis,
167 ITypeInfo* ptinfo,
168 IUnknown** ppunkStdDisp)
170 TRACE("(%p, %p, %p, %p)\n", punkOuter, pvThis, ptinfo, ppunkStdDisp);
172 *ppunkStdDisp = (LPUNKNOWN)StdDispatch_Construct(punkOuter, pvThis, ptinfo);
173 if (!*ppunkStdDisp)
174 return E_OUTOFMEMORY;
175 return S_OK;
178 /******************************************************************************
179 * CreateDispTypeInfo [OLEAUT32.31]
181 * Build type information for an object so it can be called through an
182 * IDispatch interface.
184 * RETURNS
185 * Success: S_OK. pptinfo contains the created ITypeInfo object.
186 * Failure: E_INVALIDARG, if one or more arguments is invalid.
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.
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);
198 return 0;
201 /******************************************************************************
202 * IDispatch {OLEAUT32}
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).
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().
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.
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.
232 * METHODS
235 typedef struct
237 ICOM_VFIELD(IDispatch);
238 IUnknown * outerUnknown;
239 void * pvThis;
240 ITypeInfo * pTypeInfo;
241 ULONG ref;
242 } StdDispatch;
244 /******************************************************************************
245 * IDispatch_QueryInterface {OLEAUT32}
247 * See IUnknown_QueryInterface.
249 static HRESULT WINAPI StdDispatch_QueryInterface(
250 LPDISPATCH iface,
251 REFIID riid,
252 void** ppvObject)
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);
265 return S_OK;
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);
278 TRACE("()\n");
279 This->ref++;
280 if (This->outerUnknown)
281 return IUnknown_AddRef(This->outerUnknown);
282 else
283 return This->ref;
286 /******************************************************************************
287 * IDispatch_Release {OLEAUT32}
289 * See IUnknown_Release.
291 static ULONG WINAPI StdDispatch_Release(LPDISPATCH iface)
293 ICOM_THIS(StdDispatch, iface);
294 ULONG ret;
295 TRACE("(%p)->()\n", This);
297 This->ref--;
299 if (This->outerUnknown)
300 ret = IUnknown_Release(This->outerUnknown);
301 else
302 ret = This->ref;
304 if (This->ref <= 0)
305 CoTaskMemFree(This);
307 return ret;
310 /******************************************************************************
311 * IDispatch_GetTypeInfoCount {OLEAUT32}
313 * Get the count of type information in an IDispatch interface.
315 * PARAMS
316 * iface [I] IDispatch interface
317 * pctinfo [O] Destination for the count
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.
324 * NOTES
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;
333 return S_OK;
336 /******************************************************************************
337 * IDispatch_GetTypeInfo {OLEAUT32}
339 * Get type information from an IDispatch interface.
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
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.
351 * NOTES
352 * See IDispatch.
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);
359 *ppTInfo = NULL;
360 if (iTInfo != 0)
361 return DISP_E_BADINDEX;
363 if (This->pTypeInfo)
365 *ppTInfo = This->pTypeInfo;
366 ITypeInfo_AddRef(*ppTInfo);
368 return S_OK;
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().
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.
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.
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.
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");
405 return E_INVALIDARG;
407 return DispGetIDsOfNames(This->pTypeInfo, rgszNames, cNames, rgDispId);
410 /******************************************************************************
411 * IDispatch_Invoke {OLEAUT32}
413 * Call an object method.
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
426 * RETURNS
427 * Success: S_OK.
428 * Failure: See DispInvoke() for failure cases.
430 * NOTES
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");
443 return E_INVALIDARG;
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,
452 StdDispatch_AddRef,
453 StdDispatch_Release,
454 StdDispatch_GetTypeInfoCount,
455 StdDispatch_GetTypeInfo,
456 StdDispatch_GetIDsOfNames,
457 StdDispatch_Invoke
460 static IDispatch * WINAPI StdDispatch_Construct(
461 IUnknown * punkOuter,
462 void * pvThis,
463 ITypeInfo * pTypeInfo)
465 StdDispatch * pStdDispatch;
467 pStdDispatch = CoTaskMemAlloc(sizeof(StdDispatch));
468 if (!pStdDispatch)
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;