oledb32: Implement DataConvert DBTYPE_VARIANT->DBTYPE_UI4.
[wine] / dlls / rpcrt4 / cstub.c
1 /*
2  * COM stub (CStdStubBuffer) implementation
3  *
4  * Copyright 2001 Ove Kåven, TransGaming Technologies
5  * Copyright 2009 Alexandre Julliard
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <stdarg.h>
26
27 #define COBJMACROS
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winerror.h"
32 #include "excpt.h"
33
34 #include "objbase.h"
35 #include "rpcproxy.h"
36
37 #include "wine/debug.h"
38 #include "wine/exception.h"
39
40 #include "cpsf.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(ole);
43
44 #define STUB_HEADER(This) (((const CInterfaceStubHeader*)((This)->lpVtbl))[-1])
45
46 static LONG WINAPI stub_filter(EXCEPTION_POINTERS *eptr)
47 {
48     if (eptr->ExceptionRecord->ExceptionFlags & EXCEPTION_NONCONTINUABLE)
49         return EXCEPTION_CONTINUE_SEARCH;
50     return EXCEPTION_EXECUTE_HANDLER;
51 }
52
53 typedef struct
54 {
55     IUnknownVtbl *base_obj;
56     IRpcStubBuffer *base_stub;
57     CStdStubBuffer stub_buffer;
58 } cstdstubbuffer_delegating_t;
59
60 static inline cstdstubbuffer_delegating_t *impl_from_delegating( IRpcStubBuffer *iface )
61 {
62     return (cstdstubbuffer_delegating_t*)((char *)iface - FIELD_OFFSET(cstdstubbuffer_delegating_t, stub_buffer));
63 }
64
65 HRESULT CStdStubBuffer_Construct(REFIID riid,
66                                  LPUNKNOWN pUnkServer,
67                                  PCInterfaceName name,
68                                  CInterfaceStubVtbl *vtbl,
69                                  LPPSFACTORYBUFFER pPSFactory,
70                                  LPRPCSTUBBUFFER *ppStub)
71 {
72   CStdStubBuffer *This;
73   IUnknown *pvServer;
74   HRESULT r;
75   TRACE("(%p,%p,%p,%p) %s\n", pUnkServer, vtbl, pPSFactory, ppStub, name);
76   TRACE("iid=%s\n", debugstr_guid(vtbl->header.piid));
77   TRACE("vtbl=%p\n", &vtbl->Vtbl);
78
79   if (!IsEqualGUID(vtbl->header.piid, riid)) {
80     ERR("IID mismatch during stub creation\n");
81     return RPC_E_UNEXPECTED;
82   }
83
84   r = IUnknown_QueryInterface(pUnkServer, riid, (void**)&pvServer);
85   if(FAILED(r))
86     return r;
87
88   This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CStdStubBuffer));
89   if (!This) {
90     IUnknown_Release(pvServer);
91     return E_OUTOFMEMORY;
92   }
93
94   This->lpVtbl = &vtbl->Vtbl;
95   This->RefCount = 1;
96   This->pvServerObject = pvServer;
97   This->pPSFactory = pPSFactory;
98   *ppStub = (LPRPCSTUBBUFFER)This;
99
100   IPSFactoryBuffer_AddRef(pPSFactory);
101   return S_OK;
102 }
103
104 static CRITICAL_SECTION delegating_vtbl_section;
105 static CRITICAL_SECTION_DEBUG critsect_debug =
106 {
107     0, 0, &delegating_vtbl_section,
108     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
109       0, 0, { (DWORD_PTR)(__FILE__ ": delegating_vtbl_section") }
110 };
111 static CRITICAL_SECTION delegating_vtbl_section = { &critsect_debug, -1, 0, 0, 0, 0 };
112
113 typedef struct
114 {
115     DWORD ref;
116     DWORD size;
117     IUnknownVtbl vtbl;
118     /* remaining entries in vtbl */
119 } ref_counted_vtbl;
120
121 static ref_counted_vtbl *current_vtbl;
122
123
124 static HRESULT WINAPI delegating_QueryInterface(IUnknown *pUnk, REFIID iid, void **ppv)
125 {
126     *ppv = pUnk;
127     return S_OK;
128 }
129
130 static ULONG WINAPI delegating_AddRef(IUnknown *pUnk)
131 {
132     return 1;
133 }
134
135 static ULONG WINAPI delegating_Release(IUnknown *pUnk)
136 {
137     return 1;
138 }
139
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.
144 */
145 #ifdef __i386__
146
147 #include "pshpack1.h"
148 typedef struct {
149     BYTE mov1[4];    /* mov 0x4(%esp),%eax     8b 44 24 04 */
150     BYTE mov2[3];    /* mov 0x10(%eax),%eax    8b 40 10 */
151     BYTE mov3[4];    /* mov %eax,0x4(%esp)     89 44 24 04 */
152     BYTE mov4[2];    /* mov (%eax),%eax        8b 00 */
153     BYTE mov5[2];    /* jmp *offset(%eax)      ff a0 offset */
154     DWORD offset;
155     BYTE pad[1];     /* nop                    90 */
156 } vtbl_method_t;
157 #include "poppack.h"
158
159 static const BYTE opcodes[20] = { 0x8b, 0x44, 0x24, 0x04, 0x8b, 0x40, 0x10, 0x89, 0x44, 0x24, 0x04,
160                                   0x8b, 0x00, 0xff, 0xa0, 0, 0, 0, 0, 0x90 };
161
162 #elif defined(__x86_64__)
163
164 #include "pshpack1.h"
165 typedef struct
166 {
167     BYTE mov1[4];    /* movq 0x20(%rcx),%rcx   48 8b 49 20 */
168     BYTE mov2[3];    /* movq (%rcx),%rax       48 8b 01 */
169     BYTE jmp[2];     /* jmp *offset(%rax)      ff a0 offset */
170     DWORD offset;
171     BYTE pad[3];     /* lea 0x0(%rsi),%rsi     48 8d 36 */
172 } vtbl_method_t;
173 #include "poppack.h"
174
175 static const BYTE opcodes[16] = { 0x48, 0x8b, 0x49, 0x20, 0x48, 0x8b, 0x01,
176                                   0xff, 0xa0, 0, 0, 0, 0, 0x48, 0x8d, 0x36 };
177 #else
178
179 #warning You must implement delegated proxies/stubs for your CPU
180 typedef struct
181 {
182     DWORD offset;
183 } vtbl_method_t;
184 static const BYTE opcodes[1];
185
186 #endif
187
188 #define BLOCK_SIZE 1024
189 #define MAX_BLOCKS 64  /* 64k methods should be enough for anybody */
190
191 static const vtbl_method_t *method_blocks[MAX_BLOCKS];
192
193 static const vtbl_method_t *allocate_block( unsigned int num )
194 {
195     unsigned int i;
196     vtbl_method_t *prev, *block;
197
198     block = VirtualAlloc( NULL, BLOCK_SIZE * sizeof(*block),
199                           MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE );
200     if (!block) return NULL;
201
202     for (i = 0; i < BLOCK_SIZE; i++)
203     {
204         memcpy( &block[i], opcodes, sizeof(opcodes) );
205         block[i].offset = (BLOCK_SIZE * num + i + 3) * sizeof(void *);
206     }
207     VirtualProtect( block, BLOCK_SIZE * sizeof(*block), PAGE_EXECUTE_READ, NULL );
208     prev = InterlockedCompareExchangePointer( (void **)&method_blocks[num], block, NULL );
209     if (prev) /* someone beat us to it */
210     {
211         VirtualFree( block, 0, MEM_RELEASE );
212         block = prev;
213     }
214     return block;
215 }
216
217 static BOOL fill_delegated_stub_table(IUnknownVtbl *vtbl, DWORD num)
218 {
219     const void **entry = (const void **)(vtbl + 1);
220     DWORD i, j;
221
222     if (num - 3 > BLOCK_SIZE * MAX_BLOCKS)
223     {
224         FIXME( "%u methods not supported\n", num );
225         return FALSE;
226     }
227     vtbl->QueryInterface = delegating_QueryInterface;
228     vtbl->AddRef = delegating_AddRef;
229     vtbl->Release = delegating_Release;
230     for (i = 0; i < (num - 3 + BLOCK_SIZE - 1) / BLOCK_SIZE; i++)
231     {
232         const vtbl_method_t *block = method_blocks[i];
233         if (!block && !(block = allocate_block( i ))) return FALSE;
234         for (j = 0; j < BLOCK_SIZE && j < num - 3 - i * BLOCK_SIZE; j++) *entry++ = &block[j];
235     }
236     return TRUE;
237 }
238
239 BOOL fill_delegated_proxy_table(IUnknownVtbl *vtbl, DWORD num)
240 {
241     const void **entry = (const void **)(vtbl + 1);
242     DWORD i, j;
243
244     if (num - 3 > BLOCK_SIZE * MAX_BLOCKS)
245     {
246         FIXME( "%u methods not supported\n", num );
247         return FALSE;
248     }
249     vtbl->QueryInterface = IUnknown_QueryInterface_Proxy;
250     vtbl->AddRef = IUnknown_AddRef_Proxy;
251     vtbl->Release = IUnknown_Release_Proxy;
252     for (i = 0; i < (num - 3 + BLOCK_SIZE - 1) / BLOCK_SIZE; i++)
253     {
254         const vtbl_method_t *block = method_blocks[i];
255         if (!block && !(block = allocate_block( i ))) return FALSE;
256         for (j = 0; j < BLOCK_SIZE && j < num - 3 - i * BLOCK_SIZE; j++, entry++)
257             if (!*entry) *entry = &block[j];
258     }
259     return TRUE;
260 }
261
262 static IUnknownVtbl *get_delegating_vtbl(DWORD num_methods)
263 {
264     IUnknownVtbl *ret;
265
266     if (num_methods < 256) num_methods = 256;  /* avoid frequent reallocations */
267
268     EnterCriticalSection(&delegating_vtbl_section);
269
270     if(!current_vtbl || num_methods > current_vtbl->size)
271     {
272         ref_counted_vtbl *table = HeapAlloc(GetProcessHeap(), 0,
273                                             FIELD_OFFSET(ref_counted_vtbl, vtbl) + num_methods * sizeof(void*));
274         if (!table)
275         {
276             LeaveCriticalSection(&delegating_vtbl_section);
277             return NULL;
278         }
279
280         table->ref = 0;
281         table->size = num_methods;
282         fill_delegated_stub_table(&table->vtbl, num_methods);
283
284         if (current_vtbl && current_vtbl->ref == 0)
285         {
286             TRACE("freeing old table\n");
287             HeapFree(GetProcessHeap(), 0, current_vtbl);
288         }
289         current_vtbl = table;
290     }
291
292     current_vtbl->ref++;
293     ret = &current_vtbl->vtbl;
294     LeaveCriticalSection(&delegating_vtbl_section);
295     return ret;
296 }
297
298 static void release_delegating_vtbl(IUnknownVtbl *vtbl)
299 {
300     ref_counted_vtbl *table = (ref_counted_vtbl*)((DWORD *)vtbl - 1);
301
302     EnterCriticalSection(&delegating_vtbl_section);
303     table->ref--;
304     TRACE("ref now %d\n", table->ref);
305     if(table->ref == 0 && table != current_vtbl)
306     {
307         TRACE("... and we're not current so free'ing\n");
308         HeapFree(GetProcessHeap(), 0, table);
309     }
310     LeaveCriticalSection(&delegating_vtbl_section);
311 }
312
313 HRESULT CStdStubBuffer_Delegating_Construct(REFIID riid,
314                                             LPUNKNOWN pUnkServer,
315                                             PCInterfaceName name,
316                                             CInterfaceStubVtbl *vtbl,
317                                             REFIID delegating_iid,
318                                             LPPSFACTORYBUFFER pPSFactory,
319                                             LPRPCSTUBBUFFER *ppStub)
320 {
321     cstdstubbuffer_delegating_t *This;
322     IUnknown *pvServer;
323     HRESULT r;
324
325     TRACE("(%p,%p,%p,%p) %s\n", pUnkServer, vtbl, pPSFactory, ppStub, name);
326     TRACE("iid=%s delegating to %s\n", debugstr_guid(vtbl->header.piid), debugstr_guid(delegating_iid));
327     TRACE("vtbl=%p\n", &vtbl->Vtbl);
328
329     if (!IsEqualGUID(vtbl->header.piid, riid))
330     {
331         ERR("IID mismatch during stub creation\n");
332         return RPC_E_UNEXPECTED;
333     }
334
335     r = IUnknown_QueryInterface(pUnkServer, riid, (void**)&pvServer);
336     if(FAILED(r)) return r;
337
338     This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This));
339     if (!This)
340     {
341         IUnknown_Release(pvServer);
342         return E_OUTOFMEMORY;
343     }
344
345     This->base_obj = get_delegating_vtbl( vtbl->header.DispatchTableCount );
346     r = create_stub(delegating_iid, (IUnknown*)&This->base_obj, &This->base_stub);
347     if(FAILED(r))
348     {
349         release_delegating_vtbl(This->base_obj);
350         HeapFree(GetProcessHeap(), 0, This);
351         IUnknown_Release(pvServer);
352         return r;
353     }
354
355     This->stub_buffer.lpVtbl = &vtbl->Vtbl;
356     This->stub_buffer.RefCount = 1;
357     This->stub_buffer.pvServerObject = pvServer;
358     This->stub_buffer.pPSFactory = pPSFactory;
359     *ppStub = (LPRPCSTUBBUFFER)&This->stub_buffer;
360
361     IPSFactoryBuffer_AddRef(pPSFactory);
362     return S_OK;
363 }
364
365 HRESULT WINAPI CStdStubBuffer_QueryInterface(LPRPCSTUBBUFFER iface,
366                                             REFIID riid,
367                                             LPVOID *obj)
368 {
369   CStdStubBuffer *This = (CStdStubBuffer *)iface;
370   TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
371
372   if (IsEqualIID(&IID_IUnknown, riid) ||
373       IsEqualIID(&IID_IRpcStubBuffer, riid))
374   {
375     IRpcStubBuffer_AddRef(iface);
376     *obj = iface;
377     return S_OK;
378   }
379   *obj = NULL;
380   return E_NOINTERFACE;
381 }
382
383 ULONG WINAPI CStdStubBuffer_AddRef(LPRPCSTUBBUFFER iface)
384 {
385   CStdStubBuffer *This = (CStdStubBuffer *)iface;
386   TRACE("(%p)->AddRef()\n",This);
387   return InterlockedIncrement(&This->RefCount);
388 }
389
390 ULONG WINAPI NdrCStdStubBuffer_Release(LPRPCSTUBBUFFER iface,
391                                       LPPSFACTORYBUFFER pPSF)
392 {
393   CStdStubBuffer *This = (CStdStubBuffer *)iface;
394   ULONG refs;
395
396   TRACE("(%p)->Release()\n",This);
397
398   refs = InterlockedDecrement(&This->RefCount);
399   if (!refs)
400   {
401     /* test_Release shows that native doesn't call Disconnect here.
402        We'll leave it in for the time being. */
403     IRpcStubBuffer_Disconnect(iface);
404
405     IPSFactoryBuffer_Release(pPSF);
406     HeapFree(GetProcessHeap(),0,This);
407   }
408   return refs;
409 }
410
411 ULONG WINAPI NdrCStdStubBuffer2_Release(LPRPCSTUBBUFFER iface,
412                                         LPPSFACTORYBUFFER pPSF)
413 {
414     cstdstubbuffer_delegating_t *This = impl_from_delegating( iface );
415     ULONG refs;
416
417     TRACE("(%p)->Release()\n", This);
418
419     refs = InterlockedDecrement(&This->stub_buffer.RefCount);
420     if (!refs)
421     {
422         /* Just like NdrCStdStubBuffer_Release, we shouldn't call
423            Disconnect here */
424         IRpcStubBuffer_Disconnect((IRpcStubBuffer *)&This->stub_buffer);
425
426         IRpcStubBuffer_Release(This->base_stub);
427         release_delegating_vtbl(This->base_obj);
428
429         IPSFactoryBuffer_Release(pPSF);
430         HeapFree(GetProcessHeap(), 0, This);
431     }
432
433     return refs;
434 }
435
436 HRESULT WINAPI CStdStubBuffer_Connect(LPRPCSTUBBUFFER iface,
437                                      LPUNKNOWN lpUnkServer)
438 {
439     CStdStubBuffer *This = (CStdStubBuffer *)iface;
440     HRESULT r;
441     IUnknown *new = NULL;
442
443     TRACE("(%p)->Connect(%p)\n",This,lpUnkServer);
444
445     r = IUnknown_QueryInterface(lpUnkServer, STUB_HEADER(This).piid, (void**)&new);
446     new = InterlockedExchangePointer((void**)&This->pvServerObject, new);
447     if(new)
448         IUnknown_Release(new);
449     return r;
450 }
451
452 void WINAPI CStdStubBuffer_Disconnect(LPRPCSTUBBUFFER iface)
453 {
454     CStdStubBuffer *This = (CStdStubBuffer *)iface;
455     IUnknown *old;
456     TRACE("(%p)->Disconnect()\n",This);
457
458     old = InterlockedExchangePointer((void**)&This->pvServerObject, NULL);
459
460     if(old)
461         IUnknown_Release(old);
462 }
463
464 HRESULT WINAPI CStdStubBuffer_Invoke(LPRPCSTUBBUFFER iface,
465                                     PRPCOLEMESSAGE pMsg,
466                                     LPRPCCHANNELBUFFER pChannel)
467 {
468   CStdStubBuffer *This = (CStdStubBuffer *)iface;
469   DWORD dwPhase = STUB_UNMARSHAL;
470   HRESULT hr = S_OK;
471
472   TRACE("(%p)->Invoke(%p,%p)\n",This,pMsg,pChannel);
473
474   __TRY
475   {
476     if (STUB_HEADER(This).pDispatchTable)
477       STUB_HEADER(This).pDispatchTable[pMsg->iMethod](iface, pChannel, (PRPC_MESSAGE)pMsg, &dwPhase);
478     else /* pure interpreted */
479       NdrStubCall2(iface, pChannel, (PRPC_MESSAGE)pMsg, &dwPhase);
480   }
481   __EXCEPT(stub_filter)
482   {
483     DWORD dwExceptionCode = GetExceptionCode();
484     WARN("a stub call failed with exception 0x%08x (%d)\n", dwExceptionCode, dwExceptionCode);
485     if (FAILED(dwExceptionCode))
486       hr = dwExceptionCode;
487     else
488       hr = HRESULT_FROM_WIN32(dwExceptionCode);
489   }
490   __ENDTRY
491
492   return hr;
493 }
494
495 LPRPCSTUBBUFFER WINAPI CStdStubBuffer_IsIIDSupported(LPRPCSTUBBUFFER iface,
496                                                     REFIID riid)
497 {
498   CStdStubBuffer *This = (CStdStubBuffer *)iface;
499   TRACE("(%p)->IsIIDSupported(%s)\n",This,debugstr_guid(riid));
500   return IsEqualGUID(STUB_HEADER(This).piid, riid) ? iface : NULL;
501 }
502
503 ULONG WINAPI CStdStubBuffer_CountRefs(LPRPCSTUBBUFFER iface)
504 {
505   CStdStubBuffer *This = (CStdStubBuffer *)iface;
506   TRACE("(%p)->CountRefs()\n",This);
507   return This->RefCount;
508 }
509
510 HRESULT WINAPI CStdStubBuffer_DebugServerQueryInterface(LPRPCSTUBBUFFER iface,
511                                                        LPVOID *ppv)
512 {
513   CStdStubBuffer *This = (CStdStubBuffer *)iface;
514   TRACE("(%p)->DebugServerQueryInterface(%p)\n",This,ppv);
515   return S_OK;
516 }
517
518 void WINAPI CStdStubBuffer_DebugServerRelease(LPRPCSTUBBUFFER iface,
519                                              LPVOID pv)
520 {
521   CStdStubBuffer *This = (CStdStubBuffer *)iface;
522   TRACE("(%p)->DebugServerRelease(%p)\n",This,pv);
523 }
524
525 const IRpcStubBufferVtbl CStdStubBuffer_Vtbl =
526 {
527     CStdStubBuffer_QueryInterface,
528     CStdStubBuffer_AddRef,
529     NULL,
530     CStdStubBuffer_Connect,
531     CStdStubBuffer_Disconnect,
532     CStdStubBuffer_Invoke,
533     CStdStubBuffer_IsIIDSupported,
534     CStdStubBuffer_CountRefs,
535     CStdStubBuffer_DebugServerQueryInterface,
536     CStdStubBuffer_DebugServerRelease
537 };
538
539 static HRESULT WINAPI CStdStubBuffer_Delegating_Connect(LPRPCSTUBBUFFER iface,
540                                                         LPUNKNOWN lpUnkServer)
541 {
542     cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
543     HRESULT r;
544     TRACE("(%p)->Connect(%p)\n", This, lpUnkServer);
545
546     r = CStdStubBuffer_Connect(iface, lpUnkServer);
547     if(SUCCEEDED(r))
548         r = IRpcStubBuffer_Connect(This->base_stub, (IUnknown*)&This->base_obj);
549
550     return r;
551 }
552
553 static void WINAPI CStdStubBuffer_Delegating_Disconnect(LPRPCSTUBBUFFER iface)
554 {
555     cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
556     TRACE("(%p)->Disconnect()\n", This);
557
558     IRpcStubBuffer_Disconnect(This->base_stub);
559     CStdStubBuffer_Disconnect(iface);
560 }
561
562 static ULONG WINAPI CStdStubBuffer_Delegating_CountRefs(LPRPCSTUBBUFFER iface)
563 {
564     cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
565     ULONG ret;
566     TRACE("(%p)->CountRefs()\n", This);
567
568     ret = CStdStubBuffer_CountRefs(iface);
569     ret += IRpcStubBuffer_CountRefs(This->base_stub);
570
571     return ret;
572 }
573
574 const IRpcStubBufferVtbl CStdStubBuffer_Delegating_Vtbl =
575 {
576     CStdStubBuffer_QueryInterface,
577     CStdStubBuffer_AddRef,
578     NULL,
579     CStdStubBuffer_Delegating_Connect,
580     CStdStubBuffer_Delegating_Disconnect,
581     CStdStubBuffer_Invoke,
582     CStdStubBuffer_IsIIDSupported,
583     CStdStubBuffer_Delegating_CountRefs,
584     CStdStubBuffer_DebugServerQueryInterface,
585     CStdStubBuffer_DebugServerRelease
586 };
587
588 const MIDL_SERVER_INFO *CStdStubBuffer_GetServerInfo(IRpcStubBuffer *iface)
589 {
590   CStdStubBuffer *This = (CStdStubBuffer *)iface;
591   return STUB_HEADER(This).pServerInfo;
592 }
593
594 /************************************************************************
595  *           NdrStubForwardingFunction [RPCRT4.@]
596  */
597 void __RPC_STUB NdrStubForwardingFunction( IRpcStubBuffer *iface, IRpcChannelBuffer *pChannel,
598                                            PRPC_MESSAGE pMsg, DWORD *pdwStubPhase )
599 {
600     /* Note pMsg is passed intact since RPCOLEMESSAGE is basically a RPC_MESSAGE. */
601
602     cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
603     HRESULT r = IRpcStubBuffer_Invoke(This->base_stub, (RPCOLEMESSAGE*)pMsg, pChannel);
604     if(FAILED(r)) RpcRaiseException(r);
605     return;
606 }
607
608 /***********************************************************************
609  *           NdrStubInitialize [RPCRT4.@]
610  */
611 void WINAPI NdrStubInitialize(PRPC_MESSAGE pRpcMsg,
612                              PMIDL_STUB_MESSAGE pStubMsg,
613                              PMIDL_STUB_DESC pStubDescriptor,
614                              LPRPCCHANNELBUFFER pRpcChannelBuffer)
615 {
616   TRACE("(%p,%p,%p,%p)\n", pRpcMsg, pStubMsg, pStubDescriptor, pRpcChannelBuffer);
617   NdrServerInitializeNew(pRpcMsg, pStubMsg, pStubDescriptor);
618   pStubMsg->pRpcChannelBuffer = pRpcChannelBuffer;
619   IRpcChannelBuffer_GetDestCtx(pStubMsg->pRpcChannelBuffer,
620                                &pStubMsg->dwDestContext,
621                                &pStubMsg->pvDestContext);
622 }
623
624 /***********************************************************************
625  *           NdrStubGetBuffer [RPCRT4.@]
626  */
627 void WINAPI NdrStubGetBuffer(LPRPCSTUBBUFFER iface,
628                             LPRPCCHANNELBUFFER pRpcChannelBuffer,
629                             PMIDL_STUB_MESSAGE pStubMsg)
630 {
631   CStdStubBuffer *This = (CStdStubBuffer *)iface;
632   HRESULT hr;
633
634   TRACE("(%p, %p, %p)\n", This, pRpcChannelBuffer, pStubMsg);
635
636   pStubMsg->RpcMsg->BufferLength = pStubMsg->BufferLength;
637   hr = IRpcChannelBuffer_GetBuffer(pRpcChannelBuffer,
638     (RPCOLEMESSAGE *)pStubMsg->RpcMsg, STUB_HEADER(This).piid);
639   if (FAILED(hr))
640   {
641     RpcRaiseException(hr);
642     return;
643   }
644
645   pStubMsg->Buffer = pStubMsg->RpcMsg->Buffer;
646 }