push cc8bc80451cc24f4d7cf75168b569f0ebfe19547
[wine/hacks.git] / dlls / rpcrt4 / cproxy.c
blob0887aa2399bf18a09e9c2103e1054958e5104ffa
1 /*
2 * COM proxy implementation
4 * Copyright 2001 Ove Kåven, TransGaming Technologies
5 * Copyright 2009 Alexandre Julliard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * TODO: Handle non-i386 architectures
24 #include "config.h"
25 #include "wine/port.h"
27 #include <stdarg.h>
29 #define COBJMACROS
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winerror.h"
35 #include "objbase.h"
36 #include "rpcproxy.h"
38 #include "cpsf.h"
39 #include "ndr_misc.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(ole);
44 /* I don't know what MS's std proxy structure looks like,
45 so this probably doesn't match, but that shouldn't matter */
46 typedef struct {
47 const IRpcProxyBufferVtbl *lpVtbl;
48 LPVOID *PVtbl;
49 LONG RefCount;
50 const IID* piid;
51 LPUNKNOWN pUnkOuter;
52 IUnknown *base_object; /* must be at offset 0x10 from PVtbl */
53 IRpcProxyBuffer *base_proxy;
54 PCInterfaceName name;
55 LPPSFACTORYBUFFER pPSFactory;
56 LPRPCCHANNELBUFFER pChannel;
57 } StdProxyImpl;
59 static const IRpcProxyBufferVtbl StdProxy_Vtbl;
61 #define ICOM_THIS_MULTI(impl,field,iface) impl* const This=(impl*)((char*)(iface) - offsetof(impl,field))
63 #if defined(__i386__)
65 #include "pshpack1.h"
67 struct thunk {
68 BYTE push;
69 DWORD index;
70 BYTE jmp;
71 LONG handler;
74 #include "poppack.h"
76 extern void call_stubless_func(void);
77 __ASM_GLOBAL_FUNC(call_stubless_func,
78 "pushl %esp\n\t" /* pointer to index */
79 "call " __ASM_NAME("ObjectStubless") __ASM_STDCALL(4) "\n\t"
80 "popl %edx\n\t" /* args size */
81 "movl (%esp),%ecx\n\t" /* return address */
82 "addl %edx,%esp\n\t"
83 "jmp *%ecx" );
85 HRESULT WINAPI ObjectStubless(DWORD *args)
87 DWORD index = args[0];
88 void **iface = (void **)args[2];
89 const void **vtbl = (const void **)*iface;
90 const MIDL_STUBLESS_PROXY_INFO *stubless = *(const MIDL_STUBLESS_PROXY_INFO **)(vtbl - 2);
91 const PFORMAT_STRING fs = stubless->ProcFormatString + stubless->FormatStringOffset[index];
93 /* store bytes to remove from stack */
94 args[0] = *(const WORD*)(fs + 8);
95 TRACE("(%p)->(%d)([%d bytes]) ret=%08x\n", iface, index, args[0], args[1]);
97 return NdrClientCall2(stubless->pStubDesc, fs, args + 2);
100 #define BLOCK_SIZE 1024
101 #define MAX_BLOCKS 64 /* 64k methods should be enough for anybody */
103 static const struct thunk *method_blocks[MAX_BLOCKS];
105 static const struct thunk *allocate_block( unsigned int num )
107 unsigned int i;
108 struct thunk *prev, *block;
110 block = VirtualAlloc( NULL, BLOCK_SIZE * sizeof(*block),
111 MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE );
112 if (!block) return NULL;
114 for (i = 0; i < BLOCK_SIZE; i++)
116 block[i].push = 0x68; /* pushl */
117 block[i].index = BLOCK_SIZE * num + i + 3;
118 block[i].jmp = 0xe9; /* jmp */
119 block[i].handler = (char *)call_stubless_func - (char *)(&block[i].handler + 1);
121 VirtualProtect( block, BLOCK_SIZE * sizeof(*block), PAGE_EXECUTE_READ, NULL );
122 prev = InterlockedCompareExchangePointer( (void **)&method_blocks[num], block, NULL );
123 if (prev) /* someone beat us to it */
125 VirtualFree( block, 0, MEM_RELEASE );
126 block = prev;
128 return block;
131 static BOOL fill_stubless_table( IUnknownVtbl *vtbl, DWORD num )
133 const void **entry = (const void **)(vtbl + 1);
134 DWORD i, j;
136 if (num - 3 > BLOCK_SIZE * MAX_BLOCKS)
138 FIXME( "%u methods not supported\n", num );
139 return FALSE;
141 for (i = 0; i < (num - 3 + BLOCK_SIZE - 1) / BLOCK_SIZE; i++)
143 const struct thunk *block = method_blocks[i];
144 if (!block && !(block = allocate_block( i ))) return FALSE;
145 for (j = 0; j < BLOCK_SIZE && j < num - 3 - i * BLOCK_SIZE; j++, entry++)
146 if (*entry == (LPVOID)-1) *entry = &block[j];
148 return TRUE;
151 #else /* __i386__ */
153 static BOOL fill_stubless_table( IUnknownVtbl *vtbl, DWORD num )
155 ERR("stubless proxies are not supported on this architecture\n");
156 return FALSE;
159 #endif /* __i386__ */
161 HRESULT StdProxy_Construct(REFIID riid,
162 LPUNKNOWN pUnkOuter,
163 const ProxyFileInfo *ProxyInfo,
164 int Index,
165 LPPSFACTORYBUFFER pPSFactory,
166 LPRPCPROXYBUFFER *ppProxy,
167 LPVOID *ppvObj)
169 StdProxyImpl *This;
170 PCInterfaceName name = ProxyInfo->pNamesArray[Index];
171 CInterfaceProxyVtbl *vtbl = ProxyInfo->pProxyVtblList[Index];
173 TRACE("(%p,%p,%p,%p,%p) %s\n", pUnkOuter, vtbl, pPSFactory, ppProxy, ppvObj, name);
175 /* TableVersion = 2 means it is the stubless version of CInterfaceProxyVtbl */
176 if (ProxyInfo->TableVersion > 1) {
177 ULONG count = ProxyInfo->pStubVtblList[Index]->header.DispatchTableCount;
178 vtbl = (CInterfaceProxyVtbl *)((const void **)vtbl + 1);
179 TRACE("stubless vtbl %p: count=%d\n", vtbl->Vtbl, count );
180 fill_stubless_table( (IUnknownVtbl *)vtbl->Vtbl, count );
183 if (!IsEqualGUID(vtbl->header.piid, riid)) {
184 ERR("IID mismatch during proxy creation\n");
185 return RPC_E_UNEXPECTED;
188 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(StdProxyImpl));
189 if (!This) return E_OUTOFMEMORY;
191 if (!pUnkOuter) pUnkOuter = (IUnknown *)This;
192 This->lpVtbl = &StdProxy_Vtbl;
193 This->PVtbl = vtbl->Vtbl;
194 /* one reference for the proxy */
195 This->RefCount = 1;
196 This->piid = vtbl->header.piid;
197 This->base_object = NULL;
198 This->base_proxy = NULL;
199 This->pUnkOuter = pUnkOuter;
200 This->name = name;
201 This->pPSFactory = pPSFactory;
202 This->pChannel = NULL;
204 if(ProxyInfo->pDelegatedIIDs && ProxyInfo->pDelegatedIIDs[Index])
206 HRESULT r = create_proxy( ProxyInfo->pDelegatedIIDs[Index], NULL,
207 &This->base_proxy, (void **)&This->base_object );
208 if (FAILED(r))
210 HeapFree( GetProcessHeap(), 0, This );
211 return r;
215 *ppProxy = (LPRPCPROXYBUFFER)&This->lpVtbl;
216 *ppvObj = &This->PVtbl;
217 IUnknown_AddRef((IUnknown *)*ppvObj);
218 IPSFactoryBuffer_AddRef(pPSFactory);
220 TRACE( "iid=%s this %p proxy %p obj %p vtbl %p base proxy %p base obj %p\n",
221 debugstr_guid(riid), This, *ppProxy, *ppvObj, This->PVtbl, This->base_proxy, This->base_object );
222 return S_OK;
225 static void StdProxy_Destruct(LPRPCPROXYBUFFER iface)
227 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
229 if (This->pChannel)
230 IRpcProxyBuffer_Disconnect(iface);
232 if (This->base_object) IUnknown_Release( This->base_object );
233 if (This->base_proxy) IRpcProxyBuffer_Release( This->base_proxy );
235 IPSFactoryBuffer_Release(This->pPSFactory);
236 HeapFree(GetProcessHeap(),0,This);
239 static HRESULT WINAPI StdProxy_QueryInterface(LPRPCPROXYBUFFER iface,
240 REFIID riid,
241 LPVOID *obj)
243 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
244 TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
246 if (IsEqualGUID(&IID_IUnknown,riid) ||
247 IsEqualGUID(This->piid,riid)) {
248 *obj = &This->PVtbl;
249 InterlockedIncrement(&This->RefCount);
250 return S_OK;
253 if (IsEqualGUID(&IID_IRpcProxyBuffer,riid)) {
254 *obj = &This->lpVtbl;
255 InterlockedIncrement(&This->RefCount);
256 return S_OK;
259 return E_NOINTERFACE;
262 static ULONG WINAPI StdProxy_AddRef(LPRPCPROXYBUFFER iface)
264 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
265 TRACE("(%p)->AddRef()\n",This);
267 return InterlockedIncrement(&This->RefCount);
270 static ULONG WINAPI StdProxy_Release(LPRPCPROXYBUFFER iface)
272 ULONG refs;
273 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
274 TRACE("(%p)->Release()\n",This);
276 refs = InterlockedDecrement(&This->RefCount);
277 if (!refs)
278 StdProxy_Destruct((LPRPCPROXYBUFFER)&This->lpVtbl);
279 return refs;
282 static HRESULT WINAPI StdProxy_Connect(LPRPCPROXYBUFFER iface,
283 LPRPCCHANNELBUFFER pChannel)
285 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
286 TRACE("(%p)->Connect(%p)\n",This,pChannel);
288 This->pChannel = pChannel;
289 IRpcChannelBuffer_AddRef(pChannel);
290 if (This->base_proxy) IRpcProxyBuffer_Connect( This->base_proxy, pChannel );
291 return S_OK;
294 static VOID WINAPI StdProxy_Disconnect(LPRPCPROXYBUFFER iface)
296 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
297 TRACE("(%p)->Disconnect()\n",This);
299 if (This->base_proxy) IRpcProxyBuffer_Disconnect( This->base_proxy );
301 IRpcChannelBuffer_Release(This->pChannel);
302 This->pChannel = NULL;
305 static const IRpcProxyBufferVtbl StdProxy_Vtbl =
307 StdProxy_QueryInterface,
308 StdProxy_AddRef,
309 StdProxy_Release,
310 StdProxy_Connect,
311 StdProxy_Disconnect
314 static void StdProxy_GetChannel(LPVOID iface,
315 LPRPCCHANNELBUFFER *ppChannel)
317 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
318 TRACE("(%p)->GetChannel(%p) %s\n",This,ppChannel,This->name);
320 *ppChannel = This->pChannel;
323 static void StdProxy_GetIID(LPVOID iface,
324 const IID **ppiid)
326 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
327 TRACE("(%p)->GetIID(%p) %s\n",This,ppiid,This->name);
329 *ppiid = This->piid;
332 HRESULT WINAPI IUnknown_QueryInterface_Proxy(LPUNKNOWN iface,
333 REFIID riid,
334 LPVOID *ppvObj)
336 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
337 TRACE("(%p)->QueryInterface(%s,%p) %s\n",This,debugstr_guid(riid),ppvObj,This->name);
338 return IUnknown_QueryInterface(This->pUnkOuter,riid,ppvObj);
341 ULONG WINAPI IUnknown_AddRef_Proxy(LPUNKNOWN iface)
343 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
344 TRACE("(%p)->AddRef() %s\n",This,This->name);
345 return IUnknown_AddRef(This->pUnkOuter);
348 ULONG WINAPI IUnknown_Release_Proxy(LPUNKNOWN iface)
350 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
351 TRACE("(%p)->Release() %s\n",This,This->name);
352 return IUnknown_Release(This->pUnkOuter);
355 /***********************************************************************
356 * NdrProxyInitialize [RPCRT4.@]
358 void WINAPI NdrProxyInitialize(void *This,
359 PRPC_MESSAGE pRpcMsg,
360 PMIDL_STUB_MESSAGE pStubMsg,
361 PMIDL_STUB_DESC pStubDescriptor,
362 unsigned int ProcNum)
364 TRACE("(%p,%p,%p,%p,%d)\n", This, pRpcMsg, pStubMsg, pStubDescriptor, ProcNum);
365 NdrClientInitializeNew(pRpcMsg, pStubMsg, pStubDescriptor, ProcNum);
366 StdProxy_GetChannel(This, &pStubMsg->pRpcChannelBuffer);
367 IRpcChannelBuffer_GetDestCtx(pStubMsg->pRpcChannelBuffer,
368 &pStubMsg->dwDestContext,
369 &pStubMsg->pvDestContext);
370 TRACE("channel=%p\n", pStubMsg->pRpcChannelBuffer);
373 /***********************************************************************
374 * NdrProxyGetBuffer [RPCRT4.@]
376 void WINAPI NdrProxyGetBuffer(void *This,
377 PMIDL_STUB_MESSAGE pStubMsg)
379 HRESULT hr;
380 const IID *riid = NULL;
382 TRACE("(%p,%p)\n", This, pStubMsg);
383 pStubMsg->RpcMsg->BufferLength = pStubMsg->BufferLength;
384 pStubMsg->dwStubPhase = PROXY_GETBUFFER;
385 StdProxy_GetIID(This, &riid);
386 hr = IRpcChannelBuffer_GetBuffer(pStubMsg->pRpcChannelBuffer,
387 (RPCOLEMESSAGE*)pStubMsg->RpcMsg,
388 riid);
389 if (FAILED(hr))
391 RpcRaiseException(hr);
392 return;
394 pStubMsg->fBufferValid = TRUE;
395 pStubMsg->BufferStart = pStubMsg->RpcMsg->Buffer;
396 pStubMsg->BufferEnd = pStubMsg->BufferStart + pStubMsg->BufferLength;
397 pStubMsg->Buffer = pStubMsg->BufferStart;
398 pStubMsg->dwStubPhase = PROXY_MARSHAL;
401 /***********************************************************************
402 * NdrProxySendReceive [RPCRT4.@]
404 void WINAPI NdrProxySendReceive(void *This,
405 PMIDL_STUB_MESSAGE pStubMsg)
407 ULONG Status = 0;
408 HRESULT hr;
410 TRACE("(%p,%p)\n", This, pStubMsg);
412 if (!pStubMsg->pRpcChannelBuffer)
414 WARN("Trying to use disconnected proxy %p\n", This);
415 RpcRaiseException(RPC_E_DISCONNECTED);
418 pStubMsg->dwStubPhase = PROXY_SENDRECEIVE;
419 /* avoid sending uninitialised parts of the buffer on the wire */
420 pStubMsg->RpcMsg->BufferLength = pStubMsg->Buffer - (unsigned char *)pStubMsg->RpcMsg->Buffer;
421 hr = IRpcChannelBuffer_SendReceive(pStubMsg->pRpcChannelBuffer,
422 (RPCOLEMESSAGE*)pStubMsg->RpcMsg,
423 &Status);
424 pStubMsg->dwStubPhase = PROXY_UNMARSHAL;
425 pStubMsg->BufferLength = pStubMsg->RpcMsg->BufferLength;
426 pStubMsg->BufferStart = pStubMsg->RpcMsg->Buffer;
427 pStubMsg->BufferEnd = pStubMsg->BufferStart + pStubMsg->BufferLength;
428 pStubMsg->Buffer = pStubMsg->BufferStart;
430 /* raise exception if call failed */
431 if (hr == RPC_S_CALL_FAILED) RpcRaiseException(*(DWORD*)pStubMsg->Buffer);
432 else if (FAILED(hr)) RpcRaiseException(hr);
435 /***********************************************************************
436 * NdrProxyFreeBuffer [RPCRT4.@]
438 void WINAPI NdrProxyFreeBuffer(void *This,
439 PMIDL_STUB_MESSAGE pStubMsg)
441 TRACE("(%p,%p)\n", This, pStubMsg);
443 if (pStubMsg->fBufferValid)
445 IRpcChannelBuffer_FreeBuffer(pStubMsg->pRpcChannelBuffer,
446 (RPCOLEMESSAGE*)pStubMsg->RpcMsg);
447 pStubMsg->fBufferValid = TRUE;
451 /***********************************************************************
452 * NdrProxyErrorHandler [RPCRT4.@]
454 HRESULT WINAPI NdrProxyErrorHandler(DWORD dwExceptionCode)
456 WARN("(0x%08x): a proxy call failed\n", dwExceptionCode);
458 if (FAILED(dwExceptionCode))
459 return dwExceptionCode;
460 else
461 return HRESULT_FROM_WIN32(dwExceptionCode);
464 HRESULT WINAPI
465 CreateProxyFromTypeInfo( LPTYPEINFO pTypeInfo, LPUNKNOWN pUnkOuter, REFIID riid,
466 LPRPCPROXYBUFFER *ppProxy, LPVOID *ppv )
468 typedef INT (WINAPI *MessageBoxA)(HWND,LPCSTR,LPCSTR,UINT);
469 HMODULE hUser32 = LoadLibraryA("user32");
470 MessageBoxA pMessageBoxA = (void *)GetProcAddress(hUser32, "MessageBoxA");
472 FIXME("%p %p %s %p %p\n", pTypeInfo, pUnkOuter, debugstr_guid(riid), ppProxy, ppv);
473 if (pMessageBoxA)
475 pMessageBoxA(NULL,
476 "The native implementation of OLEAUT32.DLL cannot be used "
477 "with Wine's RPCRT4.DLL. Remove OLEAUT32.DLL and try again.\n",
478 "Wine: Unimplemented CreateProxyFromTypeInfo",
479 0x10);
480 ExitProcess(1);
482 return E_NOTIMPL;