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))
65 struct StublessThunk
{
77 /* adjust the stack size since we don't use Windows's method */
78 #define STACK_ADJUST sizeof(DWORD)
80 #define FILL_STUBLESS(x,idx,stk) \
81 x->push = 0x68; /* pushl [immediate] */ \
83 x->call = 0xe8; /* call [near] */ \
84 x->handler = (char*)ObjectStubless - (char*)&x->ret; \
85 x->ret = 0xc2; /* ret [immediate] */ \
87 x->pad[0] = 0x8d; /* leal (%esi),%esi */ \
91 static HRESULT WINAPI
ObjectStubless(DWORD index
)
93 char *args
= (char*)(&index
+ 2);
94 LPVOID iface
= *(LPVOID
*)args
;
96 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
98 PFORMAT_STRING fs
= This
->stubless
->ProcFormatString
+ This
->stubless
->FormatStringOffset
[index
];
99 unsigned bytes
= *(const WORD
*)(fs
+8) - STACK_ADJUST
;
100 TRACE("(%p)->(%d)([%d bytes]) ret=%08x\n", iface
, index
, bytes
, *(DWORD
*)(args
+bytes
));
102 return NdrClientCall2(This
->stubless
->pStubDesc
, fs
, args
);
107 /* can't do that on this arch */
108 struct StublessThunk
{ int dummy
; };
109 #define FILL_STUBLESS(x,idx,stk) \
110 ERR("stubless proxies are not supported on this architecture\n");
111 #define STACK_ADJUST 0
113 #endif /* __i386__ */
115 HRESULT WINAPI
StdProxy_Construct(REFIID riid
,
117 const ProxyFileInfo
*ProxyInfo
,
119 LPPSFACTORYBUFFER pPSFactory
,
120 LPRPCPROXYBUFFER
*ppProxy
,
124 const MIDL_STUBLESS_PROXY_INFO
*stubless
= NULL
;
125 PCInterfaceName name
= ProxyInfo
->pNamesArray
[Index
];
126 CInterfaceProxyVtbl
*vtbl
= ProxyInfo
->pProxyVtblList
[Index
];
128 TRACE("(%p,%p,%p,%p,%p) %s\n", pUnkOuter
, vtbl
, pPSFactory
, ppProxy
, ppvObj
, name
);
130 /* TableVersion = 2 means it is the stubless version of CInterfaceProxyVtbl */
131 if (ProxyInfo
->TableVersion
> 1) {
132 stubless
= *(const void **)vtbl
;
133 vtbl
= (CInterfaceProxyVtbl
*)((const void **)vtbl
+ 1);
134 TRACE("stubless=%p\n", stubless
);
137 TRACE("iid=%s\n", debugstr_guid(vtbl
->header
.piid
));
138 TRACE("vtbl=%p\n", vtbl
->Vtbl
);
140 if (!IsEqualGUID(vtbl
->header
.piid
, riid
)) {
141 ERR("IID mismatch during proxy creation\n");
142 return RPC_E_UNEXPECTED
;
145 This
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(StdProxyImpl
));
146 if (!This
) return E_OUTOFMEMORY
;
149 CInterfaceStubVtbl
*svtbl
= ProxyInfo
->pStubVtblList
[Index
];
150 unsigned long i
, count
= svtbl
->header
.DispatchTableCount
;
151 /* Maybe the original vtbl is just modified directly to point at
152 * ObjectStublessClientXXX thunks in real Windows, but I don't like it
154 TRACE("stubless thunks: count=%ld\n", count
);
155 This
->thunks
= HeapAlloc(GetProcessHeap(),0,sizeof(struct StublessThunk
)*count
);
156 This
->PVtbl
= HeapAlloc(GetProcessHeap(),0,sizeof(LPVOID
)*count
);
157 for (i
=0; i
<count
; i
++) {
158 struct StublessThunk
*thunk
= &This
->thunks
[i
];
159 if (vtbl
->Vtbl
[i
] == (LPVOID
)-1) {
160 PFORMAT_STRING fs
= stubless
->ProcFormatString
+ stubless
->FormatStringOffset
[i
];
161 unsigned bytes
= *(const WORD
*)(fs
+8) - STACK_ADJUST
;
162 TRACE("method %ld: stacksize=%d\n", i
, bytes
);
163 FILL_STUBLESS(thunk
, i
, bytes
)
164 This
->PVtbl
[i
] = thunk
;
167 memset(thunk
, 0, sizeof(struct StublessThunk
));
168 This
->PVtbl
[i
] = vtbl
->Vtbl
[i
];
173 This
->PVtbl
= vtbl
->Vtbl
;
175 This
->lpVtbl
= &StdProxy_Vtbl
;
176 /* one reference for the proxy */
178 This
->stubless
= stubless
;
179 This
->piid
= vtbl
->header
.piid
;
180 This
->pUnkOuter
= pUnkOuter
;
182 This
->pPSFactory
= pPSFactory
;
183 This
->pChannel
= NULL
;
184 *ppProxy
= (LPRPCPROXYBUFFER
)&This
->lpVtbl
;
185 *ppvObj
= &This
->PVtbl
;
186 /* if there is no outer unknown then the caller will control the lifetime
187 * of the proxy object through the proxy buffer, so no need to increment the
188 * ref count of the proxy object */
190 IUnknown_AddRef((IUnknown
*)*ppvObj
);
191 IPSFactoryBuffer_AddRef(pPSFactory
);
196 static void WINAPI
StdProxy_Destruct(LPRPCPROXYBUFFER iface
)
198 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
201 IRpcProxyBuffer_Disconnect(iface
);
203 IPSFactoryBuffer_Release(This
->pPSFactory
);
205 HeapFree(GetProcessHeap(),0,This
->PVtbl
);
206 HeapFree(GetProcessHeap(),0,This
->thunks
);
208 HeapFree(GetProcessHeap(),0,This
);
211 static HRESULT WINAPI
StdProxy_QueryInterface(LPRPCPROXYBUFFER iface
,
215 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
216 TRACE("(%p)->QueryInterface(%s,%p)\n",This
,debugstr_guid(riid
),obj
);
218 if (IsEqualGUID(&IID_IUnknown
,riid
) ||
219 IsEqualGUID(This
->piid
,riid
)) {
221 InterlockedIncrement(&This
->RefCount
);
225 if (IsEqualGUID(&IID_IRpcProxyBuffer
,riid
)) {
226 *obj
= &This
->lpVtbl
;
227 InterlockedIncrement(&This
->RefCount
);
231 return E_NOINTERFACE
;
234 static ULONG WINAPI
StdProxy_AddRef(LPRPCPROXYBUFFER iface
)
236 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
237 TRACE("(%p)->AddRef()\n",This
);
239 return InterlockedIncrement(&This
->RefCount
);
242 static ULONG WINAPI
StdProxy_Release(LPRPCPROXYBUFFER iface
)
245 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
246 TRACE("(%p)->Release()\n",This
);
248 refs
= InterlockedDecrement(&This
->RefCount
);
250 StdProxy_Destruct((LPRPCPROXYBUFFER
)&This
->lpVtbl
);
254 static HRESULT WINAPI
StdProxy_Connect(LPRPCPROXYBUFFER iface
,
255 LPRPCCHANNELBUFFER pChannel
)
257 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
258 TRACE("(%p)->Connect(%p)\n",This
,pChannel
);
260 This
->pChannel
= pChannel
;
261 IRpcChannelBuffer_AddRef(pChannel
);
265 static VOID WINAPI
StdProxy_Disconnect(LPRPCPROXYBUFFER iface
)
267 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
268 TRACE("(%p)->Disconnect()\n",This
);
270 IRpcChannelBuffer_Release(This
->pChannel
);
271 This
->pChannel
= NULL
;
274 static const IRpcProxyBufferVtbl StdProxy_Vtbl
=
276 StdProxy_QueryInterface
,
283 static void StdProxy_GetChannel(LPVOID iface
,
284 LPRPCCHANNELBUFFER
*ppChannel
)
286 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
287 TRACE("(%p)->GetChannel(%p) %s\n",This
,ppChannel
,This
->name
);
289 *ppChannel
= This
->pChannel
;
292 static void StdProxy_GetIID(LPVOID iface
,
295 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
296 TRACE("(%p)->GetIID(%p) %s\n",This
,ppiid
,This
->name
);
301 HRESULT WINAPI
IUnknown_QueryInterface_Proxy(LPUNKNOWN iface
,
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
);
310 ULONG WINAPI
IUnknown_AddRef_Proxy(LPUNKNOWN iface
)
312 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
313 TRACE("(%p)->AddRef() %s\n",This
,This
->name
);
314 return IUnknown_AddRef(This
->pUnkOuter
);
317 ULONG WINAPI
IUnknown_Release_Proxy(LPUNKNOWN iface
)
319 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
320 TRACE("(%p)->Release() %s\n",This
,This
->name
);
321 return IUnknown_Release(This
->pUnkOuter
);
324 /***********************************************************************
325 * NdrProxyInitialize [RPCRT4.@]
327 void WINAPI
NdrProxyInitialize(void *This
,
328 PRPC_MESSAGE pRpcMsg
,
329 PMIDL_STUB_MESSAGE pStubMsg
,
330 PMIDL_STUB_DESC pStubDescriptor
,
331 unsigned int ProcNum
)
333 TRACE("(%p,%p,%p,%p,%d)\n", This
, pRpcMsg
, pStubMsg
, pStubDescriptor
, ProcNum
);
334 NdrClientInitializeNew(pRpcMsg
, pStubMsg
, pStubDescriptor
, ProcNum
);
335 StdProxy_GetChannel(This
, &pStubMsg
->pRpcChannelBuffer
);
336 IRpcChannelBuffer_GetDestCtx(pStubMsg
->pRpcChannelBuffer
,
337 &pStubMsg
->dwDestContext
,
338 &pStubMsg
->pvDestContext
);
339 TRACE("channel=%p\n", pStubMsg
->pRpcChannelBuffer
);
342 /***********************************************************************
343 * NdrProxyGetBuffer [RPCRT4.@]
345 void WINAPI
NdrProxyGetBuffer(void *This
,
346 PMIDL_STUB_MESSAGE pStubMsg
)
349 const IID
*riid
= NULL
;
351 TRACE("(%p,%p)\n", This
, pStubMsg
);
352 pStubMsg
->RpcMsg
->BufferLength
= pStubMsg
->BufferLength
;
353 pStubMsg
->dwStubPhase
= PROXY_GETBUFFER
;
354 StdProxy_GetIID(This
, &riid
);
355 hr
= IRpcChannelBuffer_GetBuffer(pStubMsg
->pRpcChannelBuffer
,
356 (RPCOLEMESSAGE
*)pStubMsg
->RpcMsg
,
360 RpcRaiseException(hr
);
363 pStubMsg
->fBufferValid
= TRUE
;
364 pStubMsg
->BufferStart
= pStubMsg
->RpcMsg
->Buffer
;
365 pStubMsg
->BufferEnd
= pStubMsg
->BufferStart
+ pStubMsg
->BufferLength
;
366 pStubMsg
->Buffer
= pStubMsg
->BufferStart
;
367 pStubMsg
->dwStubPhase
= PROXY_MARSHAL
;
370 /***********************************************************************
371 * NdrProxySendReceive [RPCRT4.@]
373 void WINAPI
NdrProxySendReceive(void *This
,
374 PMIDL_STUB_MESSAGE pStubMsg
)
379 TRACE("(%p,%p)\n", This
, pStubMsg
);
381 if (!pStubMsg
->pRpcChannelBuffer
)
383 WARN("Trying to use disconnected proxy %p\n", This
);
384 RpcRaiseException(RPC_E_DISCONNECTED
);
387 pStubMsg
->dwStubPhase
= PROXY_SENDRECEIVE
;
388 /* avoid sending uninitialised parts of the buffer on the wire */
389 pStubMsg
->RpcMsg
->BufferLength
= pStubMsg
->Buffer
- (unsigned char *)pStubMsg
->RpcMsg
->Buffer
;
390 hr
= IRpcChannelBuffer_SendReceive(pStubMsg
->pRpcChannelBuffer
,
391 (RPCOLEMESSAGE
*)pStubMsg
->RpcMsg
,
393 pStubMsg
->dwStubPhase
= PROXY_UNMARSHAL
;
394 pStubMsg
->BufferLength
= pStubMsg
->RpcMsg
->BufferLength
;
395 pStubMsg
->BufferStart
= pStubMsg
->RpcMsg
->Buffer
;
396 pStubMsg
->BufferEnd
= pStubMsg
->BufferStart
+ pStubMsg
->BufferLength
;
397 pStubMsg
->Buffer
= pStubMsg
->BufferStart
;
399 /* raise exception if call failed */
400 if (hr
== RPC_S_CALL_FAILED
) RpcRaiseException(*(DWORD
*)pStubMsg
->Buffer
);
401 else if (FAILED(hr
)) RpcRaiseException(hr
);
404 /***********************************************************************
405 * NdrProxyFreeBuffer [RPCRT4.@]
407 void WINAPI
NdrProxyFreeBuffer(void *This
,
408 PMIDL_STUB_MESSAGE pStubMsg
)
410 TRACE("(%p,%p)\n", This
, pStubMsg
);
412 if (pStubMsg
->fBufferValid
)
414 IRpcChannelBuffer_FreeBuffer(pStubMsg
->pRpcChannelBuffer
,
415 (RPCOLEMESSAGE
*)pStubMsg
->RpcMsg
);
416 pStubMsg
->fBufferValid
= TRUE
;
420 /***********************************************************************
421 * NdrProxyErrorHandler [RPCRT4.@]
423 HRESULT WINAPI
NdrProxyErrorHandler(DWORD dwExceptionCode
)
425 WARN("(0x%08x): a proxy call failed\n", dwExceptionCode
);
427 if (FAILED(dwExceptionCode
))
428 return dwExceptionCode
;
430 return HRESULT_FROM_WIN32(dwExceptionCode
);
434 CreateProxyFromTypeInfo( LPTYPEINFO pTypeInfo
, LPUNKNOWN pUnkOuter
, REFIID riid
,
435 LPRPCPROXYBUFFER
*ppProxy
, LPVOID
*ppv
)
437 typedef INT (WINAPI
*MessageBoxA
)(HWND
,LPCSTR
,LPCSTR
,UINT
);
438 HMODULE hUser32
= LoadLibraryA("user32");
439 MessageBoxA pMessageBoxA
= (void *)GetProcAddress(hUser32
, "MessageBoxA");
441 FIXME("%p %p %s %p %p\n", pTypeInfo
, pUnkOuter
, debugstr_guid(riid
), ppProxy
, ppv
);
445 "The native implementation of OLEAUT32.DLL cannot be used "
446 "with Wine's RPCRT4.DLL. Remove OLEAUT32.DLL and try again.\n",
447 "Wine: Unimplemented CreateProxyFromTypeInfo",