msvcrt/tests: Remove a space before a '\n'.
[wine/gsoc-2012-control.git] / dlls / rpcrt4 / cstub.c
blob85411cd81e97b6e2108202ed1c7c467b1bdc8118
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdarg.h>
26 #define COBJMACROS
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winerror.h"
31 #include "excpt.h"
33 #include "objbase.h"
34 #include "rpcproxy.h"
36 #include "wine/debug.h"
37 #include "wine/exception.h"
39 #include "cpsf.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(ole);
43 #define STUB_HEADER(This) (((const CInterfaceStubHeader*)((This)->lpVtbl))[-1])
45 static LONG WINAPI stub_filter(EXCEPTION_POINTERS *eptr)
47 if (eptr->ExceptionRecord->ExceptionFlags & EXCEPTION_NONCONTINUABLE)
48 return EXCEPTION_CONTINUE_SEARCH;
49 return EXCEPTION_EXECUTE_HANDLER;
52 typedef struct
54 IUnknownVtbl *base_obj;
55 IRpcStubBuffer *base_stub;
56 CStdStubBuffer stub_buffer;
57 } cstdstubbuffer_delegating_t;
59 static inline cstdstubbuffer_delegating_t *impl_from_delegating( IRpcStubBuffer *iface )
61 return (cstdstubbuffer_delegating_t*)((char *)iface - FIELD_OFFSET(cstdstubbuffer_delegating_t, stub_buffer));
64 HRESULT WINAPI CStdStubBuffer_Construct(REFIID riid,
65 LPUNKNOWN pUnkServer,
66 PCInterfaceName name,
67 CInterfaceStubVtbl *vtbl,
68 LPPSFACTORYBUFFER pPSFactory,
69 LPRPCSTUBBUFFER *ppStub)
71 CStdStubBuffer *This;
72 IUnknown *pvServer;
73 HRESULT r;
74 TRACE("(%p,%p,%p,%p) %s\n", pUnkServer, vtbl, pPSFactory, ppStub, name);
75 TRACE("iid=%s\n", debugstr_guid(vtbl->header.piid));
76 TRACE("vtbl=%p\n", &vtbl->Vtbl);
78 if (!IsEqualGUID(vtbl->header.piid, riid)) {
79 ERR("IID mismatch during stub creation\n");
80 return RPC_E_UNEXPECTED;
83 r = IUnknown_QueryInterface(pUnkServer, riid, (void**)&pvServer);
84 if(FAILED(r))
85 return r;
87 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CStdStubBuffer));
88 if (!This) {
89 IUnknown_Release(pvServer);
90 return E_OUTOFMEMORY;
93 This->lpVtbl = &vtbl->Vtbl;
94 This->RefCount = 1;
95 This->pvServerObject = pvServer;
96 This->pPSFactory = pPSFactory;
97 *ppStub = (LPRPCSTUBBUFFER)This;
99 IPSFactoryBuffer_AddRef(pPSFactory);
100 return S_OK;
103 static CRITICAL_SECTION delegating_vtbl_section;
104 static CRITICAL_SECTION_DEBUG critsect_debug =
106 0, 0, &delegating_vtbl_section,
107 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
108 0, 0, { (DWORD_PTR)(__FILE__ ": delegating_vtbl_section") }
110 static CRITICAL_SECTION delegating_vtbl_section = { &critsect_debug, -1, 0, 0, 0, 0 };
112 typedef struct
114 DWORD ref;
115 DWORD size;
116 void **methods;
117 IUnknownVtbl vtbl;
118 /* remaining entries in vtbl */
119 } ref_counted_vtbl;
121 static struct
123 ref_counted_vtbl *table;
124 } current_vtbl;
127 static HRESULT WINAPI delegating_QueryInterface(IUnknown *pUnk, REFIID iid, void **ppv)
129 *ppv = (void *)pUnk;
130 return S_OK;
133 static ULONG WINAPI delegating_AddRef(IUnknown *pUnk)
135 return 1;
138 static ULONG WINAPI delegating_Release(IUnknown *pUnk)
140 return 1;
143 #if defined(__i386__)
145 /* The idea here is to replace the first param on the stack
146 ie. This (which will point to cstdstubbuffer_delegating_t)
147 with This->stub_buffer.pvServerObject and then jump to the
148 relevant offset in This->stub_buffer.pvServerObject's vtbl.
150 #include "pshpack1.h"
151 typedef struct {
152 DWORD mov1; /* mov 0x4(%esp), %eax 8b 44 24 04 */
153 WORD mov2; /* mov 0x10(%eax), %eax 8b 40 */
154 BYTE sixteen; /* 10 */
155 DWORD mov3; /* mov %eax, 0x4(%esp) 89 44 24 04 */
156 WORD mov4; /* mov (%eax), %eax 8b 00 */
157 WORD mov5; /* mov offset(%eax), %eax 8b 80 */
158 DWORD offset; /* xx xx xx xx */
159 WORD jmp; /* jmp *%eax ff e0 */
160 BYTE pad[3]; /* lea 0x0(%esi), %esi 8d 76 00 */
161 } vtbl_method_t;
162 #include "poppack.h"
164 static void fill_table(IUnknownVtbl *vtbl, void **methods, DWORD num)
166 vtbl_method_t *method;
167 void **entry;
168 DWORD i;
170 vtbl->QueryInterface = delegating_QueryInterface;
171 vtbl->AddRef = delegating_AddRef;
172 vtbl->Release = delegating_Release;
174 method = (vtbl_method_t*)methods;
175 entry = (void**)(vtbl + 1);
177 for(i = 3; i < num; i++)
179 *entry = method;
180 method->mov1 = 0x0424448b;
181 method->mov2 = 0x408b;
182 method->sixteen = 0x10;
183 method->mov3 = 0x04244489;
184 method->mov4 = 0x008b;
185 method->mov5 = 0x808b;
186 method->offset = i << 2;
187 method->jmp = 0xe0ff;
188 method->pad[0] = 0x8d;
189 method->pad[1] = 0x76;
190 method->pad[2] = 0x00;
192 method++;
193 entry++;
197 #else /* __i386__ */
199 typedef struct {int dummy;} vtbl_method_t;
200 static void fill_table(IUnknownVtbl *vtbl, void **methods, DWORD num)
202 ERR("delegated stubs are not supported on this architecture\n");
205 #endif /* __i386__ */
207 void create_delegating_vtbl(DWORD num_methods)
209 TRACE("%d\n", num_methods);
210 if(num_methods <= 3)
212 ERR("should have more than %d methods\n", num_methods);
213 return;
216 EnterCriticalSection(&delegating_vtbl_section);
217 if(!current_vtbl.table || num_methods > current_vtbl.table->size)
219 DWORD size;
220 DWORD old_protect;
221 if(current_vtbl.table && current_vtbl.table->ref == 0)
223 TRACE("freeing old table\n");
224 VirtualFree(current_vtbl.table->methods, 0, MEM_RELEASE);
225 HeapFree(GetProcessHeap(), 0, current_vtbl.table);
227 size = (num_methods - 3) * sizeof(vtbl_method_t);
228 current_vtbl.table = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(ref_counted_vtbl, vtbl) + num_methods * sizeof(void*));
229 current_vtbl.table->ref = 0;
230 current_vtbl.table->size = num_methods;
231 current_vtbl.table->methods = VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
232 fill_table(&current_vtbl.table->vtbl, current_vtbl.table->methods, num_methods);
233 VirtualProtect(current_vtbl.table->methods, size, PAGE_EXECUTE_READ, &old_protect);
235 LeaveCriticalSection(&delegating_vtbl_section);
238 static IUnknownVtbl *get_delegating_vtbl(void)
240 IUnknownVtbl *ret;
242 EnterCriticalSection(&delegating_vtbl_section);
243 current_vtbl.table->ref++;
244 ret = &current_vtbl.table->vtbl;
245 LeaveCriticalSection(&delegating_vtbl_section);
246 return ret;
249 static void release_delegating_vtbl(IUnknownVtbl *vtbl)
251 ref_counted_vtbl *table = (ref_counted_vtbl*)((DWORD *)vtbl - 1);
253 EnterCriticalSection(&delegating_vtbl_section);
254 table->ref--;
255 TRACE("ref now %d\n", table->ref);
256 if(table->ref == 0 && table != current_vtbl.table)
258 TRACE("... and we're not current so free'ing\n");
259 VirtualFree(current_vtbl.table->methods, 0, MEM_RELEASE);
260 HeapFree(GetProcessHeap(), 0, table);
262 LeaveCriticalSection(&delegating_vtbl_section);
265 HRESULT WINAPI CStdStubBuffer_Delegating_Construct(REFIID riid,
266 LPUNKNOWN pUnkServer,
267 PCInterfaceName name,
268 CInterfaceStubVtbl *vtbl,
269 REFIID delegating_iid,
270 LPPSFACTORYBUFFER pPSFactory,
271 LPRPCSTUBBUFFER *ppStub)
273 cstdstubbuffer_delegating_t *This;
274 IUnknown *pvServer;
275 HRESULT r;
277 TRACE("(%p,%p,%p,%p) %s\n", pUnkServer, vtbl, pPSFactory, ppStub, name);
278 TRACE("iid=%s delegating to %s\n", debugstr_guid(vtbl->header.piid), debugstr_guid(delegating_iid));
279 TRACE("vtbl=%p\n", &vtbl->Vtbl);
281 if (!IsEqualGUID(vtbl->header.piid, riid))
283 ERR("IID mismatch during stub creation\n");
284 return RPC_E_UNEXPECTED;
287 r = IUnknown_QueryInterface(pUnkServer, riid, (void**)&pvServer);
288 if(FAILED(r)) return r;
290 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This));
291 if (!This)
293 IUnknown_Release(pvServer);
294 return E_OUTOFMEMORY;
297 This->base_obj = get_delegating_vtbl();
298 r = create_stub(delegating_iid, (IUnknown*)&This->base_obj, &This->base_stub);
299 if(FAILED(r))
301 release_delegating_vtbl(This->base_obj);
302 HeapFree(GetProcessHeap(), 0, This);
303 IUnknown_Release(pvServer);
304 return r;
307 This->stub_buffer.lpVtbl = &vtbl->Vtbl;
308 This->stub_buffer.RefCount = 1;
309 This->stub_buffer.pvServerObject = pvServer;
310 This->stub_buffer.pPSFactory = pPSFactory;
311 *ppStub = (LPRPCSTUBBUFFER)&This->stub_buffer;
313 IPSFactoryBuffer_AddRef(pPSFactory);
314 return S_OK;
317 HRESULT WINAPI CStdStubBuffer_QueryInterface(LPRPCSTUBBUFFER iface,
318 REFIID riid,
319 LPVOID *obj)
321 CStdStubBuffer *This = (CStdStubBuffer *)iface;
322 TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
324 if (IsEqualIID(&IID_IUnknown, riid) ||
325 IsEqualIID(&IID_IRpcStubBuffer, riid))
327 IUnknown_AddRef(iface);
328 *obj = iface;
329 return S_OK;
331 *obj = NULL;
332 return E_NOINTERFACE;
335 ULONG WINAPI CStdStubBuffer_AddRef(LPRPCSTUBBUFFER iface)
337 CStdStubBuffer *This = (CStdStubBuffer *)iface;
338 TRACE("(%p)->AddRef()\n",This);
339 return InterlockedIncrement(&This->RefCount);
342 ULONG WINAPI NdrCStdStubBuffer_Release(LPRPCSTUBBUFFER iface,
343 LPPSFACTORYBUFFER pPSF)
345 CStdStubBuffer *This = (CStdStubBuffer *)iface;
346 ULONG refs;
348 TRACE("(%p)->Release()\n",This);
350 refs = InterlockedDecrement(&This->RefCount);
351 if (!refs)
353 /* test_Release shows that native doesn't call Disconnect here.
354 We'll leave it in for the time being. */
355 IRpcStubBuffer_Disconnect(iface);
357 IPSFactoryBuffer_Release(pPSF);
358 HeapFree(GetProcessHeap(),0,This);
360 return refs;
363 ULONG WINAPI NdrCStdStubBuffer2_Release(LPRPCSTUBBUFFER iface,
364 LPPSFACTORYBUFFER pPSF)
366 cstdstubbuffer_delegating_t *This = impl_from_delegating( iface );
367 ULONG refs;
369 TRACE("(%p)->Release()\n", This);
371 refs = InterlockedDecrement(&This->stub_buffer.RefCount);
372 if (!refs)
374 /* Just like NdrCStdStubBuffer_Release, we shouldn't call
375 Disconnect here */
376 IRpcStubBuffer_Disconnect((IRpcStubBuffer *)&This->stub_buffer);
378 IRpcStubBuffer_Release(This->base_stub);
379 release_delegating_vtbl(This->base_obj);
381 IPSFactoryBuffer_Release(pPSF);
382 HeapFree(GetProcessHeap(), 0, This);
385 return refs;
388 HRESULT WINAPI CStdStubBuffer_Connect(LPRPCSTUBBUFFER iface,
389 LPUNKNOWN lpUnkServer)
391 CStdStubBuffer *This = (CStdStubBuffer *)iface;
392 HRESULT r;
393 IUnknown *new = NULL;
395 TRACE("(%p)->Connect(%p)\n",This,lpUnkServer);
397 r = IUnknown_QueryInterface(lpUnkServer, STUB_HEADER(This).piid, (void**)&new);
398 new = InterlockedExchangePointer((void**)&This->pvServerObject, new);
399 if(new)
400 IUnknown_Release(new);
401 return r;
404 void WINAPI CStdStubBuffer_Disconnect(LPRPCSTUBBUFFER iface)
406 CStdStubBuffer *This = (CStdStubBuffer *)iface;
407 IUnknown *old;
408 TRACE("(%p)->Disconnect()\n",This);
410 old = InterlockedExchangePointer((void**)&This->pvServerObject, NULL);
412 if(old)
413 IUnknown_Release(old);
416 HRESULT WINAPI CStdStubBuffer_Invoke(LPRPCSTUBBUFFER iface,
417 PRPCOLEMESSAGE pMsg,
418 LPRPCCHANNELBUFFER pChannel)
420 CStdStubBuffer *This = (CStdStubBuffer *)iface;
421 DWORD dwPhase = STUB_UNMARSHAL;
422 HRESULT hr = S_OK;
424 TRACE("(%p)->Invoke(%p,%p)\n",This,pMsg,pChannel);
426 __TRY
428 if (STUB_HEADER(This).pDispatchTable)
429 STUB_HEADER(This).pDispatchTable[pMsg->iMethod](iface, pChannel, (PRPC_MESSAGE)pMsg, &dwPhase);
430 else /* pure interpreted */
431 NdrStubCall2(iface, pChannel, (PRPC_MESSAGE)pMsg, &dwPhase);
433 __EXCEPT(stub_filter)
435 DWORD dwExceptionCode = GetExceptionCode();
436 WARN("a stub call failed with exception 0x%08x (%d)\n", dwExceptionCode, dwExceptionCode);
437 if (FAILED(dwExceptionCode))
438 hr = dwExceptionCode;
439 else
440 hr = HRESULT_FROM_WIN32(dwExceptionCode);
442 __ENDTRY
444 return hr;
447 LPRPCSTUBBUFFER WINAPI CStdStubBuffer_IsIIDSupported(LPRPCSTUBBUFFER iface,
448 REFIID riid)
450 CStdStubBuffer *This = (CStdStubBuffer *)iface;
451 TRACE("(%p)->IsIIDSupported(%s)\n",This,debugstr_guid(riid));
452 return IsEqualGUID(STUB_HEADER(This).piid, riid) ? iface : NULL;
455 ULONG WINAPI CStdStubBuffer_CountRefs(LPRPCSTUBBUFFER iface)
457 CStdStubBuffer *This = (CStdStubBuffer *)iface;
458 TRACE("(%p)->CountRefs()\n",This);
459 return This->RefCount;
462 HRESULT WINAPI CStdStubBuffer_DebugServerQueryInterface(LPRPCSTUBBUFFER iface,
463 LPVOID *ppv)
465 CStdStubBuffer *This = (CStdStubBuffer *)iface;
466 TRACE("(%p)->DebugServerQueryInterface(%p)\n",This,ppv);
467 return S_OK;
470 void WINAPI CStdStubBuffer_DebugServerRelease(LPRPCSTUBBUFFER iface,
471 LPVOID pv)
473 CStdStubBuffer *This = (CStdStubBuffer *)iface;
474 TRACE("(%p)->DebugServerRelease(%p)\n",This,pv);
477 const IRpcStubBufferVtbl CStdStubBuffer_Vtbl =
479 CStdStubBuffer_QueryInterface,
480 CStdStubBuffer_AddRef,
481 NULL,
482 CStdStubBuffer_Connect,
483 CStdStubBuffer_Disconnect,
484 CStdStubBuffer_Invoke,
485 CStdStubBuffer_IsIIDSupported,
486 CStdStubBuffer_CountRefs,
487 CStdStubBuffer_DebugServerQueryInterface,
488 CStdStubBuffer_DebugServerRelease
491 static HRESULT WINAPI CStdStubBuffer_Delegating_Connect(LPRPCSTUBBUFFER iface,
492 LPUNKNOWN lpUnkServer)
494 cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
495 HRESULT r;
496 TRACE("(%p)->Connect(%p)\n", This, lpUnkServer);
498 r = CStdStubBuffer_Connect(iface, lpUnkServer);
499 if(SUCCEEDED(r))
500 r = IRpcStubBuffer_Connect(This->base_stub, (IUnknown*)&This->base_obj);
502 return r;
505 static void WINAPI CStdStubBuffer_Delegating_Disconnect(LPRPCSTUBBUFFER iface)
507 cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
508 TRACE("(%p)->Disconnect()\n", This);
510 IRpcStubBuffer_Disconnect(This->base_stub);
511 CStdStubBuffer_Disconnect(iface);
514 static ULONG WINAPI CStdStubBuffer_Delegating_CountRefs(LPRPCSTUBBUFFER iface)
516 cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
517 ULONG ret;
518 TRACE("(%p)->CountRefs()\n", This);
520 ret = CStdStubBuffer_CountRefs(iface);
521 ret += IRpcStubBuffer_CountRefs(This->base_stub);
523 return ret;
526 const IRpcStubBufferVtbl CStdStubBuffer_Delegating_Vtbl =
528 CStdStubBuffer_QueryInterface,
529 CStdStubBuffer_AddRef,
530 NULL,
531 CStdStubBuffer_Delegating_Connect,
532 CStdStubBuffer_Delegating_Disconnect,
533 CStdStubBuffer_Invoke,
534 CStdStubBuffer_IsIIDSupported,
535 CStdStubBuffer_Delegating_CountRefs,
536 CStdStubBuffer_DebugServerQueryInterface,
537 CStdStubBuffer_DebugServerRelease
540 const MIDL_SERVER_INFO *CStdStubBuffer_GetServerInfo(IRpcStubBuffer *iface)
542 CStdStubBuffer *This = (CStdStubBuffer *)iface;
543 return STUB_HEADER(This).pServerInfo;
546 /************************************************************************
547 * NdrStubForwardingFunction [RPCRT4.@]
549 void __RPC_STUB NdrStubForwardingFunction( IRpcStubBuffer *iface, IRpcChannelBuffer *pChannel,
550 PRPC_MESSAGE pMsg, DWORD *pdwStubPhase )
552 /* Note pMsg is passed intact since RPCOLEMESSAGE is basically a RPC_MESSAGE. */
554 cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
555 HRESULT r = IRpcStubBuffer_Invoke(This->base_stub, (RPCOLEMESSAGE*)pMsg, pChannel);
556 if(FAILED(r)) RpcRaiseException(r);
557 return;
560 /***********************************************************************
561 * NdrStubInitialize [RPCRT4.@]
563 void WINAPI NdrStubInitialize(PRPC_MESSAGE pRpcMsg,
564 PMIDL_STUB_MESSAGE pStubMsg,
565 PMIDL_STUB_DESC pStubDescriptor,
566 LPRPCCHANNELBUFFER pRpcChannelBuffer)
568 TRACE("(%p,%p,%p,%p)\n", pRpcMsg, pStubMsg, pStubDescriptor, pRpcChannelBuffer);
569 NdrServerInitializeNew(pRpcMsg, pStubMsg, pStubDescriptor);
570 pStubMsg->pRpcChannelBuffer = pRpcChannelBuffer;
571 IRpcChannelBuffer_GetDestCtx(pStubMsg->pRpcChannelBuffer,
572 &pStubMsg->dwDestContext,
573 &pStubMsg->pvDestContext);
576 /***********************************************************************
577 * NdrStubGetBuffer [RPCRT4.@]
579 void WINAPI NdrStubGetBuffer(LPRPCSTUBBUFFER iface,
580 LPRPCCHANNELBUFFER pRpcChannelBuffer,
581 PMIDL_STUB_MESSAGE pStubMsg)
583 CStdStubBuffer *This = (CStdStubBuffer *)iface;
584 HRESULT hr;
586 TRACE("(%p, %p, %p)\n", This, pRpcChannelBuffer, pStubMsg);
588 pStubMsg->RpcMsg->BufferLength = pStubMsg->BufferLength;
589 hr = IRpcChannelBuffer_GetBuffer(pRpcChannelBuffer,
590 (RPCOLEMESSAGE *)pStubMsg->RpcMsg, STUB_HEADER(This).piid);
591 if (FAILED(hr))
593 RpcRaiseException(hr);
594 return;
597 pStubMsg->Buffer = pStubMsg->RpcMsg->Buffer;