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.
28 #include "wine/obj_base.h"
29 #include "wine/obj_channel.h"
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
;
50 LPPSFACTORYBUFFER pPSFactory
;
51 LPRPCCHANNELBUFFER pChannel
;
52 struct StublessThunk
*thunks
;
55 static ICOM_VTABLE(IRpcProxyBuffer
) StdProxy_Vtbl
;
57 /* How the Windows stubless proxy thunks work is explained at
58 * http://msdn.microsoft.com/library/en-us/dnmsj99/html/com0199.asp,
59 * but I'll use a slightly different method, to make life easier */
63 struct StublessThunk
{
64 BYTE push WINE_PACKED
;
65 DWORD index WINE_PACKED
;
66 BYTE call WINE_PACKED
;
67 LONG handler WINE_PACKED
;
69 WORD bytes WINE_PACKED
;
73 /* adjust the stack size since we don't use Windows's method */
74 #define STACK_ADJUST sizeof(DWORD)
76 #define FILL_STUBLESS(x,idx,stk) \
77 x->push = 0x68; /* pushl [immediate] */ \
79 x->call = 0xe8; /* call [near] */ \
80 x->handler = (char*)ObjectStubless - (char*)&x->ret; \
81 x->ret = 0xc2; /* ret [immediate] */ \
83 x->pad[0] = 0x8d; /* leal (%esi),%esi */ \
87 static HRESULT WINAPI
ObjectStubless(DWORD index
)
89 char *args
= (char*)(&index
+ 2);
90 LPVOID iface
= *(LPVOID
*)args
;
92 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
94 PFORMAT_STRING fs
= This
->stubless
->ProcFormatString
+ This
->stubless
->FormatStringOffset
[index
];
95 unsigned bytes
= *(WORD
*)(fs
+8) - STACK_ADJUST
;
96 TRACE("(%p)->(%ld)([%d bytes]) ret=%08lx\n", iface
, index
, bytes
, *(DWORD
*)(args
+bytes
));
98 return RPCRT4_NdrClientCall2(This
->stubless
->pStubDesc
, fs
, args
);
103 /* can't do that on this arch */
104 struct StublessThunk
{ int dummy
; };
105 #define FILL_STUBLESS(x,idx,stk) \
106 ERR("stubless proxies are not supported on this architecture\n");
107 #define STACK_ADJUST 0
109 #endif /* __i386__ */
111 HRESULT WINAPI
StdProxy_Construct(REFIID riid
,
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)\n", pUnkOuter
, vtbl
, pPSFactory
, ppProxy
, ppvObj
);
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
;
172 This
->pPSFactory
= pPSFactory
;
173 This
->pChannel
= NULL
;
174 *ppProxy
= (LPRPCPROXYBUFFER
)&This
->lpVtbl
;
175 *ppvObj
= &This
->PVtbl
;
176 IPSFactoryBuffer_AddRef(pPSFactory
);
181 static void WINAPI
StdProxy_Destruct(LPRPCPROXYBUFFER iface
)
183 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
185 IPSFactoryBuffer_Release(This
->pPSFactory
);
187 HeapFree(GetProcessHeap(),0,This
->PVtbl
);
188 HeapFree(GetProcessHeap(),0,This
->thunks
);
190 HeapFree(GetProcessHeap(),0,This
);
193 static HRESULT WINAPI
StdProxy_QueryInterface(LPRPCPROXYBUFFER iface
,
197 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
198 TRACE("(%p)->QueryInterface(%s,%p)\n",This
,debugstr_guid(riid
),obj
);
200 if (IsEqualGUID(&IID_IUnknown
,riid
) ||
201 IsEqualGUID(This
->piid
,riid
)) {
207 if (IsEqualGUID(&IID_IRpcProxyBuffer
,riid
)) {
208 *obj
= &This
->lpVtbl
;
213 return E_NOINTERFACE
;
216 static ULONG WINAPI
StdProxy_AddRef(LPRPCPROXYBUFFER iface
)
218 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
219 TRACE("(%p)->AddRef()\n",This
);
221 return ++(This
->RefCount
);
224 static ULONG WINAPI
StdProxy_Release(LPRPCPROXYBUFFER iface
)
226 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
227 TRACE("(%p)->Release()\n",This
);
229 if (!--(This
->RefCount
)) {
230 StdProxy_Destruct((LPRPCPROXYBUFFER
)&This
->lpVtbl
);
233 return This
->RefCount
;
236 static HRESULT WINAPI
StdProxy_Connect(LPRPCPROXYBUFFER iface
,
237 LPRPCCHANNELBUFFER pChannel
)
239 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
240 TRACE("(%p)->Connect(%p)\n",This
,pChannel
);
242 This
->pChannel
= pChannel
;
246 static VOID WINAPI
StdProxy_Disconnect(LPRPCPROXYBUFFER iface
)
248 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
249 TRACE("(%p)->Disconnect()\n",This
);
251 This
->pChannel
= NULL
;
254 static ICOM_VTABLE(IRpcProxyBuffer
) StdProxy_Vtbl
=
256 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
257 StdProxy_QueryInterface
,
264 HRESULT WINAPI
StdProxy_GetChannel(LPVOID iface
,
265 LPRPCCHANNELBUFFER
*ppChannel
)
267 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
268 TRACE("(%p)->GetChannel(%p)\n",This
,ppChannel
);
270 *ppChannel
= This
->pChannel
;
274 HRESULT WINAPI
StdProxy_GetIID(LPVOID iface
,
277 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
278 TRACE("(%p)->GetIID(%p)\n",This
,ppiid
);
284 HRESULT WINAPI
IUnknown_QueryInterface_Proxy(LPUNKNOWN iface
,
288 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
289 TRACE("(%p)->QueryInterface(%s,%p)\n",This
,debugstr_guid(riid
),ppvObj
);
290 return IUnknown_QueryInterface(This
->pUnkOuter
,riid
,ppvObj
);
293 ULONG WINAPI
IUnknown_AddRef_Proxy(LPUNKNOWN iface
)
295 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
296 TRACE("(%p)->AddRef()\n",This
);
297 #if 0 /* interface refcounting */
298 return ++(This
->RefCount
);
299 #else /* object refcounting */
300 return IUnknown_AddRef(This
->pUnkOuter
);
304 ULONG WINAPI
IUnknown_Release_Proxy(LPUNKNOWN iface
)
306 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
307 TRACE("(%p)->Release()\n",This
);
308 #if 0 /* interface refcounting */
309 if (!--(This
->RefCount
)) {
310 StdProxy_Destruct((LPRPCPROXYBUFFER
)&This
->lpVtbl
);
313 return This
->RefCount
;
314 #else /* object refcounting */
315 return IUnknown_Release(This
->pUnkOuter
);