rpcrt4: Fix the MSVC version of call_server_func.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  * 
20  * TODO: Handle non-i386 architectures
21  *       Get rid of #if 0'ed code.
22  */
23
24 #include <stdarg.h>
25
26 #define COBJMACROS
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winerror.h"
31
32 #include "objbase.h"
33 #include "rpcproxy.h"
34
35 #include "cpsf.h"
36 #include "ndr_misc.h"
37 #include "wine/debug.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(ole);
40
41 struct StublessThunk;
42
43 /* I don't know what MS's std proxy structure looks like,
44    so this probably doesn't match, but that shouldn't matter */
45 typedef struct {
46   const IRpcProxyBufferVtbl *lpVtbl;
47   LPVOID *PVtbl;
48   DWORD RefCount;
49   const MIDL_STUBLESS_PROXY_INFO *stubless;
50   const IID* piid;
51   LPUNKNOWN pUnkOuter;
52   PCInterfaceName name;
53   LPPSFACTORYBUFFER pPSFactory;
54   LPRPCCHANNELBUFFER pChannel;
55   struct StublessThunk *thunks;
56 } StdProxyImpl;
57
58 static const IRpcProxyBufferVtbl StdProxy_Vtbl;
59
60 #define ICOM_THIS_MULTI(impl,field,iface) impl* const This=(impl*)((char*)(iface) - offsetof(impl,field))
61
62 /* How the Windows stubless proxy thunks work is explained at
63  * http://msdn.microsoft.com/library/en-us/dnmsj99/html/com0199.asp,
64  * but I'll use a slightly different method, to make life easier */
65
66 #if defined(__i386__)
67
68 #include "pshpack1.h"
69
70 struct StublessThunk {
71   BYTE push;
72   DWORD index;
73   BYTE call;
74   LONG handler;
75   BYTE ret;
76   WORD bytes;
77   BYTE pad[3];
78 };
79
80 #include "poppack.h"
81
82 /* adjust the stack size since we don't use Windows's method */
83 #define STACK_ADJUST sizeof(DWORD)
84
85 #define FILL_STUBLESS(x,idx,stk) \
86  x->push = 0x68; /* pushl [immediate] */ \
87  x->index = (idx); \
88  x->call = 0xe8; /* call [near] */ \
89  x->handler = (char*)ObjectStubless - (char*)&x->ret; \
90  x->ret = 0xc2; /* ret [immediate] */ \
91  x->bytes = stk; \
92  x->pad[0] = 0x8d; /* leal (%esi),%esi */ \
93  x->pad[1] = 0x76; \
94  x->pad[2] = 0x00;
95
96 static HRESULT WINAPI ObjectStubless(DWORD index)
97 {
98   char *args = (char*)(&index + 2);
99   LPVOID iface = *(LPVOID*)args;
100
101   ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
102
103   PFORMAT_STRING fs = This->stubless->ProcFormatString + This->stubless->FormatStringOffset[index];
104   unsigned bytes = *(const WORD*)(fs+8) - STACK_ADJUST;
105   TRACE("(%p)->(%ld)([%d bytes]) ret=%08lx\n", iface, index, bytes, *(DWORD*)(args+bytes));
106
107   return NdrClientCall2(This->stubless->pStubDesc, fs, args);
108 }
109
110 #else  /* __i386__ */
111
112 /* can't do that on this arch */
113 struct StublessThunk { int dummy; };
114 #define FILL_STUBLESS(x,idx,stk) \
115  ERR("stubless proxies are not supported on this architecture\n");
116 #define STACK_ADJUST 0
117
118 #endif  /* __i386__ */
119
120 HRESULT WINAPI StdProxy_Construct(REFIID riid,
121                                  LPUNKNOWN pUnkOuter,
122                                  const ProxyFileInfo *ProxyInfo,
123                                  int Index,
124                                  LPPSFACTORYBUFFER pPSFactory,
125                                  LPRPCPROXYBUFFER *ppProxy,
126                                  LPVOID *ppvObj)
127 {
128   StdProxyImpl *This;
129   const MIDL_STUBLESS_PROXY_INFO *stubless = NULL;
130   PCInterfaceName name = ProxyInfo->pNamesArray[Index];
131   CInterfaceProxyVtbl *vtbl = ProxyInfo->pProxyVtblList[Index];
132
133   TRACE("(%p,%p,%p,%p,%p) %s\n", pUnkOuter, vtbl, pPSFactory, ppProxy, ppvObj, name);
134
135   /* TableVersion = 2 means it is the stubless version of CInterfaceProxyVtbl */
136   if (ProxyInfo->TableVersion > 1) {
137     stubless = *(const void **)vtbl;
138     vtbl = (CInterfaceProxyVtbl *)((const void **)vtbl + 1);
139     TRACE("stubless=%p\n", stubless);
140   }
141
142   TRACE("iid=%s\n", debugstr_guid(vtbl->header.piid));
143   TRACE("vtbl=%p\n", vtbl->Vtbl);
144
145   if (!IsEqualGUID(vtbl->header.piid, riid)) {
146     ERR("IID mismatch during proxy creation\n");
147     return RPC_E_UNEXPECTED;
148   }
149
150   This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(StdProxyImpl));
151   if (!This) return E_OUTOFMEMORY;
152
153   if (stubless) {
154     CInterfaceStubVtbl *svtbl = ProxyInfo->pStubVtblList[Index];
155     unsigned long i, count = svtbl->header.DispatchTableCount;
156     /* Maybe the original vtbl is just modified directly to point at
157      * ObjectStublessClientXXX thunks in real Windows, but I don't like it
158      */
159     TRACE("stubless thunks: count=%ld\n", count);
160     This->thunks = HeapAlloc(GetProcessHeap(),0,sizeof(struct StublessThunk)*count);
161     This->PVtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPVOID)*count);
162     for (i=0; i<count; i++) {
163       struct StublessThunk *thunk = &This->thunks[i];
164       if (vtbl->Vtbl[i] == (LPVOID)-1) {
165         PFORMAT_STRING fs = stubless->ProcFormatString + stubless->FormatStringOffset[i];
166         unsigned bytes = *(const WORD*)(fs+8) - STACK_ADJUST;
167         TRACE("method %ld: stacksize=%d\n", i, bytes);
168         FILL_STUBLESS(thunk, i, bytes)
169         This->PVtbl[i] = thunk;
170       }
171       else {
172         memset(thunk, 0, sizeof(struct StublessThunk));
173         This->PVtbl[i] = vtbl->Vtbl[i];
174       }
175     }
176   }
177   else 
178     This->PVtbl = vtbl->Vtbl;
179
180   This->lpVtbl = &StdProxy_Vtbl;
181   /* one reference for the proxy */
182   This->RefCount = 1;
183   This->stubless = stubless;
184   This->piid = vtbl->header.piid;
185   This->pUnkOuter = pUnkOuter;
186   This->name = name;
187   This->pPSFactory = pPSFactory;
188   This->pChannel = NULL;
189   *ppProxy = (LPRPCPROXYBUFFER)&This->lpVtbl;
190   *ppvObj = &This->PVtbl;
191   /* if there is no outer unknown then the caller will control the lifetime
192    * of the proxy object through the proxy buffer, so no need to increment the
193    * ref count of the proxy object */
194   if (pUnkOuter)
195     IUnknown_AddRef((IUnknown *)*ppvObj);
196   IPSFactoryBuffer_AddRef(pPSFactory);
197
198   return S_OK;
199 }
200
201 static void WINAPI StdProxy_Destruct(LPRPCPROXYBUFFER iface)
202 {
203   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
204
205   if (This->pChannel)
206     IRpcProxyBuffer_Disconnect(iface);
207
208   IPSFactoryBuffer_Release(This->pPSFactory);
209   if (This->thunks) {
210     HeapFree(GetProcessHeap(),0,This->PVtbl);
211     HeapFree(GetProcessHeap(),0,This->thunks);
212   }
213   HeapFree(GetProcessHeap(),0,This);
214 }
215
216 static HRESULT WINAPI StdProxy_QueryInterface(LPRPCPROXYBUFFER iface,
217                                              REFIID riid,
218                                              LPVOID *obj)
219 {
220   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
221   TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
222
223   if (IsEqualGUID(&IID_IUnknown,riid) ||
224       IsEqualGUID(This->piid,riid)) {
225     *obj = &This->PVtbl;
226     This->RefCount++;
227     return S_OK;
228   }
229
230   if (IsEqualGUID(&IID_IRpcProxyBuffer,riid)) {
231     *obj = &This->lpVtbl;
232     This->RefCount++;
233     return S_OK;
234   }
235
236   return E_NOINTERFACE;
237 }
238
239 static ULONG WINAPI StdProxy_AddRef(LPRPCPROXYBUFFER iface)
240 {
241   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
242   TRACE("(%p)->AddRef()\n",This);
243
244   return ++(This->RefCount);
245 }
246
247 static ULONG WINAPI StdProxy_Release(LPRPCPROXYBUFFER iface)
248 {
249   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
250   TRACE("(%p)->Release()\n",This);
251
252   if (!--(This->RefCount)) {
253     StdProxy_Destruct((LPRPCPROXYBUFFER)&This->lpVtbl);
254     return 0;
255   }
256   return This->RefCount;
257 }
258
259 static HRESULT WINAPI StdProxy_Connect(LPRPCPROXYBUFFER iface,
260                                       LPRPCCHANNELBUFFER pChannel)
261 {
262   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
263   TRACE("(%p)->Connect(%p)\n",This,pChannel);
264
265   This->pChannel = pChannel;
266   IRpcChannelBuffer_AddRef(pChannel);
267   return S_OK;
268 }
269
270 static VOID WINAPI StdProxy_Disconnect(LPRPCPROXYBUFFER iface)
271 {
272   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
273   TRACE("(%p)->Disconnect()\n",This);
274
275   IRpcChannelBuffer_Release(This->pChannel);
276   This->pChannel = NULL;
277 }
278
279 static const IRpcProxyBufferVtbl StdProxy_Vtbl =
280 {
281   StdProxy_QueryInterface,
282   StdProxy_AddRef,
283   StdProxy_Release,
284   StdProxy_Connect,
285   StdProxy_Disconnect
286 };
287
288 HRESULT WINAPI StdProxy_GetChannel(LPVOID iface,
289                                   LPRPCCHANNELBUFFER *ppChannel)
290 {
291   ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
292   TRACE("(%p)->GetChannel(%p) %s\n",This,ppChannel,This->name);
293
294   *ppChannel = This->pChannel;
295   return S_OK;
296 }
297
298 HRESULT WINAPI StdProxy_GetIID(LPVOID iface,
299                               const IID **ppiid)
300 {
301   ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
302   TRACE("(%p)->GetIID(%p) %s\n",This,ppiid,This->name);
303
304   *ppiid = This->piid;
305   return S_OK;
306 }
307
308 HRESULT WINAPI IUnknown_QueryInterface_Proxy(LPUNKNOWN iface,
309                                             REFIID riid,
310                                             LPVOID *ppvObj)
311 {
312   ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
313   TRACE("(%p)->QueryInterface(%s,%p) %s\n",This,debugstr_guid(riid),ppvObj,This->name);
314   return IUnknown_QueryInterface(This->pUnkOuter,riid,ppvObj);
315 }
316
317 ULONG WINAPI IUnknown_AddRef_Proxy(LPUNKNOWN iface)
318 {
319   ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
320   TRACE("(%p)->AddRef() %s\n",This,This->name);
321 #if 0 /* interface refcounting */
322   return ++(This->RefCount);
323 #else /* object refcounting */
324   return IUnknown_AddRef(This->pUnkOuter);
325 #endif
326 }
327
328 ULONG WINAPI IUnknown_Release_Proxy(LPUNKNOWN iface)
329 {
330   ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
331   TRACE("(%p)->Release() %s\n",This,This->name);
332 #if 0 /* interface refcounting */
333   if (!--(This->RefCount)) {
334     StdProxy_Destruct((LPRPCPROXYBUFFER)&This->lpVtbl);
335     return 0;
336   }
337   return This->RefCount;
338 #else /* object refcounting */
339   return IUnknown_Release(This->pUnkOuter);
340 #endif
341 }
342
343 HRESULT WINAPI
344 CreateProxyFromTypeInfo( LPTYPEINFO pTypeInfo, LPUNKNOWN pUnkOuter, REFIID riid,
345                          LPRPCPROXYBUFFER *ppProxy, LPVOID *ppv )
346 {
347     FIXME("%p %p %s %p %p\n", pTypeInfo, pUnkOuter, debugstr_guid(riid), ppProxy, ppv);
348     return E_NOTIMPL;
349 }