Added semi-stub for NdrProxyErrorHandler.
[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 <stdio.h>
26 #include <string.h>
27 #include <assert.h>
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winerror.h"
32 #include "winreg.h"
33
34 #include "objbase.h"
35
36 #include "rpcproxy.h"
37
38 #include "wine/debug.h"
39
40 #include "cpsf.h"
41 #include "ndr_misc.h"
42 #include "rpcndr.h"
43
44 WINE_DEFAULT_DEBUG_CHANNEL(ole);
45
46 /***********************************************************************
47  *           NdrProxyInitialize [RPCRT4.@]
48  */
49 void WINAPI NdrProxyInitialize(void *This,
50                               PRPC_MESSAGE pRpcMsg,
51                               PMIDL_STUB_MESSAGE pStubMsg,
52                               PMIDL_STUB_DESC pStubDescriptor,
53                               unsigned int ProcNum)
54 {
55   HRESULT hr;
56
57   TRACE("(%p,%p,%p,%p,%d)\n", This, pRpcMsg, pStubMsg, pStubDescriptor, ProcNum);
58   NdrClientInitializeNew(pRpcMsg, pStubMsg, pStubDescriptor, ProcNum);
59   if (This) StdProxy_GetChannel(This, &pStubMsg->pRpcChannelBuffer);
60   if (pStubMsg->pRpcChannelBuffer) {
61     hr = IRpcChannelBuffer_GetDestCtx(pStubMsg->pRpcChannelBuffer,
62                                      &pStubMsg->dwDestContext,
63                                      &pStubMsg->pvDestContext);
64   }
65   TRACE("channel=%p\n", pStubMsg->pRpcChannelBuffer);
66 }
67
68 /***********************************************************************
69  *           NdrProxyGetBuffer [RPCRT4.@]
70  */
71 void WINAPI NdrProxyGetBuffer(void *This,
72                              PMIDL_STUB_MESSAGE pStubMsg)
73 {
74   HRESULT hr;
75   const IID *riid = NULL;
76
77   TRACE("(%p,%p)\n", This, pStubMsg);
78   pStubMsg->RpcMsg->BufferLength = pStubMsg->BufferLength;
79   pStubMsg->dwStubPhase = PROXY_GETBUFFER;
80   hr = StdProxy_GetIID(This, &riid);
81   hr = IRpcChannelBuffer_GetBuffer(pStubMsg->pRpcChannelBuffer,
82                                   (RPCOLEMESSAGE*)pStubMsg->RpcMsg,
83                                   riid);
84   pStubMsg->BufferStart = pStubMsg->RpcMsg->Buffer;
85   pStubMsg->BufferEnd = pStubMsg->BufferStart + pStubMsg->BufferLength;
86   pStubMsg->Buffer = pStubMsg->BufferStart;
87   pStubMsg->dwStubPhase = PROXY_MARSHAL;
88 }
89
90 /***********************************************************************
91  *           NdrProxySendReceive [RPCRT4.@]
92  */
93 void WINAPI NdrProxySendReceive(void *This,
94                                PMIDL_STUB_MESSAGE pStubMsg)
95 {
96   ULONG Status = 0;
97   HRESULT hr;
98
99   TRACE("(%p,%p)\n", This, pStubMsg);
100   pStubMsg->dwStubPhase = PROXY_SENDRECEIVE;
101   hr = IRpcChannelBuffer_SendReceive(pStubMsg->pRpcChannelBuffer,
102                                     (RPCOLEMESSAGE*)pStubMsg->RpcMsg,
103                                     &Status);
104   pStubMsg->dwStubPhase = PROXY_UNMARSHAL;
105   pStubMsg->BufferLength = pStubMsg->RpcMsg->BufferLength;
106   pStubMsg->BufferStart = pStubMsg->RpcMsg->Buffer;
107   pStubMsg->BufferEnd = pStubMsg->BufferStart + pStubMsg->BufferLength;
108   pStubMsg->Buffer = pStubMsg->BufferStart;
109 }
110
111 /***********************************************************************
112  *           NdrProxyFreeBuffer [RPCRT4.@]
113  */
114 void WINAPI NdrProxyFreeBuffer(void *This,
115                               PMIDL_STUB_MESSAGE pStubMsg)
116 {
117   HRESULT hr;
118
119   TRACE("(%p,%p)\n", This, pStubMsg);
120   hr = IRpcChannelBuffer_FreeBuffer(pStubMsg->pRpcChannelBuffer,
121                                    (RPCOLEMESSAGE*)pStubMsg->RpcMsg);
122 }
123
124 /***********************************************************************
125  *           NdrProxyErrorHandler [RPCRT4.@]
126  */
127 HRESULT WINAPI NdrProxyErrorHandler(DWORD dwExceptionCode)
128 {
129   FIXME("(0x%08lx): semi-stub\n", dwExceptionCode);
130   return MAKE_HRESULT(SEVERITY_ERROR, FACILITY_RPC, RPC_S_CALL_FAILED);
131 }
132
133 /***********************************************************************
134  *           NdrStubInitialize [RPCRT4.@]
135  */
136 void WINAPI NdrStubInitialize(PRPC_MESSAGE pRpcMsg,
137                              PMIDL_STUB_MESSAGE pStubMsg,
138                              PMIDL_STUB_DESC pStubDescriptor,
139                              LPRPCCHANNELBUFFER pRpcChannelBuffer)
140 {
141   TRACE("(%p,%p,%p,%p)\n", pRpcMsg, pStubMsg, pStubDescriptor, pRpcChannelBuffer);
142   NdrServerInitializeNew(pRpcMsg, pStubMsg, pStubDescriptor);
143   pStubMsg->pRpcChannelBuffer = pRpcChannelBuffer;
144 }
145
146 /***********************************************************************
147  *           NdrStubGetBuffer [RPCRT4.@]
148  */
149 void WINAPI NdrStubGetBuffer(LPRPCSTUBBUFFER This,
150                             LPRPCCHANNELBUFFER pRpcChannelBuffer,
151                             PMIDL_STUB_MESSAGE pStubMsg)
152 {
153   TRACE("(%p,%p)\n", This, pStubMsg);
154   pStubMsg->pRpcChannelBuffer = pRpcChannelBuffer;
155   pStubMsg->RpcMsg->BufferLength = pStubMsg->BufferLength;
156   I_RpcGetBuffer(pStubMsg->RpcMsg); /* ? */
157   pStubMsg->BufferStart = pStubMsg->RpcMsg->Buffer;
158   pStubMsg->BufferEnd = pStubMsg->BufferStart + pStubMsg->BufferLength;
159   pStubMsg->Buffer = pStubMsg->BufferStart;
160 }
161
162 /************************************************************************
163  *             NdrClientInitializeNew [RPCRT4.@]
164  */
165 void WINAPI NdrClientInitializeNew( PRPC_MESSAGE pRpcMessage, PMIDL_STUB_MESSAGE pStubMsg, 
166                                     PMIDL_STUB_DESC pStubDesc, unsigned int ProcNum )
167 {
168   TRACE("(pRpcMessage == ^%p, pStubMsg == ^%p, pStubDesc == ^%p, ProcNum == %d)\n",
169     pRpcMessage, pStubMsg, pStubDesc, ProcNum);
170
171   assert( pRpcMessage && pStubMsg && pStubDesc );
172
173   memset(pRpcMessage, 0, sizeof(RPC_MESSAGE));
174
175   /* not everyone allocates stack space for w2kReserved */
176   memset(pStubMsg, 0, sizeof(*pStubMsg) - sizeof(pStubMsg->w2kReserved));
177
178   pStubMsg->ReuseBuffer = FALSE;
179   pStubMsg->IsClient = TRUE;
180   pStubMsg->StubDesc = pStubDesc;
181   pStubMsg->pfnAllocate = pStubDesc->pfnAllocate;
182   pStubMsg->pfnFree = pStubDesc->pfnFree;
183   pStubMsg->RpcMsg = pRpcMessage;
184
185   pRpcMessage->ProcNum = ProcNum;
186   pRpcMessage->RpcInterfaceInformation = pStubDesc->RpcInterfaceInformation;
187 }
188
189 /***********************************************************************
190  *             NdrServerInitializeNew [RPCRT4.@]
191  */
192 unsigned char* WINAPI NdrServerInitializeNew( PRPC_MESSAGE pRpcMsg, PMIDL_STUB_MESSAGE pStubMsg,
193                                               PMIDL_STUB_DESC pStubDesc )
194 {
195   TRACE("(pRpcMsg == ^%p, pStubMsg == ^%p, pStubDesc == ^%p)\n", pRpcMsg, pStubMsg, pStubDesc);
196
197   assert( pRpcMsg && pStubMsg && pStubDesc );
198
199   /* not everyone allocates stack space for w2kReserved */
200   memset(pStubMsg, 0, sizeof(*pStubMsg) - sizeof(pStubMsg->w2kReserved));
201
202   pStubMsg->ReuseBuffer = TRUE;
203   pStubMsg->IsClient = FALSE;
204   pStubMsg->StubDesc = pStubDesc;
205   pStubMsg->pfnAllocate = pStubDesc->pfnAllocate;
206   pStubMsg->pfnFree = pStubDesc->pfnFree;
207   pStubMsg->RpcMsg = pRpcMsg;
208   pStubMsg->Buffer = pStubMsg->BufferStart = pRpcMsg->Buffer;
209   pStubMsg->BufferLength = pRpcMsg->BufferLength;
210   pStubMsg->BufferEnd = pStubMsg->Buffer + pStubMsg->BufferLength;
211
212   /* FIXME: determine the proper return value */
213   return NULL;
214 }
215
216 /***********************************************************************
217  *           NdrGetBuffer [RPCRT4.@]
218  */
219 unsigned char *WINAPI NdrGetBuffer(MIDL_STUB_MESSAGE *stubmsg, unsigned long buflen, RPC_BINDING_HANDLE handle)
220 {
221   TRACE("(stubmsg == ^%p, buflen == %lu, handle == %p): wild guess.\n", stubmsg, buflen, handle);
222   
223   assert( stubmsg && stubmsg->RpcMsg );
224
225   /* I guess this is our chance to put the binding handle into the RPC_MESSAGE */
226   stubmsg->RpcMsg->Handle = handle;
227   
228   stubmsg->RpcMsg->BufferLength = buflen;
229   if (I_RpcGetBuffer(stubmsg->RpcMsg) != S_OK)
230     return NULL;
231
232   stubmsg->Buffer = stubmsg->BufferStart = stubmsg->RpcMsg->Buffer;
233   stubmsg->BufferLength = stubmsg->RpcMsg->BufferLength;
234   stubmsg->BufferEnd = stubmsg->Buffer + stubmsg->BufferLength;
235   return (stubmsg->Buffer = (unsigned char *)stubmsg->RpcMsg->Buffer);
236 }
237 /***********************************************************************
238  *           NdrFreeBuffer [RPCRT4.@]
239  */
240 void WINAPI NdrFreeBuffer(MIDL_STUB_MESSAGE *pStubMsg)
241 {
242   TRACE("(pStubMsg == ^%p): wild guess.\n", pStubMsg);
243   I_RpcFreeBuffer(pStubMsg->RpcMsg);
244   pStubMsg->BufferLength = 0;
245   pStubMsg->Buffer = pStubMsg->BufferEnd = (unsigned char *)(pStubMsg->RpcMsg->Buffer = NULL);
246 }
247
248 /************************************************************************
249  *           NdrSendReceive [RPCRT4.@]
250  */
251 unsigned char *WINAPI NdrSendReceive( MIDL_STUB_MESSAGE *stubmsg, unsigned char *buffer  )
252 {
253   TRACE("(stubmsg == ^%p, buffer == ^%p)\n", stubmsg, buffer);
254
255   /* FIXME: how to handle errors? (raise exception?) */
256   if (!stubmsg) {
257     ERR("NULL stub message.  No action taken.\n");
258     return NULL;
259   }
260   if (!stubmsg->RpcMsg) {
261     ERR("RPC Message not present in stub message.  No action taken.\n");
262     return NULL;
263   }
264   if (stubmsg->RpcMsg->Buffer != buffer) {
265     ERR("Ambiguous buffer doesn't match rpc message buffer.  No action taken.\n");
266     return NULL;
267   }
268
269   /* not sure where MS does this; for now I'll stick it here */
270   stubmsg->RpcMsg->DataRepresentation = NDR_LOCAL_DATA_REPRESENTATION;
271
272   if (I_RpcSendReceive(stubmsg->RpcMsg) != RPC_S_OK) {
273     WARN("I_RpcSendReceive did not return success.\n");
274   }
275
276   /* FIXME: is this the right return value? */
277   return NULL;
278 }