Beginnings of a script to initialize the .wine directory (with help
[wine.git] / dlls / rpcrt4 / cproxy.c
blob21a824e8e9d45a228188ca67a59103b6f2c02b6f
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 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 PCInterfaceName name;
51 LPPSFACTORYBUFFER pPSFactory;
52 LPRPCCHANNELBUFFER pChannel;
53 struct StublessThunk *thunks;
54 } StdProxyImpl;
56 static ICOM_VTABLE(IRpcProxyBuffer) StdProxy_Vtbl;
58 /* How the Windows stubless proxy thunks work is explained at
59 * http://msdn.microsoft.com/library/en-us/dnmsj99/html/com0199.asp,
60 * but I'll use a slightly different method, to make life easier */
62 #if defined(__i386__)
64 #include "pshpack1.h"
66 struct StublessThunk {
67 BYTE push;
68 DWORD index;
69 BYTE call;
70 LONG handler;
71 BYTE ret;
72 WORD bytes;
73 BYTE pad[3];
76 #include "poppack.h"
78 /* adjust the stack size since we don't use Windows's method */
79 #define STACK_ADJUST sizeof(DWORD)
81 #define FILL_STUBLESS(x,idx,stk) \
82 x->push = 0x68; /* pushl [immediate] */ \
83 x->index = (idx); \
84 x->call = 0xe8; /* call [near] */ \
85 x->handler = (char*)ObjectStubless - (char*)&x->ret; \
86 x->ret = 0xc2; /* ret [immediate] */ \
87 x->bytes = stk; \
88 x->pad[0] = 0x8d; /* leal (%esi),%esi */ \
89 x->pad[1] = 0x76; \
90 x->pad[2] = 0x00;
92 static HRESULT WINAPI ObjectStubless(DWORD index)
94 char *args = (char*)(&index + 2);
95 LPVOID iface = *(LPVOID*)args;
97 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
99 PFORMAT_STRING fs = This->stubless->ProcFormatString + This->stubless->FormatStringOffset[index];
100 unsigned bytes = *(WORD*)(fs+8) - STACK_ADJUST;
101 TRACE("(%p)->(%ld)([%d bytes]) ret=%08lx\n", iface, index, bytes, *(DWORD*)(args+bytes));
103 return RPCRT4_NdrClientCall2(This->stubless->pStubDesc, fs, args);
106 #else /* __i386__ */
108 /* can't do that on this arch */
109 struct StublessThunk { int dummy; };
110 #define FILL_STUBLESS(x,idx,stk) \
111 ERR("stubless proxies are not supported on this architecture\n");
112 #define STACK_ADJUST 0
114 #endif /* __i386__ */
116 HRESULT WINAPI StdProxy_Construct(REFIID riid,
117 LPUNKNOWN pUnkOuter,
118 PCInterfaceName name,
119 CInterfaceProxyVtbl *vtbl,
120 CInterfaceStubVtbl *svtbl,
121 LPPSFACTORYBUFFER pPSFactory,
122 LPRPCPROXYBUFFER *ppProxy,
123 LPVOID *ppvObj)
125 StdProxyImpl *This;
126 const MIDL_STUBLESS_PROXY_INFO *stubless = NULL;
128 TRACE("(%p,%p,%p,%p,%p) %s\n", pUnkOuter, vtbl, pPSFactory, ppProxy, ppvObj, name);
130 /* I can't find any other way to detect stubless proxies than this hack */
131 if (!IsEqualGUID(vtbl->header.piid, riid)) {
132 stubless = *((const void **)vtbl)++;
133 TRACE("stubless=%p\n", stubless);
136 TRACE("iid=%s\n", debugstr_guid(vtbl->header.piid));
137 TRACE("vtbl=%p\n", vtbl->Vtbl);
139 if (!IsEqualGUID(vtbl->header.piid, riid)) {
140 ERR("IID mismatch during proxy creation\n");
141 return RPC_E_UNEXPECTED;
144 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(StdProxyImpl));
145 if (!This) return E_OUTOFMEMORY;
147 if (stubless) {
148 unsigned i, count = svtbl->header.DispatchTableCount;
149 /* Maybe the original vtbl is just modified directly to point at
150 * ObjectStublessClientXXX thunks in real Windows, but I don't like it
152 TRACE("stubless thunks: count=%d\n", count);
153 This->thunks = HeapAlloc(GetProcessHeap(),0,sizeof(struct StublessThunk)*count);
154 This->PVtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPVOID)*count);
155 for (i=0; i<count; i++) {
156 struct StublessThunk *thunk = &This->thunks[i];
157 if (vtbl->Vtbl[i] == (LPVOID)-1) {
158 PFORMAT_STRING fs = stubless->ProcFormatString + stubless->FormatStringOffset[i];
159 unsigned bytes = *(WORD*)(fs+8) - STACK_ADJUST;
160 TRACE("method %d: stacksize=%d\n", i, bytes);
161 FILL_STUBLESS(thunk, i, bytes)
162 This->PVtbl[i] = thunk;
164 else {
165 memset(thunk, 0, sizeof(struct StublessThunk));
166 This->PVtbl[i] = vtbl->Vtbl[i];
170 else
171 This->PVtbl = vtbl->Vtbl;
173 This->lpVtbl = &StdProxy_Vtbl;
174 This->RefCount = 1;
175 This->stubless = stubless;
176 This->piid = vtbl->header.piid;
177 This->pUnkOuter = pUnkOuter;
178 This->name = name;
179 This->pPSFactory = pPSFactory;
180 This->pChannel = NULL;
181 *ppProxy = (LPRPCPROXYBUFFER)&This->lpVtbl;
182 *ppvObj = &This->PVtbl;
183 IPSFactoryBuffer_AddRef(pPSFactory);
185 return S_OK;
188 static void WINAPI StdProxy_Destruct(LPRPCPROXYBUFFER iface)
190 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
192 IPSFactoryBuffer_Release(This->pPSFactory);
193 if (This->thunks) {
194 HeapFree(GetProcessHeap(),0,This->PVtbl);
195 HeapFree(GetProcessHeap(),0,This->thunks);
197 HeapFree(GetProcessHeap(),0,This);
200 static HRESULT WINAPI StdProxy_QueryInterface(LPRPCPROXYBUFFER iface,
201 REFIID riid,
202 LPVOID *obj)
204 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
205 TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
207 if (IsEqualGUID(&IID_IUnknown,riid) ||
208 IsEqualGUID(This->piid,riid)) {
209 *obj = &This->PVtbl;
210 This->RefCount++;
211 return S_OK;
214 if (IsEqualGUID(&IID_IRpcProxyBuffer,riid)) {
215 *obj = &This->lpVtbl;
216 This->RefCount++;
217 return S_OK;
220 return E_NOINTERFACE;
223 static ULONG WINAPI StdProxy_AddRef(LPRPCPROXYBUFFER iface)
225 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
226 TRACE("(%p)->AddRef()\n",This);
228 return ++(This->RefCount);
231 static ULONG WINAPI StdProxy_Release(LPRPCPROXYBUFFER iface)
233 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
234 TRACE("(%p)->Release()\n",This);
236 if (!--(This->RefCount)) {
237 StdProxy_Destruct((LPRPCPROXYBUFFER)&This->lpVtbl);
238 return 0;
240 return This->RefCount;
243 static HRESULT WINAPI StdProxy_Connect(LPRPCPROXYBUFFER iface,
244 LPRPCCHANNELBUFFER pChannel)
246 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
247 TRACE("(%p)->Connect(%p)\n",This,pChannel);
249 This->pChannel = pChannel;
250 return S_OK;
253 static VOID WINAPI StdProxy_Disconnect(LPRPCPROXYBUFFER iface)
255 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
256 TRACE("(%p)->Disconnect()\n",This);
258 This->pChannel = NULL;
261 static ICOM_VTABLE(IRpcProxyBuffer) StdProxy_Vtbl =
263 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
264 StdProxy_QueryInterface,
265 StdProxy_AddRef,
266 StdProxy_Release,
267 StdProxy_Connect,
268 StdProxy_Disconnect
271 HRESULT WINAPI StdProxy_GetChannel(LPVOID iface,
272 LPRPCCHANNELBUFFER *ppChannel)
274 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
275 TRACE("(%p)->GetChannel(%p) %s\n",This,ppChannel,This->name);
277 *ppChannel = This->pChannel;
278 return S_OK;
281 HRESULT WINAPI StdProxy_GetIID(LPVOID iface,
282 const IID **ppiid)
284 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
285 TRACE("(%p)->GetIID(%p) %s\n",This,ppiid,This->name);
287 *ppiid = This->piid;
288 return S_OK;
291 HRESULT WINAPI IUnknown_QueryInterface_Proxy(LPUNKNOWN iface,
292 REFIID riid,
293 LPVOID *ppvObj)
295 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
296 TRACE("(%p)->QueryInterface(%s,%p) %s\n",This,debugstr_guid(riid),ppvObj,This->name);
297 return IUnknown_QueryInterface(This->pUnkOuter,riid,ppvObj);
300 ULONG WINAPI IUnknown_AddRef_Proxy(LPUNKNOWN iface)
302 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
303 TRACE("(%p)->AddRef() %s\n",This,This->name);
304 #if 0 /* interface refcounting */
305 return ++(This->RefCount);
306 #else /* object refcounting */
307 return IUnknown_AddRef(This->pUnkOuter);
308 #endif
311 ULONG WINAPI IUnknown_Release_Proxy(LPUNKNOWN iface)
313 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
314 TRACE("(%p)->Release() %s\n",This,This->name);
315 #if 0 /* interface refcounting */
316 if (!--(This->RefCount)) {
317 StdProxy_Destruct((LPRPCPROXYBUFFER)&This->lpVtbl);
318 return 0;
320 return This->RefCount;
321 #else /* object refcounting */
322 return IUnknown_Release(This->pUnkOuter);
323 #endif