include: Make RpcRaiseException DECLSPEC_NORETURN, like it is in the PSDK.
[wine] / dlls / rpcrt4 / cproxy.c
1 /*
2  * COM proxy implementation
3  *
4  * Copyright 2001 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  * 
20  * TODO: Handle non-i386 architectures
21  */
22
23 #include <stdarg.h>
24
25 #define COBJMACROS
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winerror.h"
30
31 #include "objbase.h"
32 #include "rpcproxy.h"
33
34 #include "cpsf.h"
35 #include "ndr_misc.h"
36 #include "wine/debug.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(ole);
39
40 struct StublessThunk;
41
42 /* I don't know what MS's std proxy structure looks like,
43    so this probably doesn't match, but that shouldn't matter */
44 typedef struct {
45   const IRpcProxyBufferVtbl *lpVtbl;
46   LPVOID *PVtbl;
47   LONG RefCount;
48   const MIDL_STUBLESS_PROXY_INFO *stubless;
49   const IID* piid;
50   LPUNKNOWN pUnkOuter;
51   PCInterfaceName name;
52   LPPSFACTORYBUFFER pPSFactory;
53   LPRPCCHANNELBUFFER pChannel;
54   struct StublessThunk *thunks;
55 } StdProxyImpl;
56
57 static const IRpcProxyBufferVtbl StdProxy_Vtbl;
58
59 #define ICOM_THIS_MULTI(impl,field,iface) impl* const This=(impl*)((char*)(iface) - offsetof(impl,field))
60
61 /* How the Windows stubless proxy thunks work is explained at
62  * http://msdn.microsoft.com/library/en-us/dnmsj99/html/com0199.asp,
63  * but I'll use a slightly different method, to make life easier */
64
65 #if defined(__i386__)
66
67 #include "pshpack1.h"
68
69 struct StublessThunk {
70   BYTE push;
71   DWORD index;
72   BYTE call;
73   LONG handler;
74   BYTE ret;
75   WORD bytes;
76   BYTE pad[3];
77 };
78
79 #include "poppack.h"
80
81 /* adjust the stack size since we don't use Windows's method */
82 #define STACK_ADJUST sizeof(DWORD)
83
84 #define FILL_STUBLESS(x,idx,stk) \
85  x->push = 0x68; /* pushl [immediate] */ \
86  x->index = (idx); \
87  x->call = 0xe8; /* call [near] */ \
88  x->handler = (char*)ObjectStubless - (char*)&x->ret; \
89  x->ret = 0xc2; /* ret [immediate] */ \
90  x->bytes = stk; \
91  x->pad[0] = 0x8d; /* leal (%esi),%esi */ \
92  x->pad[1] = 0x76; \
93  x->pad[2] = 0x00;
94
95 static HRESULT WINAPI ObjectStubless(DWORD index)
96 {
97   char *args = (char*)(&index + 2);
98   LPVOID iface = *(LPVOID*)args;
99
100   ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
101
102   PFORMAT_STRING fs = This->stubless->ProcFormatString + This->stubless->FormatStringOffset[index];
103   unsigned bytes = *(const WORD*)(fs+8) - STACK_ADJUST;
104   TRACE("(%p)->(%d)([%d bytes]) ret=%08x\n", iface, index, bytes, *(DWORD*)(args+bytes));
105
106   return NdrClientCall2(This->stubless->pStubDesc, fs, args);
107 }
108
109 #else  /* __i386__ */
110
111 /* can't do that on this arch */
112 struct StublessThunk { int dummy; };
113 #define FILL_STUBLESS(x,idx,stk) \
114  ERR("stubless proxies are not supported on this architecture\n");
115 #define STACK_ADJUST 0
116
117 #endif  /* __i386__ */
118
119 HRESULT WINAPI StdProxy_Construct(REFIID riid,
120                                  LPUNKNOWN pUnkOuter,
121                                  const ProxyFileInfo *ProxyInfo,
122                                  int Index,
123                                  LPPSFACTORYBUFFER pPSFactory,
124                                  LPRPCPROXYBUFFER *ppProxy,
125                                  LPVOID *ppvObj)
126 {
127   StdProxyImpl *This;
128   const MIDL_STUBLESS_PROXY_INFO *stubless = NULL;
129   PCInterfaceName name = ProxyInfo->pNamesArray[Index];
130   CInterfaceProxyVtbl *vtbl = ProxyInfo->pProxyVtblList[Index];
131
132   TRACE("(%p,%p,%p,%p,%p) %s\n", pUnkOuter, vtbl, pPSFactory, ppProxy, ppvObj, name);
133
134   /* TableVersion = 2 means it is the stubless version of CInterfaceProxyVtbl */
135   if (ProxyInfo->TableVersion > 1) {
136     stubless = *(const void **)vtbl;
137     vtbl = (CInterfaceProxyVtbl *)((const void **)vtbl + 1);
138     TRACE("stubless=%p\n", stubless);
139   }
140
141   TRACE("iid=%s\n", debugstr_guid(vtbl->header.piid));
142   TRACE("vtbl=%p\n", vtbl->Vtbl);
143
144   if (!IsEqualGUID(vtbl->header.piid, riid)) {
145     ERR("IID mismatch during proxy creation\n");
146     return RPC_E_UNEXPECTED;
147   }
148
149   This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(StdProxyImpl));
150   if (!This) return E_OUTOFMEMORY;
151
152   if (stubless) {
153     CInterfaceStubVtbl *svtbl = ProxyInfo->pStubVtblList[Index];
154     unsigned long i, count = svtbl->header.DispatchTableCount;
155     /* Maybe the original vtbl is just modified directly to point at
156      * ObjectStublessClientXXX thunks in real Windows, but I don't like it
157      */
158     TRACE("stubless thunks: count=%ld\n", count);
159     This->thunks = HeapAlloc(GetProcessHeap(),0,sizeof(struct StublessThunk)*count);
160     This->PVtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPVOID)*count);
161     for (i=0; i<count; i++) {
162       struct StublessThunk *thunk = &This->thunks[i];
163       if (vtbl->Vtbl[i] == (LPVOID)-1) {
164         PFORMAT_STRING fs = stubless->ProcFormatString + stubless->FormatStringOffset[i];
165         unsigned bytes = *(const WORD*)(fs+8) - STACK_ADJUST;
166         TRACE("method %ld: stacksize=%d\n", i, bytes);
167         FILL_STUBLESS(thunk, i, bytes)
168         This->PVtbl[i] = thunk;
169       }
170       else {
171         memset(thunk, 0, sizeof(struct StublessThunk));
172         This->PVtbl[i] = vtbl->Vtbl[i];
173       }
174     }
175   }
176   else 
177     This->PVtbl = vtbl->Vtbl;
178
179   This->lpVtbl = &StdProxy_Vtbl;
180   /* one reference for the proxy */
181   This->RefCount = 1;
182   This->stubless = stubless;
183   This->piid = vtbl->header.piid;
184   This->pUnkOuter = pUnkOuter;
185   This->name = name;
186   This->pPSFactory = pPSFactory;
187   This->pChannel = NULL;
188   *ppProxy = (LPRPCPROXYBUFFER)&This->lpVtbl;
189   *ppvObj = &This->PVtbl;
190   /* if there is no outer unknown then the caller will control the lifetime
191    * of the proxy object through the proxy buffer, so no need to increment the
192    * ref count of the proxy object */
193   if (pUnkOuter)
194     IUnknown_AddRef((IUnknown *)*ppvObj);
195   IPSFactoryBuffer_AddRef(pPSFactory);
196
197   return S_OK;
198 }
199
200 static void WINAPI StdProxy_Destruct(LPRPCPROXYBUFFER iface)
201 {
202   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
203
204   if (This->pChannel)
205     IRpcProxyBuffer_Disconnect(iface);
206
207   IPSFactoryBuffer_Release(This->pPSFactory);
208   if (This->thunks) {
209     HeapFree(GetProcessHeap(),0,This->PVtbl);
210     HeapFree(GetProcessHeap(),0,This->thunks);
211   }
212   HeapFree(GetProcessHeap(),0,This);
213 }
214
215 static HRESULT WINAPI StdProxy_QueryInterface(LPRPCPROXYBUFFER iface,
216                                              REFIID riid,
217                                              LPVOID *obj)
218 {
219   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
220   TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
221
222   if (IsEqualGUID(&IID_IUnknown,riid) ||
223       IsEqualGUID(This->piid,riid)) {
224     *obj = &This->PVtbl;
225     InterlockedIncrement(&This->RefCount);
226     return S_OK;
227   }
228
229   if (IsEqualGUID(&IID_IRpcProxyBuffer,riid)) {
230     *obj = &This->lpVtbl;
231     InterlockedIncrement(&This->RefCount);
232     return S_OK;
233   }
234
235   return E_NOINTERFACE;
236 }
237
238 static ULONG WINAPI StdProxy_AddRef(LPRPCPROXYBUFFER iface)
239 {
240   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
241   TRACE("(%p)->AddRef()\n",This);
242
243   return InterlockedIncrement(&This->RefCount);
244 }
245
246 static ULONG WINAPI StdProxy_Release(LPRPCPROXYBUFFER iface)
247 {
248   ULONG refs;
249   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
250   TRACE("(%p)->Release()\n",This);
251
252   refs = InterlockedDecrement(&This->RefCount);
253   if (!refs)
254     StdProxy_Destruct((LPRPCPROXYBUFFER)&This->lpVtbl);
255   return refs;
256 }
257
258 static HRESULT WINAPI StdProxy_Connect(LPRPCPROXYBUFFER iface,
259                                       LPRPCCHANNELBUFFER pChannel)
260 {
261   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
262   TRACE("(%p)->Connect(%p)\n",This,pChannel);
263
264   This->pChannel = pChannel;
265   IRpcChannelBuffer_AddRef(pChannel);
266   return S_OK;
267 }
268
269 static VOID WINAPI StdProxy_Disconnect(LPRPCPROXYBUFFER iface)
270 {
271   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
272   TRACE("(%p)->Disconnect()\n",This);
273
274   IRpcChannelBuffer_Release(This->pChannel);
275   This->pChannel = NULL;
276 }
277
278 static const IRpcProxyBufferVtbl StdProxy_Vtbl =
279 {
280   StdProxy_QueryInterface,
281   StdProxy_AddRef,
282   StdProxy_Release,
283   StdProxy_Connect,
284   StdProxy_Disconnect
285 };
286
287 static void StdProxy_GetChannel(LPVOID iface,
288                                    LPRPCCHANNELBUFFER *ppChannel)
289 {
290   ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
291   TRACE("(%p)->GetChannel(%p) %s\n",This,ppChannel,This->name);
292
293   *ppChannel = This->pChannel;
294 }
295
296 static void StdProxy_GetIID(LPVOID iface,
297                                const IID **ppiid)
298 {
299   ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
300   TRACE("(%p)->GetIID(%p) %s\n",This,ppiid,This->name);
301
302   *ppiid = This->piid;
303 }
304
305 HRESULT WINAPI IUnknown_QueryInterface_Proxy(LPUNKNOWN iface,
306                                             REFIID riid,
307                                             LPVOID *ppvObj)
308 {
309   ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
310   TRACE("(%p)->QueryInterface(%s,%p) %s\n",This,debugstr_guid(riid),ppvObj,This->name);
311   return IUnknown_QueryInterface(This->pUnkOuter,riid,ppvObj);
312 }
313
314 ULONG WINAPI IUnknown_AddRef_Proxy(LPUNKNOWN iface)
315 {
316   ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
317   TRACE("(%p)->AddRef() %s\n",This,This->name);
318   return IUnknown_AddRef(This->pUnkOuter);
319 }
320
321 ULONG WINAPI IUnknown_Release_Proxy(LPUNKNOWN iface)
322 {
323   ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
324   TRACE("(%p)->Release() %s\n",This,This->name);
325   return IUnknown_Release(This->pUnkOuter);
326 }
327
328 /***********************************************************************
329  *           NdrProxyInitialize [RPCRT4.@]
330  */
331 void WINAPI NdrProxyInitialize(void *This,
332                               PRPC_MESSAGE pRpcMsg,
333                               PMIDL_STUB_MESSAGE pStubMsg,
334                               PMIDL_STUB_DESC pStubDescriptor,
335                               unsigned int ProcNum)
336 {
337   TRACE("(%p,%p,%p,%p,%d)\n", This, pRpcMsg, pStubMsg, pStubDescriptor, ProcNum);
338   NdrClientInitializeNew(pRpcMsg, pStubMsg, pStubDescriptor, ProcNum);
339   StdProxy_GetChannel(This, &pStubMsg->pRpcChannelBuffer);
340   IRpcChannelBuffer_GetDestCtx(pStubMsg->pRpcChannelBuffer,
341                                &pStubMsg->dwDestContext,
342                                &pStubMsg->pvDestContext);
343   TRACE("channel=%p\n", pStubMsg->pRpcChannelBuffer);
344 }
345
346 /***********************************************************************
347  *           NdrProxyGetBuffer [RPCRT4.@]
348  */
349 void WINAPI NdrProxyGetBuffer(void *This,
350                              PMIDL_STUB_MESSAGE pStubMsg)
351 {
352   HRESULT hr;
353   const IID *riid = NULL;
354
355   TRACE("(%p,%p)\n", This, pStubMsg);
356   pStubMsg->RpcMsg->BufferLength = pStubMsg->BufferLength;
357   pStubMsg->dwStubPhase = PROXY_GETBUFFER;
358   StdProxy_GetIID(This, &riid);
359   hr = IRpcChannelBuffer_GetBuffer(pStubMsg->pRpcChannelBuffer,
360                                   (RPCOLEMESSAGE*)pStubMsg->RpcMsg,
361                                   riid);
362   if (FAILED(hr))
363   {
364     RpcRaiseException(hr);
365     return;
366   }
367   pStubMsg->BufferStart = pStubMsg->RpcMsg->Buffer;
368   pStubMsg->BufferEnd = pStubMsg->BufferStart + pStubMsg->BufferLength;
369   pStubMsg->Buffer = pStubMsg->BufferStart;
370   pStubMsg->dwStubPhase = PROXY_MARSHAL;
371 }
372
373 /***********************************************************************
374  *           NdrProxySendReceive [RPCRT4.@]
375  */
376 void WINAPI NdrProxySendReceive(void *This,
377                                PMIDL_STUB_MESSAGE pStubMsg)
378 {
379   ULONG Status = 0;
380   HRESULT hr;
381
382   TRACE("(%p,%p)\n", This, pStubMsg);
383
384   if (!pStubMsg->pRpcChannelBuffer)
385   {
386     WARN("Trying to use disconnected proxy %p\n", This);
387     RpcRaiseException(RPC_E_DISCONNECTED);
388   }
389
390   pStubMsg->dwStubPhase = PROXY_SENDRECEIVE;
391   hr = IRpcChannelBuffer_SendReceive(pStubMsg->pRpcChannelBuffer,
392                                     (RPCOLEMESSAGE*)pStubMsg->RpcMsg,
393                                     &Status);
394   pStubMsg->dwStubPhase = PROXY_UNMARSHAL;
395   pStubMsg->BufferLength = pStubMsg->RpcMsg->BufferLength;
396   pStubMsg->BufferStart = pStubMsg->RpcMsg->Buffer;
397   pStubMsg->BufferEnd = pStubMsg->BufferStart + pStubMsg->BufferLength;
398   pStubMsg->Buffer = pStubMsg->BufferStart;
399
400   /* raise exception if call failed */
401   if (hr == RPC_S_CALL_FAILED) RpcRaiseException(*(DWORD*)pStubMsg->Buffer);
402   else if (FAILED(hr)) RpcRaiseException(hr);
403 }
404
405 /***********************************************************************
406  *           NdrProxyFreeBuffer [RPCRT4.@]
407  */
408 void WINAPI NdrProxyFreeBuffer(void *This,
409                               PMIDL_STUB_MESSAGE pStubMsg)
410 {
411   HRESULT hr;
412
413   TRACE("(%p,%p)\n", This, pStubMsg);
414   hr = IRpcChannelBuffer_FreeBuffer(pStubMsg->pRpcChannelBuffer,
415                                    (RPCOLEMESSAGE*)pStubMsg->RpcMsg);
416 }
417
418 /***********************************************************************
419  *           NdrProxyErrorHandler [RPCRT4.@]
420  */
421 HRESULT WINAPI NdrProxyErrorHandler(DWORD dwExceptionCode)
422 {
423   WARN("(0x%08x): a proxy call failed\n", dwExceptionCode);
424
425   if (FAILED(dwExceptionCode))
426     return dwExceptionCode;
427   else
428     return HRESULT_FROM_WIN32(dwExceptionCode);
429 }
430
431 HRESULT WINAPI
432 CreateProxyFromTypeInfo( LPTYPEINFO pTypeInfo, LPUNKNOWN pUnkOuter, REFIID riid,
433                          LPRPCPROXYBUFFER *ppProxy, LPVOID *ppv )
434 {
435     typedef INT (WINAPI *MessageBoxA)(HWND,LPCSTR,LPCSTR,UINT);
436     HMODULE hUser32 = LoadLibraryA("user32");
437     MessageBoxA pMessageBoxA = (void *)GetProcAddress(hUser32, "MessageBoxA");
438
439     FIXME("%p %p %s %p %p\n", pTypeInfo, pUnkOuter, debugstr_guid(riid), ppProxy, ppv);
440     if (pMessageBoxA)
441     {
442         pMessageBoxA(NULL,
443             "The native implementation of OLEAUT32.DLL cannot be used "
444             "with Wine's RPCRT4.DLL. Remove OLEAUT32.DLL and try again.\n",
445             "Wine: Unimplemented CreateProxyFromTypeInfo",
446             0x10);
447         ExitProcess(1);
448     }
449     return E_NOTIMPL;
450 }