msvcr71: Forward more functions to msvcrt.dll.
[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 #if defined(__i386__)
141
142 /* The idea here is to replace the first param on the stack
143    ie. This (which will point to cstdstubbuffer_delegating_t)
144    with This->stub_buffer.pvServerObject and then jump to the
145    relevant offset in This->stub_buffer.pvServerObject's vtbl.
146 */
147 #include "pshpack1.h"
148 typedef struct {
149     DWORD mov1;    /* mov 0x4(%esp), %eax      8b 44 24 04 */
150     WORD mov2;     /* mov 0x10(%eax), %eax     8b 40 */
151     BYTE sixteen;  /*                          10   */
152     DWORD mov3;    /* mov %eax, 0x4(%esp)      89 44 24 04 */
153     WORD mov4;     /* mov (%eax), %eax         8b 00 */
154     WORD mov5;     /* mov offset(%eax), %eax   8b 80 */
155     DWORD offset;  /*                          xx xx xx xx */
156     WORD jmp;      /* jmp *%eax                ff e0 */
157     BYTE pad[3];   /* lea 0x0(%esi), %esi      8d 76 00 */
158 } vtbl_method_t;
159 #include "poppack.h"
160
161 #define BLOCK_SIZE 1024
162 #define MAX_BLOCKS 64  /* 64k methods should be enough for anybody */
163
164 static const vtbl_method_t *method_blocks[MAX_BLOCKS];
165
166 static const vtbl_method_t *allocate_block( unsigned int num )
167 {
168     unsigned int i;
169     vtbl_method_t *prev, *block;
170
171     block = VirtualAlloc( NULL, BLOCK_SIZE * sizeof(*block),
172                           MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE );
173     if (!block) return NULL;
174
175     for (i = 0; i < BLOCK_SIZE; i++)
176     {
177         block[i].mov1 = 0x0424448b;
178         block[i].mov2 = 0x408b;
179         block[i].sixteen = 0x10;
180         block[i].mov3 = 0x04244489;
181         block[i].mov4 = 0x008b;
182         block[i].mov5 = 0x808b;
183         block[i].offset = (BLOCK_SIZE * num + i + 3) << 2;
184         block[i].jmp = 0xe0ff;
185         block[i].pad[0] = 0x8d;
186         block[i].pad[1] = 0x76;
187         block[i].pad[2] = 0x00;
188     }
189     VirtualProtect( block, BLOCK_SIZE * sizeof(*block), PAGE_EXECUTE_READ, NULL );
190     prev = InterlockedCompareExchangePointer( (void **)&method_blocks[num], block, NULL );
191     if (prev) /* someone beat us to it */
192     {
193         VirtualFree( block, 0, MEM_RELEASE );
194         block = prev;
195     }
196     return block;
197 }
198
199 static BOOL fill_delegated_stub_table(IUnknownVtbl *vtbl, DWORD num)
200 {
201     const void **entry = (const void **)(vtbl + 1);
202     DWORD i, j;
203
204     if (num - 3 > BLOCK_SIZE * MAX_BLOCKS)
205     {
206         FIXME( "%u methods not supported\n", num );
207         return FALSE;
208     }
209     vtbl->QueryInterface = delegating_QueryInterface;
210     vtbl->AddRef = delegating_AddRef;
211     vtbl->Release = delegating_Release;
212     for (i = 0; i < (num - 3 + BLOCK_SIZE - 1) / BLOCK_SIZE; i++)
213     {
214         const vtbl_method_t *block = method_blocks[i];
215         if (!block && !(block = allocate_block( i ))) return FALSE;
216         for (j = 0; j < BLOCK_SIZE && j < num - 3 - i * BLOCK_SIZE; j++) *entry++ = &block[j];
217     }
218     return TRUE;
219 }
220
221 BOOL fill_delegated_proxy_table(IUnknownVtbl *vtbl, DWORD num)
222 {
223     const void **entry = (const void **)(vtbl + 1);
224     DWORD i, j;
225
226     if (num - 3 > BLOCK_SIZE * MAX_BLOCKS)
227     {
228         FIXME( "%u methods not supported\n", num );
229         return FALSE;
230     }
231     vtbl->QueryInterface = IUnknown_QueryInterface_Proxy;
232     vtbl->AddRef = IUnknown_AddRef_Proxy;
233     vtbl->Release = IUnknown_Release_Proxy;
234     for (i = 0; i < (num - 3 + BLOCK_SIZE - 1) / BLOCK_SIZE; i++)
235     {
236         const vtbl_method_t *block = method_blocks[i];
237         if (!block && !(block = allocate_block( i ))) return FALSE;
238         for (j = 0; j < BLOCK_SIZE && j < num - 3 - i * BLOCK_SIZE; j++, entry++)
239             if (!*entry) *entry = &block[j];
240     }
241     return TRUE;
242 }
243
244 #else  /* __i386__ */
245
246 static BOOL fill_delegated_stub_table(IUnknownVtbl *vtbl, DWORD num)
247 {
248     ERR("delegated stubs are not supported on this architecture\n");
249     return FALSE;
250 }
251
252 BOOL fill_delegated_proxy_table(IUnknownVtbl *vtbl, DWORD num)
253 {
254     ERR("delegated proxies are not supported on this architecture\n");
255     return FALSE;
256 }
257
258 #endif  /* __i386__ */
259
260 static IUnknownVtbl *get_delegating_vtbl(DWORD num_methods)
261 {
262     IUnknownVtbl *ret;
263
264     if (num_methods < 256) num_methods = 256;  /* avoid frequent reallocations */
265
266     EnterCriticalSection(&delegating_vtbl_section);
267
268     if(!current_vtbl || num_methods > current_vtbl->size)
269     {
270         ref_counted_vtbl *table = HeapAlloc(GetProcessHeap(), 0,
271                                             FIELD_OFFSET(ref_counted_vtbl, vtbl) + num_methods * sizeof(void*));
272         if (!table)
273         {
274             LeaveCriticalSection(&delegating_vtbl_section);
275             return NULL;
276         }
277
278         table->ref = 0;
279         table->size = num_methods;
280         fill_delegated_stub_table(&table->vtbl, num_methods);
281
282         if (current_vtbl && current_vtbl->ref == 0)
283         {
284             TRACE("freeing old table\n");
285             HeapFree(GetProcessHeap(), 0, current_vtbl);
286         }
287         current_vtbl = table;
288     }
289
290     current_vtbl->ref++;
291     ret = &current_vtbl->vtbl;
292     LeaveCriticalSection(&delegating_vtbl_section);
293     return ret;
294 }
295
296 static void release_delegating_vtbl(IUnknownVtbl *vtbl)
297 {
298     ref_counted_vtbl *table = (ref_counted_vtbl*)((DWORD *)vtbl - 1);
299
300     EnterCriticalSection(&delegating_vtbl_section);
301     table->ref--;
302     TRACE("ref now %d\n", table->ref);
303     if(table->ref == 0 && table != current_vtbl)
304     {
305         TRACE("... and we're not current so free'ing\n");
306         HeapFree(GetProcessHeap(), 0, table);
307     }
308     LeaveCriticalSection(&delegating_vtbl_section);
309 }
310
311 HRESULT CStdStubBuffer_Delegating_Construct(REFIID riid,
312                                             LPUNKNOWN pUnkServer,
313                                             PCInterfaceName name,
314                                             CInterfaceStubVtbl *vtbl,
315                                             REFIID delegating_iid,
316                                             LPPSFACTORYBUFFER pPSFactory,
317                                             LPRPCSTUBBUFFER *ppStub)
318 {
319     cstdstubbuffer_delegating_t *This;
320     IUnknown *pvServer;
321     HRESULT r;
322
323     TRACE("(%p,%p,%p,%p) %s\n", pUnkServer, vtbl, pPSFactory, ppStub, name);
324     TRACE("iid=%s delegating to %s\n", debugstr_guid(vtbl->header.piid), debugstr_guid(delegating_iid));
325     TRACE("vtbl=%p\n", &vtbl->Vtbl);
326
327     if (!IsEqualGUID(vtbl->header.piid, riid))
328     {
329         ERR("IID mismatch during stub creation\n");
330         return RPC_E_UNEXPECTED;
331     }
332
333     r = IUnknown_QueryInterface(pUnkServer, riid, (void**)&pvServer);
334     if(FAILED(r)) return r;
335
336     This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This));
337     if (!This)
338     {
339         IUnknown_Release(pvServer);
340         return E_OUTOFMEMORY;
341     }
342
343     This->base_obj = get_delegating_vtbl( vtbl->header.DispatchTableCount );
344     r = create_stub(delegating_iid, (IUnknown*)&This->base_obj, &This->base_stub);
345     if(FAILED(r))
346     {
347         release_delegating_vtbl(This->base_obj);
348         HeapFree(GetProcessHeap(), 0, This);
349         IUnknown_Release(pvServer);
350         return r;
351     }
352
353     This->stub_buffer.lpVtbl = &vtbl->Vtbl;
354     This->stub_buffer.RefCount = 1;
355     This->stub_buffer.pvServerObject = pvServer;
356     This->stub_buffer.pPSFactory = pPSFactory;
357     *ppStub = (LPRPCSTUBBUFFER)&This->stub_buffer;
358
359     IPSFactoryBuffer_AddRef(pPSFactory);
360     return S_OK;
361 }
362
363 HRESULT WINAPI CStdStubBuffer_QueryInterface(LPRPCSTUBBUFFER iface,
364                                             REFIID riid,
365                                             LPVOID *obj)
366 {
367   CStdStubBuffer *This = (CStdStubBuffer *)iface;
368   TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
369
370   if (IsEqualIID(&IID_IUnknown, riid) ||
371       IsEqualIID(&IID_IRpcStubBuffer, riid))
372   {
373     IUnknown_AddRef(iface);
374     *obj = iface;
375     return S_OK;
376   }
377   *obj = NULL;
378   return E_NOINTERFACE;
379 }
380
381 ULONG WINAPI CStdStubBuffer_AddRef(LPRPCSTUBBUFFER iface)
382 {
383   CStdStubBuffer *This = (CStdStubBuffer *)iface;
384   TRACE("(%p)->AddRef()\n",This);
385   return InterlockedIncrement(&This->RefCount);
386 }
387
388 ULONG WINAPI NdrCStdStubBuffer_Release(LPRPCSTUBBUFFER iface,
389                                       LPPSFACTORYBUFFER pPSF)
390 {
391   CStdStubBuffer *This = (CStdStubBuffer *)iface;
392   ULONG refs;
393
394   TRACE("(%p)->Release()\n",This);
395
396   refs = InterlockedDecrement(&This->RefCount);
397   if (!refs)
398   {
399     /* test_Release shows that native doesn't call Disconnect here.
400        We'll leave it in for the time being. */
401     IRpcStubBuffer_Disconnect(iface);
402
403     IPSFactoryBuffer_Release(pPSF);
404     HeapFree(GetProcessHeap(),0,This);
405   }
406   return refs;
407 }
408
409 ULONG WINAPI NdrCStdStubBuffer2_Release(LPRPCSTUBBUFFER iface,
410                                         LPPSFACTORYBUFFER pPSF)
411 {
412     cstdstubbuffer_delegating_t *This = impl_from_delegating( iface );
413     ULONG refs;
414
415     TRACE("(%p)->Release()\n", This);
416
417     refs = InterlockedDecrement(&This->stub_buffer.RefCount);
418     if (!refs)
419     {
420         /* Just like NdrCStdStubBuffer_Release, we shouldn't call
421            Disconnect here */
422         IRpcStubBuffer_Disconnect((IRpcStubBuffer *)&This->stub_buffer);
423
424         IRpcStubBuffer_Release(This->base_stub);
425         release_delegating_vtbl(This->base_obj);
426
427         IPSFactoryBuffer_Release(pPSF);
428         HeapFree(GetProcessHeap(), 0, This);
429     }
430
431     return refs;
432 }
433
434 HRESULT WINAPI CStdStubBuffer_Connect(LPRPCSTUBBUFFER iface,
435                                      LPUNKNOWN lpUnkServer)
436 {
437     CStdStubBuffer *This = (CStdStubBuffer *)iface;
438     HRESULT r;
439     IUnknown *new = NULL;
440
441     TRACE("(%p)->Connect(%p)\n",This,lpUnkServer);
442
443     r = IUnknown_QueryInterface(lpUnkServer, STUB_HEADER(This).piid, (void**)&new);
444     new = InterlockedExchangePointer((void**)&This->pvServerObject, new);
445     if(new)
446         IUnknown_Release(new);
447     return r;
448 }
449
450 void WINAPI CStdStubBuffer_Disconnect(LPRPCSTUBBUFFER iface)
451 {
452     CStdStubBuffer *This = (CStdStubBuffer *)iface;
453     IUnknown *old;
454     TRACE("(%p)->Disconnect()\n",This);
455
456     old = InterlockedExchangePointer((void**)&This->pvServerObject, NULL);
457
458     if(old)
459         IUnknown_Release(old);
460 }
461
462 HRESULT WINAPI CStdStubBuffer_Invoke(LPRPCSTUBBUFFER iface,
463                                     PRPCOLEMESSAGE pMsg,
464                                     LPRPCCHANNELBUFFER pChannel)
465 {
466   CStdStubBuffer *This = (CStdStubBuffer *)iface;
467   DWORD dwPhase = STUB_UNMARSHAL;
468   HRESULT hr = S_OK;
469
470   TRACE("(%p)->Invoke(%p,%p)\n",This,pMsg,pChannel);
471
472   __TRY
473   {
474     if (STUB_HEADER(This).pDispatchTable)
475       STUB_HEADER(This).pDispatchTable[pMsg->iMethod](iface, pChannel, (PRPC_MESSAGE)pMsg, &dwPhase);
476     else /* pure interpreted */
477       NdrStubCall2(iface, pChannel, (PRPC_MESSAGE)pMsg, &dwPhase);
478   }
479   __EXCEPT(stub_filter)
480   {
481     DWORD dwExceptionCode = GetExceptionCode();
482     WARN("a stub call failed with exception 0x%08x (%d)\n", dwExceptionCode, dwExceptionCode);
483     if (FAILED(dwExceptionCode))
484       hr = dwExceptionCode;
485     else
486       hr = HRESULT_FROM_WIN32(dwExceptionCode);
487   }
488   __ENDTRY
489
490   return hr;
491 }
492
493 LPRPCSTUBBUFFER WINAPI CStdStubBuffer_IsIIDSupported(LPRPCSTUBBUFFER iface,
494                                                     REFIID riid)
495 {
496   CStdStubBuffer *This = (CStdStubBuffer *)iface;
497   TRACE("(%p)->IsIIDSupported(%s)\n",This,debugstr_guid(riid));
498   return IsEqualGUID(STUB_HEADER(This).piid, riid) ? iface : NULL;
499 }
500
501 ULONG WINAPI CStdStubBuffer_CountRefs(LPRPCSTUBBUFFER iface)
502 {
503   CStdStubBuffer *This = (CStdStubBuffer *)iface;
504   TRACE("(%p)->CountRefs()\n",This);
505   return This->RefCount;
506 }
507
508 HRESULT WINAPI CStdStubBuffer_DebugServerQueryInterface(LPRPCSTUBBUFFER iface,
509                                                        LPVOID *ppv)
510 {
511   CStdStubBuffer *This = (CStdStubBuffer *)iface;
512   TRACE("(%p)->DebugServerQueryInterface(%p)\n",This,ppv);
513   return S_OK;
514 }
515
516 void WINAPI CStdStubBuffer_DebugServerRelease(LPRPCSTUBBUFFER iface,
517                                              LPVOID pv)
518 {
519   CStdStubBuffer *This = (CStdStubBuffer *)iface;
520   TRACE("(%p)->DebugServerRelease(%p)\n",This,pv);
521 }
522
523 const IRpcStubBufferVtbl CStdStubBuffer_Vtbl =
524 {
525     CStdStubBuffer_QueryInterface,
526     CStdStubBuffer_AddRef,
527     NULL,
528     CStdStubBuffer_Connect,
529     CStdStubBuffer_Disconnect,
530     CStdStubBuffer_Invoke,
531     CStdStubBuffer_IsIIDSupported,
532     CStdStubBuffer_CountRefs,
533     CStdStubBuffer_DebugServerQueryInterface,
534     CStdStubBuffer_DebugServerRelease
535 };
536
537 static HRESULT WINAPI CStdStubBuffer_Delegating_Connect(LPRPCSTUBBUFFER iface,
538                                                         LPUNKNOWN lpUnkServer)
539 {
540     cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
541     HRESULT r;
542     TRACE("(%p)->Connect(%p)\n", This, lpUnkServer);
543
544     r = CStdStubBuffer_Connect(iface, lpUnkServer);
545     if(SUCCEEDED(r))
546         r = IRpcStubBuffer_Connect(This->base_stub, (IUnknown*)&This->base_obj);
547
548     return r;
549 }
550
551 static void WINAPI CStdStubBuffer_Delegating_Disconnect(LPRPCSTUBBUFFER iface)
552 {
553     cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
554     TRACE("(%p)->Disconnect()\n", This);
555
556     IRpcStubBuffer_Disconnect(This->base_stub);
557     CStdStubBuffer_Disconnect(iface);
558 }
559
560 static ULONG WINAPI CStdStubBuffer_Delegating_CountRefs(LPRPCSTUBBUFFER iface)
561 {
562     cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
563     ULONG ret;
564     TRACE("(%p)->CountRefs()\n", This);
565
566     ret = CStdStubBuffer_CountRefs(iface);
567     ret += IRpcStubBuffer_CountRefs(This->base_stub);
568
569     return ret;
570 }
571
572 const IRpcStubBufferVtbl CStdStubBuffer_Delegating_Vtbl =
573 {
574     CStdStubBuffer_QueryInterface,
575     CStdStubBuffer_AddRef,
576     NULL,
577     CStdStubBuffer_Delegating_Connect,
578     CStdStubBuffer_Delegating_Disconnect,
579     CStdStubBuffer_Invoke,
580     CStdStubBuffer_IsIIDSupported,
581     CStdStubBuffer_Delegating_CountRefs,
582     CStdStubBuffer_DebugServerQueryInterface,
583     CStdStubBuffer_DebugServerRelease
584 };
585
586 const MIDL_SERVER_INFO *CStdStubBuffer_GetServerInfo(IRpcStubBuffer *iface)
587 {
588   CStdStubBuffer *This = (CStdStubBuffer *)iface;
589   return STUB_HEADER(This).pServerInfo;
590 }
591
592 /************************************************************************
593  *           NdrStubForwardingFunction [RPCRT4.@]
594  */
595 void __RPC_STUB NdrStubForwardingFunction( IRpcStubBuffer *iface, IRpcChannelBuffer *pChannel,
596                                            PRPC_MESSAGE pMsg, DWORD *pdwStubPhase )
597 {
598     /* Note pMsg is passed intact since RPCOLEMESSAGE is basically a RPC_MESSAGE. */
599
600     cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
601     HRESULT r = IRpcStubBuffer_Invoke(This->base_stub, (RPCOLEMESSAGE*)pMsg, pChannel);
602     if(FAILED(r)) RpcRaiseException(r);
603     return;
604 }
605
606 /***********************************************************************
607  *           NdrStubInitialize [RPCRT4.@]
608  */
609 void WINAPI NdrStubInitialize(PRPC_MESSAGE pRpcMsg,
610                              PMIDL_STUB_MESSAGE pStubMsg,
611                              PMIDL_STUB_DESC pStubDescriptor,
612                              LPRPCCHANNELBUFFER pRpcChannelBuffer)
613 {
614   TRACE("(%p,%p,%p,%p)\n", pRpcMsg, pStubMsg, pStubDescriptor, pRpcChannelBuffer);
615   NdrServerInitializeNew(pRpcMsg, pStubMsg, pStubDescriptor);
616   pStubMsg->pRpcChannelBuffer = pRpcChannelBuffer;
617   IRpcChannelBuffer_GetDestCtx(pStubMsg->pRpcChannelBuffer,
618                                &pStubMsg->dwDestContext,
619                                &pStubMsg->pvDestContext);
620 }
621
622 /***********************************************************************
623  *           NdrStubGetBuffer [RPCRT4.@]
624  */
625 void WINAPI NdrStubGetBuffer(LPRPCSTUBBUFFER iface,
626                             LPRPCCHANNELBUFFER pRpcChannelBuffer,
627                             PMIDL_STUB_MESSAGE pStubMsg)
628 {
629   CStdStubBuffer *This = (CStdStubBuffer *)iface;
630   HRESULT hr;
631
632   TRACE("(%p, %p, %p)\n", This, pRpcChannelBuffer, pStubMsg);
633
634   pStubMsg->RpcMsg->BufferLength = pStubMsg->BufferLength;
635   hr = IRpcChannelBuffer_GetBuffer(pRpcChannelBuffer,
636     (RPCOLEMESSAGE *)pStubMsg->RpcMsg, STUB_HEADER(This).piid);
637   if (FAILED(hr))
638   {
639     RpcRaiseException(hr);
640     return;
641   }
642
643   pStubMsg->Buffer = pStubMsg->RpcMsg->Buffer;
644 }