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 IRpcProxyBufferVtbl
*lpVtbl
;
47 const MIDL_STUBLESS_PROXY_INFO
*stubless
;
51 LPPSFACTORYBUFFER pPSFactory
;
52 LPRPCCHANNELBUFFER pChannel
;
53 struct StublessThunk
*thunks
;
56 static IRpcProxyBufferVtbl StdProxy_Vtbl
;
58 #define ICOM_THIS_MULTI(impl,field,iface) impl* const This=(impl*)((char*)(iface) - offsetof(impl,field))
60 /* How the Windows stubless proxy thunks work is explained at
61 * http://msdn.microsoft.com/library/en-us/dnmsj99/html/com0199.asp,
62 * but I'll use a slightly different method, to make life easier */
68 struct StublessThunk
{
80 /* adjust the stack size since we don't use Windows's method */
81 #define STACK_ADJUST sizeof(DWORD)
83 #define FILL_STUBLESS(x,idx,stk) \
84 x->push = 0x68; /* pushl [immediate] */ \
86 x->call = 0xe8; /* call [near] */ \
87 x->handler = (char*)ObjectStubless - (char*)&x->ret; \
88 x->ret = 0xc2; /* ret [immediate] */ \
90 x->pad[0] = 0x8d; /* leal (%esi),%esi */ \
94 static HRESULT WINAPI
ObjectStubless(DWORD index
)
96 char *args
= (char*)(&index
+ 2);
97 LPVOID iface
= *(LPVOID
*)args
;
99 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
101 PFORMAT_STRING fs
= This
->stubless
->ProcFormatString
+ This
->stubless
->FormatStringOffset
[index
];
102 unsigned bytes
= *(WORD
*)(fs
+8) - STACK_ADJUST
;
103 TRACE("(%p)->(%ld)([%d bytes]) ret=%08lx\n", iface
, index
, bytes
, *(DWORD
*)(args
+bytes
));
105 return RPCRT4_NdrClientCall2(This
->stubless
->pStubDesc
, fs
, args
);
110 /* can't do that on this arch */
111 struct StublessThunk
{ int dummy
; };
112 #define FILL_STUBLESS(x,idx,stk) \
113 ERR("stubless proxies are not supported on this architecture\n");
114 #define STACK_ADJUST 0
116 #endif /* __i386__ */
118 HRESULT WINAPI
StdProxy_Construct(REFIID riid
,
120 PCInterfaceName name
,
121 CInterfaceProxyVtbl
*vtbl
,
122 CInterfaceStubVtbl
*svtbl
,
123 LPPSFACTORYBUFFER pPSFactory
,
124 LPRPCPROXYBUFFER
*ppProxy
,
128 const MIDL_STUBLESS_PROXY_INFO
*stubless
= NULL
;
130 TRACE("(%p,%p,%p,%p,%p) %s\n", pUnkOuter
, vtbl
, pPSFactory
, ppProxy
, ppvObj
, name
);
132 /* I can't find any other way to detect stubless proxies than this hack */
133 if (!IsEqualGUID(vtbl
->header
.piid
, riid
)) {
134 stubless
= *(const void **)vtbl
;
135 vtbl
= (CInterfaceProxyVtbl
*)((const void **)vtbl
+ 1);
136 TRACE("stubless=%p\n", stubless
);
139 TRACE("iid=%s\n", debugstr_guid(vtbl
->header
.piid
));
140 TRACE("vtbl=%p\n", vtbl
->Vtbl
);
142 if (!IsEqualGUID(vtbl
->header
.piid
, riid
)) {
143 ERR("IID mismatch during proxy creation\n");
144 return RPC_E_UNEXPECTED
;
147 This
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(StdProxyImpl
));
148 if (!This
) return E_OUTOFMEMORY
;
151 unsigned i
, count
= svtbl
->header
.DispatchTableCount
;
152 /* Maybe the original vtbl is just modified directly to point at
153 * ObjectStublessClientXXX thunks in real Windows, but I don't like it
155 TRACE("stubless thunks: count=%d\n", count
);
156 This
->thunks
= HeapAlloc(GetProcessHeap(),0,sizeof(struct StublessThunk
)*count
);
157 This
->PVtbl
= HeapAlloc(GetProcessHeap(),0,sizeof(LPVOID
)*count
);
158 for (i
=0; i
<count
; i
++) {
159 struct StublessThunk
*thunk
= &This
->thunks
[i
];
160 if (vtbl
->Vtbl
[i
] == (LPVOID
)-1) {
161 PFORMAT_STRING fs
= stubless
->ProcFormatString
+ stubless
->FormatStringOffset
[i
];
162 unsigned bytes
= *(WORD
*)(fs
+8) - STACK_ADJUST
;
163 TRACE("method %d: stacksize=%d\n", i
, bytes
);
164 FILL_STUBLESS(thunk
, i
, bytes
)
165 This
->PVtbl
[i
] = thunk
;
168 memset(thunk
, 0, sizeof(struct StublessThunk
));
169 This
->PVtbl
[i
] = vtbl
->Vtbl
[i
];
174 This
->PVtbl
= vtbl
->Vtbl
;
176 This
->lpVtbl
= &StdProxy_Vtbl
;
177 /* 1 reference for the proxy and 1 for the object */
179 This
->stubless
= stubless
;
180 This
->piid
= vtbl
->header
.piid
;
181 This
->pUnkOuter
= pUnkOuter
;
183 This
->pPSFactory
= pPSFactory
;
184 This
->pChannel
= NULL
;
185 *ppProxy
= (LPRPCPROXYBUFFER
)&This
->lpVtbl
;
186 *ppvObj
= &This
->PVtbl
;
187 IPSFactoryBuffer_AddRef(pPSFactory
);
192 static void WINAPI
StdProxy_Destruct(LPRPCPROXYBUFFER iface
)
194 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
197 IRpcProxyBuffer_Disconnect(iface
);
199 IPSFactoryBuffer_Release(This
->pPSFactory
);
201 HeapFree(GetProcessHeap(),0,This
->PVtbl
);
202 HeapFree(GetProcessHeap(),0,This
->thunks
);
204 HeapFree(GetProcessHeap(),0,This
);
207 static HRESULT WINAPI
StdProxy_QueryInterface(LPRPCPROXYBUFFER iface
,
211 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
212 TRACE("(%p)->QueryInterface(%s,%p)\n",This
,debugstr_guid(riid
),obj
);
214 if (IsEqualGUID(&IID_IUnknown
,riid
) ||
215 IsEqualGUID(This
->piid
,riid
)) {
221 if (IsEqualGUID(&IID_IRpcProxyBuffer
,riid
)) {
222 *obj
= &This
->lpVtbl
;
227 return E_NOINTERFACE
;
230 static ULONG WINAPI
StdProxy_AddRef(LPRPCPROXYBUFFER iface
)
232 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
233 TRACE("(%p)->AddRef()\n",This
);
235 return ++(This
->RefCount
);
238 static ULONG WINAPI
StdProxy_Release(LPRPCPROXYBUFFER iface
)
240 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
241 TRACE("(%p)->Release()\n",This
);
243 if (!--(This
->RefCount
)) {
244 StdProxy_Destruct((LPRPCPROXYBUFFER
)&This
->lpVtbl
);
247 return This
->RefCount
;
250 static HRESULT WINAPI
StdProxy_Connect(LPRPCPROXYBUFFER iface
,
251 LPRPCCHANNELBUFFER pChannel
)
253 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
254 TRACE("(%p)->Connect(%p)\n",This
,pChannel
);
256 This
->pChannel
= pChannel
;
257 IRpcChannelBuffer_AddRef(pChannel
);
261 static VOID WINAPI
StdProxy_Disconnect(LPRPCPROXYBUFFER iface
)
263 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
264 TRACE("(%p)->Disconnect()\n",This
);
266 IRpcChannelBuffer_Release(This
->pChannel
);
267 This
->pChannel
= NULL
;
270 static IRpcProxyBufferVtbl StdProxy_Vtbl
=
272 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
273 StdProxy_QueryInterface
,
280 HRESULT WINAPI
StdProxy_GetChannel(LPVOID iface
,
281 LPRPCCHANNELBUFFER
*ppChannel
)
283 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
284 TRACE("(%p)->GetChannel(%p) %s\n",This
,ppChannel
,This
->name
);
286 *ppChannel
= This
->pChannel
;
290 HRESULT WINAPI
StdProxy_GetIID(LPVOID iface
,
293 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
294 TRACE("(%p)->GetIID(%p) %s\n",This
,ppiid
,This
->name
);
300 HRESULT WINAPI
IUnknown_QueryInterface_Proxy(LPUNKNOWN iface
,
304 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
305 TRACE("(%p)->QueryInterface(%s,%p) %s\n",This
,debugstr_guid(riid
),ppvObj
,This
->name
);
306 return IUnknown_QueryInterface(This
->pUnkOuter
,riid
,ppvObj
);
309 ULONG WINAPI
IUnknown_AddRef_Proxy(LPUNKNOWN iface
)
311 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
312 TRACE("(%p)->AddRef() %s\n",This
,This
->name
);
313 #if 0 /* interface refcounting */
314 return ++(This
->RefCount
);
315 #else /* object refcounting */
316 return IUnknown_AddRef(This
->pUnkOuter
);
320 ULONG WINAPI
IUnknown_Release_Proxy(LPUNKNOWN iface
)
322 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
323 TRACE("(%p)->Release() %s\n",This
,This
->name
);
324 #if 0 /* interface refcounting */
325 if (!--(This
->RefCount
)) {
326 StdProxy_Destruct((LPRPCPROXYBUFFER
)&This
->lpVtbl
);
329 return This
->RefCount
;
330 #else /* object refcounting */
331 return IUnknown_Release(This
->pUnkOuter
);