ftmarshal.c 11.8 KB
Newer Older
1
/*
2
 *	free threaded marshaller
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 *  Copyright 2002  Juergen Schmied
 *
 * 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"

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

29 30
#define COBJMACROS

31
#include "windef.h"
32
#include "winbase.h"
33
#include "objbase.h"
34 35 36

#include "wine/debug.h"

37 38
#include "compobj_private.h"

39 40 41
WINE_DEFAULT_DEBUG_CHANNEL(ole);

typedef struct _FTMarshalImpl {
42
        IUnknown IUnknown_iface;
43
	LONG ref;
44
        IMarshal IMarshal_iface;
45 46 47 48

	IUnknown *pUnkOuter;
} FTMarshalImpl;

49 50 51 52
static inline FTMarshalImpl *impl_from_IUnknown(IUnknown *iface)
{
    return CONTAINING_RECORD(iface, FTMarshalImpl, IUnknown_iface);
}
53

54 55
static inline FTMarshalImpl *impl_from_IMarshal( IMarshal *iface )
{
56
    return CONTAINING_RECORD(iface, FTMarshalImpl, IMarshal_iface);
57
}
58 59

/* inner IUnknown to handle aggregation */
60 61
static HRESULT WINAPI
IiFTMUnknown_fnQueryInterface (IUnknown * iface, REFIID riid, LPVOID * ppv)
62 63
{

64
    FTMarshalImpl *This = impl_from_IUnknown(iface);
65 66 67 68 69

    TRACE ("\n");
    *ppv = NULL;

    if (IsEqualIID (&IID_IUnknown, riid))
70
        *ppv = &This->IUnknown_iface;
71
    else if (IsEqualIID (&IID_IMarshal, riid))
72
        *ppv = &This->IMarshal_iface;
73 74 75 76 77 78 79 80
    else {
	FIXME ("No interface for %s.\n", debugstr_guid (riid));
	return E_NOINTERFACE;
    }
    IUnknown_AddRef ((IUnknown *) * ppv);
    return S_OK;
}

81
static ULONG WINAPI IiFTMUnknown_fnAddRef (IUnknown * iface)
82 83
{

84
    FTMarshalImpl *This = impl_from_IUnknown(iface);
85 86 87 88 89

    TRACE ("\n");
    return InterlockedIncrement (&This->ref);
}

90
static ULONG WINAPI IiFTMUnknown_fnRelease (IUnknown * iface)
91 92
{

93
    FTMarshalImpl *This = impl_from_IUnknown(iface);
94 95 96 97 98 99 100 101

    TRACE ("\n");
    if (InterlockedDecrement (&This->ref))
	return This->ref;
    HeapFree (GetProcessHeap (), 0, This);
    return 0;
}

102
static const IUnknownVtbl iunkvt =
103 104 105 106 107 108
{
	IiFTMUnknown_fnQueryInterface,
	IiFTMUnknown_fnAddRef,
	IiFTMUnknown_fnRelease
};

109 110
static HRESULT WINAPI
FTMarshalImpl_QueryInterface (LPMARSHAL iface, REFIID riid, LPVOID * ppv)
111 112
{

113
    FTMarshalImpl *This = impl_from_IMarshal(iface);
114

115
    TRACE ("(%p)->(%s,%p)\n", This, debugstr_guid (riid), ppv);
116 117 118
    return IUnknown_QueryInterface (This->pUnkOuter, riid, ppv);
}

119 120
static ULONG WINAPI
FTMarshalImpl_AddRef (LPMARSHAL iface)
121 122
{

123
    FTMarshalImpl *This = impl_from_IMarshal(iface);
124 125 126 127 128

    TRACE ("\n");
    return IUnknown_AddRef (This->pUnkOuter);
}

129 130
static ULONG WINAPI
FTMarshalImpl_Release (LPMARSHAL iface)
131 132
{

133
    FTMarshalImpl *This = impl_from_IMarshal(iface);
134 135 136 137 138

    TRACE ("\n");
    return IUnknown_Release (This->pUnkOuter);
}

139 140
static HRESULT WINAPI
FTMarshalImpl_GetUnmarshalClass (LPMARSHAL iface, REFIID riid, void *pv, DWORD dwDestContext,
141 142
						void *pvDestContext, DWORD mshlflags, CLSID * pCid)
{
143
    TRACE("(%s, %p, 0x%x, %p, 0x%x, %p)\n", debugstr_guid(riid), pv,
144 145
        dwDestContext, pvDestContext, mshlflags, pCid);
    if (dwDestContext == MSHCTX_INPROC || dwDestContext == MSHCTX_CROSSCTX)
146
        *pCid = CLSID_InProcFreeMarshaler;
147
    else
148
        *pCid = CLSID_DfMarshal;
149 150 151
    return S_OK;
}

152 153
static HRESULT WINAPI
FTMarshalImpl_GetMarshalSizeMax (LPMARSHAL iface, REFIID riid, void *pv, DWORD dwDestContext,
154 155 156 157 158 159
						void *pvDestContext, DWORD mshlflags, DWORD * pSize)
{

    IMarshal *pMarshal = NULL;
    HRESULT hres;

160
    TRACE("(%s, %p, 0x%x, %p, 0x%x, %p)\n", debugstr_guid(riid), pv,
161
        dwDestContext, pvDestContext, mshlflags, pSize);
162

163 164
    /* if the marshalling happens inside the same process the interface pointer is
       copied between the apartments */
165
    if (dwDestContext == MSHCTX_INPROC || dwDestContext == MSHCTX_CROSSCTX) {
166 167
        *pSize = sizeof (mshlflags) + sizeof (pv) + sizeof (DWORD) + sizeof (GUID);
        return S_OK;
168 169
    }

170
    /* use the standard marshaller to handle all other cases */
171 172 173 174 175 176
    CoGetStandardMarshal (riid, pv, dwDestContext, pvDestContext, mshlflags, &pMarshal);
    hres = IMarshal_GetMarshalSizeMax (pMarshal, riid, pv, dwDestContext, pvDestContext, mshlflags, pSize);
    IMarshal_Release (pMarshal);
    return hres;
}

177 178 179
static HRESULT WINAPI
FTMarshalImpl_MarshalInterface (LPMARSHAL iface, IStream * pStm, REFIID riid, void *pv,
			       DWORD dwDestContext, void *pvDestContext, DWORD mshlflags)
180 181 182 183 184
{

    IMarshal *pMarshal = NULL;
    HRESULT hres;

185
    TRACE("(%p, %s, %p, 0x%x, %p, 0x%x)\n", pStm, debugstr_guid(riid), pv,
186
        dwDestContext, pvDestContext, mshlflags);
187

188 189
    /* if the marshalling happens inside the same process the interface pointer is
       copied between the apartments */
190
    if (dwDestContext == MSHCTX_INPROC || dwDestContext == MSHCTX_CROSSCTX) {
191 192 193 194 195 196 197 198
        void *object;
        DWORD constant = 0;
        GUID unknown_guid = { 0 };

        hres = IUnknown_QueryInterface((IUnknown *)pv, riid, &object);
        if (FAILED(hres))
            return hres;

199 200 201 202
        /* don't hold a reference to table-weak marshaled interfaces */
        if (mshlflags & MSHLFLAGS_TABLEWEAK)
            IUnknown_Release((IUnknown *)object);

203 204 205 206 207 208
        hres = IStream_Write (pStm, &mshlflags, sizeof (mshlflags), NULL);
        if (hres != S_OK) return STG_E_MEDIUMFULL;

        hres = IStream_Write (pStm, &object, sizeof (object), NULL);
        if (hres != S_OK) return STG_E_MEDIUMFULL;

209 210 211 212 213
        if (sizeof(object) == sizeof(DWORD))
        {
            hres = IStream_Write (pStm, &constant, sizeof (constant), NULL);
            if (hres != S_OK) return STG_E_MEDIUMFULL;
        }
214 215 216 217 218

        hres = IStream_Write (pStm, &unknown_guid, sizeof (unknown_guid), NULL);
        if (hres != S_OK) return STG_E_MEDIUMFULL;

        return S_OK;
219 220 221 222 223 224 225 226 227
    }

    /* use the standard marshaler to handle all other cases */
    CoGetStandardMarshal (riid, pv, dwDestContext, pvDestContext, mshlflags, &pMarshal);
    hres = IMarshal_MarshalInterface (pMarshal, pStm, riid, pv, dwDestContext, pvDestContext, mshlflags);
    IMarshal_Release (pMarshal);
    return hres;
}

228 229
static HRESULT WINAPI
FTMarshalImpl_UnmarshalInterface (LPMARSHAL iface, IStream * pStm, REFIID riid, void **ppv)
230
{
231
    DWORD mshlflags;
232 233 234
    IUnknown *object;
    DWORD constant;
    GUID unknown_guid;
235 236 237 238 239 240 241
    HRESULT hres;

    TRACE ("(%p, %s, %p)\n", pStm, debugstr_guid(riid), ppv);

    hres = IStream_Read (pStm, &mshlflags, sizeof (mshlflags), NULL);
    if (hres != S_OK) return STG_E_READFAULT;

242 243
    hres = IStream_Read (pStm, &object, sizeof (object), NULL);
    if (hres != S_OK) return STG_E_READFAULT;
244

245 246 247 248 249 250 251
    if (sizeof(object) == sizeof(DWORD))
    {
        hres = IStream_Read (pStm, &constant, sizeof (constant), NULL);
        if (hres != S_OK) return STG_E_READFAULT;
        if (constant != 0)
            FIXME("constant is 0x%x instead of 0\n", constant);
    }
252

253 254
    hres = IStream_Read (pStm, &unknown_guid, sizeof (unknown_guid), NULL);
    if (hres != S_OK) return STG_E_READFAULT;
255

256 257 258 259
    hres = IUnknown_QueryInterface(object, riid, ppv);
    if (!(mshlflags & (MSHLFLAGS_TABLEWEAK|MSHLFLAGS_TABLESTRONG)))
        IUnknown_Release(object);
    return hres;
260 261
}

262
static HRESULT WINAPI FTMarshalImpl_ReleaseMarshalData (LPMARSHAL iface, IStream * pStm)
263
{
264
    DWORD mshlflags;
265 266 267
    IUnknown *object;
    DWORD constant;
    GUID unknown_guid;
268 269 270 271 272 273 274
    HRESULT hres;

    TRACE ("(%p)\n", pStm);

    hres = IStream_Read (pStm, &mshlflags, sizeof (mshlflags), NULL);
    if (hres != S_OK) return STG_E_READFAULT;

275 276
    hres = IStream_Read (pStm, &object, sizeof (object), NULL);
    if (hres != S_OK) return STG_E_READFAULT;
277

278 279 280 281 282 283 284
    if (sizeof(object) == sizeof(DWORD))
    {
        hres = IStream_Read (pStm, &constant, sizeof (constant), NULL);
        if (hres != S_OK) return STG_E_READFAULT;
        if (constant != 0)
            FIXME("constant is 0x%x instead of 0\n", constant);
    }
285

286 287
    hres = IStream_Read (pStm, &unknown_guid, sizeof (unknown_guid), NULL);
    if (hres != S_OK) return STG_E_READFAULT;
288

289 290
    IUnknown_Release(object);
    return S_OK;
291 292
}

293
static HRESULT WINAPI FTMarshalImpl_DisconnectObject (LPMARSHAL iface, DWORD dwReserved)
294
{
295 296
    TRACE ("()\n");
    /* nothing to do */
297 298 299
    return S_OK;
}

300
static const IMarshalVtbl ftmvtbl =
301 302 303 304 305 306 307 308 309 310 311 312 313
{
	FTMarshalImpl_QueryInterface,
	FTMarshalImpl_AddRef,
	FTMarshalImpl_Release,
	FTMarshalImpl_GetUnmarshalClass,
	FTMarshalImpl_GetMarshalSizeMax,
	FTMarshalImpl_MarshalInterface,
	FTMarshalImpl_UnmarshalInterface,
	FTMarshalImpl_ReleaseMarshalData,
	FTMarshalImpl_DisconnectObject
};

/***********************************************************************
314
 *          CoCreateFreeThreadedMarshaler [OLE32.@]
315
 *
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
 * Creates a free-threaded marshaler.
 *
 * PARAMS
 *  punkOuter    [I] Optional. Outer unknown.
 *  ppunkMarshal [O] On return, the inner unknown of the created free-threaded marshaler.
 *
 * RETURNS
 *  Success: S_OK
 *  Failure: E_OUTOFMEMORY if no memory available to create object.
 *
 * NOTES
 *  Objects that ensure their state is maintained consistent when used by
 *  multiple threads and reference no single-threaded objects are known as
 *  free-threaded. The free-threaded marshaler enables these objects to be
 *  efficiently marshaled within the same process, by not creating proxies
 *  (as they aren't needed for the object to be safely used), whilst still
 *  allowing the object to be used in inter-process and inter-machine contexts.
333 334 335 336 337 338 339 340
 */
HRESULT WINAPI CoCreateFreeThreadedMarshaler (LPUNKNOWN punkOuter, LPUNKNOWN * ppunkMarshal)
{

    FTMarshalImpl *ftm;

    TRACE ("(%p %p)\n", punkOuter, ppunkMarshal);

341
    ftm = HeapAlloc (GetProcessHeap (), 0, sizeof (FTMarshalImpl));
342 343 344
    if (!ftm)
	return E_OUTOFMEMORY;

345 346
    ftm->IUnknown_iface.lpVtbl = &iunkvt;
    ftm->IMarshal_iface.lpVtbl = &ftmvtbl;
347
    ftm->ref = 1;
348
    ftm->pUnkOuter = punkOuter ? punkOuter : &ftm->IUnknown_iface;
349

350
    *ppunkMarshal = &ftm->IUnknown_iface;
351 352
    return S_OK;
}
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417

static HRESULT WINAPI FTMarshalCF_QueryInterface(LPCLASSFACTORY iface,
                                                  REFIID riid, LPVOID *ppv)
{
    *ppv = NULL;
    if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
    {
        *ppv = iface;
        IUnknown_AddRef(iface);
        return S_OK;
    }
    return E_NOINTERFACE;
}

static ULONG WINAPI FTMarshalCF_AddRef(LPCLASSFACTORY iface)
{
    return 2; /* non-heap based object */
}

static ULONG WINAPI FTMarshalCF_Release(LPCLASSFACTORY iface)
{
    return 1; /* non-heap based object */
}

static HRESULT WINAPI FTMarshalCF_CreateInstance(LPCLASSFACTORY iface,
    LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
{
    IUnknown *pUnknown;
    HRESULT  hr;

    TRACE("(%p, %s, %p)\n", pUnk, debugstr_guid(riid), ppv);

    *ppv = NULL;

    hr = CoCreateFreeThreadedMarshaler(pUnk, &pUnknown);

    if (SUCCEEDED(hr))
    {
        hr = IUnknown_QueryInterface(pUnknown, riid, ppv);
        IUnknown_Release(pUnknown);
    }

    return hr;
}

static HRESULT WINAPI FTMarshalCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
{
    FIXME("(%d), stub!\n",fLock);
    return S_OK;
}

static const IClassFactoryVtbl FTMarshalCFVtbl =
{
    FTMarshalCF_QueryInterface,
    FTMarshalCF_AddRef,
    FTMarshalCF_Release,
    FTMarshalCF_CreateInstance,
    FTMarshalCF_LockServer
};
static const IClassFactoryVtbl *FTMarshalCF = &FTMarshalCFVtbl;

HRESULT FTMarshalCF_Create(REFIID riid, LPVOID *ppv)
{
    return IClassFactory_QueryInterface((IClassFactory *)&FTMarshalCF, riid, ppv);
}