2 * COM proxy implementation
4 * Copyright 2001 Ove Kåven, TransGaming Technologies
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.
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.
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
20 * TODO: Handle non-i386 architectures
21 * Get rid of #if 0'ed code.
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(ole);
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 */
44 ICOM_VTABLE(IRpcProxyBuffer) *lpVtbl;
47 const MIDL_STUBLESS_PROXY_INFO *stubless;
51 LPPSFACTORYBUFFER pPSFactory;
52 LPRPCCHANNELBUFFER pChannel;
53 struct StublessThunk *thunks;
56 static ICOM_VTABLE(IRpcProxyBuffer) StdProxy_Vtbl;
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 */
64 struct StublessThunk {
65 BYTE push WINE_PACKED;
66 DWORD index WINE_PACKED;
67 BYTE call WINE_PACKED;
68 LONG handler WINE_PACKED;
70 WORD bytes WINE_PACKED;
74 /* adjust the stack size since we don't use Windows's method */
75 #define STACK_ADJUST sizeof(DWORD)
77 #define FILL_STUBLESS(x,idx,stk) \
78 x->push = 0x68; /* pushl [immediate] */ \
80 x->call = 0xe8; /* call [near] */ \
81 x->handler = (char*)ObjectStubless - (char*)&x->ret; \
82 x->ret = 0xc2; /* ret [immediate] */ \
84 x->pad[0] = 0x8d; /* leal (%esi),%esi */ \
88 static HRESULT WINAPI ObjectStubless(DWORD index)
90 char *args = (char*)(&index + 2);
91 LPVOID iface = *(LPVOID*)args;
93 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
95 PFORMAT_STRING fs = This->stubless->ProcFormatString + This->stubless->FormatStringOffset[index];
96 unsigned bytes = *(WORD*)(fs+8) - STACK_ADJUST;
97 TRACE("(%p)->(%ld)([%d bytes]) ret=%08lx\n", iface, index, bytes, *(DWORD*)(args+bytes));
99 return RPCRT4_NdrClientCall2(This->stubless->pStubDesc, fs, args);
104 /* can't do that on this arch */
105 struct StublessThunk { int dummy; };
106 #define FILL_STUBLESS(x,idx,stk) \
107 ERR("stubless proxies are not supported on this architecture\n");
108 #define STACK_ADJUST 0
110 #endif /* __i386__ */
112 HRESULT WINAPI StdProxy_Construct(REFIID riid,
114 PCInterfaceName name,
115 CInterfaceProxyVtbl *vtbl,
116 CInterfaceStubVtbl *svtbl,
117 LPPSFACTORYBUFFER pPSFactory,
118 LPRPCPROXYBUFFER *ppProxy,
122 const MIDL_STUBLESS_PROXY_INFO *stubless = NULL;
124 TRACE("(%p,%p,%p,%p,%p) %s\n", pUnkOuter, vtbl, pPSFactory, ppProxy, ppvObj, name);
126 /* I can't find any other way to detect stubless proxies than this hack */
127 if (!IsEqualGUID(vtbl->header.piid, riid)) {
128 stubless = *((const void **)vtbl)++;
129 TRACE("stubless=%p\n", stubless);
132 TRACE("iid=%s\n", debugstr_guid(vtbl->header.piid));
133 TRACE("vtbl=%p\n", vtbl->Vtbl);
135 if (!IsEqualGUID(vtbl->header.piid, riid)) {
136 ERR("IID mismatch during proxy creation\n");
137 return RPC_E_UNEXPECTED;
140 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(StdProxyImpl));
141 if (!This) return E_OUTOFMEMORY;
144 unsigned i, count = svtbl->header.DispatchTableCount;
145 /* Maybe the original vtbl is just modified directly to point at
146 * ObjectStublessClientXXX thunks in real Windows, but I don't like it
148 TRACE("stubless thunks: count=%d\n", count);
149 This->thunks = HeapAlloc(GetProcessHeap(),0,sizeof(struct StublessThunk)*count);
150 This->PVtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPVOID)*count);
151 for (i=0; i<count; i++) {
152 struct StublessThunk *thunk = &This->thunks[i];
153 if (vtbl->Vtbl[i] == (LPVOID)-1) {
154 PFORMAT_STRING fs = stubless->ProcFormatString + stubless->FormatStringOffset[i];
155 unsigned bytes = *(WORD*)(fs+8) - STACK_ADJUST;
156 TRACE("method %d: stacksize=%d\n", i, bytes);
157 FILL_STUBLESS(thunk, i, bytes)
158 This->PVtbl[i] = thunk;
161 memset(thunk, 0, sizeof(struct StublessThunk));
162 This->PVtbl[i] = vtbl->Vtbl[i];
167 This->PVtbl = vtbl->Vtbl;
169 This->lpVtbl = &StdProxy_Vtbl;
171 This->stubless = stubless;
172 This->piid = vtbl->header.piid;
173 This->pUnkOuter = pUnkOuter;
175 This->pPSFactory = pPSFactory;
176 This->pChannel = NULL;
177 *ppProxy = (LPRPCPROXYBUFFER)&This->lpVtbl;
178 *ppvObj = &This->PVtbl;
179 IPSFactoryBuffer_AddRef(pPSFactory);
184 static void WINAPI StdProxy_Destruct(LPRPCPROXYBUFFER iface)
186 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
188 IPSFactoryBuffer_Release(This->pPSFactory);
190 HeapFree(GetProcessHeap(),0,This->PVtbl);
191 HeapFree(GetProcessHeap(),0,This->thunks);
193 HeapFree(GetProcessHeap(),0,This);
196 static HRESULT WINAPI StdProxy_QueryInterface(LPRPCPROXYBUFFER iface,
200 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
201 TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
203 if (IsEqualGUID(&IID_IUnknown,riid) ||
204 IsEqualGUID(This->piid,riid)) {
210 if (IsEqualGUID(&IID_IRpcProxyBuffer,riid)) {
211 *obj = &This->lpVtbl;
216 return E_NOINTERFACE;
219 static ULONG WINAPI StdProxy_AddRef(LPRPCPROXYBUFFER iface)
221 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
222 TRACE("(%p)->AddRef()\n",This);
224 return ++(This->RefCount);
227 static ULONG WINAPI StdProxy_Release(LPRPCPROXYBUFFER iface)
229 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
230 TRACE("(%p)->Release()\n",This);
232 if (!--(This->RefCount)) {
233 StdProxy_Destruct((LPRPCPROXYBUFFER)&This->lpVtbl);
236 return This->RefCount;
239 static HRESULT WINAPI StdProxy_Connect(LPRPCPROXYBUFFER iface,
240 LPRPCCHANNELBUFFER pChannel)
242 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
243 TRACE("(%p)->Connect(%p)\n",This,pChannel);
245 This->pChannel = pChannel;
249 static VOID WINAPI StdProxy_Disconnect(LPRPCPROXYBUFFER iface)
251 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
252 TRACE("(%p)->Disconnect()\n",This);
254 This->pChannel = NULL;
257 static ICOM_VTABLE(IRpcProxyBuffer) StdProxy_Vtbl =
259 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
260 StdProxy_QueryInterface,
267 HRESULT WINAPI StdProxy_GetChannel(LPVOID iface,
268 LPRPCCHANNELBUFFER *ppChannel)
270 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
271 TRACE("(%p)->GetChannel(%p) %s\n",This,ppChannel,This->name);
273 *ppChannel = This->pChannel;
277 HRESULT WINAPI StdProxy_GetIID(LPVOID iface,
280 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
281 TRACE("(%p)->GetIID(%p) %s\n",This,ppiid,This->name);
287 HRESULT WINAPI IUnknown_QueryInterface_Proxy(LPUNKNOWN iface,
291 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
292 TRACE("(%p)->QueryInterface(%s,%p) %s\n",This,debugstr_guid(riid),ppvObj,This->name);
293 return IUnknown_QueryInterface(This->pUnkOuter,riid,ppvObj);
296 ULONG WINAPI IUnknown_AddRef_Proxy(LPUNKNOWN iface)
298 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
299 TRACE("(%p)->AddRef() %s\n",This,This->name);
300 #if 0 /* interface refcounting */
301 return ++(This->RefCount);
302 #else /* object refcounting */
303 return IUnknown_AddRef(This->pUnkOuter);
307 ULONG WINAPI IUnknown_Release_Proxy(LPUNKNOWN iface)
309 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
310 TRACE("(%p)->Release() %s\n",This,This->name);
311 #if 0 /* interface refcounting */
312 if (!--(This->RefCount)) {
313 StdProxy_Destruct((LPRPCPROXYBUFFER)&This->lpVtbl);
316 return This->RefCount;
317 #else /* object refcounting */
318 return IUnknown_Release(This->pUnkOuter);