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.
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(ole);
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 */
46 const IRpcProxyBufferVtbl *lpVtbl;
49 const MIDL_STUBLESS_PROXY_INFO *stubless;
53 LPPSFACTORYBUFFER pPSFactory;
54 LPRPCCHANNELBUFFER pChannel;
55 struct StublessThunk *thunks;
58 static const IRpcProxyBufferVtbl StdProxy_Vtbl;
60 #define ICOM_THIS_MULTI(impl,field,iface) impl* const This=(impl*)((char*)(iface) - offsetof(impl,field))
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 */
70 struct StublessThunk {
82 /* adjust the stack size since we don't use Windows's method */
83 #define STACK_ADJUST sizeof(DWORD)
85 #define FILL_STUBLESS(x,idx,stk) \
86 x->push = 0x68; /* pushl [immediate] */ \
88 x->call = 0xe8; /* call [near] */ \
89 x->handler = (char*)ObjectStubless - (char*)&x->ret; \
90 x->ret = 0xc2; /* ret [immediate] */ \
92 x->pad[0] = 0x8d; /* leal (%esi),%esi */ \
96 static HRESULT WINAPI ObjectStubless(DWORD index)
98 char *args = (char*)(&index + 2);
99 LPVOID iface = *(LPVOID*)args;
101 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
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));
107 return RPCRT4_NdrClientCall2(This->stubless->pStubDesc, fs, args);
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
118 #endif /* __i386__ */
120 HRESULT WINAPI StdProxy_Construct(REFIID riid,
122 PCInterfaceName name,
123 CInterfaceProxyVtbl *vtbl,
124 CInterfaceStubVtbl *svtbl,
125 LPPSFACTORYBUFFER pPSFactory,
126 LPRPCPROXYBUFFER *ppProxy,
130 const MIDL_STUBLESS_PROXY_INFO *stubless = NULL;
132 TRACE("(%p,%p,%p,%p,%p) %s\n", pUnkOuter, vtbl, pPSFactory, ppProxy, ppvObj, name);
134 /* I can't find any other way to detect stubless proxies than this hack */
135 if (!IsEqualGUID(vtbl->header.piid, riid)) {
136 stubless = *(const void **)vtbl;
137 vtbl = (CInterfaceProxyVtbl *)((const void **)vtbl + 1);
138 TRACE("stubless=%p\n", stubless);
141 TRACE("iid=%s\n", debugstr_guid(vtbl->header.piid));
142 TRACE("vtbl=%p\n", vtbl->Vtbl);
144 if (!IsEqualGUID(vtbl->header.piid, riid)) {
145 ERR("IID mismatch during proxy creation\n");
146 return RPC_E_UNEXPECTED;
149 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(StdProxyImpl));
150 if (!This) return E_OUTOFMEMORY;
153 unsigned i, count = svtbl->header.DispatchTableCount;
154 /* Maybe the original vtbl is just modified directly to point at
155 * ObjectStublessClientXXX thunks in real Windows, but I don't like it
157 TRACE("stubless thunks: count=%d\n", count);
158 This->thunks = HeapAlloc(GetProcessHeap(),0,sizeof(struct StublessThunk)*count);
159 This->PVtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPVOID)*count);
160 for (i=0; i<count; i++) {
161 struct StublessThunk *thunk = &This->thunks[i];
162 if (vtbl->Vtbl[i] == (LPVOID)-1) {
163 PFORMAT_STRING fs = stubless->ProcFormatString + stubless->FormatStringOffset[i];
164 unsigned bytes = *(const WORD*)(fs+8) - STACK_ADJUST;
165 TRACE("method %d: stacksize=%d\n", i, bytes);
166 FILL_STUBLESS(thunk, i, bytes)
167 This->PVtbl[i] = thunk;
170 memset(thunk, 0, sizeof(struct StublessThunk));
171 This->PVtbl[i] = vtbl->Vtbl[i];
176 This->PVtbl = vtbl->Vtbl;
178 This->lpVtbl = &StdProxy_Vtbl;
179 /* one reference for the proxy */
181 This->stubless = stubless;
182 This->piid = vtbl->header.piid;
183 This->pUnkOuter = pUnkOuter;
185 This->pPSFactory = pPSFactory;
186 This->pChannel = NULL;
187 *ppProxy = (LPRPCPROXYBUFFER)&This->lpVtbl;
188 *ppvObj = &This->PVtbl;
189 IUnknown_AddRef((IUnknown *)*ppvObj);
190 IPSFactoryBuffer_AddRef(pPSFactory);
195 static void WINAPI StdProxy_Destruct(LPRPCPROXYBUFFER iface)
197 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
200 IRpcProxyBuffer_Disconnect(iface);
202 IPSFactoryBuffer_Release(This->pPSFactory);
204 HeapFree(GetProcessHeap(),0,This->PVtbl);
205 HeapFree(GetProcessHeap(),0,This->thunks);
207 HeapFree(GetProcessHeap(),0,This);
210 static HRESULT WINAPI StdProxy_QueryInterface(LPRPCPROXYBUFFER iface,
214 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
215 TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
217 if (IsEqualGUID(&IID_IUnknown,riid) ||
218 IsEqualGUID(This->piid,riid)) {
224 if (IsEqualGUID(&IID_IRpcProxyBuffer,riid)) {
225 *obj = &This->lpVtbl;
230 return E_NOINTERFACE;
233 static ULONG WINAPI StdProxy_AddRef(LPRPCPROXYBUFFER iface)
235 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
236 TRACE("(%p)->AddRef()\n",This);
238 return ++(This->RefCount);
241 static ULONG WINAPI StdProxy_Release(LPRPCPROXYBUFFER iface)
243 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
244 TRACE("(%p)->Release()\n",This);
246 if (!--(This->RefCount)) {
247 StdProxy_Destruct((LPRPCPROXYBUFFER)&This->lpVtbl);
250 return This->RefCount;
253 static HRESULT WINAPI StdProxy_Connect(LPRPCPROXYBUFFER iface,
254 LPRPCCHANNELBUFFER pChannel)
256 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
257 TRACE("(%p)->Connect(%p)\n",This,pChannel);
259 This->pChannel = pChannel;
260 IRpcChannelBuffer_AddRef(pChannel);
264 static VOID WINAPI StdProxy_Disconnect(LPRPCPROXYBUFFER iface)
266 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
267 TRACE("(%p)->Disconnect()\n",This);
269 IRpcChannelBuffer_Release(This->pChannel);
270 This->pChannel = NULL;
273 static const IRpcProxyBufferVtbl StdProxy_Vtbl =
275 StdProxy_QueryInterface,
282 HRESULT WINAPI StdProxy_GetChannel(LPVOID iface,
283 LPRPCCHANNELBUFFER *ppChannel)
285 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
286 TRACE("(%p)->GetChannel(%p) %s\n",This,ppChannel,This->name);
288 *ppChannel = This->pChannel;
292 HRESULT WINAPI StdProxy_GetIID(LPVOID iface,
295 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
296 TRACE("(%p)->GetIID(%p) %s\n",This,ppiid,This->name);
302 HRESULT WINAPI IUnknown_QueryInterface_Proxy(LPUNKNOWN iface,
306 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
307 TRACE("(%p)->QueryInterface(%s,%p) %s\n",This,debugstr_guid(riid),ppvObj,This->name);
308 return IUnknown_QueryInterface(This->pUnkOuter,riid,ppvObj);
311 ULONG WINAPI IUnknown_AddRef_Proxy(LPUNKNOWN iface)
313 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
314 TRACE("(%p)->AddRef() %s\n",This,This->name);
315 #if 0 /* interface refcounting */
316 return ++(This->RefCount);
317 #else /* object refcounting */
318 return IUnknown_AddRef(This->pUnkOuter);
322 ULONG WINAPI IUnknown_Release_Proxy(LPUNKNOWN iface)
324 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
325 TRACE("(%p)->Release() %s\n",This,This->name);
326 #if 0 /* interface refcounting */
327 if (!--(This->RefCount)) {
328 StdProxy_Destruct((LPRPCPROXYBUFFER)&This->lpVtbl);
331 return This->RefCount;
332 #else /* object refcounting */
333 return IUnknown_Release(This->pUnkOuter);
338 CreateProxyFromTypeInfo( LPTYPEINFO pTypeInfo, LPUNKNOWN pUnkOuter, REFIID riid,
339 LPRPCPROXYBUFFER *ppProxy, LPVOID *ppv )
341 FIXME("%p %p %s %p %p\n", pTypeInfo, pUnkOuter, debugstr_guid(riid), ppProxy, ppv);