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.
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
39 /* I don't know what MS's std proxy structure looks like,
40 so this probably doesn't match, but that shouldn't matter */
42 ICOM_VTABLE(IRpcProxyBuffer
) *lpVtbl
;
45 const MIDL_STUBLESS_PROXY_INFO
*stubless
;
49 LPPSFACTORYBUFFER pPSFactory
;
50 LPRPCCHANNELBUFFER pChannel
;
51 struct StublessThunk
*thunks
;
54 static ICOM_VTABLE(IRpcProxyBuffer
) StdProxy_Vtbl
;
56 /* How the Windows stubless proxy thunks work is explained at
57 * http://msdn.microsoft.com/library/en-us/dnmsj99/html/com0199.asp,
58 * but I'll use a slightly different method, to make life easier */
62 struct StublessThunk
{
63 BYTE push WINE_PACKED
;
64 DWORD index WINE_PACKED
;
65 BYTE call WINE_PACKED
;
66 LONG handler WINE_PACKED
;
68 WORD bytes WINE_PACKED
;
72 /* adjust the stack size since we don't use Windows's method */
73 #define STACK_ADJUST sizeof(DWORD)
75 #define FILL_STUBLESS(x,idx,stk) \
76 x->push = 0x68; /* pushl [immediate] */ \
78 x->call = 0xe8; /* call [near] */ \
79 x->handler = (char*)ObjectStubless - (char*)&x->ret; \
80 x->ret = 0xc2; /* ret [immediate] */ \
82 x->pad[0] = 0x8d; /* leal (%esi),%esi */ \
86 static HRESULT WINAPI
ObjectStubless(DWORD index
)
88 char *args
= (char*)(&index
+ 2);
89 LPVOID iface
= *(LPVOID
*)args
;
91 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
93 PFORMAT_STRING fs
= This
->stubless
->ProcFormatString
+ This
->stubless
->FormatStringOffset
[index
];
94 unsigned bytes
= *(WORD
*)(fs
+8) - STACK_ADJUST
;
95 TRACE("(%p)->(%ld)([%d bytes]) ret=%08lx\n", iface
, index
, bytes
, *(DWORD
*)(args
+bytes
));
97 return RPCRT4_NdrClientCall2(This
->stubless
->pStubDesc
, fs
, args
);
102 /* can't do that on this arch */
103 struct StublessThunk
{ int dummy
; };
104 #define FILL_STUBLESS(x,idx,stk) \
105 ERR("stubless proxies are not supported on this architecture\n");
106 #define STACK_ADJUST 0
108 #endif /* __i386__ */
110 HRESULT WINAPI
StdProxy_Construct(REFIID riid
,
112 PCInterfaceName name
,
113 CInterfaceProxyVtbl
*vtbl
,
114 CInterfaceStubVtbl
*svtbl
,
115 LPPSFACTORYBUFFER pPSFactory
,
116 LPRPCPROXYBUFFER
*ppProxy
,
120 const MIDL_STUBLESS_PROXY_INFO
*stubless
= NULL
;
122 TRACE("(%p,%p,%p,%p,%p) %s\n", pUnkOuter
, vtbl
, pPSFactory
, ppProxy
, ppvObj
, name
);
124 /* I can't find any other way to detect stubless proxies than this hack */
125 if (!IsEqualGUID(vtbl
->header
.piid
, riid
)) {
126 stubless
= *((const void **)vtbl
)++;
127 TRACE("stubless=%p\n", stubless
);
130 TRACE("iid=%s\n", debugstr_guid(vtbl
->header
.piid
));
131 TRACE("vtbl=%p\n", vtbl
->Vtbl
);
133 if (!IsEqualGUID(vtbl
->header
.piid
, riid
)) {
134 ERR("IID mismatch during proxy creation\n");
135 return RPC_E_UNEXPECTED
;
138 This
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(StdProxyImpl
));
139 if (!This
) return E_OUTOFMEMORY
;
142 unsigned i
, count
= svtbl
->header
.DispatchTableCount
;
143 /* Maybe the original vtbl is just modified directly to point at
144 * ObjectStublessClientXXX thunks in real Windows, but I don't like it
146 TRACE("stubless thunks: count=%d\n", count
);
147 This
->thunks
= HeapAlloc(GetProcessHeap(),0,sizeof(struct StublessThunk
)*count
);
148 This
->PVtbl
= HeapAlloc(GetProcessHeap(),0,sizeof(LPVOID
)*count
);
149 for (i
=0; i
<count
; i
++) {
150 struct StublessThunk
*thunk
= &This
->thunks
[i
];
151 if (vtbl
->Vtbl
[i
] == (LPVOID
)-1) {
152 PFORMAT_STRING fs
= stubless
->ProcFormatString
+ stubless
->FormatStringOffset
[i
];
153 unsigned bytes
= *(WORD
*)(fs
+8) - STACK_ADJUST
;
154 TRACE("method %d: stacksize=%d\n", i
, bytes
);
155 FILL_STUBLESS(thunk
, i
, bytes
)
156 This
->PVtbl
[i
] = thunk
;
159 memset(thunk
, 0, sizeof(struct StublessThunk
));
160 This
->PVtbl
[i
] = vtbl
->Vtbl
[i
];
165 This
->PVtbl
= vtbl
->Vtbl
;
167 This
->lpVtbl
= &StdProxy_Vtbl
;
169 This
->stubless
= stubless
;
170 This
->piid
= vtbl
->header
.piid
;
171 This
->pUnkOuter
= pUnkOuter
;
173 This
->pPSFactory
= pPSFactory
;
174 This
->pChannel
= NULL
;
175 *ppProxy
= (LPRPCPROXYBUFFER
)&This
->lpVtbl
;
176 *ppvObj
= &This
->PVtbl
;
177 IPSFactoryBuffer_AddRef(pPSFactory
);
182 static void WINAPI
StdProxy_Destruct(LPRPCPROXYBUFFER iface
)
184 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
186 IPSFactoryBuffer_Release(This
->pPSFactory
);
188 HeapFree(GetProcessHeap(),0,This
->PVtbl
);
189 HeapFree(GetProcessHeap(),0,This
->thunks
);
191 HeapFree(GetProcessHeap(),0,This
);
194 static HRESULT WINAPI
StdProxy_QueryInterface(LPRPCPROXYBUFFER iface
,
198 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
199 TRACE("(%p)->QueryInterface(%s,%p)\n",This
,debugstr_guid(riid
),obj
);
201 if (IsEqualGUID(&IID_IUnknown
,riid
) ||
202 IsEqualGUID(This
->piid
,riid
)) {
208 if (IsEqualGUID(&IID_IRpcProxyBuffer
,riid
)) {
209 *obj
= &This
->lpVtbl
;
214 return E_NOINTERFACE
;
217 static ULONG WINAPI
StdProxy_AddRef(LPRPCPROXYBUFFER iface
)
219 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
220 TRACE("(%p)->AddRef()\n",This
);
222 return ++(This
->RefCount
);
225 static ULONG WINAPI
StdProxy_Release(LPRPCPROXYBUFFER iface
)
227 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
228 TRACE("(%p)->Release()\n",This
);
230 if (!--(This
->RefCount
)) {
231 StdProxy_Destruct((LPRPCPROXYBUFFER
)&This
->lpVtbl
);
234 return This
->RefCount
;
237 static HRESULT WINAPI
StdProxy_Connect(LPRPCPROXYBUFFER iface
,
238 LPRPCCHANNELBUFFER pChannel
)
240 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
241 TRACE("(%p)->Connect(%p)\n",This
,pChannel
);
243 This
->pChannel
= pChannel
;
247 static VOID WINAPI
StdProxy_Disconnect(LPRPCPROXYBUFFER iface
)
249 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
250 TRACE("(%p)->Disconnect()\n",This
);
252 This
->pChannel
= NULL
;
255 static ICOM_VTABLE(IRpcProxyBuffer
) StdProxy_Vtbl
=
257 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
258 StdProxy_QueryInterface
,
265 HRESULT WINAPI
StdProxy_GetChannel(LPVOID iface
,
266 LPRPCCHANNELBUFFER
*ppChannel
)
268 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
269 TRACE("(%p)->GetChannel(%p) %s\n",This
,ppChannel
,This
->name
);
271 *ppChannel
= This
->pChannel
;
275 HRESULT WINAPI
StdProxy_GetIID(LPVOID iface
,
278 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
279 TRACE("(%p)->GetIID(%p) %s\n",This
,ppiid
,This
->name
);
285 HRESULT WINAPI
IUnknown_QueryInterface_Proxy(LPUNKNOWN iface
,
289 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
290 TRACE("(%p)->QueryInterface(%s,%p) %s\n",This
,debugstr_guid(riid
),ppvObj
,This
->name
);
291 return IUnknown_QueryInterface(This
->pUnkOuter
,riid
,ppvObj
);
294 ULONG WINAPI
IUnknown_AddRef_Proxy(LPUNKNOWN iface
)
296 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
297 TRACE("(%p)->AddRef() %s\n",This
,This
->name
);
298 #if 0 /* interface refcounting */
299 return ++(This
->RefCount
);
300 #else /* object refcounting */
301 return IUnknown_AddRef(This
->pUnkOuter
);
305 ULONG WINAPI
IUnknown_Release_Proxy(LPUNKNOWN iface
)
307 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
308 TRACE("(%p)->Release() %s\n",This
,This
->name
);
309 #if 0 /* interface refcounting */
310 if (!--(This
->RefCount
)) {
311 StdProxy_Destruct((LPRPCPROXYBUFFER
)&This
->lpVtbl
);
314 return This
->RefCount
;
315 #else /* object refcounting */
316 return IUnknown_Release(This
->pUnkOuter
);