cproxy.c 13.8 KB
Newer Older
1 2 3
/*
 * COM proxy implementation
 *
4
 * Copyright 2001 Ove Kåven, TransGaming Technologies
5 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
 * 
 * TODO: Handle non-i386 architectures
 */

23 24
#include <stdarg.h>

25 26
#define COBJMACROS

27 28 29 30
#include "windef.h"
#include "winbase.h"
#include "winerror.h"

31
#include "objbase.h"
32 33 34 35 36 37 38 39 40 41 42 43 44
#include "rpcproxy.h"

#include "cpsf.h"
#include "ndr_misc.h"
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(ole);

struct StublessThunk;

/* I don't know what MS's std proxy structure looks like,
   so this probably doesn't match, but that shouldn't matter */
typedef struct {
45
  const IRpcProxyBufferVtbl *lpVtbl;
46
  LPVOID *PVtbl;
47
  LONG RefCount;
48 49 50
  const MIDL_STUBLESS_PROXY_INFO *stubless;
  const IID* piid;
  LPUNKNOWN pUnkOuter;
51
  PCInterfaceName name;
52 53 54 55 56
  LPPSFACTORYBUFFER pPSFactory;
  LPRPCCHANNELBUFFER pChannel;
  struct StublessThunk *thunks;
} StdProxyImpl;

57
static const IRpcProxyBufferVtbl StdProxy_Vtbl;
58

59 60
#define ICOM_THIS_MULTI(impl,field,iface) impl* const This=(impl*)((char*)(iface) - offsetof(impl,field))

61 62
#if defined(__i386__)

63 64
#include "pshpack1.h"

65
struct StublessThunk {
66 67 68 69 70 71
  BYTE push;
  DWORD index;
  BYTE call;
  LONG handler;
  BYTE ret;
  WORD bytes;
72 73 74
  BYTE pad[3];
};

75 76
#include "poppack.h"

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
/* adjust the stack size since we don't use Windows's method */
#define STACK_ADJUST sizeof(DWORD)

#define FILL_STUBLESS(x,idx,stk) \
 x->push = 0x68; /* pushl [immediate] */ \
 x->index = (idx); \
 x->call = 0xe8; /* call [near] */ \
 x->handler = (char*)ObjectStubless - (char*)&x->ret; \
 x->ret = 0xc2; /* ret [immediate] */ \
 x->bytes = stk; \
 x->pad[0] = 0x8d; /* leal (%esi),%esi */ \
 x->pad[1] = 0x76; \
 x->pad[2] = 0x00;

static HRESULT WINAPI ObjectStubless(DWORD index)
{
  char *args = (char*)(&index + 2);
  LPVOID iface = *(LPVOID*)args;

  ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);

  PFORMAT_STRING fs = This->stubless->ProcFormatString + This->stubless->FormatStringOffset[index];
Eric Pouech's avatar
Eric Pouech committed
99
  unsigned bytes = *(const WORD*)(fs+8) - STACK_ADJUST;
100
  TRACE("(%p)->(%d)([%d bytes]) ret=%08x\n", iface, index, bytes, *(DWORD*)(args+bytes));
101

102
  return NdrClientCall2(This->stubless->pStubDesc, fs, args);
103 104
}

105 106 107 108 109 110 111 112 113 114
#else  /* __i386__ */

/* can't do that on this arch */
struct StublessThunk { int dummy; };
#define FILL_STUBLESS(x,idx,stk) \
 ERR("stubless proxies are not supported on this architecture\n");
#define STACK_ADJUST 0

#endif  /* __i386__ */

115 116
HRESULT WINAPI StdProxy_Construct(REFIID riid,
                                 LPUNKNOWN pUnkOuter,
117 118
                                 const ProxyFileInfo *ProxyInfo,
                                 int Index,
119 120 121 122 123 124
                                 LPPSFACTORYBUFFER pPSFactory,
                                 LPRPCPROXYBUFFER *ppProxy,
                                 LPVOID *ppvObj)
{
  StdProxyImpl *This;
  const MIDL_STUBLESS_PROXY_INFO *stubless = NULL;
125 126
  PCInterfaceName name = ProxyInfo->pNamesArray[Index];
  CInterfaceProxyVtbl *vtbl = ProxyInfo->pProxyVtblList[Index];
127

128
  TRACE("(%p,%p,%p,%p,%p) %s\n", pUnkOuter, vtbl, pPSFactory, ppProxy, ppvObj, name);
129

130 131
  /* TableVersion = 2 means it is the stubless version of CInterfaceProxyVtbl */
  if (ProxyInfo->TableVersion > 1) {
Hans Leidekker's avatar
Hans Leidekker committed
132 133
    stubless = *(const void **)vtbl;
    vtbl = (CInterfaceProxyVtbl *)((const void **)vtbl + 1);
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    TRACE("stubless=%p\n", stubless);
  }

  TRACE("iid=%s\n", debugstr_guid(vtbl->header.piid));
  TRACE("vtbl=%p\n", vtbl->Vtbl);

  if (!IsEqualGUID(vtbl->header.piid, riid)) {
    ERR("IID mismatch during proxy creation\n");
    return RPC_E_UNEXPECTED;
  }

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

  if (stubless) {
149 150
    CInterfaceStubVtbl *svtbl = ProxyInfo->pStubVtblList[Index];
    unsigned long i, count = svtbl->header.DispatchTableCount;
151 152 153
    /* Maybe the original vtbl is just modified directly to point at
     * ObjectStublessClientXXX thunks in real Windows, but I don't like it
     */
154
    TRACE("stubless thunks: count=%ld\n", count);
155 156 157 158 159 160
    This->thunks = HeapAlloc(GetProcessHeap(),0,sizeof(struct StublessThunk)*count);
    This->PVtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPVOID)*count);
    for (i=0; i<count; i++) {
      struct StublessThunk *thunk = &This->thunks[i];
      if (vtbl->Vtbl[i] == (LPVOID)-1) {
        PFORMAT_STRING fs = stubless->ProcFormatString + stubless->FormatStringOffset[i];
Eric Pouech's avatar
Eric Pouech committed
161
        unsigned bytes = *(const WORD*)(fs+8) - STACK_ADJUST;
162
        TRACE("method %ld: stacksize=%d\n", i, bytes);
163 164 165 166 167 168 169 170 171 172 173 174 175
        FILL_STUBLESS(thunk, i, bytes)
        This->PVtbl[i] = thunk;
      }
      else {
        memset(thunk, 0, sizeof(struct StublessThunk));
        This->PVtbl[i] = vtbl->Vtbl[i];
      }
    }
  }
  else 
    This->PVtbl = vtbl->Vtbl;

  This->lpVtbl = &StdProxy_Vtbl;
176 177
  /* one reference for the proxy */
  This->RefCount = 1;
178 179 180
  This->stubless = stubless;
  This->piid = vtbl->header.piid;
  This->pUnkOuter = pUnkOuter;
181
  This->name = name;
182 183 184 185
  This->pPSFactory = pPSFactory;
  This->pChannel = NULL;
  *ppProxy = (LPRPCPROXYBUFFER)&This->lpVtbl;
  *ppvObj = &This->PVtbl;
186 187 188 189 190
  /* if there is no outer unknown then the caller will control the lifetime
   * of the proxy object through the proxy buffer, so no need to increment the
   * ref count of the proxy object */
  if (pUnkOuter)
    IUnknown_AddRef((IUnknown *)*ppvObj);
191 192 193 194 195
  IPSFactoryBuffer_AddRef(pPSFactory);

  return S_OK;
}

196
static void StdProxy_Destruct(LPRPCPROXYBUFFER iface)
197 198 199
{
  ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);

200 201 202
  if (This->pChannel)
    IRpcProxyBuffer_Disconnect(iface);

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
  IPSFactoryBuffer_Release(This->pPSFactory);
  if (This->thunks) {
    HeapFree(GetProcessHeap(),0,This->PVtbl);
    HeapFree(GetProcessHeap(),0,This->thunks);
  }
  HeapFree(GetProcessHeap(),0,This);
}

static HRESULT WINAPI StdProxy_QueryInterface(LPRPCPROXYBUFFER iface,
                                             REFIID riid,
                                             LPVOID *obj)
{
  ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
  TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);

  if (IsEqualGUID(&IID_IUnknown,riid) ||
      IsEqualGUID(This->piid,riid)) {
    *obj = &This->PVtbl;
221
    InterlockedIncrement(&This->RefCount);
222 223 224 225 226
    return S_OK;
  }

  if (IsEqualGUID(&IID_IRpcProxyBuffer,riid)) {
    *obj = &This->lpVtbl;
227
    InterlockedIncrement(&This->RefCount);
228 229 230 231 232 233 234 235 236 237 238
    return S_OK;
  }

  return E_NOINTERFACE;
}

static ULONG WINAPI StdProxy_AddRef(LPRPCPROXYBUFFER iface)
{
  ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
  TRACE("(%p)->AddRef()\n",This);

239
  return InterlockedIncrement(&This->RefCount);
240 241 242 243
}

static ULONG WINAPI StdProxy_Release(LPRPCPROXYBUFFER iface)
{
244
  ULONG refs;
245 246 247
  ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
  TRACE("(%p)->Release()\n",This);

248 249
  refs = InterlockedDecrement(&This->RefCount);
  if (!refs)
250
    StdProxy_Destruct((LPRPCPROXYBUFFER)&This->lpVtbl);
251
  return refs;
252 253 254 255 256 257 258 259 260
}

static HRESULT WINAPI StdProxy_Connect(LPRPCPROXYBUFFER iface,
                                      LPRPCCHANNELBUFFER pChannel)
{
  ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
  TRACE("(%p)->Connect(%p)\n",This,pChannel);

  This->pChannel = pChannel;
261
  IRpcChannelBuffer_AddRef(pChannel);
262 263 264 265 266 267 268 269
  return S_OK;
}

static VOID WINAPI StdProxy_Disconnect(LPRPCPROXYBUFFER iface)
{
  ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
  TRACE("(%p)->Disconnect()\n",This);

270
  IRpcChannelBuffer_Release(This->pChannel);
271 272 273
  This->pChannel = NULL;
}

274
static const IRpcProxyBufferVtbl StdProxy_Vtbl =
275 276 277 278 279 280 281 282
{
  StdProxy_QueryInterface,
  StdProxy_AddRef,
  StdProxy_Release,
  StdProxy_Connect,
  StdProxy_Disconnect
};

283
static void StdProxy_GetChannel(LPVOID iface,
284
                                   LPRPCCHANNELBUFFER *ppChannel)
285 286
{
  ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
287
  TRACE("(%p)->GetChannel(%p) %s\n",This,ppChannel,This->name);
288 289 290 291

  *ppChannel = This->pChannel;
}

292
static void StdProxy_GetIID(LPVOID iface,
293
                               const IID **ppiid)
294 295
{
  ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
296
  TRACE("(%p)->GetIID(%p) %s\n",This,ppiid,This->name);
297 298 299 300 301 302 303 304 305

  *ppiid = This->piid;
}

HRESULT WINAPI IUnknown_QueryInterface_Proxy(LPUNKNOWN iface,
                                            REFIID riid,
                                            LPVOID *ppvObj)
{
  ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
306
  TRACE("(%p)->QueryInterface(%s,%p) %s\n",This,debugstr_guid(riid),ppvObj,This->name);
307 308 309 310 311 312
  return IUnknown_QueryInterface(This->pUnkOuter,riid,ppvObj);
}

ULONG WINAPI IUnknown_AddRef_Proxy(LPUNKNOWN iface)
{
  ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
313
  TRACE("(%p)->AddRef() %s\n",This,This->name);
314 315 316 317 318 319
  return IUnknown_AddRef(This->pUnkOuter);
}

ULONG WINAPI IUnknown_Release_Proxy(LPUNKNOWN iface)
{
  ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
320
  TRACE("(%p)->Release() %s\n",This,This->name);
321 322
  return IUnknown_Release(This->pUnkOuter);
}
323

324 325 326 327 328 329 330 331 332 333 334
/***********************************************************************
 *           NdrProxyInitialize [RPCRT4.@]
 */
void WINAPI NdrProxyInitialize(void *This,
                              PRPC_MESSAGE pRpcMsg,
                              PMIDL_STUB_MESSAGE pStubMsg,
                              PMIDL_STUB_DESC pStubDescriptor,
                              unsigned int ProcNum)
{
  TRACE("(%p,%p,%p,%p,%d)\n", This, pRpcMsg, pStubMsg, pStubDescriptor, ProcNum);
  NdrClientInitializeNew(pRpcMsg, pStubMsg, pStubDescriptor, ProcNum);
335 336 337 338
  StdProxy_GetChannel(This, &pStubMsg->pRpcChannelBuffer);
  IRpcChannelBuffer_GetDestCtx(pStubMsg->pRpcChannelBuffer,
                               &pStubMsg->dwDestContext,
                               &pStubMsg->pvDestContext);
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
  TRACE("channel=%p\n", pStubMsg->pRpcChannelBuffer);
}

/***********************************************************************
 *           NdrProxyGetBuffer [RPCRT4.@]
 */
void WINAPI NdrProxyGetBuffer(void *This,
                             PMIDL_STUB_MESSAGE pStubMsg)
{
  HRESULT hr;
  const IID *riid = NULL;

  TRACE("(%p,%p)\n", This, pStubMsg);
  pStubMsg->RpcMsg->BufferLength = pStubMsg->BufferLength;
  pStubMsg->dwStubPhase = PROXY_GETBUFFER;
354
  StdProxy_GetIID(This, &riid);
355 356 357
  hr = IRpcChannelBuffer_GetBuffer(pStubMsg->pRpcChannelBuffer,
                                  (RPCOLEMESSAGE*)pStubMsg->RpcMsg,
                                  riid);
358 359 360 361 362
  if (FAILED(hr))
  {
    RpcRaiseException(hr);
    return;
  }
363
  pStubMsg->fBufferValid = TRUE;
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
  pStubMsg->BufferStart = pStubMsg->RpcMsg->Buffer;
  pStubMsg->BufferEnd = pStubMsg->BufferStart + pStubMsg->BufferLength;
  pStubMsg->Buffer = pStubMsg->BufferStart;
  pStubMsg->dwStubPhase = PROXY_MARSHAL;
}

/***********************************************************************
 *           NdrProxySendReceive [RPCRT4.@]
 */
void WINAPI NdrProxySendReceive(void *This,
                               PMIDL_STUB_MESSAGE pStubMsg)
{
  ULONG Status = 0;
  HRESULT hr;

  TRACE("(%p,%p)\n", This, pStubMsg);

  if (!pStubMsg->pRpcChannelBuffer)
  {
    WARN("Trying to use disconnected proxy %p\n", This);
    RpcRaiseException(RPC_E_DISCONNECTED);
  }

  pStubMsg->dwStubPhase = PROXY_SENDRECEIVE;
388 389
  /* avoid sending uninitialised parts of the buffer on the wire */
  pStubMsg->RpcMsg->BufferLength = pStubMsg->Buffer - (unsigned char *)pStubMsg->RpcMsg->Buffer;
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
  hr = IRpcChannelBuffer_SendReceive(pStubMsg->pRpcChannelBuffer,
                                    (RPCOLEMESSAGE*)pStubMsg->RpcMsg,
                                    &Status);
  pStubMsg->dwStubPhase = PROXY_UNMARSHAL;
  pStubMsg->BufferLength = pStubMsg->RpcMsg->BufferLength;
  pStubMsg->BufferStart = pStubMsg->RpcMsg->Buffer;
  pStubMsg->BufferEnd = pStubMsg->BufferStart + pStubMsg->BufferLength;
  pStubMsg->Buffer = pStubMsg->BufferStart;

  /* raise exception if call failed */
  if (hr == RPC_S_CALL_FAILED) RpcRaiseException(*(DWORD*)pStubMsg->Buffer);
  else if (FAILED(hr)) RpcRaiseException(hr);
}

/***********************************************************************
 *           NdrProxyFreeBuffer [RPCRT4.@]
 */
void WINAPI NdrProxyFreeBuffer(void *This,
                              PMIDL_STUB_MESSAGE pStubMsg)
{
  TRACE("(%p,%p)\n", This, pStubMsg);
411 412 413 414 415 416 417

  if (pStubMsg->fBufferValid)
  {
    IRpcChannelBuffer_FreeBuffer(pStubMsg->pRpcChannelBuffer,
                                 (RPCOLEMESSAGE*)pStubMsg->RpcMsg);
    pStubMsg->fBufferValid = TRUE;
  }
418 419 420 421 422 423 424
}

/***********************************************************************
 *           NdrProxyErrorHandler [RPCRT4.@]
 */
HRESULT WINAPI NdrProxyErrorHandler(DWORD dwExceptionCode)
{
425
  WARN("(0x%08x): a proxy call failed\n", dwExceptionCode);
426 427 428 429 430 431 432

  if (FAILED(dwExceptionCode))
    return dwExceptionCode;
  else
    return HRESULT_FROM_WIN32(dwExceptionCode);
}

433 434 435 436
HRESULT WINAPI
CreateProxyFromTypeInfo( LPTYPEINFO pTypeInfo, LPUNKNOWN pUnkOuter, REFIID riid,
                         LPRPCPROXYBUFFER *ppProxy, LPVOID *ppv )
{
437 438
    typedef INT (WINAPI *MessageBoxA)(HWND,LPCSTR,LPCSTR,UINT);
    HMODULE hUser32 = LoadLibraryA("user32");
439
    MessageBoxA pMessageBoxA = (void *)GetProcAddress(hUser32, "MessageBoxA");
440

441
    FIXME("%p %p %s %p %p\n", pTypeInfo, pUnkOuter, debugstr_guid(riid), ppProxy, ppv);
442 443 444 445 446 447 448 449 450
    if (pMessageBoxA)
    {
        pMessageBoxA(NULL,
            "The native implementation of OLEAUT32.DLL cannot be used "
            "with Wine's RPCRT4.DLL. Remove OLEAUT32.DLL and try again.\n",
            "Wine: Unimplemented CreateProxyFromTypeInfo",
            0x10);
        ExitProcess(1);
    }
451 452
    return E_NOTIMPL;
}