oleproxy.c 28.3 KB
Newer Older
1 2 3 4
/*
 *	OLE32 proxy/stub handler
 *
 *  Copyright 2002  Marcus Meissner
5
 *  Copyright 2001  Ove Kven, TransGaming Technologies
6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * 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
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 21 22
 */

/* Documentation on MSDN:
23 24 25
 *
 * (Top level COM documentation)
 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnanchor/html/componentdevelopmentank.asp
26 27
 *
 * (COM Proxy)
28
 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/comext_1q0p.asp
29 30
 *
 * (COM Stub)
31
 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/comext_1lia.asp
32 33
 *
 * (Marshal)
34
 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/comext_1gfn.asp
35 36 37 38 39 40
 *
 */

#include "config.h"

#include <stdlib.h>
41
#include <stdarg.h>
42 43 44
#include <stdio.h>
#include <string.h>

45
#define COBJMACROS
46 47
#define NONAMELESSUNION
#define NONAMELESSSTRUCT
48

49
#include "windef.h"
50
#include "winbase.h"
51
#include "winuser.h"
52 53 54 55 56 57 58 59
#include "objbase.h"
#include "ole2.h"
#include "rpc.h"
#include "winerror.h"
#include "winreg.h"
#include "wtypes.h"

#include "compobj_private.h"
60
#include "moniker.h"
61

62
#include "wine/debug.h"
63

64
WINE_DEFAULT_DEBUG_CHANNEL(ole);
65

66 67 68
const CLSID CLSID_DfMarshal       = { 0x0000030b, 0, 0, {0xc0, 0, 0, 0, 0, 0, 0, 0x46} };
const CLSID CLSID_PSFactoryBuffer = { 0x00000320, 0, 0, {0xc0, 0, 0, 0, 0, 0, 0, 0x46} };

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
/* From: http://msdn.microsoft.com/library/en-us/com/cmi_m_4lda.asp
 *
 * The first time a client requests a pointer to an interface on a
 * particular object, COM loads an IClassFactory stub in the server
 * process and uses it to marshal the first pointer back to the
 * client. In the client process, COM loads the generic proxy for the
 * class factory object and calls its implementation of IMarshal to
 * unmarshal that first pointer. COM then creates the first interface
 * proxy and hands it a pointer to the RPC channel. Finally, COM returns
 * the IClassFactory pointer to the client, which uses it to call
 * IClassFactory::CreateInstance, passing it a reference to the interface.
 *
 * Back in the server process, COM now creates a new instance of the
 * object, along with a stub for the requested interface. This stub marshals
 * the interface pointer back to the client process, where another object
 * proxy is created, this time for the object itself. Also created is a
 * proxy for the requested interface, a pointer to which is returned to
 * the client. With subsequent calls to other interfaces on the object,
 * COM will load the appropriate interface stubs and proxies as needed.
 */
typedef struct _CFStub {
90
    const IRpcStubBufferVtbl   *lpvtbl;
91
    LONG			ref;
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

    LPUNKNOWN			pUnkServer;
} CFStub;

static HRESULT WINAPI
CFStub_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv) {
    if (IsEqualIID(&IID_IUnknown,riid)||IsEqualIID(&IID_IRpcStubBuffer,riid)) {
	*ppv = (LPVOID)iface;
	IUnknown_AddRef(iface);
	return S_OK;
    }
    FIXME("(%s), interface not supported.\n",debugstr_guid(riid));
    return E_NOINTERFACE;
}

static ULONG WINAPI
CFStub_AddRef(LPRPCSTUBBUFFER iface) {
109
    CFStub *This = (CFStub *)iface;
110
    return InterlockedIncrement(&This->ref);
111 112 113 114
}

static ULONG WINAPI
CFStub_Release(LPRPCSTUBBUFFER iface) {
115
    CFStub *This = (CFStub *)iface;
116
    ULONG ref;
117

118
    ref = InterlockedDecrement(&This->ref);
119 120 121 122
    if (!ref) {
        IRpcStubBuffer_Disconnect(iface);
        HeapFree(GetProcessHeap(),0,This);
    }
123
    return ref;
124 125 126 127
}

static HRESULT WINAPI
CFStub_Connect(LPRPCSTUBBUFFER iface, IUnknown *pUnkServer) {
128
    CFStub *This = (CFStub *)iface;
129 130 131 132 133 134 135 136

    This->pUnkServer = pUnkServer;
    IUnknown_AddRef(pUnkServer);
    return S_OK;
}

static void WINAPI
CFStub_Disconnect(LPRPCSTUBBUFFER iface) {
137
    CFStub *This = (CFStub *)iface;
138

139 140 141 142
    if (This->pUnkServer) {
        IUnknown_Release(This->pUnkServer);
        This->pUnkServer = NULL;
    }
143
}
144

145 146 147 148
static HRESULT WINAPI
CFStub_Invoke(
    LPRPCSTUBBUFFER iface,RPCOLEMESSAGE* msg,IRpcChannelBuffer* chanbuf
) {
149
    CFStub *This = (CFStub *)iface;
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    HRESULT hres;

    if (msg->iMethod == 3) { /* CreateInstance */
	IID iid;
	IClassFactory	*classfac;
	IUnknown	*ppv;
	IStream		*pStm;
	STATSTG		ststg;
	ULARGE_INTEGER	newpos;
	LARGE_INTEGER	seekto;
	ULONG		res;

	if (msg->cbBuffer < sizeof(IID)) {
	    FIXME("Not enough bytes in buffer (%ld instead of %d)?\n",msg->cbBuffer,sizeof(IID));
	    return E_FAIL;
	}
	memcpy(&iid,msg->Buffer,sizeof(iid));
	TRACE("->CreateInstance(%s)\n",debugstr_guid(&iid));
168
	hres = IUnknown_QueryInterface(This->pUnkServer,&IID_IClassFactory,(LPVOID*)&classfac);
169
	if (hres) {
170
	    FIXME("Ole server does not provide an IClassFactory?\n");
171 172
	    return hres;
	}
173
	hres = IClassFactory_CreateInstance(classfac,NULL,&iid,(LPVOID*)&ppv);
174 175 176 177 178 179 180 181 182 183 184 185
	IClassFactory_Release(classfac);
	if (hres) {
	    msg->cbBuffer = 0;
	    FIXME("Failed to create an instance of %s\n",debugstr_guid(&iid));
	    return hres;
	}
	hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
	if (hres) {
	    FIXME("Failed to create stream on hglobal\n");
	    return hres;
	}
	hres = CoMarshalInterface(pStm,&iid,ppv,0,NULL,0);
186
	IUnknown_Release((IUnknown*)ppv);
187 188 189 190 191 192 193 194 195 196 197
	if (hres) {
	    FIXME("CoMarshalInterface failed, %lx!\n",hres);
	    msg->cbBuffer = 0;
	    return hres;
	}
	hres = IStream_Stat(pStm,&ststg,0);
	if (hres) {
	    FIXME("Stat failed.\n");
	    return hres;
	}

198
	msg->cbBuffer = ststg.cbSize.u.LowPart;
199

200 201
        I_RpcGetBuffer((RPC_MESSAGE *)msg);
        if (hres) return hres;
202

203
	seekto.u.LowPart = 0;seekto.u.HighPart = 0;
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
	hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
	if (hres) {
	    FIXME("IStream_Seek failed, %lx\n",hres);
	    return hres;
	}
	hres = IStream_Read(pStm,msg->Buffer,msg->cbBuffer,&res);
	if (hres) {
	    FIXME("Stream Read failed, %lx\n",hres);
	    return hres;
	}
	IStream_Release(pStm);
	return S_OK;
    }
    FIXME("(%p,%p), stub!\n",msg,chanbuf);
    FIXME("iMethod is %ld\n",msg->iMethod);
    FIXME("cbBuffer is %ld\n",msg->cbBuffer);
    return E_FAIL;
}

static LPRPCSTUBBUFFER WINAPI
CFStub_IsIIDSupported(LPRPCSTUBBUFFER iface,REFIID riid) {
    FIXME("(%s), stub!\n",debugstr_guid(riid));
    return NULL;
}

static ULONG WINAPI
CFStub_CountRefs(LPRPCSTUBBUFFER iface) {
    FIXME("(), stub!\n");
    return 1;
}

static HRESULT WINAPI
CFStub_DebugServerQueryInterface(LPRPCSTUBBUFFER iface,void** ppv) {
    FIXME("(%p), stub!\n",ppv);
    return E_FAIL;
}
static void    WINAPI
CFStub_DebugServerRelease(LPRPCSTUBBUFFER iface,void *pv) {
    FIXME("(%p), stub!\n",pv);
}

245
static const IRpcStubBufferVtbl cfstubvt = {
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
    CFStub_QueryInterface,
    CFStub_AddRef,
    CFStub_Release,
    CFStub_Connect,
    CFStub_Disconnect,
    CFStub_Invoke,
    CFStub_IsIIDSupported,
    CFStub_CountRefs,
    CFStub_DebugServerQueryInterface,
    CFStub_DebugServerRelease
};

static HRESULT
CFStub_Construct(LPRPCSTUBBUFFER *ppv) {
    CFStub *cfstub;
    cfstub = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CFStub));
    if (!cfstub)
	return E_OUTOFMEMORY;
    *ppv = (LPRPCSTUBBUFFER)cfstub;
    cfstub->lpvtbl	= &cfstubvt;
    cfstub->ref		= 1;
    return S_OK;
}

270
/* Since we create proxy buffers and classfactory in a pair, there is
271
 * no need for 2 separate structs. Just put them in one, but remember
272 273 274
 * the refcount.
 */
typedef struct _CFProxy {
275 276
    const IClassFactoryVtbl		*lpvtbl_cf;
    const IRpcProxyBufferVtbl	*lpvtbl_proxy;
277
    LONG				ref;
278 279

    IRpcChannelBuffer			*chanbuf;
280
    IUnknown *outer_unknown;
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
} CFProxy;

static HRESULT WINAPI IRpcProxyBufferImpl_QueryInterface(LPRPCPROXYBUFFER iface,REFIID riid,LPVOID *ppv) {
    *ppv = NULL;
    if (IsEqualIID(riid,&IID_IRpcProxyBuffer)||IsEqualIID(riid,&IID_IUnknown)) {
	IRpcProxyBuffer_AddRef(iface);
	*ppv = (LPVOID)iface;
	return S_OK;
    }
    FIXME("(%s), no interface.\n",debugstr_guid(riid));
    return E_NOINTERFACE;
}

static ULONG WINAPI IRpcProxyBufferImpl_AddRef(LPRPCPROXYBUFFER iface) {
    ICOM_THIS_MULTI(CFProxy,lpvtbl_proxy,iface);
296
    return InterlockedIncrement(&This->ref);
297 298 299 300
}

static ULONG WINAPI IRpcProxyBufferImpl_Release(LPRPCPROXYBUFFER iface) {
    ICOM_THIS_MULTI(CFProxy,lpvtbl_proxy,iface);
301
    ULONG ref = InterlockedDecrement(&This->ref);
302

303
    if (!ref) {
304 305
        IRpcProxyBuffer_Disconnect(iface);
        HeapFree(GetProcessHeap(),0,This);
306
    }
307
    return ref;
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
}

static HRESULT WINAPI IRpcProxyBufferImpl_Connect(LPRPCPROXYBUFFER iface,IRpcChannelBuffer* pRpcChannelBuffer) {
    ICOM_THIS_MULTI(CFProxy,lpvtbl_proxy,iface);

    This->chanbuf = pRpcChannelBuffer;
    IRpcChannelBuffer_AddRef(This->chanbuf);
    return S_OK;
}
static void WINAPI IRpcProxyBufferImpl_Disconnect(LPRPCPROXYBUFFER iface) {
    ICOM_THIS_MULTI(CFProxy,lpvtbl_proxy,iface);
    if (This->chanbuf) {
	IRpcChannelBuffer_Release(This->chanbuf);
	This->chanbuf = NULL;
    }
}

static HRESULT WINAPI
CFProxy_QueryInterface(LPCLASSFACTORY iface,REFIID riid, LPVOID *ppv) {
327
    ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);
328
    if (This->outer_unknown) return IUnknown_QueryInterface(This->outer_unknown, riid, ppv);
329 330 331 332 333 334
    *ppv = NULL;
    if (IsEqualIID(&IID_IClassFactory,riid) || IsEqualIID(&IID_IUnknown,riid)) {
	*ppv = (LPVOID)iface;
	IClassFactory_AddRef(iface);
	return S_OK;
    }
335
    if (IsEqualIID(riid,&IID_IMarshal)) /* just to avoid debug output */
336 337 338 339 340 341 342
	return E_NOINTERFACE;
    FIXME("Unhandled interface: %s\n",debugstr_guid(riid));
    return E_NOINTERFACE;
}

static ULONG   WINAPI CFProxy_AddRef(LPCLASSFACTORY iface) {
    ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);
343
    if (This->outer_unknown) return IUnknown_AddRef(This->outer_unknown);
344
    return InterlockedIncrement(&This->ref);
345 346 347
}

static ULONG   WINAPI CFProxy_Release(LPCLASSFACTORY iface) {
348
    ULONG ref;
349
    ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);
350 351 352 353 354 355
    if (This->outer_unknown)
        ref = IUnknown_Release(This->outer_unknown);
    else    
        ref = InterlockedDecrement(&This->ref);

    if (!ref) {
356
      	if (This->chanbuf) IRpcChannelBuffer_Release(This->chanbuf);
357 358
        HeapFree(GetProcessHeap(),0,This);
    }
359
    return ref;
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
}

static HRESULT WINAPI CFProxy_CreateInstance(
    LPCLASSFACTORY iface,
    LPUNKNOWN pUnkOuter,/* [in] */
    REFIID riid,	/* [in] */
    LPVOID *ppv		/* [out] */
) {
    ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);
    HRESULT		hres;
    LPSTREAM		pStream;
    HGLOBAL		hGlobal;
    ULONG		srstatus;
    RPCOLEMESSAGE	msg;

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

377
    /* Send CreateInstance to the remote classfactory.
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
     *
     * Data: Only the 'IID'.
     */
    msg.iMethod  = 3;
    msg.cbBuffer = sizeof(*riid);
    msg.Buffer	 = NULL;
    hres = IRpcChannelBuffer_GetBuffer(This->chanbuf,&msg,&IID_IClassFactory);
    if (hres) {
	FIXME("IRpcChannelBuffer_GetBuffer failed with %lx?\n",hres);
	return hres;
    }
    memcpy(msg.Buffer,riid,sizeof(*riid));
    hres = IRpcChannelBuffer_SendReceive(This->chanbuf,&msg,&srstatus);
    if (hres) {
	FIXME("IRpcChannelBuffer_SendReceive failed with %lx?\n",hres);
	return hres;
    }

    if (!msg.cbBuffer) /* interface not found on remote */
	return srstatus;

399
    /* We got back: [Marshalled Interface data] */
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 426 427
    TRACE("got %ld bytes data.\n",msg.cbBuffer);
    hGlobal = GlobalAlloc(GMEM_MOVEABLE|GMEM_NODISCARD|GMEM_SHARE,msg.cbBuffer);
    memcpy(GlobalLock(hGlobal),msg.Buffer,msg.cbBuffer);
    hres = CreateStreamOnHGlobal(hGlobal,TRUE,&pStream);
    if (hres) {
	FIXME("CreateStreamOnHGlobal failed with %lx\n",hres);
	return hres;
    }
    hres = CoUnmarshalInterface(
	    pStream,
	    riid,
	    ppv
    );
    IStream_Release(pStream); /* Does GlobalFree hGlobal too. */
    if (hres) {
	FIXME("CoMarshalInterface failed, %lx\n",hres);
	return hres;
    }
    return S_OK;
}

static HRESULT WINAPI CFProxy_LockServer(LPCLASSFACTORY iface,BOOL fLock) {
    /*ICOM_THIS_MULTI(CFProxy,lpvtbl_cf,iface);*/
    FIXME("(%d), stub!\n",fLock);
    /* basically: write BOOL, read empty */
    return S_OK;
}

428
static const IRpcProxyBufferVtbl pspbvtbl = {
429 430 431 432 433 434
    IRpcProxyBufferImpl_QueryInterface,
    IRpcProxyBufferImpl_AddRef,
    IRpcProxyBufferImpl_Release,
    IRpcProxyBufferImpl_Connect,
    IRpcProxyBufferImpl_Disconnect
};
435
static const IClassFactoryVtbl cfproxyvt = {
436 437 438 439 440 441 442 443
    CFProxy_QueryInterface,
    CFProxy_AddRef,
    CFProxy_Release,
    CFProxy_CreateInstance,
    CFProxy_LockServer
};

static HRESULT
444
CFProxy_Construct(IUnknown *pUnkOuter, LPVOID *ppv,LPVOID *ppProxy) {
445 446 447 448 449 450 451 452
    CFProxy *cf;

    cf = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CFProxy));
    if (!cf)
	return E_OUTOFMEMORY;

    cf->lpvtbl_cf	= &cfproxyvt;
    cf->lpvtbl_proxy	= &pspbvtbl;
453
    /* one reference for the proxy buffer */
454 455
    cf->ref		= 1;
    cf->outer_unknown = pUnkOuter;
456 457
    *ppv		= &(cf->lpvtbl_cf);
    *ppProxy		= &(cf->lpvtbl_proxy);
458 459
    /* and one reference for the object */
    IUnknown_AddRef((IUnknown *)*ppv);
460 461 462 463
    return S_OK;
}


464 465 466 467 468
/********************* IRemUnknown Proxy/Stub ********************************/

typedef struct
{
    const IRpcStubBufferVtbl *lpVtbl;
469
    LONG refs;
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
    IRemUnknown *iface;
} RemUnkStub;

static HRESULT WINAPI RemUnkStub_QueryInterface(LPRPCSTUBBUFFER iface,
					     REFIID riid,
					     LPVOID *obj)
{
  RemUnkStub *This = (RemUnkStub *)iface;
  TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
  if (IsEqualGUID(&IID_IUnknown,riid) ||
      IsEqualGUID(&IID_IRpcStubBuffer,riid)) {
    *obj = This;
    return S_OK;
  }
  return E_NOINTERFACE;
}

static ULONG WINAPI RemUnkStub_AddRef(LPRPCSTUBBUFFER iface)
{
  RemUnkStub *This = (RemUnkStub *)iface;
  TRACE("(%p)->AddRef()\n",This);
  return InterlockedIncrement(&This->refs);
}

static ULONG WINAPI RemUnkStub_Release(LPRPCSTUBBUFFER iface)
{
  RemUnkStub *This = (RemUnkStub *)iface;
  ULONG refs;
  TRACE("(%p)->Release()\n",This);
  refs = InterlockedDecrement(&This->refs);
  if (!refs)
    HeapFree(GetProcessHeap(), 0, This);
  return refs;
}

static HRESULT WINAPI RemUnkStub_Connect(LPRPCSTUBBUFFER iface,
				      LPUNKNOWN lpUnkServer)
{
  RemUnkStub *This = (RemUnkStub *)iface;
  TRACE("(%p)->Connect(%p)\n",This,lpUnkServer);
  This->iface = (IRemUnknown*)lpUnkServer;
  IRemUnknown_AddRef(This->iface);
  return S_OK;
}

static void WINAPI RemUnkStub_Disconnect(LPRPCSTUBBUFFER iface)
{
  RemUnkStub *This = (RemUnkStub *)iface;
  TRACE("(%p)->Disconnect()\n",This);
519 520
  IUnknown_Release(This->iface);
  This->iface = NULL;
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
}

static HRESULT WINAPI RemUnkStub_Invoke(LPRPCSTUBBUFFER iface,
				     PRPCOLEMESSAGE pMsg,
				     LPRPCCHANNELBUFFER pChannel)
{
  RemUnkStub *This = (RemUnkStub *)iface;
  ULONG iMethod = pMsg->iMethod;
  LPBYTE buf = pMsg->Buffer;
  HRESULT hr = RPC_E_INVALIDMETHOD;

  TRACE("(%p)->Invoke(%p,%p) method %ld\n", This, pMsg, pChannel, iMethod);
  switch (iMethod)
  {
  case 3: /* RemQueryInterface */
  {
    IPID ipid;
    ULONG cRefs;
    USHORT cIids;
    IID *iids;
    REMQIRESULT *pQIResults = NULL;

    /* in */
    memcpy(&ipid, buf, sizeof(ipid));
    buf += sizeof(ipid);
    memcpy(&cRefs, buf, sizeof(cRefs));
    buf += sizeof(cRefs);
    memcpy(&cIids, buf, sizeof(cIids));
    buf += sizeof(cIids);
    iids = (IID *)buf;

    hr = IRemUnknown_RemQueryInterface(This->iface, &ipid, cRefs, cIids, iids, &pQIResults);

    /* out */
555
    pMsg->cbBuffer = cIids * sizeof(REMQIRESULT) + sizeof(HRESULT);
556 557 558

    I_RpcGetBuffer((RPC_MESSAGE *)pMsg);

559
    buf = pMsg->Buffer;
560 561 562 563
    *(HRESULT *)buf = hr;
    buf += sizeof(HRESULT);
    
    if (hr) return hr;
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
    /* FIXME: pQIResults is a unique pointer so pQIResults can be NULL! */
    memcpy(buf, pQIResults, cIids * sizeof(REMQIRESULT));

    break;
  }
  case 4: /* RemAddRef */
  {
    USHORT cIids;
    REMINTERFACEREF *ir;
    HRESULT *pResults;

    /* in */
    memcpy(&cIids, buf, sizeof(USHORT));
    buf += sizeof(USHORT);
    ir = (REMINTERFACEREF*)buf;
    pResults = CoTaskMemAlloc(cIids * sizeof(HRESULT));
    if (!pResults) return E_OUTOFMEMORY;

    hr = IRemUnknown_RemAddRef(This->iface, cIids, ir, pResults);

    /* out */
    pMsg->cbBuffer = cIids * sizeof(HRESULT);
586 587 588 589 590 591 592

    I_RpcGetBuffer((RPC_MESSAGE *)pMsg);
    if (!hr)
    {
        buf = pMsg->Buffer;
        memcpy(buf, pResults, cIids * sizeof(HRESULT));
    }
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666

    CoTaskMemFree(pResults);

    break;
  }
  case 5: /* RemRelease */
  {
    USHORT cIids;
    REMINTERFACEREF *ir;

    /* in */
    memcpy(&cIids, buf, sizeof(USHORT));
    buf += sizeof(USHORT);
    ir = (REMINTERFACEREF*)buf;

    hr = IRemUnknown_RemRelease(This->iface, cIids, ir);

    /* out */
    pMsg->cbBuffer = 0;
    break;
  }
  }
  return hr;
}

static LPRPCSTUBBUFFER WINAPI RemUnkStub_IsIIDSupported(LPRPCSTUBBUFFER iface,
						     REFIID riid)
{
  RemUnkStub *This = (RemUnkStub *)iface;
  TRACE("(%p)->IsIIDSupported(%s)\n", This, debugstr_guid(riid));
  return IsEqualGUID(&IID_IRemUnknown, riid) ? iface : NULL;
}

static ULONG WINAPI RemUnkStub_CountRefs(LPRPCSTUBBUFFER iface)
{
  RemUnkStub *This = (RemUnkStub *)iface;
  FIXME("(%p)->CountRefs()\n", This);
  return 1;
}

static HRESULT WINAPI RemUnkStub_DebugServerQueryInterface(LPRPCSTUBBUFFER iface,
							LPVOID *ppv)
{
  RemUnkStub *This = (RemUnkStub *)iface;
  FIXME("(%p)->DebugServerQueryInterface(%p)\n",This,ppv);
  return E_NOINTERFACE;
}

static void WINAPI RemUnkStub_DebugServerRelease(LPRPCSTUBBUFFER iface,
					      LPVOID pv)
{
  RemUnkStub *This = (RemUnkStub *)iface;
  FIXME("(%p)->DebugServerRelease(%p)\n", This, pv);
}

static const IRpcStubBufferVtbl RemUnkStub_VTable =
{
  RemUnkStub_QueryInterface,
  RemUnkStub_AddRef,
  RemUnkStub_Release,
  RemUnkStub_Connect,
  RemUnkStub_Disconnect,
  RemUnkStub_Invoke,
  RemUnkStub_IsIIDSupported,
  RemUnkStub_CountRefs,
  RemUnkStub_DebugServerQueryInterface,
  RemUnkStub_DebugServerRelease
};

static HRESULT RemUnkStub_Construct(IRpcStubBuffer **ppStub)
{
    RemUnkStub *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
    if (!This) return E_OUTOFMEMORY;
    This->lpVtbl = &RemUnkStub_VTable;
667
    This->refs = 1;
668 669 670 671 672 673 674 675 676
    This->iface = NULL;
    *ppStub = (IRpcStubBuffer*)This;
    return S_OK;
}


typedef struct _RemUnkProxy {
    const IRemUnknownVtbl		*lpvtbl_remunk;
    const IRpcProxyBufferVtbl	*lpvtbl_proxy;
677
    LONG				refs;
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700

    IRpcChannelBuffer			*chan;
    IUnknown *outer_unknown;
} RemUnkProxy;

static HRESULT WINAPI RemUnkProxy_QueryInterface(LPREMUNKNOWN iface, REFIID riid, void **ppv)
{
    RemUnkProxy *This = (RemUnkProxy *)iface;
    if (This->outer_unknown)
        return IUnknown_QueryInterface(This->outer_unknown, riid, ppv);
    if (IsEqualIID(riid, &IID_IUnknown) ||
        IsEqualIID(riid, &IID_IRemUnknown))
    {
        IRemUnknown_AddRef(iface);
        *ppv = (LPVOID)iface;
        return S_OK;
    }
    return E_NOINTERFACE;
}

static ULONG WINAPI RemUnkProxy_AddRef(LPREMUNKNOWN iface)
{
  RemUnkProxy *This = (RemUnkProxy *)iface;
701
  ULONG refs;
702 703

  TRACE("(%p)->AddRef()\n",This);
704 705 706 707 708 709

  if (This->outer_unknown)
      refs = IUnknown_AddRef(This->outer_unknown);
  else
      refs = InterlockedIncrement(&This->refs);
  return refs;
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
}

static ULONG WINAPI RemUnkProxy_Release(LPREMUNKNOWN iface)
{
  RemUnkProxy *This = (RemUnkProxy *)iface;
  ULONG refs;

  TRACE("(%p)->Release()\n",This);
  if (This->outer_unknown)
      refs = IUnknown_Release(This->outer_unknown);
  else    
      refs = InterlockedDecrement(&This->refs);

  if (!refs) {
      if (This->chan) IRpcChannelBuffer_Release(This->chan);
      HeapFree(GetProcessHeap(),0,This);
  }
  return refs;
}

static HRESULT WINAPI RemUnkProxy_RemQueryInterface(LPREMUNKNOWN iface,
						 REFIPID ripid,
						 ULONG cRefs,
						 USHORT cIids,
						 IID* iids,
						 REMQIRESULT** ppQIResults)
{
  RemUnkProxy *This = (RemUnkProxy *)iface;
  RPCOLEMESSAGE msg;
  HRESULT hr = S_OK;
  ULONG status;

  TRACE("(%p)->(%s,%ld,%d,%p,%p)\n",This,
	debugstr_guid(ripid),cRefs,cIids,iids,ppQIResults);

  *ppQIResults = NULL;
  memset(&msg, 0, sizeof(msg));
  msg.iMethod = 3;
  msg.cbBuffer = sizeof(IPID) + sizeof(ULONG) +
    sizeof(USHORT) + cIids*sizeof(IID);
  hr = IRpcChannelBuffer_GetBuffer(This->chan, &msg, &IID_IRemUnknown);
  if (SUCCEEDED(hr)) {
    LPBYTE buf = msg.Buffer;
    memcpy(buf, ripid, sizeof(IPID));
    buf += sizeof(IPID);
    memcpy(buf, &cRefs, sizeof(ULONG));
    buf += sizeof(ULONG);
    memcpy(buf, &cIids, sizeof(USHORT));
    buf += sizeof(USHORT);
    memcpy(buf, iids, cIids*sizeof(IID));

    hr = IRpcChannelBuffer_SendReceive(This->chan, &msg, &status);

763 764 765 766 767 768 769
    buf = msg.Buffer;

    if (SUCCEEDED(hr)) {
        hr = *(HRESULT *)buf;
        buf += sizeof(HRESULT);
    }

770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
    if (SUCCEEDED(hr)) {
      *ppQIResults = CoTaskMemAlloc(cIids*sizeof(REMQIRESULT));
      memcpy(*ppQIResults, buf, cIids*sizeof(REMQIRESULT));
    }

    IRpcChannelBuffer_FreeBuffer(This->chan, &msg);
  }

  return hr;
}

static HRESULT WINAPI RemUnkProxy_RemAddRef(LPREMUNKNOWN iface,
					 USHORT cInterfaceRefs,
					 REMINTERFACEREF* InterfaceRefs,
					 HRESULT* pResults)
{
  RemUnkProxy *This = (RemUnkProxy *)iface;
  RPCOLEMESSAGE msg;
  HRESULT hr = S_OK;
  ULONG status;

  TRACE("(%p)->(%d,%p,%p)\n",This,
	cInterfaceRefs,InterfaceRefs,pResults);

  memset(&msg, 0, sizeof(msg));
  msg.iMethod = 4;
  msg.cbBuffer = sizeof(USHORT) + cInterfaceRefs*sizeof(REMINTERFACEREF);
  hr = IRpcChannelBuffer_GetBuffer(This->chan, &msg, &IID_IRemUnknown);
  if (SUCCEEDED(hr)) {
    LPBYTE buf = msg.Buffer;
    memcpy(buf, &cInterfaceRefs, sizeof(USHORT));
    buf += sizeof(USHORT);
    memcpy(buf, InterfaceRefs, cInterfaceRefs*sizeof(REMINTERFACEREF));

    hr = IRpcChannelBuffer_SendReceive(This->chan, &msg, &status);

    if (SUCCEEDED(hr)) {
      buf = msg.Buffer;
      memcpy(pResults, buf, cInterfaceRefs*sizeof(HRESULT));
    }

    IRpcChannelBuffer_FreeBuffer(This->chan, &msg);
  }

  return hr;
}

static HRESULT WINAPI RemUnkProxy_RemRelease(LPREMUNKNOWN iface,
					  USHORT cInterfaceRefs,
					  REMINTERFACEREF* InterfaceRefs)
{
  RemUnkProxy *This = (RemUnkProxy *)iface;
  RPCOLEMESSAGE msg;
  HRESULT hr = S_OK;
  ULONG status;

  TRACE("(%p)->(%d,%p)\n",This,
	cInterfaceRefs,InterfaceRefs);

  memset(&msg, 0, sizeof(msg));
  msg.iMethod = 5;
  msg.cbBuffer = sizeof(USHORT) + cInterfaceRefs*sizeof(REMINTERFACEREF);
  hr = IRpcChannelBuffer_GetBuffer(This->chan, &msg, &IID_IRemUnknown);
  if (SUCCEEDED(hr)) {
    LPBYTE buf = msg.Buffer;
    memcpy(buf, &cInterfaceRefs, sizeof(USHORT));
    buf += sizeof(USHORT);
    memcpy(buf, InterfaceRefs, cInterfaceRefs*sizeof(REMINTERFACEREF));

    hr = IRpcChannelBuffer_SendReceive(This->chan, &msg, &status);

    IRpcChannelBuffer_FreeBuffer(This->chan, &msg);
  }

  return hr;
}

static const IRemUnknownVtbl RemUnkProxy_VTable =
{
  RemUnkProxy_QueryInterface,
  RemUnkProxy_AddRef,
  RemUnkProxy_Release,
  RemUnkProxy_RemQueryInterface,
  RemUnkProxy_RemAddRef,
  RemUnkProxy_RemRelease
};


static HRESULT WINAPI RURpcProxyBufferImpl_QueryInterface(LPRPCPROXYBUFFER iface,REFIID riid,LPVOID *ppv) {
    *ppv = NULL;
    if (IsEqualIID(riid,&IID_IRpcProxyBuffer)||IsEqualIID(riid,&IID_IUnknown)) {
	IRpcProxyBuffer_AddRef(iface);
	*ppv = (LPVOID)iface;
	return S_OK;
    }
    FIXME("(%s), no interface.\n",debugstr_guid(riid));
    return E_NOINTERFACE;
}

static ULONG WINAPI RURpcProxyBufferImpl_AddRef(LPRPCPROXYBUFFER iface) {
    ICOM_THIS_MULTI(RemUnkProxy,lpvtbl_proxy,iface);
871
    TRACE("%p, %ld\n", iface, This->refs + 1);
872 873 874 875 876 877
    return InterlockedIncrement(&This->refs);
}

static ULONG WINAPI RURpcProxyBufferImpl_Release(LPRPCPROXYBUFFER iface) {
    ICOM_THIS_MULTI(RemUnkProxy,lpvtbl_proxy,iface);
    ULONG ref = InterlockedDecrement(&This->refs);
878
    TRACE("%p, %ld\n", iface, ref);
879
    if (!ref) {
880 881
        IRpcProxyBuffer_Disconnect(iface);
        HeapFree(GetProcessHeap(),0,This);
882 883 884 885 886 887 888
    }
    return ref;
}

static HRESULT WINAPI RURpcProxyBufferImpl_Connect(LPRPCPROXYBUFFER iface,IRpcChannelBuffer* pRpcChannelBuffer) {
    ICOM_THIS_MULTI(RemUnkProxy,lpvtbl_proxy,iface);

889
    TRACE("%p, %p\n", iface, pRpcChannelBuffer);
890 891 892 893 894 895
    This->chan = pRpcChannelBuffer;
    IRpcChannelBuffer_AddRef(This->chan);
    return S_OK;
}
static void WINAPI RURpcProxyBufferImpl_Disconnect(LPRPCPROXYBUFFER iface) {
    ICOM_THIS_MULTI(RemUnkProxy,lpvtbl_proxy,iface);
896
    TRACE("%p, %p\n", iface, This->chan);
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
    if (This->chan) {
	IRpcChannelBuffer_Release(This->chan);
	This->chan = NULL;
    }
}


static const IRpcProxyBufferVtbl RURpcProxyBuffer_VTable = {
    RURpcProxyBufferImpl_QueryInterface,
    RURpcProxyBufferImpl_AddRef,
    RURpcProxyBufferImpl_Release,
    RURpcProxyBufferImpl_Connect,
    RURpcProxyBufferImpl_Disconnect
};

static HRESULT
RemUnkProxy_Construct(IUnknown *pUnkOuter, LPVOID *ppv,LPVOID *ppProxy) {
    RemUnkProxy *This;

    This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*This));
    if (!This)
	return E_OUTOFMEMORY;

    This->lpvtbl_remunk	= &RemUnkProxy_VTable;
    This->lpvtbl_proxy	= &RURpcProxyBuffer_VTable;
    /* only one reference for the proxy buffer */
    This->refs		= 1;
    This->outer_unknown = pUnkOuter;
    *ppv		= &(This->lpvtbl_remunk);
    *ppProxy		= &(This->lpvtbl_proxy);
927 928
    /* and one reference for the object */
    IUnknown_AddRef((IUnknown *)*ppv);
929 930 931 932
    return S_OK;
}


933 934 935 936
/********************* OLE Proxy/Stub Factory ********************************/
static HRESULT WINAPI
PSFacBuf_QueryInterface(LPPSFACTORYBUFFER iface, REFIID iid, LPVOID *ppv) {
    if (IsEqualIID(iid,&IID_IPSFactoryBuffer)||IsEqualIID(iid,&IID_IUnknown)) {
937
	*ppv = (LPVOID)iface;
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
	/* No ref counting, static class */
	return S_OK;
    }
    FIXME("(%s) unknown IID?\n",debugstr_guid(iid));
    return E_NOINTERFACE;
}

static ULONG WINAPI PSFacBuf_AddRef(LPPSFACTORYBUFFER iface) { return 2; }
static ULONG WINAPI PSFacBuf_Release(LPPSFACTORYBUFFER iface) { return 1; }

static HRESULT WINAPI
PSFacBuf_CreateProxy(
    LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
    IRpcProxyBuffer **ppProxy, LPVOID *ppv
) {
953
    if (IsEqualIID(&IID_IClassFactory,riid))
954
	return CFProxy_Construct(pUnkOuter, ppv,(LPVOID*)ppProxy);
955 956
    else if (IsEqualIID(&IID_IRemUnknown,riid))
	return RemUnkProxy_Construct(pUnkOuter, ppv,(LPVOID*)ppProxy);
957 958 959 960 961 962 963 964 965 966 967 968 969
    FIXME("proxying not implemented for (%s) yet!\n",debugstr_guid(riid));
    return E_FAIL;
}

static HRESULT WINAPI
PSFacBuf_CreateStub(
    LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
    IRpcStubBuffer** ppStub
) {
    HRESULT hres;

    TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);

970 971
    if (IsEqualIID(&IID_IClassFactory, riid) ||
        IsEqualIID(&IID_IUnknown, riid) /* FIXME: fixup stub manager and remove this*/) {
972 973 974 975
	hres = CFStub_Construct(ppStub);
	if (!hres)
	    IRpcStubBuffer_Connect((*ppStub),pUnkServer);
	return hres;
976 977 978 979 980
    } else if (IsEqualIID(&IID_IRemUnknown,riid)) {
	hres = RemUnkStub_Construct(ppStub);
	if (!hres)
	    IRpcStubBuffer_Connect((*ppStub),pUnkServer);
	return hres;
981 982 983 984 985
    }
    FIXME("stubbing not implemented for (%s) yet!\n",debugstr_guid(riid));
    return E_FAIL;
}

986
static const IPSFactoryBufferVtbl psfacbufvtbl = {
987 988 989 990 991 992 993 994
    PSFacBuf_QueryInterface,
    PSFacBuf_AddRef,
    PSFacBuf_Release,
    PSFacBuf_CreateProxy,
    PSFacBuf_CreateStub
};

/* This is the whole PSFactoryBuffer object, just the vtableptr */
995
static const IPSFactoryBufferVtbl *lppsfac = &psfacbufvtbl;
996 997

/***********************************************************************
998
 *           DllGetClassObject [OLE32.@]
999
 */
1000
HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
1001 1002
{
    *ppv = NULL;
1003 1004
    if (IsEqualIID(rclsid, &CLSID_PSFactoryBuffer))
        return IPSFactoryBuffer_QueryInterface((IPSFactoryBuffer *)&lppsfac, iid, ppv);
1005 1006 1007 1008 1009
    if (IsEqualIID(rclsid,&CLSID_DfMarshal)&&(
		IsEqualIID(iid,&IID_IClassFactory) ||
		IsEqualIID(iid,&IID_IUnknown)
	)
    )
1010
	return MARSHAL_GetStandardMarshalCF(ppv);
1011 1012
    if (IsEqualIID(rclsid,&CLSID_StdGlobalInterfaceTable) && (IsEqualIID(iid,&IID_IClassFactory) || IsEqualIID(iid,&IID_IUnknown)))
        return StdGlobalInterfaceTable_GetFactory(ppv);
1013 1014 1015 1016
    if (IsEqualCLSID(rclsid, &CLSID_FileMoniker))
        return FileMonikerCF_Create(iid, ppv);
    if (IsEqualCLSID(rclsid, &CLSID_ItemMoniker))
        return ItemMonikerCF_Create(iid, ppv);
1017

1018 1019 1020
    FIXME("\n\tCLSID:\t%s,\n\tIID:\t%s\n",debugstr_guid(rclsid),debugstr_guid(iid));
    return CLASS_E_CLASSNOTAVAILABLE;
}