Fixed compilation for non-i386.
[wine/multimedia.git] / dlls / rpcrt4 / cproxy.c
blob433d5ab69055919bd01cda26f0db25da997c4e35
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 "windef.h"
25 #include "winbase.h"
26 #include "winerror.h"
28 #include "wine/obj_base.h"
29 #include "wine/obj_channel.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 ICOM_VTABLE(IRpcProxyBuffer) *lpVtbl;
45 LPVOID *PVtbl;
46 DWORD RefCount;
47 const MIDL_STUBLESS_PROXY_INFO *stubless;
48 const IID* piid;
49 LPUNKNOWN pUnkOuter;
50 LPPSFACTORYBUFFER pPSFactory;
51 LPRPCCHANNELBUFFER pChannel;
52 struct StublessThunk *thunks;
53 } StdProxyImpl;
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 */
61 #if defined(__i386__)
63 struct StublessThunk {
64 BYTE push WINE_PACKED;
65 DWORD index WINE_PACKED;
66 BYTE call WINE_PACKED;
67 LONG handler WINE_PACKED;
68 BYTE ret WINE_PACKED;
69 WORD bytes WINE_PACKED;
70 BYTE pad[3];
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] */ \
78 x->index = (idx); \
79 x->call = 0xe8; /* call [near] */ \
80 x->handler = (char*)ObjectStubless - (char*)&x->ret; \
81 x->ret = 0xc2; /* ret [immediate] */ \
82 x->bytes = stk; \
83 x->pad[0] = 0x8d; /* leal (%esi),%esi */ \
84 x->pad[1] = 0x76; \
85 x->pad[2] = 0x00;
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);
101 #else /* __i386__ */
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,
112 LPUNKNOWN pUnkOuter,
113 CInterfaceProxyVtbl *vtbl,
114 CInterfaceStubVtbl *svtbl,
115 LPPSFACTORYBUFFER pPSFactory,
116 LPRPCPROXYBUFFER *ppProxy,
117 LPVOID *ppvObj)
119 StdProxyImpl *This;
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;
141 if (stubless) {
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;
158 else {
159 memset(thunk, 0, sizeof(struct StublessThunk));
160 This->PVtbl[i] = vtbl->Vtbl[i];
164 else
165 This->PVtbl = vtbl->Vtbl;
167 This->lpVtbl = &StdProxy_Vtbl;
168 This->RefCount = 1;
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);
178 return S_OK;
181 static void WINAPI StdProxy_Destruct(LPRPCPROXYBUFFER iface)
183 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
185 IPSFactoryBuffer_Release(This->pPSFactory);
186 if (This->thunks) {
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,
194 REFIID riid,
195 LPVOID *obj)
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)) {
202 *obj = &This->PVtbl;
203 This->RefCount++;
204 return S_OK;
207 if (IsEqualGUID(&IID_IRpcProxyBuffer,riid)) {
208 *obj = &This->lpVtbl;
209 This->RefCount++;
210 return S_OK;
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);
231 return 0;
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;
243 return S_OK;
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,
258 StdProxy_AddRef,
259 StdProxy_Release,
260 StdProxy_Connect,
261 StdProxy_Disconnect
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;
271 return S_OK;
274 HRESULT WINAPI StdProxy_GetIID(LPVOID iface,
275 const IID **ppiid)
277 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
278 TRACE("(%p)->GetIID(%p)\n",This,ppiid);
280 *ppiid = This->piid;
281 return S_OK;
284 HRESULT WINAPI IUnknown_QueryInterface_Proxy(LPUNKNOWN iface,
285 REFIID riid,
286 LPVOID *ppvObj)
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);
301 #endif
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);
311 return 0;
313 return This->RefCount;
314 #else /* object refcounting */
315 return IUnknown_Release(This->pUnkOuter);
316 #endif