Split Win16/32 printer dialogs.
[wine/wine64.git] / dlls / rpcrt4 / cproxy.c
blob82916a61a2c93ec040b9f678cad4cae046cf85b8
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 PCInterfaceName name;
49 LPPSFACTORYBUFFER pPSFactory;
50 LPRPCCHANNELBUFFER pChannel;
51 struct StublessThunk *thunks;
52 } StdProxyImpl;
54 static ICOM_VTABLE(IRpcProxyBuffer) StdProxy_Vtbl;
56 /* How the Windows stubless proxy thunks work is explained at
57 * http://msdn.microsoft.com/library/en-us/dnmsj99/html/com0199.asp,
58 * but I'll use a slightly different method, to make life easier */
60 #if defined(__i386__)
62 struct StublessThunk {
63 BYTE push WINE_PACKED;
64 DWORD index WINE_PACKED;
65 BYTE call WINE_PACKED;
66 LONG handler WINE_PACKED;
67 BYTE ret WINE_PACKED;
68 WORD bytes WINE_PACKED;
69 BYTE pad[3];
72 /* adjust the stack size since we don't use Windows's method */
73 #define STACK_ADJUST sizeof(DWORD)
75 #define FILL_STUBLESS(x,idx,stk) \
76 x->push = 0x68; /* pushl [immediate] */ \
77 x->index = (idx); \
78 x->call = 0xe8; /* call [near] */ \
79 x->handler = (char*)ObjectStubless - (char*)&x->ret; \
80 x->ret = 0xc2; /* ret [immediate] */ \
81 x->bytes = stk; \
82 x->pad[0] = 0x8d; /* leal (%esi),%esi */ \
83 x->pad[1] = 0x76; \
84 x->pad[2] = 0x00;
86 static HRESULT WINAPI ObjectStubless(DWORD index)
88 char *args = (char*)(&index + 2);
89 LPVOID iface = *(LPVOID*)args;
91 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
93 PFORMAT_STRING fs = This->stubless->ProcFormatString + This->stubless->FormatStringOffset[index];
94 unsigned bytes = *(WORD*)(fs+8) - STACK_ADJUST;
95 TRACE("(%p)->(%ld)([%d bytes]) ret=%08lx\n", iface, index, bytes, *(DWORD*)(args+bytes));
97 return RPCRT4_NdrClientCall2(This->stubless->pStubDesc, fs, args);
100 #else /* __i386__ */
102 /* can't do that on this arch */
103 struct StublessThunk { int dummy; };
104 #define FILL_STUBLESS(x,idx,stk) \
105 ERR("stubless proxies are not supported on this architecture\n");
106 #define STACK_ADJUST 0
108 #endif /* __i386__ */
110 HRESULT WINAPI StdProxy_Construct(REFIID riid,
111 LPUNKNOWN pUnkOuter,
112 PCInterfaceName name,
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) %s\n", pUnkOuter, vtbl, pPSFactory, ppProxy, ppvObj, name);
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->name = name;
173 This->pPSFactory = pPSFactory;
174 This->pChannel = NULL;
175 *ppProxy = (LPRPCPROXYBUFFER)&This->lpVtbl;
176 *ppvObj = &This->PVtbl;
177 IPSFactoryBuffer_AddRef(pPSFactory);
179 return S_OK;
182 static void WINAPI StdProxy_Destruct(LPRPCPROXYBUFFER iface)
184 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
186 IPSFactoryBuffer_Release(This->pPSFactory);
187 if (This->thunks) {
188 HeapFree(GetProcessHeap(),0,This->PVtbl);
189 HeapFree(GetProcessHeap(),0,This->thunks);
191 HeapFree(GetProcessHeap(),0,This);
194 static HRESULT WINAPI StdProxy_QueryInterface(LPRPCPROXYBUFFER iface,
195 REFIID riid,
196 LPVOID *obj)
198 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
199 TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
201 if (IsEqualGUID(&IID_IUnknown,riid) ||
202 IsEqualGUID(This->piid,riid)) {
203 *obj = &This->PVtbl;
204 This->RefCount++;
205 return S_OK;
208 if (IsEqualGUID(&IID_IRpcProxyBuffer,riid)) {
209 *obj = &This->lpVtbl;
210 This->RefCount++;
211 return S_OK;
214 return E_NOINTERFACE;
217 static ULONG WINAPI StdProxy_AddRef(LPRPCPROXYBUFFER iface)
219 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
220 TRACE("(%p)->AddRef()\n",This);
222 return ++(This->RefCount);
225 static ULONG WINAPI StdProxy_Release(LPRPCPROXYBUFFER iface)
227 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
228 TRACE("(%p)->Release()\n",This);
230 if (!--(This->RefCount)) {
231 StdProxy_Destruct((LPRPCPROXYBUFFER)&This->lpVtbl);
232 return 0;
234 return This->RefCount;
237 static HRESULT WINAPI StdProxy_Connect(LPRPCPROXYBUFFER iface,
238 LPRPCCHANNELBUFFER pChannel)
240 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
241 TRACE("(%p)->Connect(%p)\n",This,pChannel);
243 This->pChannel = pChannel;
244 return S_OK;
247 static VOID WINAPI StdProxy_Disconnect(LPRPCPROXYBUFFER iface)
249 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
250 TRACE("(%p)->Disconnect()\n",This);
252 This->pChannel = NULL;
255 static ICOM_VTABLE(IRpcProxyBuffer) StdProxy_Vtbl =
257 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
258 StdProxy_QueryInterface,
259 StdProxy_AddRef,
260 StdProxy_Release,
261 StdProxy_Connect,
262 StdProxy_Disconnect
265 HRESULT WINAPI StdProxy_GetChannel(LPVOID iface,
266 LPRPCCHANNELBUFFER *ppChannel)
268 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
269 TRACE("(%p)->GetChannel(%p) %s\n",This,ppChannel,This->name);
271 *ppChannel = This->pChannel;
272 return S_OK;
275 HRESULT WINAPI StdProxy_GetIID(LPVOID iface,
276 const IID **ppiid)
278 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
279 TRACE("(%p)->GetIID(%p) %s\n",This,ppiid,This->name);
281 *ppiid = This->piid;
282 return S_OK;
285 HRESULT WINAPI IUnknown_QueryInterface_Proxy(LPUNKNOWN iface,
286 REFIID riid,
287 LPVOID *ppvObj)
289 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
290 TRACE("(%p)->QueryInterface(%s,%p) %s\n",This,debugstr_guid(riid),ppvObj,This->name);
291 return IUnknown_QueryInterface(This->pUnkOuter,riid,ppvObj);
294 ULONG WINAPI IUnknown_AddRef_Proxy(LPUNKNOWN iface)
296 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
297 TRACE("(%p)->AddRef() %s\n",This,This->name);
298 #if 0 /* interface refcounting */
299 return ++(This->RefCount);
300 #else /* object refcounting */
301 return IUnknown_AddRef(This->pUnkOuter);
302 #endif
305 ULONG WINAPI IUnknown_Release_Proxy(LPUNKNOWN iface)
307 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
308 TRACE("(%p)->Release() %s\n",This,This->name);
309 #if 0 /* interface refcounting */
310 if (!--(This->RefCount)) {
311 StdProxy_Destruct((LPRPCPROXYBUFFER)&This->lpVtbl);
312 return 0;
314 return This->RefCount;
315 #else /* object refcounting */
316 return IUnknown_Release(This->pUnkOuter);
317 #endif