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 */
66 struct StublessThunk
{
78 /* adjust the stack size since we don't use Windows's method */
79 #define STACK_ADJUST sizeof(DWORD)
81 #define FILL_STUBLESS(x,idx,stk) \
82 x->push = 0x68; /* pushl [immediate] */ \
84 x->call = 0xe8; /* call [near] */ \
85 x->handler = (char*)ObjectStubless - (char*)&x->ret; \
86 x->ret = 0xc2; /* ret [immediate] */ \
88 x->pad[0] = 0x8d; /* leal (%esi),%esi */ \
92 static HRESULT WINAPI
ObjectStubless(DWORD index
)
94 char *args
= (char*)(&index
+ 2);
95 LPVOID iface
= *(LPVOID
*)args
;
97 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
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
));
103 return RPCRT4_NdrClientCall2(This
->stubless
->pStubDesc
, fs
, args
);
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
114 #endif /* __i386__ */
116 HRESULT WINAPI
StdProxy_Construct(REFIID riid
,
118 PCInterfaceName name
,
119 CInterfaceProxyVtbl
*vtbl
,
120 CInterfaceStubVtbl
*svtbl
,
121 LPPSFACTORYBUFFER pPSFactory
,
122 LPRPCPROXYBUFFER
*ppProxy
,
126 const MIDL_STUBLESS_PROXY_INFO
*stubless
= NULL
;
128 TRACE("(%p,%p,%p,%p,%p) %s\n", pUnkOuter
, vtbl
, pPSFactory
, ppProxy
, ppvObj
, name
);
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
);
137 TRACE("iid=%s\n", debugstr_guid(vtbl
->header
.piid
));
138 TRACE("vtbl=%p\n", vtbl
->Vtbl
);
140 if (!IsEqualGUID(vtbl
->header
.piid
, riid
)) {
141 ERR("IID mismatch during proxy creation\n");
142 return RPC_E_UNEXPECTED
;
145 This
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(StdProxyImpl
));
146 if (!This
) return E_OUTOFMEMORY
;
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
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
;
166 memset(thunk
, 0, sizeof(struct StublessThunk
));
167 This
->PVtbl
[i
] = vtbl
->Vtbl
[i
];
172 This
->PVtbl
= vtbl
->Vtbl
;
174 This
->lpVtbl
= &StdProxy_Vtbl
;
176 This
->stubless
= stubless
;
177 This
->piid
= vtbl
->header
.piid
;
178 This
->pUnkOuter
= pUnkOuter
;
180 This
->pPSFactory
= pPSFactory
;
181 This
->pChannel
= NULL
;
182 *ppProxy
= (LPRPCPROXYBUFFER
)&This
->lpVtbl
;
183 *ppvObj
= &This
->PVtbl
;
184 IPSFactoryBuffer_AddRef(pPSFactory
);
189 static void WINAPI
StdProxy_Destruct(LPRPCPROXYBUFFER iface
)
191 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
193 IPSFactoryBuffer_Release(This
->pPSFactory
);
195 HeapFree(GetProcessHeap(),0,This
->PVtbl
);
196 HeapFree(GetProcessHeap(),0,This
->thunks
);
198 HeapFree(GetProcessHeap(),0,This
);
201 static HRESULT WINAPI
StdProxy_QueryInterface(LPRPCPROXYBUFFER iface
,
205 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
206 TRACE("(%p)->QueryInterface(%s,%p)\n",This
,debugstr_guid(riid
),obj
);
208 if (IsEqualGUID(&IID_IUnknown
,riid
) ||
209 IsEqualGUID(This
->piid
,riid
)) {
215 if (IsEqualGUID(&IID_IRpcProxyBuffer
,riid
)) {
216 *obj
= &This
->lpVtbl
;
221 return E_NOINTERFACE
;
224 static ULONG WINAPI
StdProxy_AddRef(LPRPCPROXYBUFFER iface
)
226 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
227 TRACE("(%p)->AddRef()\n",This
);
229 return ++(This
->RefCount
);
232 static ULONG WINAPI
StdProxy_Release(LPRPCPROXYBUFFER iface
)
234 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
235 TRACE("(%p)->Release()\n",This
);
237 if (!--(This
->RefCount
)) {
238 StdProxy_Destruct((LPRPCPROXYBUFFER
)&This
->lpVtbl
);
241 return This
->RefCount
;
244 static HRESULT WINAPI
StdProxy_Connect(LPRPCPROXYBUFFER iface
,
245 LPRPCCHANNELBUFFER pChannel
)
247 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
248 TRACE("(%p)->Connect(%p)\n",This
,pChannel
);
250 This
->pChannel
= pChannel
;
254 static VOID WINAPI
StdProxy_Disconnect(LPRPCPROXYBUFFER iface
)
256 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
257 TRACE("(%p)->Disconnect()\n",This
);
259 This
->pChannel
= NULL
;
262 static ICOM_VTABLE(IRpcProxyBuffer
) StdProxy_Vtbl
=
264 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
265 StdProxy_QueryInterface
,
272 HRESULT WINAPI
StdProxy_GetChannel(LPVOID iface
,
273 LPRPCCHANNELBUFFER
*ppChannel
)
275 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
276 TRACE("(%p)->GetChannel(%p) %s\n",This
,ppChannel
,This
->name
);
278 *ppChannel
= This
->pChannel
;
282 HRESULT WINAPI
StdProxy_GetIID(LPVOID iface
,
285 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
286 TRACE("(%p)->GetIID(%p) %s\n",This
,ppiid
,This
->name
);
292 HRESULT WINAPI
IUnknown_QueryInterface_Proxy(LPUNKNOWN iface
,
296 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
297 TRACE("(%p)->QueryInterface(%s,%p) %s\n",This
,debugstr_guid(riid
),ppvObj
,This
->name
);
298 return IUnknown_QueryInterface(This
->pUnkOuter
,riid
,ppvObj
);
301 ULONG WINAPI
IUnknown_AddRef_Proxy(LPUNKNOWN iface
)
303 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
304 TRACE("(%p)->AddRef() %s\n",This
,This
->name
);
305 #if 0 /* interface refcounting */
306 return ++(This
->RefCount
);
307 #else /* object refcounting */
308 return IUnknown_AddRef(This
->pUnkOuter
);
312 ULONG WINAPI
IUnknown_Release_Proxy(LPUNKNOWN iface
)
314 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
315 TRACE("(%p)->Release() %s\n",This
,This
->name
);
316 #if 0 /* interface refcounting */
317 if (!--(This
->RefCount
)) {
318 StdProxy_Destruct((LPRPCPROXYBUFFER
)&This
->lpVtbl
);
321 return This
->RefCount
;
322 #else /* object refcounting */
323 return IUnknown_Release(This
->pUnkOuter
);