rpcrt4: Fill in the IRpcStubBuffer vtables for interfaces that don't
[wine] / dlls / rpcrt4 / ndr_midl.c
1 /*
2  * MIDL proxy/stub stuff
3  *
4  * Copyright 2002 Ove Kåven, TransGaming Technologies
5  *
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.
10  *
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.
15  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * TODO:
21  *  - figure out whether we *really* got this right
22  *  - check for errors and throw exceptions
23  */
24
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <assert.h>
29
30 #define COBJMACROS
31
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winerror.h"
35 #include "winreg.h"
36
37 #include "objbase.h"
38
39 #include "rpcproxy.h"
40
41 #include "wine/debug.h"
42
43 #include "cpsf.h"
44 #include "ndr_misc.h"
45 #include "rpcndr.h"
46
47 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
48
49 /***********************************************************************
50  *           NdrProxyInitialize [RPCRT4.@]
51  */
52 void WINAPI NdrProxyInitialize(void *This,
53                               PRPC_MESSAGE pRpcMsg,
54                               PMIDL_STUB_MESSAGE pStubMsg,
55                               PMIDL_STUB_DESC pStubDescriptor,
56                               unsigned int ProcNum)
57 {
58   HRESULT hr;
59
60   TRACE("(%p,%p,%p,%p,%d)\n", This, pRpcMsg, pStubMsg, pStubDescriptor, ProcNum);
61   NdrClientInitializeNew(pRpcMsg, pStubMsg, pStubDescriptor, ProcNum);
62   if (This) StdProxy_GetChannel(This, &pStubMsg->pRpcChannelBuffer);
63   if (pStubMsg->pRpcChannelBuffer) {
64     hr = IRpcChannelBuffer_GetDestCtx(pStubMsg->pRpcChannelBuffer,
65                                      &pStubMsg->dwDestContext,
66                                      &pStubMsg->pvDestContext);
67   }
68   TRACE("channel=%p\n", pStubMsg->pRpcChannelBuffer);
69 }
70
71 /***********************************************************************
72  *           NdrProxyGetBuffer [RPCRT4.@]
73  */
74 void WINAPI NdrProxyGetBuffer(void *This,
75                              PMIDL_STUB_MESSAGE pStubMsg)
76 {
77   HRESULT hr;
78   const IID *riid = NULL;
79
80   TRACE("(%p,%p)\n", This, pStubMsg);
81   pStubMsg->RpcMsg->BufferLength = pStubMsg->BufferLength;
82   pStubMsg->dwStubPhase = PROXY_GETBUFFER;
83   hr = StdProxy_GetIID(This, &riid);
84   hr = IRpcChannelBuffer_GetBuffer(pStubMsg->pRpcChannelBuffer,
85                                   (RPCOLEMESSAGE*)pStubMsg->RpcMsg,
86                                   riid);
87   pStubMsg->BufferStart = pStubMsg->RpcMsg->Buffer;
88   pStubMsg->BufferEnd = pStubMsg->BufferStart + pStubMsg->BufferLength;
89   pStubMsg->Buffer = pStubMsg->BufferStart;
90   pStubMsg->dwStubPhase = PROXY_MARSHAL;
91 }
92
93 /***********************************************************************
94  *           NdrProxySendReceive [RPCRT4.@]
95  */
96 void WINAPI NdrProxySendReceive(void *This,
97                                PMIDL_STUB_MESSAGE pStubMsg)
98 {
99   ULONG Status = 0;
100   HRESULT hr;
101
102   TRACE("(%p,%p)\n", This, pStubMsg);
103
104   if (!pStubMsg->pRpcChannelBuffer)
105   {
106     WARN("Trying to use disconnected proxy %p\n", This);
107     RpcRaiseException(RPC_E_DISCONNECTED);
108   }
109
110   pStubMsg->dwStubPhase = PROXY_SENDRECEIVE;
111   hr = IRpcChannelBuffer_SendReceive(pStubMsg->pRpcChannelBuffer,
112                                     (RPCOLEMESSAGE*)pStubMsg->RpcMsg,
113                                     &Status);
114   pStubMsg->dwStubPhase = PROXY_UNMARSHAL;
115   pStubMsg->BufferLength = pStubMsg->RpcMsg->BufferLength;
116   pStubMsg->BufferStart = pStubMsg->RpcMsg->Buffer;
117   pStubMsg->BufferEnd = pStubMsg->BufferStart + pStubMsg->BufferLength;
118   pStubMsg->Buffer = pStubMsg->BufferStart;
119
120   /* raise exception if call failed */
121   if (hr == RPC_S_CALL_FAILED) RpcRaiseException(*(DWORD*)pStubMsg->Buffer);
122   else if (FAILED(hr)) RpcRaiseException(hr);
123 }
124
125 /***********************************************************************
126  *           NdrProxyFreeBuffer [RPCRT4.@]
127  */
128 void WINAPI NdrProxyFreeBuffer(void *This,
129                               PMIDL_STUB_MESSAGE pStubMsg)
130 {
131   HRESULT hr;
132
133   TRACE("(%p,%p)\n", This, pStubMsg);
134   hr = IRpcChannelBuffer_FreeBuffer(pStubMsg->pRpcChannelBuffer,
135                                    (RPCOLEMESSAGE*)pStubMsg->RpcMsg);
136 }
137
138 /***********************************************************************
139  *           NdrProxyErrorHandler [RPCRT4.@]
140  */
141 HRESULT WINAPI NdrProxyErrorHandler(DWORD dwExceptionCode)
142 {
143   WARN("(0x%08lx): a proxy call failed\n", dwExceptionCode);
144
145   if (FAILED(dwExceptionCode))
146     return dwExceptionCode;
147   else
148     return HRESULT_FROM_WIN32(dwExceptionCode);
149 }
150
151 /***********************************************************************
152  *           NdrStubInitialize [RPCRT4.@]
153  */
154 void WINAPI NdrStubInitialize(PRPC_MESSAGE pRpcMsg,
155                              PMIDL_STUB_MESSAGE pStubMsg,
156                              PMIDL_STUB_DESC pStubDescriptor,
157                              LPRPCCHANNELBUFFER pRpcChannelBuffer)
158 {
159   TRACE("(%p,%p,%p,%p)\n", pRpcMsg, pStubMsg, pStubDescriptor, pRpcChannelBuffer);
160   NdrServerInitializeNew(pRpcMsg, pStubMsg, pStubDescriptor);
161   pStubMsg->pRpcChannelBuffer = pRpcChannelBuffer;
162 }
163
164 /***********************************************************************
165  *           NdrStubGetBuffer [RPCRT4.@]
166  */
167 void WINAPI NdrStubGetBuffer(LPRPCSTUBBUFFER This,
168                             LPRPCCHANNELBUFFER pRpcChannelBuffer,
169                             PMIDL_STUB_MESSAGE pStubMsg)
170 {
171   TRACE("(%p,%p)\n", This, pStubMsg);
172   pStubMsg->pRpcChannelBuffer = pRpcChannelBuffer;
173   pStubMsg->RpcMsg->BufferLength = pStubMsg->BufferLength;
174   I_RpcGetBuffer(pStubMsg->RpcMsg); /* ? */
175   pStubMsg->BufferStart = pStubMsg->RpcMsg->Buffer;
176   pStubMsg->BufferEnd = pStubMsg->BufferStart + pStubMsg->BufferLength;
177   pStubMsg->Buffer = pStubMsg->BufferStart;
178 }
179
180 /************************************************************************
181  *             NdrClientInitializeNew [RPCRT4.@]
182  */
183 void WINAPI NdrClientInitializeNew( PRPC_MESSAGE pRpcMessage, PMIDL_STUB_MESSAGE pStubMsg, 
184                                     PMIDL_STUB_DESC pStubDesc, unsigned int ProcNum )
185 {
186   TRACE("(pRpcMessage == ^%p, pStubMsg == ^%p, pStubDesc == ^%p, ProcNum == %d)\n",
187     pRpcMessage, pStubMsg, pStubDesc, ProcNum);
188
189   assert( pRpcMessage && pStubMsg && pStubDesc );
190
191   memset(pRpcMessage, 0, sizeof(RPC_MESSAGE));
192   pRpcMessage->DataRepresentation = NDR_LOCAL_DATA_REPRESENTATION;
193
194   /* not everyone allocates stack space for w2kReserved */
195   memset(pStubMsg, 0, FIELD_OFFSET(MIDL_STUB_MESSAGE,pCSInfo));
196
197   pStubMsg->ReuseBuffer = FALSE;
198   pStubMsg->IsClient = TRUE;
199   pStubMsg->StubDesc = pStubDesc;
200   pStubMsg->pfnAllocate = pStubDesc->pfnAllocate;
201   pStubMsg->pfnFree = pStubDesc->pfnFree;
202   pStubMsg->RpcMsg = pRpcMessage;
203
204   pRpcMessage->ProcNum = ProcNum;
205   pRpcMessage->RpcInterfaceInformation = pStubDesc->RpcInterfaceInformation;
206 }
207
208 /***********************************************************************
209  *             NdrServerInitializeNew [RPCRT4.@]
210  */
211 unsigned char* WINAPI NdrServerInitializeNew( PRPC_MESSAGE pRpcMsg, PMIDL_STUB_MESSAGE pStubMsg,
212                                               PMIDL_STUB_DESC pStubDesc )
213 {
214   TRACE("(pRpcMsg == ^%p, pStubMsg == ^%p, pStubDesc == ^%p)\n", pRpcMsg, pStubMsg, pStubDesc);
215
216   assert( pRpcMsg && pStubMsg && pStubDesc );
217
218   /* not everyone allocates stack space for w2kReserved */
219   memset(pStubMsg, 0, FIELD_OFFSET(MIDL_STUB_MESSAGE,pCSInfo));
220
221   pStubMsg->ReuseBuffer = TRUE;
222   pStubMsg->IsClient = FALSE;
223   pStubMsg->StubDesc = pStubDesc;
224   pStubMsg->pfnAllocate = pStubDesc->pfnAllocate;
225   pStubMsg->pfnFree = pStubDesc->pfnFree;
226   pStubMsg->RpcMsg = pRpcMsg;
227   pStubMsg->Buffer = pStubMsg->BufferStart = pRpcMsg->Buffer;
228   pStubMsg->BufferLength = pRpcMsg->BufferLength;
229   pStubMsg->BufferEnd = pStubMsg->Buffer + pStubMsg->BufferLength;
230
231   /* FIXME: determine the proper return value */
232   return NULL;
233 }
234
235 /***********************************************************************
236  *           NdrGetBuffer [RPCRT4.@]
237  */
238 unsigned char *WINAPI NdrGetBuffer(MIDL_STUB_MESSAGE *stubmsg, unsigned long buflen, RPC_BINDING_HANDLE handle)
239 {
240   TRACE("(stubmsg == ^%p, buflen == %lu, handle == %p): wild guess.\n", stubmsg, buflen, handle);
241   
242   assert( stubmsg && stubmsg->RpcMsg );
243
244   /* I guess this is our chance to put the binding handle into the RPC_MESSAGE */
245   stubmsg->RpcMsg->Handle = handle;
246   
247   stubmsg->RpcMsg->BufferLength = buflen;
248   if (I_RpcGetBuffer(stubmsg->RpcMsg) != S_OK)
249     return NULL;
250
251   stubmsg->Buffer = stubmsg->BufferStart = stubmsg->RpcMsg->Buffer;
252   stubmsg->BufferLength = stubmsg->RpcMsg->BufferLength;
253   stubmsg->BufferEnd = stubmsg->Buffer + stubmsg->BufferLength;
254   return (stubmsg->Buffer = (unsigned char *)stubmsg->RpcMsg->Buffer);
255 }
256 /***********************************************************************
257  *           NdrFreeBuffer [RPCRT4.@]
258  */
259 void WINAPI NdrFreeBuffer(MIDL_STUB_MESSAGE *pStubMsg)
260 {
261   TRACE("(pStubMsg == ^%p): wild guess.\n", pStubMsg);
262   I_RpcFreeBuffer(pStubMsg->RpcMsg);
263   pStubMsg->BufferLength = 0;
264   pStubMsg->Buffer = pStubMsg->BufferEnd = (unsigned char *)(pStubMsg->RpcMsg->Buffer = NULL);
265 }
266
267 /************************************************************************
268  *           NdrSendReceive [RPCRT4.@]
269  */
270 unsigned char *WINAPI NdrSendReceive( MIDL_STUB_MESSAGE *stubmsg, unsigned char *buffer  )
271 {
272   TRACE("(stubmsg == ^%p, buffer == ^%p)\n", stubmsg, buffer);
273
274   /* FIXME: how to handle errors? (raise exception?) */
275   if (!stubmsg) {
276     ERR("NULL stub message.  No action taken.\n");
277     return NULL;
278   }
279   if (!stubmsg->RpcMsg) {
280     ERR("RPC Message not present in stub message.  No action taken.\n");
281     return NULL;
282   }
283
284   if (I_RpcSendReceive(stubmsg->RpcMsg) != RPC_S_OK) {
285     WARN("I_RpcSendReceive did not return success.\n");
286     /* FIXME: raise exception? */
287   }
288
289   stubmsg->BufferLength = stubmsg->RpcMsg->BufferLength;
290   stubmsg->BufferStart = stubmsg->RpcMsg->Buffer;
291   stubmsg->BufferEnd = stubmsg->BufferStart + stubmsg->BufferLength;
292   stubmsg->Buffer = stubmsg->BufferStart;
293
294   /* FIXME: is this the right return value? */
295   return NULL;
296 }
297
298 /************************************************************************
299  *           NdrMapCommAndFaultStatus [RPCRT4.@]
300  */
301 RPC_STATUS RPC_ENTRY NdrMapCommAndFaultStatus( PMIDL_STUB_MESSAGE pStubMsg,
302                                                unsigned long *pCommStatus,
303                                                unsigned long *pFaultStatus,
304                                                RPC_STATUS Status )
305 {
306     FIXME("(%p, %p, %p, %ld): stub\n", pStubMsg, pCommStatus, pFaultStatus, Status);
307
308     *pCommStatus = 0;
309     *pFaultStatus = 0;
310
311     return RPC_S_OK;
312 }
313
314 /************************************************************************
315  *           NdrStubForwardingFunction [RPCRT4.@]
316  */
317 void __RPC_STUB NdrStubForwardingFunction( IRpcStubBuffer *This, IRpcChannelBuffer *pChannel,
318                                            PRPC_MESSAGE pMsg, DWORD *pdwStubPhase )
319 {
320     FIXME("Not implemented\n");
321     return;
322 }