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