Fixed NdrDllRegisterProxy so it registers the key names that ole32
[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  *           NdrStubInitialize [RPCRT4.@]
126  */
127 void WINAPI NdrStubInitialize(PRPC_MESSAGE pRpcMsg,
128                              PMIDL_STUB_MESSAGE pStubMsg,
129                              PMIDL_STUB_DESC pStubDescriptor,
130                              LPRPCCHANNELBUFFER pRpcChannelBuffer)
131 {
132   TRACE("(%p,%p,%p,%p)\n", pRpcMsg, pStubMsg, pStubDescriptor, pRpcChannelBuffer);
133   NdrServerInitializeNew(pRpcMsg, pStubMsg, pStubDescriptor);
134   pStubMsg->pRpcChannelBuffer = pRpcChannelBuffer;
135 }
136
137 /***********************************************************************
138  *           NdrStubGetBuffer [RPCRT4.@]
139  */
140 void WINAPI NdrStubGetBuffer(LPRPCSTUBBUFFER This,
141                             LPRPCCHANNELBUFFER pRpcChannelBuffer,
142                             PMIDL_STUB_MESSAGE pStubMsg)
143 {
144   TRACE("(%p,%p)\n", This, pStubMsg);
145   pStubMsg->pRpcChannelBuffer = pRpcChannelBuffer;
146   pStubMsg->RpcMsg->BufferLength = pStubMsg->BufferLength;
147   I_RpcGetBuffer(pStubMsg->RpcMsg); /* ? */
148   pStubMsg->BufferStart = pStubMsg->RpcMsg->Buffer;
149   pStubMsg->BufferEnd = pStubMsg->BufferStart + pStubMsg->BufferLength;
150   pStubMsg->Buffer = pStubMsg->BufferStart;
151 }
152
153 /************************************************************************
154  *             NdrClientInitializeNew [RPCRT4.@]
155  */
156 void WINAPI NdrClientInitializeNew( PRPC_MESSAGE pRpcMessage, PMIDL_STUB_MESSAGE pStubMsg, 
157                                     PMIDL_STUB_DESC pStubDesc, unsigned int ProcNum )
158 {
159   TRACE("(pRpcMessage == ^%p, pStubMsg == ^%p, pStubDesc == ^%p, ProcNum == %d)\n",
160     pRpcMessage, pStubMsg, pStubDesc, ProcNum);
161
162   assert( pRpcMessage && pStubMsg && pStubDesc );
163
164   memset(pRpcMessage, 0, sizeof(RPC_MESSAGE));
165   memset(pStubMsg, 0, sizeof(MIDL_STUB_MESSAGE));
166
167   pStubMsg->ReuseBuffer = FALSE;
168   pStubMsg->IsClient = TRUE;
169   pStubMsg->StubDesc = pStubDesc;
170   pStubMsg->pfnAllocate = pStubDesc->pfnAllocate;
171   pStubMsg->pfnFree = pStubDesc->pfnFree;
172   pStubMsg->RpcMsg = pRpcMessage;
173
174   pRpcMessage->ProcNum = ProcNum;
175   pRpcMessage->RpcInterfaceInformation = pStubDesc->RpcInterfaceInformation;
176 }
177
178 /***********************************************************************
179  *             NdrServerInitializeNew [RPCRT4.@]
180  */
181 unsigned char* WINAPI NdrServerInitializeNew( PRPC_MESSAGE pRpcMsg, PMIDL_STUB_MESSAGE pStubMsg,
182                                               PMIDL_STUB_DESC pStubDesc )
183 {
184   TRACE("(pRpcMsg == ^%p, pStubMsg == ^%p, pStubDesc == ^%p)\n", pRpcMsg, pStubMsg, pStubDesc);
185
186   assert( pRpcMsg && pStubMsg && pStubDesc );
187
188   memset(pStubMsg, 0, sizeof(MIDL_STUB_MESSAGE));
189
190   pStubMsg->ReuseBuffer = TRUE;
191   pStubMsg->IsClient = FALSE;
192   pStubMsg->StubDesc = pStubDesc;
193   pStubMsg->pfnAllocate = pStubDesc->pfnAllocate;
194   pStubMsg->pfnFree = pStubDesc->pfnFree;
195   pStubMsg->RpcMsg = pRpcMsg;
196   pStubMsg->Buffer = pStubMsg->BufferStart = pRpcMsg->Buffer;
197   pStubMsg->BufferLength = pRpcMsg->BufferLength;
198   pStubMsg->BufferEnd = pStubMsg->Buffer + pStubMsg->BufferLength;
199
200   /* FIXME: determine the proper return value */
201   return NULL;
202 }
203
204 /***********************************************************************
205  *           NdrGetBuffer [RPCRT4.@]
206  */
207 unsigned char *WINAPI NdrGetBuffer(MIDL_STUB_MESSAGE *stubmsg, unsigned long buflen, RPC_BINDING_HANDLE handle)
208 {
209   TRACE("(stubmsg == ^%p, buflen == %lu, handle == %p): wild guess.\n", stubmsg, buflen, handle);
210   
211   assert( stubmsg && stubmsg->RpcMsg );
212
213   /* I guess this is our chance to put the binding handle into the RPC_MESSAGE */
214   stubmsg->RpcMsg->Handle = handle;
215   
216   stubmsg->RpcMsg->BufferLength = buflen;
217   if (I_RpcGetBuffer(stubmsg->RpcMsg) != S_OK)
218     return NULL;
219
220   stubmsg->Buffer = stubmsg->BufferStart = stubmsg->RpcMsg->Buffer;
221   stubmsg->BufferLength = stubmsg->RpcMsg->BufferLength;
222   stubmsg->BufferEnd = stubmsg->Buffer + stubmsg->BufferLength;
223   return (stubmsg->Buffer = (unsigned char *)stubmsg->RpcMsg->Buffer);
224 }
225 /***********************************************************************
226  *           NdrFreeBuffer [RPCRT4.@]
227  */
228 void WINAPI NdrFreeBuffer(MIDL_STUB_MESSAGE *pStubMsg)
229 {
230   TRACE("(pStubMsg == ^%p): wild guess.\n", pStubMsg);
231   I_RpcFreeBuffer(pStubMsg->RpcMsg);
232   pStubMsg->BufferLength = 0;
233   pStubMsg->Buffer = pStubMsg->BufferEnd = (unsigned char *)(pStubMsg->RpcMsg->Buffer = NULL);
234 }
235
236 /************************************************************************
237  *           NdrSendReceive [RPCRT4.@]
238  */
239 unsigned char *WINAPI NdrSendReceive( MIDL_STUB_MESSAGE *stubmsg, unsigned char *buffer  )
240 {
241   TRACE("(stubmsg == ^%p, buffer == ^%p)\n", stubmsg, buffer);
242
243   /* FIXME: how to handle errors? (raise exception?) */
244   if (!stubmsg) {
245     ERR("NULL stub message.  No action taken.\n");
246     return NULL;
247   }
248   if (!stubmsg->RpcMsg) {
249     ERR("RPC Message not present in stub message.  No action taken.\n");
250     return NULL;
251   }
252   if (stubmsg->RpcMsg->Buffer != buffer) {
253     ERR("Ambiguous buffer doesn't match rpc message buffer.  No action taken.\n");
254     return NULL;
255   }
256
257   /* not sure where MS does this; for now I'll stick it here */
258   stubmsg->RpcMsg->DataRepresentation = NDR_LOCAL_DATA_REPRESENTATION;
259
260   if (I_RpcSendReceive(stubmsg->RpcMsg) != RPC_S_OK) {
261     WARN("I_RpcSendReceive did not return success.\n");
262   }
263
264   /* FIXME: is this the right return value? */
265   return NULL;
266 }