af8709a22c51bb02f0e1df849c7c7019f7e0c1c4
[reactos.git] / reactos / lib / rpcrt4 / cproxy.c
1 /*
2 * COM proxy implementation
3 *
4 * Copyright 2001 Ove Kåven, TransGaming Technologies
5 *
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.
10 *
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.
15 *
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
19 *
20 * TODO: Handle non-i386 architectures
21 * Get rid of #if 0'ed code.
22 */
23
24 #include <stdarg.h>
25
26 #define COBJMACROS
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winerror.h"
31
32 #include "objbase.h"
33 #include "rpcproxy.h"
34
35 #include "cpsf.h"
36 #include "ndr_misc.h"
37 #include "wine/debug.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(ole);
40
41 struct StublessThunk;
42
43 /* I don't know what MS's std proxy structure looks like,
44 so this probably doesn't match, but that shouldn't matter */
45 typedef struct {
46 IRpcProxyBufferVtbl *lpVtbl;
47 LPVOID *PVtbl;
48 DWORD RefCount;
49 const MIDL_STUBLESS_PROXY_INFO *stubless;
50 const IID* piid;
51 LPUNKNOWN pUnkOuter;
52 PCInterfaceName name;
53 LPPSFACTORYBUFFER pPSFactory;
54 LPRPCCHANNELBUFFER pChannel;
55 struct StublessThunk *thunks;
56 } StdProxyImpl;
57
58 static IRpcProxyBufferVtbl StdProxy_Vtbl;
59
60 #define ICOM_THIS_MULTI(impl,field,iface) impl* const This=(impl*)((char*)(iface) - offsetof(impl,field))
61
62 /* How the Windows stubless proxy thunks work is explained at
63 * http://msdn.microsoft.com/library/en-us/dnmsj99/html/com0199.asp,
64 * but I'll use a slightly different method, to make life easier */
65
66 #if defined(__i386__)
67
68 #include "pshpack1.h"
69
70 struct StublessThunk {
71 BYTE push;
72 DWORD index;
73 BYTE call;
74 LONG handler;
75 BYTE ret;
76 WORD bytes;
77 BYTE pad[3];
78 };
79
80 #include "poppack.h"
81
82 /* adjust the stack size since we don't use Windows's method */
83 #define STACK_ADJUST sizeof(DWORD)
84
85 #define FILL_STUBLESS(x,idx,stk) \
86 x->push = 0x68; /* pushl [immediate] */ \
87 x->index = (idx); \
88 x->call = 0xe8; /* call [near] */ \
89 x->handler = (char*)ObjectStubless - (char*)&x->ret; \
90 x->ret = 0xc2; /* ret [immediate] */ \
91 x->bytes = stk; \
92 x->pad[0] = 0x8d; /* leal (%esi),%esi */ \
93 x->pad[1] = 0x76; \
94 x->pad[2] = 0x00;
95
96 static HRESULT WINAPI ObjectStubless(DWORD index)
97 {
98 char *args = (char*)(&index + 2);
99 LPVOID iface = *(LPVOID*)args;
100
101 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
102
103 PFORMAT_STRING fs = This->stubless->ProcFormatString + This->stubless->FormatStringOffset[index];
104 unsigned bytes = *(const WORD*)(fs+8) - STACK_ADJUST;
105 TRACE("(%p)->(%ld)([%d bytes]) ret=%08lx\n", iface, index, bytes, *(DWORD*)(args+bytes));
106
107 return RPCRT4_NdrClientCall2(This->stubless->pStubDesc, fs, args);
108 }
109
110 #else /* __i386__ */
111
112 /* can't do that on this arch */
113 struct StublessThunk { int dummy; };
114 #define FILL_STUBLESS(x,idx,stk) \
115 ERR("stubless proxies are not supported on this architecture\n");
116 #define STACK_ADJUST 0
117
118 #endif /* __i386__ */
119
120 HRESULT WINAPI StdProxy_Construct(REFIID riid,
121 LPUNKNOWN pUnkOuter,
122 PCInterfaceName name,
123 CInterfaceProxyVtbl *vtbl,
124 CInterfaceStubVtbl *svtbl,
125 LPPSFACTORYBUFFER pPSFactory,
126 LPRPCPROXYBUFFER *ppProxy,
127 LPVOID *ppvObj)
128 {
129 StdProxyImpl *This;
130 const MIDL_STUBLESS_PROXY_INFO *stubless = NULL;
131
132 TRACE("(%p,%p,%p,%p,%p) %s\n", pUnkOuter, vtbl, pPSFactory, ppProxy, ppvObj, name);
133
134 /* I can't find any other way to detect stubless proxies than this hack */
135 if (!IsEqualGUID(vtbl->header.piid, riid)) {
136 stubless = *(const void **)vtbl;
137 vtbl = (CInterfaceProxyVtbl *)((const void **)vtbl + 1);
138 TRACE("stubless=%p\n", stubless);
139 }
140
141 TRACE("iid=%s\n", debugstr_guid(vtbl->header.piid));
142 TRACE("vtbl=%p\n", vtbl->Vtbl);
143
144 if (!IsEqualGUID(vtbl->header.piid, riid)) {
145 ERR("IID mismatch during proxy creation\n");
146 return RPC_E_UNEXPECTED;
147 }
148
149 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(StdProxyImpl));
150 if (!This) return E_OUTOFMEMORY;
151
152 if (stubless) {
153 unsigned i, count = svtbl->header.DispatchTableCount;
154 /* Maybe the original vtbl is just modified directly to point at
155 * ObjectStublessClientXXX thunks in real Windows, but I don't like it
156 */
157 TRACE("stubless thunks: count=%d\n", count);
158 This->thunks = HeapAlloc(GetProcessHeap(),0,sizeof(struct StublessThunk)*count);
159 This->PVtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPVOID)*count);
160 for (i=0; i<count; i++) {
161 struct StublessThunk *thunk = &This->thunks[i];
162 if (vtbl->Vtbl[i] == (LPVOID)-1) {
163 PFORMAT_STRING fs = stubless->ProcFormatString + stubless->FormatStringOffset[i];
164 unsigned bytes = *(const WORD*)(fs+8) - STACK_ADJUST;
165 TRACE("method %d: stacksize=%d\n", i, bytes);
166 FILL_STUBLESS(thunk, i, bytes)
167 This->PVtbl[i] = thunk;
168 }
169 else {
170 memset(thunk, 0, sizeof(struct StublessThunk));
171 This->PVtbl[i] = vtbl->Vtbl[i];
172 }
173 }
174 }
175 else
176 This->PVtbl = vtbl->Vtbl;
177
178 This->lpVtbl = &StdProxy_Vtbl;
179 /* 1 reference for the proxy and 1 for the object */
180 This->RefCount = 2;
181 This->stubless = stubless;
182 This->piid = vtbl->header.piid;
183 This->pUnkOuter = pUnkOuter;
184 This->name = name;
185 This->pPSFactory = pPSFactory;
186 This->pChannel = NULL;
187 *ppProxy = (LPRPCPROXYBUFFER)&This->lpVtbl;
188 *ppvObj = &This->PVtbl;
189 IPSFactoryBuffer_AddRef(pPSFactory);
190
191 return S_OK;
192 }
193
194 static void WINAPI StdProxy_Destruct(LPRPCPROXYBUFFER iface)
195 {
196 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
197
198 if (This->pChannel)
199 IRpcProxyBuffer_Disconnect(iface);
200
201 IPSFactoryBuffer_Release(This->pPSFactory);
202 if (This->thunks) {
203 HeapFree(GetProcessHeap(),0,This->PVtbl);
204 HeapFree(GetProcessHeap(),0,This->thunks);
205 }
206 HeapFree(GetProcessHeap(),0,This);
207 }
208
209 static HRESULT WINAPI StdProxy_QueryInterface(LPRPCPROXYBUFFER iface,
210 REFIID riid,
211 LPVOID *obj)
212 {
213 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
214 TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
215
216 if (IsEqualGUID(&IID_IUnknown,riid) ||
217 IsEqualGUID(This->piid,riid)) {
218 *obj = &This->PVtbl;
219 This->RefCount++;
220 return S_OK;
221 }
222
223 if (IsEqualGUID(&IID_IRpcProxyBuffer,riid)) {
224 *obj = &This->lpVtbl;
225 This->RefCount++;
226 return S_OK;
227 }
228
229 return E_NOINTERFACE;
230 }
231
232 static ULONG WINAPI StdProxy_AddRef(LPRPCPROXYBUFFER iface)
233 {
234 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
235 TRACE("(%p)->AddRef()\n",This);
236
237 return ++(This->RefCount);
238 }
239
240 static ULONG WINAPI StdProxy_Release(LPRPCPROXYBUFFER iface)
241 {
242 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
243 TRACE("(%p)->Release()\n",This);
244
245 if (!--(This->RefCount)) {
246 StdProxy_Destruct((LPRPCPROXYBUFFER)&This->lpVtbl);
247 return 0;
248 }
249 return This->RefCount;
250 }
251
252 static HRESULT WINAPI StdProxy_Connect(LPRPCPROXYBUFFER iface,
253 LPRPCCHANNELBUFFER pChannel)
254 {
255 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
256 TRACE("(%p)->Connect(%p)\n",This,pChannel);
257
258 This->pChannel = pChannel;
259 IRpcChannelBuffer_AddRef(pChannel);
260 return S_OK;
261 }
262
263 static VOID WINAPI StdProxy_Disconnect(LPRPCPROXYBUFFER iface)
264 {
265 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
266 TRACE("(%p)->Disconnect()\n",This);
267
268 IRpcChannelBuffer_Release(This->pChannel);
269 This->pChannel = NULL;
270 }
271
272 static IRpcProxyBufferVtbl StdProxy_Vtbl =
273 {
274 StdProxy_QueryInterface,
275 StdProxy_AddRef,
276 StdProxy_Release,
277 StdProxy_Connect,
278 StdProxy_Disconnect
279 };
280
281 HRESULT WINAPI StdProxy_GetChannel(LPVOID iface,
282 LPRPCCHANNELBUFFER *ppChannel)
283 {
284 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
285 TRACE("(%p)->GetChannel(%p) %s\n",This,ppChannel,This->name);
286
287 *ppChannel = This->pChannel;
288 return S_OK;
289 }
290
291 HRESULT WINAPI StdProxy_GetIID(LPVOID iface,
292 const IID **ppiid)
293 {
294 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
295 TRACE("(%p)->GetIID(%p) %s\n",This,ppiid,This->name);
296
297 *ppiid = This->piid;
298 return S_OK;
299 }
300
301 HRESULT WINAPI IUnknown_QueryInterface_Proxy(LPUNKNOWN iface,
302 REFIID riid,
303 LPVOID *ppvObj)
304 {
305 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
306 TRACE("(%p)->QueryInterface(%s,%p) %s\n",This,debugstr_guid(riid),ppvObj,This->name);
307 return IUnknown_QueryInterface(This->pUnkOuter,riid,ppvObj);
308 }
309
310 ULONG WINAPI IUnknown_AddRef_Proxy(LPUNKNOWN iface)
311 {
312 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
313 TRACE("(%p)->AddRef() %s\n",This,This->name);
314 #if 0 /* interface refcounting */
315 return ++(This->RefCount);
316 #else /* object refcounting */
317 return IUnknown_AddRef(This->pUnkOuter);
318 #endif
319 }
320
321 ULONG WINAPI IUnknown_Release_Proxy(LPUNKNOWN iface)
322 {
323 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
324 TRACE("(%p)->Release() %s\n",This,This->name);
325 #if 0 /* interface refcounting */
326 if (!--(This->RefCount)) {
327 StdProxy_Destruct((LPRPCPROXYBUFFER)&This->lpVtbl);
328 return 0;
329 }
330 return This->RefCount;
331 #else /* object refcounting */
332 return IUnknown_Release(This->pUnkOuter);
333 #endif
334 }
335
336 HRESULT WINAPI
337 CreateProxyFromTypeInfo( LPTYPEINFO pTypeInfo, LPUNKNOWN pUnkOuter, REFIID riid,
338 LPRPCPROXYBUFFER *ppProxy, LPVOID *ppv )
339 {
340 FIXME("%p %p %s %p %p\n", pTypeInfo, pUnkOuter, debugstr_guid(riid), ppProxy, ppv);
341 return E_NOTIMPL;
342 }