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
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(ole);
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 */
45 const IRpcProxyBufferVtbl *lpVtbl;
48 const MIDL_STUBLESS_PROXY_INFO *stubless;
52 LPPSFACTORYBUFFER pPSFactory;
53 LPRPCCHANNELBUFFER pChannel;
54 struct StublessThunk *thunks;
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 */
69 struct StublessThunk {
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] */ \
87 x->call = 0xe8; /* call [near] */ \
88 x->handler = (char*)ObjectStubless - (char*)&x->ret; \
89 x->ret = 0xc2; /* ret [immediate] */ \
91 x->pad[0] = 0x8d; /* leal (%esi),%esi */ \
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);
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,
121 const ProxyFileInfo *ProxyInfo,
123 LPPSFACTORYBUFFER pPSFactory,
124 LPRPCPROXYBUFFER *ppProxy,
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;
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;
171 memset(thunk, 0, sizeof(struct StublessThunk));
172 This->PVtbl[i] = vtbl->Vtbl[i];
177 This->PVtbl = vtbl->Vtbl;
179 This->lpVtbl = &StdProxy_Vtbl;
180 /* one reference for the proxy */
182 This->stubless = stubless;
183 This->piid = vtbl->header.piid;
184 This->pUnkOuter = pUnkOuter;
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 */
194 IUnknown_AddRef((IUnknown *)*ppvObj);
195 IPSFactoryBuffer_AddRef(pPSFactory);
200 static void WINAPI StdProxy_Destruct(LPRPCPROXYBUFFER iface)
202 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
205 IRpcProxyBuffer_Disconnect(iface);
207 IPSFactoryBuffer_Release(This->pPSFactory);
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,
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)) {
225 InterlockedIncrement(&This->RefCount);
229 if (IsEqualGUID(&IID_IRpcProxyBuffer,riid)) {
230 *obj = &This->lpVtbl;
231 InterlockedIncrement(&This->RefCount);
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)
249 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
250 TRACE("(%p)->Release()\n",This);
252 refs = InterlockedDecrement(&This->RefCount);
254 StdProxy_Destruct((LPRPCPROXYBUFFER)&This->lpVtbl);
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);
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,
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,
299 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
300 TRACE("(%p)->GetIID(%p) %s\n",This,ppiid,This->name);
305 HRESULT WINAPI IUnknown_QueryInterface_Proxy(LPUNKNOWN iface,
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)
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,
364 RpcRaiseException(hr);
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)
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,
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)
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;
428 return HRESULT_FROM_WIN32(dwExceptionCode);
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 = GetProcAddress(hUser32, "MessageBoxA");
439 FIXME("%p %p %s %p %p\n", pTypeInfo, pUnkOuter, debugstr_guid(riid), ppProxy, ppv);
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",