push 2ebcc86255ce7ae7e0c3df6288936164e9e0c175
[wine/hacks.git] / dlls / rpcrt4 / cproxy.c
blob6580bcf143c936f8143ecfb96842b6fcd1aa403a
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * TODO: Handle non-i386 architectures
23 #include <stdarg.h>
25 #define COBJMACROS
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winerror.h"
31 #include "objbase.h"
32 #include "rpcproxy.h"
34 #include "cpsf.h"
35 #include "ndr_misc.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(ole);
40 struct StublessThunk;
42 /* I don't know what MS's std proxy structure looks like,
43 so this probably doesn't match, but that shouldn't matter */
44 typedef struct {
45 const IRpcProxyBufferVtbl *lpVtbl;
46 LPVOID *PVtbl;
47 LONG RefCount;
48 const MIDL_STUBLESS_PROXY_INFO *stubless;
49 const IID* piid;
50 LPUNKNOWN pUnkOuter;
51 PCInterfaceName name;
52 LPPSFACTORYBUFFER pPSFactory;
53 LPRPCCHANNELBUFFER pChannel;
54 struct StublessThunk *thunks;
55 } StdProxyImpl;
57 static const IRpcProxyBufferVtbl StdProxy_Vtbl;
59 #define ICOM_THIS_MULTI(impl,field,iface) impl* const This=(impl*)((char*)(iface) - offsetof(impl,field))
61 /* How the Windows stubless proxy thunks work is explained at
62 * http://msdn.microsoft.com/library/en-us/dnmsj99/html/com0199.asp,
63 * but I'll use a slightly different method, to make life easier */
65 #if defined(__i386__)
67 #include "pshpack1.h"
69 struct StublessThunk {
70 BYTE push;
71 DWORD index;
72 BYTE call;
73 LONG handler;
74 BYTE ret;
75 WORD bytes;
76 BYTE pad[3];
79 #include "poppack.h"
81 /* adjust the stack size since we don't use Windows's method */
82 #define STACK_ADJUST sizeof(DWORD)
84 #define FILL_STUBLESS(x,idx,stk) \
85 x->push = 0x68; /* pushl [immediate] */ \
86 x->index = (idx); \
87 x->call = 0xe8; /* call [near] */ \
88 x->handler = (char*)ObjectStubless - (char*)&x->ret; \
89 x->ret = 0xc2; /* ret [immediate] */ \
90 x->bytes = stk; \
91 x->pad[0] = 0x8d; /* leal (%esi),%esi */ \
92 x->pad[1] = 0x76; \
93 x->pad[2] = 0x00;
95 static HRESULT WINAPI ObjectStubless(DWORD index)
97 char *args = (char*)(&index + 2);
98 LPVOID iface = *(LPVOID*)args;
100 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
102 PFORMAT_STRING fs = This->stubless->ProcFormatString + This->stubless->FormatStringOffset[index];
103 unsigned bytes = *(const WORD*)(fs+8) - STACK_ADJUST;
104 TRACE("(%p)->(%d)([%d bytes]) ret=%08x\n", iface, index, bytes, *(DWORD*)(args+bytes));
106 return NdrClientCall2(This->stubless->pStubDesc, fs, args);
109 #else /* __i386__ */
111 /* can't do that on this arch */
112 struct StublessThunk { int dummy; };
113 #define FILL_STUBLESS(x,idx,stk) \
114 ERR("stubless proxies are not supported on this architecture\n");
115 #define STACK_ADJUST 0
117 #endif /* __i386__ */
119 HRESULT WINAPI StdProxy_Construct(REFIID riid,
120 LPUNKNOWN pUnkOuter,
121 const ProxyFileInfo *ProxyInfo,
122 int Index,
123 LPPSFACTORYBUFFER pPSFactory,
124 LPRPCPROXYBUFFER *ppProxy,
125 LPVOID *ppvObj)
127 StdProxyImpl *This;
128 const MIDL_STUBLESS_PROXY_INFO *stubless = NULL;
129 PCInterfaceName name = ProxyInfo->pNamesArray[Index];
130 CInterfaceProxyVtbl *vtbl = ProxyInfo->pProxyVtblList[Index];
132 TRACE("(%p,%p,%p,%p,%p) %s\n", pUnkOuter, vtbl, pPSFactory, ppProxy, ppvObj, name);
134 /* TableVersion = 2 means it is the stubless version of CInterfaceProxyVtbl */
135 if (ProxyInfo->TableVersion > 1) {
136 stubless = *(const void **)vtbl;
137 vtbl = (CInterfaceProxyVtbl *)((const void **)vtbl + 1);
138 TRACE("stubless=%p\n", stubless);
141 TRACE("iid=%s\n", debugstr_guid(vtbl->header.piid));
142 TRACE("vtbl=%p\n", vtbl->Vtbl);
144 if (!IsEqualGUID(vtbl->header.piid, riid)) {
145 ERR("IID mismatch during proxy creation\n");
146 return RPC_E_UNEXPECTED;
149 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(StdProxyImpl));
150 if (!This) return E_OUTOFMEMORY;
152 if (stubless) {
153 CInterfaceStubVtbl *svtbl = ProxyInfo->pStubVtblList[Index];
154 unsigned long i, count = svtbl->header.DispatchTableCount;
155 /* Maybe the original vtbl is just modified directly to point at
156 * ObjectStublessClientXXX thunks in real Windows, but I don't like it
158 TRACE("stubless thunks: count=%ld\n", count);
159 This->thunks = HeapAlloc(GetProcessHeap(),0,sizeof(struct StublessThunk)*count);
160 This->PVtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPVOID)*count);
161 for (i=0; i<count; i++) {
162 struct StublessThunk *thunk = &This->thunks[i];
163 if (vtbl->Vtbl[i] == (LPVOID)-1) {
164 PFORMAT_STRING fs = stubless->ProcFormatString + stubless->FormatStringOffset[i];
165 unsigned bytes = *(const WORD*)(fs+8) - STACK_ADJUST;
166 TRACE("method %ld: stacksize=%d\n", i, bytes);
167 FILL_STUBLESS(thunk, i, bytes)
168 This->PVtbl[i] = thunk;
170 else {
171 memset(thunk, 0, sizeof(struct StublessThunk));
172 This->PVtbl[i] = vtbl->Vtbl[i];
176 else
177 This->PVtbl = vtbl->Vtbl;
179 This->lpVtbl = &StdProxy_Vtbl;
180 /* one reference for the proxy */
181 This->RefCount = 1;
182 This->stubless = stubless;
183 This->piid = vtbl->header.piid;
184 This->pUnkOuter = pUnkOuter;
185 This->name = name;
186 This->pPSFactory = pPSFactory;
187 This->pChannel = NULL;
188 *ppProxy = (LPRPCPROXYBUFFER)&This->lpVtbl;
189 *ppvObj = &This->PVtbl;
190 /* if there is no outer unknown then the caller will control the lifetime
191 * of the proxy object through the proxy buffer, so no need to increment the
192 * ref count of the proxy object */
193 if (pUnkOuter)
194 IUnknown_AddRef((IUnknown *)*ppvObj);
195 IPSFactoryBuffer_AddRef(pPSFactory);
197 return S_OK;
200 static void WINAPI StdProxy_Destruct(LPRPCPROXYBUFFER iface)
202 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
204 if (This->pChannel)
205 IRpcProxyBuffer_Disconnect(iface);
207 IPSFactoryBuffer_Release(This->pPSFactory);
208 if (This->thunks) {
209 HeapFree(GetProcessHeap(),0,This->PVtbl);
210 HeapFree(GetProcessHeap(),0,This->thunks);
212 HeapFree(GetProcessHeap(),0,This);
215 static HRESULT WINAPI StdProxy_QueryInterface(LPRPCPROXYBUFFER iface,
216 REFIID riid,
217 LPVOID *obj)
219 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
220 TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
222 if (IsEqualGUID(&IID_IUnknown,riid) ||
223 IsEqualGUID(This->piid,riid)) {
224 *obj = &This->PVtbl;
225 InterlockedIncrement(&This->RefCount);
226 return S_OK;
229 if (IsEqualGUID(&IID_IRpcProxyBuffer,riid)) {
230 *obj = &This->lpVtbl;
231 InterlockedIncrement(&This->RefCount);
232 return S_OK;
235 return E_NOINTERFACE;
238 static ULONG WINAPI StdProxy_AddRef(LPRPCPROXYBUFFER iface)
240 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
241 TRACE("(%p)->AddRef()\n",This);
243 return InterlockedIncrement(&This->RefCount);
246 static ULONG WINAPI StdProxy_Release(LPRPCPROXYBUFFER iface)
248 ULONG refs;
249 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
250 TRACE("(%p)->Release()\n",This);
252 refs = InterlockedDecrement(&This->RefCount);
253 if (!refs)
254 StdProxy_Destruct((LPRPCPROXYBUFFER)&This->lpVtbl);
255 return refs;
258 static HRESULT WINAPI StdProxy_Connect(LPRPCPROXYBUFFER iface,
259 LPRPCCHANNELBUFFER pChannel)
261 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
262 TRACE("(%p)->Connect(%p)\n",This,pChannel);
264 This->pChannel = pChannel;
265 IRpcChannelBuffer_AddRef(pChannel);
266 return S_OK;
269 static VOID WINAPI StdProxy_Disconnect(LPRPCPROXYBUFFER iface)
271 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
272 TRACE("(%p)->Disconnect()\n",This);
274 IRpcChannelBuffer_Release(This->pChannel);
275 This->pChannel = NULL;
278 static const IRpcProxyBufferVtbl StdProxy_Vtbl =
280 StdProxy_QueryInterface,
281 StdProxy_AddRef,
282 StdProxy_Release,
283 StdProxy_Connect,
284 StdProxy_Disconnect
287 static void StdProxy_GetChannel(LPVOID iface,
288 LPRPCCHANNELBUFFER *ppChannel)
290 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
291 TRACE("(%p)->GetChannel(%p) %s\n",This,ppChannel,This->name);
293 *ppChannel = This->pChannel;
296 static void StdProxy_GetIID(LPVOID iface,
297 const IID **ppiid)
299 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
300 TRACE("(%p)->GetIID(%p) %s\n",This,ppiid,This->name);
302 *ppiid = This->piid;
305 HRESULT WINAPI IUnknown_QueryInterface_Proxy(LPUNKNOWN iface,
306 REFIID riid,
307 LPVOID *ppvObj)
309 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
310 TRACE("(%p)->QueryInterface(%s,%p) %s\n",This,debugstr_guid(riid),ppvObj,This->name);
311 return IUnknown_QueryInterface(This->pUnkOuter,riid,ppvObj);
314 ULONG WINAPI IUnknown_AddRef_Proxy(LPUNKNOWN iface)
316 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
317 TRACE("(%p)->AddRef() %s\n",This,This->name);
318 return IUnknown_AddRef(This->pUnkOuter);
321 ULONG WINAPI IUnknown_Release_Proxy(LPUNKNOWN iface)
323 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
324 TRACE("(%p)->Release() %s\n",This,This->name);
325 return IUnknown_Release(This->pUnkOuter);
328 /***********************************************************************
329 * NdrProxyInitialize [RPCRT4.@]
331 void WINAPI NdrProxyInitialize(void *This,
332 PRPC_MESSAGE pRpcMsg,
333 PMIDL_STUB_MESSAGE pStubMsg,
334 PMIDL_STUB_DESC pStubDescriptor,
335 unsigned int ProcNum)
337 TRACE("(%p,%p,%p,%p,%d)\n", This, pRpcMsg, pStubMsg, pStubDescriptor, ProcNum);
338 NdrClientInitializeNew(pRpcMsg, pStubMsg, pStubDescriptor, ProcNum);
339 StdProxy_GetChannel(This, &pStubMsg->pRpcChannelBuffer);
340 IRpcChannelBuffer_GetDestCtx(pStubMsg->pRpcChannelBuffer,
341 &pStubMsg->dwDestContext,
342 &pStubMsg->pvDestContext);
343 TRACE("channel=%p\n", pStubMsg->pRpcChannelBuffer);
346 /***********************************************************************
347 * NdrProxyGetBuffer [RPCRT4.@]
349 void WINAPI NdrProxyGetBuffer(void *This,
350 PMIDL_STUB_MESSAGE pStubMsg)
352 HRESULT hr;
353 const IID *riid = NULL;
355 TRACE("(%p,%p)\n", This, pStubMsg);
356 pStubMsg->RpcMsg->BufferLength = pStubMsg->BufferLength;
357 pStubMsg->dwStubPhase = PROXY_GETBUFFER;
358 StdProxy_GetIID(This, &riid);
359 hr = IRpcChannelBuffer_GetBuffer(pStubMsg->pRpcChannelBuffer,
360 (RPCOLEMESSAGE*)pStubMsg->RpcMsg,
361 riid);
362 if (FAILED(hr))
364 RpcRaiseException(hr);
365 return;
367 pStubMsg->BufferStart = pStubMsg->RpcMsg->Buffer;
368 pStubMsg->BufferEnd = pStubMsg->BufferStart + pStubMsg->BufferLength;
369 pStubMsg->Buffer = pStubMsg->BufferStart;
370 pStubMsg->dwStubPhase = PROXY_MARSHAL;
373 /***********************************************************************
374 * NdrProxySendReceive [RPCRT4.@]
376 void WINAPI NdrProxySendReceive(void *This,
377 PMIDL_STUB_MESSAGE pStubMsg)
379 ULONG Status = 0;
380 HRESULT hr;
382 TRACE("(%p,%p)\n", This, pStubMsg);
384 if (!pStubMsg->pRpcChannelBuffer)
386 WARN("Trying to use disconnected proxy %p\n", This);
387 RpcRaiseException(RPC_E_DISCONNECTED);
390 pStubMsg->dwStubPhase = PROXY_SENDRECEIVE;
391 hr = IRpcChannelBuffer_SendReceive(pStubMsg->pRpcChannelBuffer,
392 (RPCOLEMESSAGE*)pStubMsg->RpcMsg,
393 &Status);
394 pStubMsg->dwStubPhase = PROXY_UNMARSHAL;
395 pStubMsg->BufferLength = pStubMsg->RpcMsg->BufferLength;
396 pStubMsg->BufferStart = pStubMsg->RpcMsg->Buffer;
397 pStubMsg->BufferEnd = pStubMsg->BufferStart + pStubMsg->BufferLength;
398 pStubMsg->Buffer = pStubMsg->BufferStart;
400 /* raise exception if call failed */
401 if (hr == RPC_S_CALL_FAILED) RpcRaiseException(*(DWORD*)pStubMsg->Buffer);
402 else if (FAILED(hr)) RpcRaiseException(hr);
405 /***********************************************************************
406 * NdrProxyFreeBuffer [RPCRT4.@]
408 void WINAPI NdrProxyFreeBuffer(void *This,
409 PMIDL_STUB_MESSAGE pStubMsg)
411 HRESULT hr;
413 TRACE("(%p,%p)\n", This, pStubMsg);
414 hr = IRpcChannelBuffer_FreeBuffer(pStubMsg->pRpcChannelBuffer,
415 (RPCOLEMESSAGE*)pStubMsg->RpcMsg);
418 /***********************************************************************
419 * NdrProxyErrorHandler [RPCRT4.@]
421 HRESULT WINAPI NdrProxyErrorHandler(DWORD dwExceptionCode)
423 WARN("(0x%08x): a proxy call failed\n", dwExceptionCode);
425 if (FAILED(dwExceptionCode))
426 return dwExceptionCode;
427 else
428 return HRESULT_FROM_WIN32(dwExceptionCode);
431 HRESULT WINAPI
432 CreateProxyFromTypeInfo( LPTYPEINFO pTypeInfo, LPUNKNOWN pUnkOuter, REFIID riid,
433 LPRPCPROXYBUFFER *ppProxy, LPVOID *ppv )
435 typedef INT (WINAPI *MessageBoxA)(HWND,LPCSTR,LPCSTR,UINT);
436 HMODULE hUser32 = LoadLibraryA("user32");
437 MessageBoxA pMessageBoxA = (void *)GetProcAddress(hUser32, "MessageBoxA");
439 FIXME("%p %p %s %p %p\n", pTypeInfo, pUnkOuter, debugstr_guid(riid), ppProxy, ppv);
440 if (pMessageBoxA)
442 pMessageBoxA(NULL,
443 "The native implementation of OLEAUT32.DLL cannot be used "
444 "with Wine's RPCRT4.DLL. Remove OLEAUT32.DLL and try again.\n",
445 "Wine: Unimplemented CreateProxyFromTypeInfo",
446 0x10);
447 ExitProcess(1);
449 return E_NOTIMPL;