2 * COM stub (CStdStubBuffer) implementation
4 * Copyright 2001 Ove Kåven, TransGaming Technologies
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
33 #include "wine/debug.h"
34 #include "wine/exception.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(ole);
40 #define STUB_HEADER(This) (((const CInterfaceStubHeader*)((This)->lpVtbl))[-1])
42 static WINE_EXCEPTION_FILTER(stub_filter)
44 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
45 return EXCEPTION_CONTINUE_SEARCH;
46 return EXCEPTION_EXECUTE_HANDLER;
51 IUnknownVtbl *base_obj;
52 IRpcStubBuffer *base_stub;
53 CStdStubBuffer stub_buffer;
54 } cstdstubbuffer_delegating_t;
56 static inline cstdstubbuffer_delegating_t *impl_from_delegating( IRpcStubBuffer *iface )
58 return (cstdstubbuffer_delegating_t*)((char *)iface - FIELD_OFFSET(cstdstubbuffer_delegating_t, stub_buffer));
61 HRESULT WINAPI CStdStubBuffer_Construct(REFIID riid,
64 CInterfaceStubVtbl *vtbl,
65 LPPSFACTORYBUFFER pPSFactory,
66 LPRPCSTUBBUFFER *ppStub)
71 TRACE("(%p,%p,%p,%p) %s\n", pUnkServer, vtbl, pPSFactory, ppStub, name);
72 TRACE("iid=%s\n", debugstr_guid(vtbl->header.piid));
73 TRACE("vtbl=%p\n", &vtbl->Vtbl);
75 if (!IsEqualGUID(vtbl->header.piid, riid)) {
76 ERR("IID mismatch during stub creation\n");
77 return RPC_E_UNEXPECTED;
80 r = IUnknown_QueryInterface(pUnkServer, riid, (void**)&pvServer);
84 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CStdStubBuffer));
86 IUnknown_Release(pvServer);
90 This->lpVtbl = &vtbl->Vtbl;
92 This->pvServerObject = pvServer;
93 This->pPSFactory = pPSFactory;
94 *ppStub = (LPRPCSTUBBUFFER)This;
96 IPSFactoryBuffer_AddRef(pPSFactory);
100 static CRITICAL_SECTION delegating_vtbl_section;
101 static CRITICAL_SECTION_DEBUG critsect_debug =
103 0, 0, &delegating_vtbl_section,
104 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
105 0, 0, { (DWORD_PTR)(__FILE__ ": delegating_vtbl_section") }
107 static CRITICAL_SECTION delegating_vtbl_section = { &critsect_debug, -1, 0, 0, 0, 0 };
117 ref_counted_vtbl *table;
122 static HRESULT WINAPI delegating_QueryInterface(IUnknown *pUnk, REFIID iid, void **ppv)
128 static ULONG WINAPI delegating_AddRef(IUnknown *pUnk)
133 static ULONG WINAPI delegating_Release(IUnknown *pUnk)
138 #if defined(__i386__)
140 /* The idea here is to replace the first param on the stack
141 ie. This (which will point to cstdstubbuffer_delegating_t)
142 with This->stub_buffer.pvServerObject and then jump to the
143 relevant offset in This->stub_buffer.pvServerObject's vtbl.
145 #include "pshpack1.h"
147 DWORD mov1; /* mov 0x4(%esp), %eax 8b 44 24 04 */
148 WORD mov2; /* mov 0x10(%eax), %eax 8b 40 */
149 BYTE sixteen; /* 10 */
150 DWORD mov3; /* mov %eax, 0x4(%esp) 89 44 24 04 */
151 WORD mov4; /* mov (%eax), %eax 8b 00 */
152 WORD mov5; /* mov offset(%eax), %eax 8b 80 */
153 DWORD offset; /* xx xx xx xx */
154 WORD jmp; /* jmp *%eax ff e0 */
155 BYTE pad[3]; /* lea 0x0(%esi), %esi 8d 76 00 */
159 static void fill_table(IUnknownVtbl *vtbl, DWORD num)
161 vtbl_method_t *method;
165 vtbl->QueryInterface = delegating_QueryInterface;
166 vtbl->AddRef = delegating_AddRef;
167 vtbl->Release = delegating_Release;
169 method = (vtbl_method_t*)((void **)vtbl + num);
170 entry = (void**)(vtbl + 1);
172 for(i = 3; i < num; i++)
175 method->mov1 = 0x0424448b;
176 method->mov2 = 0x408b;
177 method->sixteen = 0x10;
178 method->mov3 = 0x04244489;
179 method->mov4 = 0x008b;
180 method->mov5 = 0x808b;
181 method->offset = i << 2;
182 method->jmp = 0xe0ff;
183 method->pad[0] = 0x8d;
184 method->pad[1] = 0x76;
185 method->pad[2] = 0x00;
194 typedef struct {int dummy;} vtbl_method_t;
195 static void fill_table(IUnknownVtbl *vtbl, DWORD num)
197 ERR("delegated stubs are not supported on this architecture\n");
200 #endif /* __i386__ */
202 void create_delegating_vtbl(DWORD num_methods)
204 TRACE("%d\n", num_methods);
207 ERR("should have more than %d methods\n", num_methods);
211 EnterCriticalSection(&delegating_vtbl_section);
212 if(num_methods > current_vtbl.size)
215 if(current_vtbl.table && current_vtbl.table->ref == 0)
217 TRACE("freeing old table\n");
218 HeapFree(GetProcessHeap(), 0, current_vtbl.table);
220 size = sizeof(DWORD) + num_methods * sizeof(void*) + (num_methods - 3) * sizeof(vtbl_method_t);
221 current_vtbl.table = HeapAlloc(GetProcessHeap(), 0, size);
222 fill_table(¤t_vtbl.table->vtbl, num_methods);
223 current_vtbl.table->ref = 0;
224 current_vtbl.size = num_methods;
226 LeaveCriticalSection(&delegating_vtbl_section);
229 static IUnknownVtbl *get_delegating_vtbl(void)
233 EnterCriticalSection(&delegating_vtbl_section);
234 current_vtbl.table->ref++;
235 ret = ¤t_vtbl.table->vtbl;
236 LeaveCriticalSection(&delegating_vtbl_section);
240 static void release_delegating_vtbl(IUnknownVtbl *vtbl)
242 ref_counted_vtbl *table = (ref_counted_vtbl*)((DWORD *)vtbl - 1);
244 EnterCriticalSection(&delegating_vtbl_section);
246 TRACE("ref now %d\n", table->ref);
247 if(table->ref == 0 && table != current_vtbl.table)
249 TRACE("... and we're not current so free'ing\n");
250 HeapFree(GetProcessHeap(), 0, table);
252 LeaveCriticalSection(&delegating_vtbl_section);
255 HRESULT WINAPI CStdStubBuffer_Delegating_Construct(REFIID riid,
256 LPUNKNOWN pUnkServer,
257 PCInterfaceName name,
258 CInterfaceStubVtbl *vtbl,
259 REFIID delegating_iid,
260 LPPSFACTORYBUFFER pPSFactory,
261 LPRPCSTUBBUFFER *ppStub)
263 cstdstubbuffer_delegating_t *This;
267 TRACE("(%p,%p,%p,%p) %s\n", pUnkServer, vtbl, pPSFactory, ppStub, name);
268 TRACE("iid=%s delegating to %s\n", debugstr_guid(vtbl->header.piid), debugstr_guid(delegating_iid));
269 TRACE("vtbl=%p\n", &vtbl->Vtbl);
271 if (!IsEqualGUID(vtbl->header.piid, riid))
273 ERR("IID mismatch during stub creation\n");
274 return RPC_E_UNEXPECTED;
277 r = IUnknown_QueryInterface(pUnkServer, riid, (void**)&pvServer);
278 if(FAILED(r)) return r;
280 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This));
283 IUnknown_Release(pvServer);
284 return E_OUTOFMEMORY;
287 This->base_obj = get_delegating_vtbl();
288 r = create_stub(delegating_iid, (IUnknown*)&This->base_obj, &This->base_stub);
291 release_delegating_vtbl(This->base_obj);
292 HeapFree(GetProcessHeap(), 0, This);
293 IUnknown_Release(pvServer);
297 This->stub_buffer.lpVtbl = &vtbl->Vtbl;
298 This->stub_buffer.RefCount = 1;
299 This->stub_buffer.pvServerObject = pvServer;
300 This->stub_buffer.pPSFactory = pPSFactory;
301 *ppStub = (LPRPCSTUBBUFFER)&This->stub_buffer;
303 IPSFactoryBuffer_AddRef(pPSFactory);
307 HRESULT WINAPI CStdStubBuffer_QueryInterface(LPRPCSTUBBUFFER iface,
311 CStdStubBuffer *This = (CStdStubBuffer *)iface;
312 TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
314 if (IsEqualIID(&IID_IUnknown, riid) ||
315 IsEqualIID(&IID_IRpcStubBuffer, riid))
317 IUnknown_AddRef(iface);
322 return E_NOINTERFACE;
325 ULONG WINAPI CStdStubBuffer_AddRef(LPRPCSTUBBUFFER iface)
327 CStdStubBuffer *This = (CStdStubBuffer *)iface;
328 TRACE("(%p)->AddRef()\n",This);
329 return InterlockedIncrement(&This->RefCount);
332 ULONG WINAPI NdrCStdStubBuffer_Release(LPRPCSTUBBUFFER iface,
333 LPPSFACTORYBUFFER pPSF)
335 CStdStubBuffer *This = (CStdStubBuffer *)iface;
338 TRACE("(%p)->Release()\n",This);
340 refs = InterlockedDecrement(&This->RefCount);
343 /* test_Release shows that native doesn't call Disconnect here.
344 We'll leave it in for the time being. */
345 IRpcStubBuffer_Disconnect(iface);
347 IPSFactoryBuffer_Release(pPSF);
348 HeapFree(GetProcessHeap(),0,This);
353 ULONG WINAPI NdrCStdStubBuffer2_Release(LPRPCSTUBBUFFER iface,
354 LPPSFACTORYBUFFER pPSF)
356 cstdstubbuffer_delegating_t *This = impl_from_delegating( iface );
359 TRACE("(%p)->Release()\n", This);
361 refs = InterlockedDecrement(&This->stub_buffer.RefCount);
364 /* Just like NdrCStdStubBuffer_Release, we shouldn't call
366 IRpcStubBuffer_Disconnect((IRpcStubBuffer *)&This->stub_buffer);
368 IRpcStubBuffer_Release(This->base_stub);
369 release_delegating_vtbl(This->base_obj);
371 IPSFactoryBuffer_Release(pPSF);
372 HeapFree(GetProcessHeap(), 0, This);
378 HRESULT WINAPI CStdStubBuffer_Connect(LPRPCSTUBBUFFER iface,
379 LPUNKNOWN lpUnkServer)
381 CStdStubBuffer *This = (CStdStubBuffer *)iface;
383 IUnknown *new = NULL;
385 TRACE("(%p)->Connect(%p)\n",This,lpUnkServer);
387 r = IUnknown_QueryInterface(lpUnkServer, STUB_HEADER(This).piid, (void**)&new);
388 new = InterlockedExchangePointer((void**)&This->pvServerObject, new);
390 IUnknown_Release(new);
394 void WINAPI CStdStubBuffer_Disconnect(LPRPCSTUBBUFFER iface)
396 CStdStubBuffer *This = (CStdStubBuffer *)iface;
398 TRACE("(%p)->Disconnect()\n",This);
400 old = InterlockedExchangePointer((void**)&This->pvServerObject, NULL);
403 IUnknown_Release(old);
406 HRESULT WINAPI CStdStubBuffer_Invoke(LPRPCSTUBBUFFER iface,
408 LPRPCCHANNELBUFFER pChannel)
410 CStdStubBuffer *This = (CStdStubBuffer *)iface;
411 DWORD dwPhase = STUB_UNMARSHAL;
414 TRACE("(%p)->Invoke(%p,%p)\n",This,pMsg,pChannel);
418 if (STUB_HEADER(This).pDispatchTable)
419 STUB_HEADER(This).pDispatchTable[pMsg->iMethod](iface, pChannel, (PRPC_MESSAGE)pMsg, &dwPhase);
420 else /* pure interpreted */
421 NdrStubCall2(iface, pChannel, (PRPC_MESSAGE)pMsg, &dwPhase);
423 __EXCEPT(stub_filter)
425 DWORD dwExceptionCode = GetExceptionCode();
426 WARN("a stub call failed with exception 0x%08x (%d)\n", dwExceptionCode, dwExceptionCode);
427 if (FAILED(dwExceptionCode))
428 hr = dwExceptionCode;
430 hr = HRESULT_FROM_WIN32(dwExceptionCode);
437 LPRPCSTUBBUFFER WINAPI CStdStubBuffer_IsIIDSupported(LPRPCSTUBBUFFER iface,
440 CStdStubBuffer *This = (CStdStubBuffer *)iface;
441 TRACE("(%p)->IsIIDSupported(%s)\n",This,debugstr_guid(riid));
442 return IsEqualGUID(STUB_HEADER(This).piid, riid) ? iface : NULL;
445 ULONG WINAPI CStdStubBuffer_CountRefs(LPRPCSTUBBUFFER iface)
447 CStdStubBuffer *This = (CStdStubBuffer *)iface;
448 TRACE("(%p)->CountRefs()\n",This);
449 return This->RefCount;
452 HRESULT WINAPI CStdStubBuffer_DebugServerQueryInterface(LPRPCSTUBBUFFER iface,
455 CStdStubBuffer *This = (CStdStubBuffer *)iface;
456 TRACE("(%p)->DebugServerQueryInterface(%p)\n",This,ppv);
460 void WINAPI CStdStubBuffer_DebugServerRelease(LPRPCSTUBBUFFER iface,
463 CStdStubBuffer *This = (CStdStubBuffer *)iface;
464 TRACE("(%p)->DebugServerRelease(%p)\n",This,pv);
467 const IRpcStubBufferVtbl CStdStubBuffer_Vtbl =
469 CStdStubBuffer_QueryInterface,
470 CStdStubBuffer_AddRef,
472 CStdStubBuffer_Connect,
473 CStdStubBuffer_Disconnect,
474 CStdStubBuffer_Invoke,
475 CStdStubBuffer_IsIIDSupported,
476 CStdStubBuffer_CountRefs,
477 CStdStubBuffer_DebugServerQueryInterface,
478 CStdStubBuffer_DebugServerRelease
481 static HRESULT WINAPI CStdStubBuffer_Delegating_Connect(LPRPCSTUBBUFFER iface,
482 LPUNKNOWN lpUnkServer)
484 cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
486 TRACE("(%p)->Connect(%p)\n", This, lpUnkServer);
488 r = CStdStubBuffer_Connect(iface, lpUnkServer);
490 r = IRpcStubBuffer_Connect(This->base_stub, (IUnknown*)&This->base_obj);
495 static void WINAPI CStdStubBuffer_Delegating_Disconnect(LPRPCSTUBBUFFER iface)
497 cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
498 TRACE("(%p)->Disconnect()\n", This);
500 IRpcStubBuffer_Disconnect(This->base_stub);
501 CStdStubBuffer_Disconnect(iface);
504 static ULONG WINAPI CStdStubBuffer_Delegating_CountRefs(LPRPCSTUBBUFFER iface)
506 cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
508 TRACE("(%p)->CountRefs()\n", This);
510 ret = CStdStubBuffer_CountRefs(iface);
511 ret += IRpcStubBuffer_CountRefs(This->base_stub);
516 const IRpcStubBufferVtbl CStdStubBuffer_Delegating_Vtbl =
518 CStdStubBuffer_QueryInterface,
519 CStdStubBuffer_AddRef,
521 CStdStubBuffer_Delegating_Connect,
522 CStdStubBuffer_Delegating_Disconnect,
523 CStdStubBuffer_Invoke,
524 CStdStubBuffer_IsIIDSupported,
525 CStdStubBuffer_Delegating_CountRefs,
526 CStdStubBuffer_DebugServerQueryInterface,
527 CStdStubBuffer_DebugServerRelease
530 const MIDL_SERVER_INFO *CStdStubBuffer_GetServerInfo(IRpcStubBuffer *iface)
532 CStdStubBuffer *This = (CStdStubBuffer *)iface;
533 return STUB_HEADER(This).pServerInfo;
536 /************************************************************************
537 * NdrStubForwardingFunction [RPCRT4.@]
539 void __RPC_STUB NdrStubForwardingFunction( IRpcStubBuffer *iface, IRpcChannelBuffer *pChannel,
540 PRPC_MESSAGE pMsg, DWORD *pdwStubPhase )
542 /* Note pMsg is passed intact since RPCOLEMESSAGE is basically a RPC_MESSAGE. */
544 cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
545 HRESULT r = IRpcStubBuffer_Invoke(This->base_stub, (RPCOLEMESSAGE*)pMsg, pChannel);
546 if(FAILED(r)) RpcRaiseException(r);
550 /***********************************************************************
551 * NdrStubInitialize [RPCRT4.@]
553 void WINAPI NdrStubInitialize(PRPC_MESSAGE pRpcMsg,
554 PMIDL_STUB_MESSAGE pStubMsg,
555 PMIDL_STUB_DESC pStubDescriptor,
556 LPRPCCHANNELBUFFER pRpcChannelBuffer)
558 TRACE("(%p,%p,%p,%p)\n", pRpcMsg, pStubMsg, pStubDescriptor, pRpcChannelBuffer);
559 NdrServerInitializeNew(pRpcMsg, pStubMsg, pStubDescriptor);
560 pStubMsg->pRpcChannelBuffer = pRpcChannelBuffer;
563 /***********************************************************************
564 * NdrStubGetBuffer [RPCRT4.@]
566 void WINAPI NdrStubGetBuffer(LPRPCSTUBBUFFER iface,
567 LPRPCCHANNELBUFFER pRpcChannelBuffer,
568 PMIDL_STUB_MESSAGE pStubMsg)
570 CStdStubBuffer *This = (CStdStubBuffer *)iface;
573 TRACE("(%p, %p, %p)\n", This, pRpcChannelBuffer, pStubMsg);
575 pStubMsg->RpcMsg->BufferLength = pStubMsg->BufferLength;
576 hr = IRpcChannelBuffer_GetBuffer(pRpcChannelBuffer,
577 (RPCOLEMESSAGE *)pStubMsg->RpcMsg, STUB_HEADER(This).piid);
580 RpcRaiseException(hr);
584 pStubMsg->BufferStart = pStubMsg->RpcMsg->Buffer;
585 pStubMsg->BufferEnd = pStubMsg->BufferStart + pStubMsg->BufferLength;
586 pStubMsg->Buffer = pStubMsg->BufferStart;