Fixed bad non-client calculation.
[wine.git] / dlls / rpcrt4 / cproxy.c
blob7885484557504115ac7c0914dde47c186bbf4bd2
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 "objbase.h"
29 #include "rpcproxy.h"
31 #include "cpsf.h"
32 #include "ndr_misc.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(ole);
37 struct StublessThunk;
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 */
41 typedef struct {
42 ICOM_VTABLE(IRpcProxyBuffer) *lpVtbl;
43 LPVOID *PVtbl;
44 DWORD RefCount;
45 const MIDL_STUBLESS_PROXY_INFO *stubless;
46 const IID* piid;
47 LPUNKNOWN pUnkOuter;
48 LPPSFACTORYBUFFER pPSFactory;
49 LPRPCCHANNELBUFFER pChannel;
50 struct StublessThunk *thunks;
51 } StdProxyImpl;
53 static ICOM_VTABLE(IRpcProxyBuffer) StdProxy_Vtbl;
55 /* How the Windows stubless proxy thunks work is explained at
56 * http://msdn.microsoft.com/library/en-us/dnmsj99/html/com0199.asp,
57 * but I'll use a slightly different method, to make life easier */
59 #if defined(__i386__)
61 struct StublessThunk {
62 BYTE push WINE_PACKED;
63 DWORD index WINE_PACKED;
64 BYTE call WINE_PACKED;
65 LONG handler WINE_PACKED;
66 BYTE ret WINE_PACKED;
67 WORD bytes WINE_PACKED;
68 BYTE pad[3];
71 /* adjust the stack size since we don't use Windows's method */
72 #define STACK_ADJUST sizeof(DWORD)
74 #define FILL_STUBLESS(x,idx,stk) \
75 x->push = 0x68; /* pushl [immediate] */ \
76 x->index = (idx); \
77 x->call = 0xe8; /* call [near] */ \
78 x->handler = (char*)ObjectStubless - (char*)&x->ret; \
79 x->ret = 0xc2; /* ret [immediate] */ \
80 x->bytes = stk; \
81 x->pad[0] = 0x8d; /* leal (%esi),%esi */ \
82 x->pad[1] = 0x76; \
83 x->pad[2] = 0x00;
85 static HRESULT WINAPI ObjectStubless(DWORD index)
87 char *args = (char*)(&index + 2);
88 LPVOID iface = *(LPVOID*)args;
90 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
92 PFORMAT_STRING fs = This->stubless->ProcFormatString + This->stubless->FormatStringOffset[index];
93 unsigned bytes = *(WORD*)(fs+8) - STACK_ADJUST;
94 TRACE("(%p)->(%ld)([%d bytes]) ret=%08lx\n", iface, index, bytes, *(DWORD*)(args+bytes));
96 return RPCRT4_NdrClientCall2(This->stubless->pStubDesc, fs, args);
99 #else /* __i386__ */
101 /* can't do that on this arch */
102 struct StublessThunk { int dummy; };
103 #define FILL_STUBLESS(x,idx,stk) \
104 ERR("stubless proxies are not supported on this architecture\n");
105 #define STACK_ADJUST 0
107 #endif /* __i386__ */
109 HRESULT WINAPI StdProxy_Construct(REFIID riid,
110 LPUNKNOWN pUnkOuter,
111 CInterfaceProxyVtbl *vtbl,
112 CInterfaceStubVtbl *svtbl,
113 LPPSFACTORYBUFFER pPSFactory,
114 LPRPCPROXYBUFFER *ppProxy,
115 LPVOID *ppvObj)
117 StdProxyImpl *This;
118 const MIDL_STUBLESS_PROXY_INFO *stubless = NULL;
120 TRACE("(%p,%p,%p,%p,%p)\n", pUnkOuter, vtbl, pPSFactory, ppProxy, ppvObj);
122 /* I can't find any other way to detect stubless proxies than this hack */
123 if (!IsEqualGUID(vtbl->header.piid, riid)) {
124 stubless = *((const void **)vtbl)++;
125 TRACE("stubless=%p\n", stubless);
128 TRACE("iid=%s\n", debugstr_guid(vtbl->header.piid));
129 TRACE("vtbl=%p\n", vtbl->Vtbl);
131 if (!IsEqualGUID(vtbl->header.piid, riid)) {
132 ERR("IID mismatch during proxy creation\n");
133 return RPC_E_UNEXPECTED;
136 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(StdProxyImpl));
137 if (!This) return E_OUTOFMEMORY;
139 if (stubless) {
140 unsigned i, count = svtbl->header.DispatchTableCount;
141 /* Maybe the original vtbl is just modified directly to point at
142 * ObjectStublessClientXXX thunks in real Windows, but I don't like it
144 TRACE("stubless thunks: count=%d\n", count);
145 This->thunks = HeapAlloc(GetProcessHeap(),0,sizeof(struct StublessThunk)*count);
146 This->PVtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPVOID)*count);
147 for (i=0; i<count; i++) {
148 struct StublessThunk *thunk = &This->thunks[i];
149 if (vtbl->Vtbl[i] == (LPVOID)-1) {
150 PFORMAT_STRING fs = stubless->ProcFormatString + stubless->FormatStringOffset[i];
151 unsigned bytes = *(WORD*)(fs+8) - STACK_ADJUST;
152 TRACE("method %d: stacksize=%d\n", i, bytes);
153 FILL_STUBLESS(thunk, i, bytes)
154 This->PVtbl[i] = thunk;
156 else {
157 memset(thunk, 0, sizeof(struct StublessThunk));
158 This->PVtbl[i] = vtbl->Vtbl[i];
162 else
163 This->PVtbl = vtbl->Vtbl;
165 This->lpVtbl = &StdProxy_Vtbl;
166 This->RefCount = 1;
167 This->stubless = stubless;
168 This->piid = vtbl->header.piid;
169 This->pUnkOuter = pUnkOuter;
170 This->pPSFactory = pPSFactory;
171 This->pChannel = NULL;
172 *ppProxy = (LPRPCPROXYBUFFER)&This->lpVtbl;
173 *ppvObj = &This->PVtbl;
174 IPSFactoryBuffer_AddRef(pPSFactory);
176 return S_OK;
179 static void WINAPI StdProxy_Destruct(LPRPCPROXYBUFFER iface)
181 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
183 IPSFactoryBuffer_Release(This->pPSFactory);
184 if (This->thunks) {
185 HeapFree(GetProcessHeap(),0,This->PVtbl);
186 HeapFree(GetProcessHeap(),0,This->thunks);
188 HeapFree(GetProcessHeap(),0,This);
191 static HRESULT WINAPI StdProxy_QueryInterface(LPRPCPROXYBUFFER iface,
192 REFIID riid,
193 LPVOID *obj)
195 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
196 TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
198 if (IsEqualGUID(&IID_IUnknown,riid) ||
199 IsEqualGUID(This->piid,riid)) {
200 *obj = &This->PVtbl;
201 This->RefCount++;
202 return S_OK;
205 if (IsEqualGUID(&IID_IRpcProxyBuffer,riid)) {
206 *obj = &This->lpVtbl;
207 This->RefCount++;
208 return S_OK;
211 return E_NOINTERFACE;
214 static ULONG WINAPI StdProxy_AddRef(LPRPCPROXYBUFFER iface)
216 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
217 TRACE("(%p)->AddRef()\n",This);
219 return ++(This->RefCount);
222 static ULONG WINAPI StdProxy_Release(LPRPCPROXYBUFFER iface)
224 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
225 TRACE("(%p)->Release()\n",This);
227 if (!--(This->RefCount)) {
228 StdProxy_Destruct((LPRPCPROXYBUFFER)&This->lpVtbl);
229 return 0;
231 return This->RefCount;
234 static HRESULT WINAPI StdProxy_Connect(LPRPCPROXYBUFFER iface,
235 LPRPCCHANNELBUFFER pChannel)
237 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
238 TRACE("(%p)->Connect(%p)\n",This,pChannel);
240 This->pChannel = pChannel;
241 return S_OK;
244 static VOID WINAPI StdProxy_Disconnect(LPRPCPROXYBUFFER iface)
246 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
247 TRACE("(%p)->Disconnect()\n",This);
249 This->pChannel = NULL;
252 static ICOM_VTABLE(IRpcProxyBuffer) StdProxy_Vtbl =
254 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
255 StdProxy_QueryInterface,
256 StdProxy_AddRef,
257 StdProxy_Release,
258 StdProxy_Connect,
259 StdProxy_Disconnect
262 HRESULT WINAPI StdProxy_GetChannel(LPVOID iface,
263 LPRPCCHANNELBUFFER *ppChannel)
265 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
266 TRACE("(%p)->GetChannel(%p)\n",This,ppChannel);
268 *ppChannel = This->pChannel;
269 return S_OK;
272 HRESULT WINAPI StdProxy_GetIID(LPVOID iface,
273 const IID **ppiid)
275 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
276 TRACE("(%p)->GetIID(%p)\n",This,ppiid);
278 *ppiid = This->piid;
279 return S_OK;
282 HRESULT WINAPI IUnknown_QueryInterface_Proxy(LPUNKNOWN iface,
283 REFIID riid,
284 LPVOID *ppvObj)
286 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
287 TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),ppvObj);
288 return IUnknown_QueryInterface(This->pUnkOuter,riid,ppvObj);
291 ULONG WINAPI IUnknown_AddRef_Proxy(LPUNKNOWN iface)
293 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
294 TRACE("(%p)->AddRef()\n",This);
295 #if 0 /* interface refcounting */
296 return ++(This->RefCount);
297 #else /* object refcounting */
298 return IUnknown_AddRef(This->pUnkOuter);
299 #endif
302 ULONG WINAPI IUnknown_Release_Proxy(LPUNKNOWN iface)
304 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
305 TRACE("(%p)->Release()\n",This);
306 #if 0 /* interface refcounting */
307 if (!--(This->RefCount)) {
308 StdProxy_Destruct((LPRPCPROXYBUFFER)&This->lpVtbl);
309 return 0;
311 return This->RefCount;
312 #else /* object refcounting */
313 return IUnknown_Release(This->pUnkOuter);
314 #endif