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