Moved ICOM_THIS_MULTI definition out of objbase.h and into the files
[wine/multimedia.git] / dlls / rpcrt4 / cproxy.c
blob10639cbfedf5a345086914d5f0ee473d8d151a0e
1 /*
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.
24 #include <stdarg.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winerror.h"
30 #include "objbase.h"
31 #include "rpcproxy.h"
33 #include "cpsf.h"
34 #include "ndr_misc.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(ole);
39 struct StublessThunk;
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 */
43 typedef struct {
44 IRpcProxyBufferVtbl *lpVtbl;
45 LPVOID *PVtbl;
46 DWORD RefCount;
47 const MIDL_STUBLESS_PROXY_INFO *stubless;
48 const IID* piid;
49 LPUNKNOWN pUnkOuter;
50 PCInterfaceName name;
51 LPPSFACTORYBUFFER pPSFactory;
52 LPRPCCHANNELBUFFER pChannel;
53 struct StublessThunk *thunks;
54 } StdProxyImpl;
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 */
64 #if defined(__i386__)
66 #include "pshpack1.h"
68 struct StublessThunk {
69 BYTE push;
70 DWORD index;
71 BYTE call;
72 LONG handler;
73 BYTE ret;
74 WORD bytes;
75 BYTE pad[3];
78 #include "poppack.h"
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] */ \
85 x->index = (idx); \
86 x->call = 0xe8; /* call [near] */ \
87 x->handler = (char*)ObjectStubless - (char*)&x->ret; \
88 x->ret = 0xc2; /* ret [immediate] */ \
89 x->bytes = stk; \
90 x->pad[0] = 0x8d; /* leal (%esi),%esi */ \
91 x->pad[1] = 0x76; \
92 x->pad[2] = 0x00;
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);
108 #else /* __i386__ */
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,
119 LPUNKNOWN pUnkOuter,
120 PCInterfaceName name,
121 CInterfaceProxyVtbl *vtbl,
122 CInterfaceStubVtbl *svtbl,
123 LPPSFACTORYBUFFER pPSFactory,
124 LPRPCPROXYBUFFER *ppProxy,
125 LPVOID *ppvObj)
127 StdProxyImpl *This;
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;
150 if (stubless) {
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;
167 else {
168 memset(thunk, 0, sizeof(struct StublessThunk));
169 This->PVtbl[i] = vtbl->Vtbl[i];
173 else
174 This->PVtbl = vtbl->Vtbl;
176 This->lpVtbl = &StdProxy_Vtbl;
177 /* 1 reference for the proxy and 1 for the object */
178 This->RefCount = 2;
179 This->stubless = stubless;
180 This->piid = vtbl->header.piid;
181 This->pUnkOuter = pUnkOuter;
182 This->name = name;
183 This->pPSFactory = pPSFactory;
184 This->pChannel = NULL;
185 *ppProxy = (LPRPCPROXYBUFFER)&This->lpVtbl;
186 *ppvObj = &This->PVtbl;
187 IPSFactoryBuffer_AddRef(pPSFactory);
189 return S_OK;
192 static void WINAPI StdProxy_Destruct(LPRPCPROXYBUFFER iface)
194 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
196 if (This->pChannel)
197 IRpcProxyBuffer_Disconnect(iface);
199 IPSFactoryBuffer_Release(This->pPSFactory);
200 if (This->thunks) {
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,
208 REFIID riid,
209 LPVOID *obj)
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)) {
216 *obj = &This->PVtbl;
217 This->RefCount++;
218 return S_OK;
221 if (IsEqualGUID(&IID_IRpcProxyBuffer,riid)) {
222 *obj = &This->lpVtbl;
223 This->RefCount++;
224 return S_OK;
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);
245 return 0;
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);
258 return S_OK;
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,
274 StdProxy_AddRef,
275 StdProxy_Release,
276 StdProxy_Connect,
277 StdProxy_Disconnect
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;
287 return S_OK;
290 HRESULT WINAPI StdProxy_GetIID(LPVOID iface,
291 const IID **ppiid)
293 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
294 TRACE("(%p)->GetIID(%p) %s\n",This,ppiid,This->name);
296 *ppiid = This->piid;
297 return S_OK;
300 HRESULT WINAPI IUnknown_QueryInterface_Proxy(LPUNKNOWN iface,
301 REFIID riid,
302 LPVOID *ppvObj)
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);
317 #endif
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);
327 return 0;
329 return This->RefCount;
330 #else /* object refcounting */
331 return IUnknown_Release(This->pUnkOuter);
332 #endif