dispatch.c 15.1 KB
Newer Older
1 2 3 4 5
/**
 * Dispatch API functions
 *
 * Copyright 2000  Francois Jacques, Macadamian Technologies Inc.
 *
6 7 8 9 10 11 12 13 14 15 16 17
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 23
#include "config.h"

24 25
#include <stdlib.h>
#include <string.h>
26
#include <stdarg.h>
27 28
#include <stdio.h>
#include <ctype.h>
29

30 31
#define COBJMACROS

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
 *
Jon Griffiths's avatar
Jon Griffiths committed
47
 * Call an object method using the information from its type library.
48 49
 *
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
50 51 52 53 54 55
 *  Success: S_OK.
 *  Failure: Returns DISP_E_EXCEPTION and updates pexcepinfo if an exception occurs.
 *           DISP_E_BADPARAMCOUNT if the number of parameters is incorrect.
 *           DISP_E_MEMBERNOTFOUND if the method does not exist.
 *           puArgErr is updated if a parameter error (see notes) occurs.
 *           Otherwise, returns the result of calling ITypeInfo_Invoke().
56
 *
Jon Griffiths's avatar
Jon Griffiths committed
57 58 59 60 61 62 63 64 65
 * NOTES
 *  Parameter errors include the following:
 *| DISP_E_BADVARTYPE
 *| E_INVALIDARG            An argument was invalid
 *| DISP_E_TYPEMISMATCH,
 *| DISP_E_OVERFLOW         An argument was valid but could not be coerced
 *| DISP_E_PARAMNOTOPTIONAL A non optional parameter was not passed
 *| DISP_E_PARAMNOTFOUND    A parameter was passed that was not expected by the method
 *  This call defers to ITypeInfo_Invoke().
66
 */
67
HRESULT WINAPI DispInvoke(
Jon Griffiths's avatar
Jon Griffiths committed
68 69 70 71 72 73 74 75
	VOID       *_this,        /* [in] Object to call method on */
	ITypeInfo  *ptinfo,       /* [in] Object type info */
	DISPID      dispidMember, /* [in] DISPID of the member (e.g. from GetIDsOfNames()) */
	USHORT      wFlags,       /* [in] Kind of method call (DISPATCH_ flags from "oaidl.h") */
	DISPPARAMS *pparams,      /* [in] Array of method arguments */
	VARIANT    *pvarResult,   /* [out] Destination for the result of the call */
	EXCEPINFO  *pexcepinfo,   /* [out] Destination for exception information */
	UINT       *puArgErr)     /* [out] Destination for bad argument */
76
{
77
    TRACE("\n");
78

Jon Griffiths's avatar
Jon Griffiths committed
79 80
    return ITypeInfo_Invoke(ptinfo, _this, dispidMember, wFlags,
                            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
Jon Griffiths's avatar
Jon Griffiths committed
89 90
 *  Success: S_OK.
 *  Failure: An HRESULT error code.
91
 *
Jon Griffiths's avatar
Jon Griffiths committed
92 93
 * NOTES
 *  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(
Jon Griffiths's avatar
Jon Griffiths committed
97
	ITypeInfo  *ptinfo,    /* [in] Object's type info */
98
	OLECHAR   **rgszNames, /* [in] Array of names to get DISPIDs for */
Jon Griffiths's avatar
Jon Griffiths committed
99
	UINT        cNames,    /* [in] Number of names in rgszNames */
100
	DISPID     *rgdispid)  /* [out] Destination for converted DISPIDs */
101
{
Jon Griffiths's avatar
Jon Griffiths committed
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
Jon Griffiths's avatar
Jon Griffiths committed
109
 * specified variant type.
110 111
 *
 * NOTES
Jon Griffiths's avatar
Jon Griffiths committed
112
 *  Coercion is done using system (0) locale.
113 114
 *
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
115 116 117 118
 *  Success: S_OK.
 *  Failure: DISP_E_PARAMNOTFOUND, if position is invalid. or
 *           DISP_E_TYPEMISMATCH, if the coercion failed. puArgErr is
 *           set to the index of the argument in pdispparams.
119
 */
120
HRESULT WINAPI DispGetParam(
Jon Griffiths's avatar
Jon Griffiths committed
121 122 123 124 125
	DISPPARAMS *pdispparams, /* [in] Parameter list */
	UINT        position,    /* [in] Position of parameter to coerce in pdispparams */
	VARTYPE     vtTarg,      /* [in] Type of value to coerce to */
	VARIANT    *pvarResult,  /* [out] Destination for resulting variant */
	UINT       *puArgErr)    /* [out] Destination for error code */
126
{
Ove Kaaven's avatar
Ove Kaaven committed
127 128 129 130 131 132
    /* position is counted backwards */
    UINT pos;
    HRESULT hr;

    TRACE("position=%d, cArgs=%d, cNamedArgs=%d\n",
          position, pdispparams->cArgs, pdispparams->cNamedArgs);
133

134 135
    if (position < pdispparams->cArgs)
    {
Ove Kaaven's avatar
Ove Kaaven committed
136 137
      /* positional arg? */
      pos = pdispparams->cArgs - position - 1;
138 139 140
    }
    else
    {
Ove Kaaven's avatar
Ove Kaaven committed
141 142 143 144 145 146 147
      /* FIXME: is this how to handle named args? */
      for (pos=0; pos<pdispparams->cNamedArgs; pos++)
        if (pdispparams->rgdispidNamedArgs[pos] == position) break;

      if (pos==pdispparams->cNamedArgs)
        return DISP_E_PARAMNOTFOUND;
    }
148 149 150 151 152 153 154 155 156 157 158 159 160

    if (pdispparams->cArgs > 0 && !pdispparams->rgvarg)
    {
        hr = E_INVALIDARG;
        goto done;
    }

    if (!pvarResult)
    {
        hr = E_INVALIDARG;
        goto done;
    }

Ove Kaaven's avatar
Ove Kaaven committed
161 162 163
    hr = VariantChangeType(pvarResult,
                           &pdispparams->rgvarg[pos],
                           0, vtTarg);
164 165 166 167 168

done:
    if (FAILED(hr))
        *puArgErr = pos;

Ove Kaaven's avatar
Ove Kaaven committed
169
    return hr;
170
}
171 172 173

/******************************************************************************
 * CreateStdDispatch [OLEAUT32.32]
Jon Griffiths's avatar
Jon Griffiths committed
174 175 176 177 178 179
 *
 * Create and return a standard IDispatch object.
 *
 * RETURNS
 *  Success: S_OK. ppunkStdDisp contains the new object.
 *  Failure: An HRESULT error code.
180 181 182
 *
 * NOTES
 *  Outer unknown appears to be completely ignored.
183 184 185 186 187 188 189
 */
HRESULT WINAPI CreateStdDispatch(
        IUnknown* punkOuter,
        void* pvThis,
	ITypeInfo* ptinfo,
	IUnknown** ppunkStdDisp)
{
190 191 192 193 194 195
    TRACE("(%p, %p, %p, %p)\n", punkOuter, pvThis, ptinfo, ppunkStdDisp);

    *ppunkStdDisp = (LPUNKNOWN)StdDispatch_Construct(punkOuter, pvThis, ptinfo);
    if (!*ppunkStdDisp)
        return E_OUTOFMEMORY;
    return S_OK;
196 197
}

198

Jon Griffiths's avatar
Jon Griffiths committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
/******************************************************************************
 * IDispatch {OLEAUT32}
 *
 * NOTES
 *  The IDispatch interface provides a single interface to dispatch method calls,
 *  regardless of whether the object to be called is in or out of process,
 *  local or remote (e.g. being called over a network). This interface is late-bound
 *  (linked at run-time), as opposed to early-bound (linked at compile time).
 *
 *  The interface is used by objects that wish to called by scripting
 *  languages such as VBA, in order to minimise the amount of COM and C/C++
 *  knowledge required, or by objects that wish to live out of process from code
 *  that will call their methods.
 *
 *  Method, property and parameter names can be localised. The details required to
 *  map names to methods and parameters are collected in a type library, usually
 *  output by an IDL compiler using the objects IDL description. This information is
216
 *  accessible programmatically through the ITypeLib interface (for a type library),
Jon Griffiths's avatar
Jon Griffiths committed
217 218 219 220 221 222 223 224
 *  and the ITypeInfo interface (for an object within the type library). Type information
 *  can also be created at run-time using CreateDispTypeInfo().
 *
 * WRAPPERS
 *  Instead of using IDispatch directly, there are several wrapper functions available
 *  to simplify the process of calling an objects methods through IDispatch.
 *
 *  A standard implementation of an IDispatch object is created by calling
225
 *  CreateStdDispatch(). Numeric Id values for the parameters and methods (DISPIDs)
Jon Griffiths's avatar
Jon Griffiths committed
226 227
 *  of an object of interest are retrieved by calling DispGetIDsOfNames(). DispGetParam()
 *  retrieves information about a particular parameter. Finally the DispInvoke()
228
 *  function is responsible for actually calling methods on an object.
Jon Griffiths's avatar
Jon Griffiths committed
229 230 231 232
 *
 * METHODS
 */

233 234
typedef struct
{
235
    const IDispatchVtbl *lpVtbl;
236 237
    void * pvThis;
    ITypeInfo * pTypeInfo;
238
    LONG ref;
239 240
} StdDispatch;

Jon Griffiths's avatar
Jon Griffiths committed
241 242 243 244 245
/******************************************************************************
 * IDispatch_QueryInterface {OLEAUT32}
 *
 * See IUnknown_QueryInterface.
 */
246 247 248 249 250
static HRESULT WINAPI StdDispatch_QueryInterface(
  LPDISPATCH iface,
  REFIID riid,
  void** ppvObject)
{
251
    StdDispatch *This = (StdDispatch *)iface;
252 253 254 255 256
    TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppvObject);

    if (IsEqualIID(riid, &IID_IDispatch) ||
        IsEqualIID(riid, &IID_IUnknown))
    {
257
        *ppvObject = This;
258 259
        IUnknown_AddRef((LPUNKNOWN)*ppvObject);
        return S_OK;
260 261 262 263
    }
    return E_NOINTERFACE;
}

Jon Griffiths's avatar
Jon Griffiths committed
264 265 266 267 268
/******************************************************************************
 * IDispatch_AddRef {OLEAUT32}
 *
 * See IUnknown_AddRef.
 */
269 270
static ULONG WINAPI StdDispatch_AddRef(LPDISPATCH iface)
{
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
}

Jon Griffiths's avatar
Jon Griffiths committed
279 280 281 282 283
/******************************************************************************
 * IDispatch_Release {OLEAUT32}
 *
 * See IUnknown_Release.
 */
284 285
static ULONG WINAPI StdDispatch_Release(LPDISPATCH iface)
{
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
}

Jon Griffiths's avatar
Jon Griffiths committed
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
/******************************************************************************
 * IDispatch_GetTypeInfoCount {OLEAUT32}
 *
 * Get the count of type information in an IDispatch interface.
 *
 * PARAMS
 *  iface   [I] IDispatch interface
 *  pctinfo [O] Destination for the count
 *
 * RETURNS
 *  Success: S_OK. pctinfo is updated with the count. This is always 1 if
 *           the object provides type information, and 0 if it does not.
 *  Failure: E_NOTIMPL. The object does not provide type information.
 *
 * NOTES
 *  See IDispatch() and IDispatch_GetTypeInfo().
 */
317 318
static HRESULT WINAPI StdDispatch_GetTypeInfoCount(LPDISPATCH iface, UINT * pctinfo)
{
319
    StdDispatch *This = (StdDispatch *)iface;
320 321
    TRACE("(%p)\n", pctinfo);

Jon Griffiths's avatar
Jon Griffiths committed
322
    *pctinfo = This->pTypeInfo ? 1 : 0;
323 324 325
    return S_OK;
}

Jon Griffiths's avatar
Jon Griffiths committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
/******************************************************************************
 * IDispatch_GetTypeInfo {OLEAUT32}
 *
 * Get type information from an IDispatch interface.
 *
 * PARAMS
 *  iface   [I] IDispatch interface
 *  iTInfo  [I] Index of type information.
 *  lcid    [I] Locale of the type information to get
 *  ppTInfo [O] Destination for the ITypeInfo object
 *
 * RETURNS
 *  Success: S_OK. ppTInfo is updated with the objects type information
 *  Failure: DISP_E_BADINDEX, if iTInfo is any value other than 0.
 *
 * NOTES
 *  See IDispatch.
 */
344 345
static HRESULT WINAPI StdDispatch_GetTypeInfo(LPDISPATCH iface, UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
{
346
    StdDispatch *This = (StdDispatch *)iface;
347
    TRACE("(%d, %x, %p)\n", iTInfo, lcid, ppTInfo);
348

Jon Griffiths's avatar
Jon Griffiths committed
349
    *ppTInfo = NULL;
350 351
    if (iTInfo != 0)
        return DISP_E_BADINDEX;
Jon Griffiths's avatar
Jon Griffiths committed
352 353 354 355 356 357

    if (This->pTypeInfo)
    {
      *ppTInfo = This->pTypeInfo;
      ITypeInfo_AddRef(*ppTInfo);
    }
358 359 360
    return S_OK;
}

Jon Griffiths's avatar
Jon Griffiths committed
361 362 363
/******************************************************************************
 * IDispatch_GetIDsOfNames {OLEAUT32}
 *
364
 * Convert a methods name and an optional set of parameter names into DISPIDs
Jon Griffiths's avatar
Jon Griffiths committed
365 366 367 368 369 370 371 372
 * for passing to IDispatch_Invoke().
 *
 * PARAMS
 *  iface     [I] IDispatch interface
 *  riid      [I] Reserved, set to IID_NULL
 *  rgszNames [I] Name to convert
 *  cNames    [I] Number of names in rgszNames
 *  lcid      [I] Locale of the type information to convert from
373
 *  rgDispId  [O] Destination for converted DISPIDs.
Jon Griffiths's avatar
Jon Griffiths committed
374 375 376 377 378
 *
 * RETURNS
 *  Success: S_OK.
 *  Failure: DISP_E_UNKNOWNNAME, if any of the names is invalid.
 *           DISP_E_UNKNOWNLCID if lcid is invalid.
379
 *           Otherwise, an HRESULT error code.
Jon Griffiths's avatar
Jon Griffiths committed
380 381 382 383 384 385 386
 *
 * NOTES
 *  This call defers to ITypeInfo_GetIDsOfNames(), using the ITypeInfo object
 *  contained within the IDispatch object.
 *  The first member of the names list must be a method name. The names following
 *  the method name are the parameters for that method.
 */
387 388
static HRESULT WINAPI StdDispatch_GetIDsOfNames(LPDISPATCH iface, REFIID riid, LPOLESTR * rgszNames, UINT cNames, LCID lcid, DISPID * rgDispId)
{
389
    StdDispatch *This = (StdDispatch *)iface;
390
    TRACE("(%s, %p, %d, 0x%x, %p)\n", debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
391 392 393 394 395 396 397 398 399

    if (!IsEqualGUID(riid, &IID_NULL))
    {
        FIXME(" expected riid == IID_NULL\n");
        return E_INVALIDARG;
    }
    return DispGetIDsOfNames(This->pTypeInfo, rgszNames, cNames, rgDispId);
}

Jon Griffiths's avatar
Jon Griffiths committed
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
/******************************************************************************
 * IDispatch_Invoke {OLEAUT32}
 *
 * Call an object method.
 *
 * PARAMS
 *  iface        [I] IDispatch interface
 *  dispIdMember [I] DISPID of the method (from GetIDsOfNames())
 *  riid         [I] Reserved, set to IID_NULL
 *  lcid         [I] Locale of the type information to convert parameters with
 *  wFlags,      [I] Kind of method call (DISPATCH_ flags from "oaidl.h")
 *  pDispParams  [I] Array of method arguments
 *  pVarResult   [O] Destination for the result of the call
 *  pExcepInfo   [O] Destination for exception information
 *  puArgErr     [O] Destination for bad argument
 *
 * RETURNS
 *  Success: S_OK.
 *  Failure: See DispInvoke() for failure cases.
 *
 * NOTES
 *  See DispInvoke() and IDispatch().
 */
static HRESULT WINAPI StdDispatch_Invoke(LPDISPATCH iface, DISPID dispIdMember, REFIID riid, LCID lcid,
                                         WORD wFlags, DISPPARAMS * pDispParams, VARIANT * pVarResult,
                                         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 431 432 433 434 435 436 437

    if (!IsEqualGUID(riid, &IID_NULL))
    {
        FIXME(" expected riid == IID_NULL\n");
        return E_INVALIDARG;
    }
    return DispInvoke(This->pvThis, This->pTypeInfo, dispIdMember, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
}

438
static const IDispatchVtbl StdDispatch_VTable =
439 440 441 442 443 444 445 446 447 448
{
  StdDispatch_QueryInterface,
  StdDispatch_AddRef,
  StdDispatch_Release,
  StdDispatch_GetTypeInfoCount,
  StdDispatch_GetTypeInfo,
  StdDispatch_GetIDsOfNames,
  StdDispatch_Invoke
};

449
static IDispatch * StdDispatch_Construct(
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
  IUnknown * punkOuter,
  void * pvThis,
  ITypeInfo * pTypeInfo)
{
    StdDispatch * pStdDispatch;

    pStdDispatch = CoTaskMemAlloc(sizeof(StdDispatch));
    if (!pStdDispatch)
        return (IDispatch *)pStdDispatch;

    pStdDispatch->lpVtbl = &StdDispatch_VTable;
    pStdDispatch->pvThis = pvThis;
    pStdDispatch->pTypeInfo = pTypeInfo;
    pStdDispatch->ref = 1;

465 466 467 468
    /* we keep a reference to the type info so prevent it from
     * being destroyed until we are done with it */
    ITypeInfo_AddRef(pTypeInfo);

469 470
    return (IDispatch *)pStdDispatch;
}