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
24 #include "wine/port.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
43 /* I don't know what MS's std proxy structure looks like,
44 so this probably doesn't match, but that shouldn't matter */
46 const IRpcProxyBufferVtbl
*lpVtbl
;
52 LPPSFACTORYBUFFER pPSFactory
;
53 LPRPCCHANNELBUFFER pChannel
;
56 static const IRpcProxyBufferVtbl StdProxy_Vtbl
;
58 #define ICOM_THIS_MULTI(impl,field,iface) impl* const This=(impl*)((char*)(iface) - offsetof(impl,field))
73 extern void call_stubless_func(void);
74 __ASM_GLOBAL_FUNC(call_stubless_func
,
75 "pushl %esp\n\t" /* pointer to index */
76 "call " __ASM_NAME("ObjectStubless") "\n\t"
77 "popl %edx\n\t" /* args size */
78 "movl (%esp),%ecx\n\t" /* return address */
82 HRESULT WINAPI
ObjectStubless(DWORD
*args
)
84 DWORD index
= args
[0];
85 void **iface
= (void **)args
[2];
86 const void **vtbl
= (const void **)*iface
;
87 const MIDL_STUBLESS_PROXY_INFO
*stubless
= *(const MIDL_STUBLESS_PROXY_INFO
**)(vtbl
- 2);
88 const PFORMAT_STRING fs
= stubless
->ProcFormatString
+ stubless
->FormatStringOffset
[index
];
90 /* store bytes to remove from stack */
91 args
[0] = *(const WORD
*)(fs
+ 8);
92 TRACE("(%p)->(%d)([%d bytes]) ret=%08x\n", iface
, index
, args
[0], args
[1]);
94 return NdrClientCall2(stubless
->pStubDesc
, fs
, args
+ 2);
97 #define BLOCK_SIZE 1024
98 #define MAX_BLOCKS 64 /* 64k methods should be enough for anybody */
100 static const struct thunk
*method_blocks
[MAX_BLOCKS
];
102 static const struct thunk
*allocate_block( unsigned int num
)
105 struct thunk
*prev
, *block
;
107 block
= VirtualAlloc( NULL
, BLOCK_SIZE
* sizeof(*block
),
108 MEM_COMMIT
| MEM_RESERVE
, PAGE_EXECUTE_READWRITE
);
109 if (!block
) return NULL
;
111 for (i
= 0; i
< BLOCK_SIZE
; i
++)
113 block
[i
].push
= 0x68; /* pushl */
114 block
[i
].index
= BLOCK_SIZE
* num
+ i
+ 3;
115 block
[i
].jmp
= 0xe9; /* jmp */
116 block
[i
].handler
= (char *)call_stubless_func
- (char *)(&block
[i
].handler
+ 1);
118 VirtualProtect( block
, BLOCK_SIZE
* sizeof(*block
), PAGE_EXECUTE_READ
, NULL
);
119 prev
= InterlockedCompareExchangePointer( (void **)&method_blocks
[num
], block
, NULL
);
120 if (prev
) /* someone beat us to it */
122 VirtualFree( block
, 0, MEM_RELEASE
);
128 static BOOL
fill_stubless_table( IUnknownVtbl
*vtbl
, DWORD num
)
130 const void **entry
= (const void **)(vtbl
+ 1);
133 for (i
= 0; i
< (num
- 3 + BLOCK_SIZE
- 1) / BLOCK_SIZE
; i
++)
135 const struct thunk
*block
= method_blocks
[i
];
136 if (!block
&& !(block
= allocate_block( i
))) return FALSE
;
137 for (j
= 0; j
< BLOCK_SIZE
&& j
< num
- 3 - i
* BLOCK_SIZE
; j
++, entry
++)
138 if (*entry
== (LPVOID
)-1) *entry
= &block
[j
];
145 static BOOL
fill_stubless_table( IUnknownVtbl
*vtbl
, DWORD num
)
147 ERR("stubless proxies are not supported on this architecture\n");
151 #endif /* __i386__ */
153 HRESULT
StdProxy_Construct(REFIID riid
,
155 const ProxyFileInfo
*ProxyInfo
,
157 LPPSFACTORYBUFFER pPSFactory
,
158 LPRPCPROXYBUFFER
*ppProxy
,
162 PCInterfaceName name
= ProxyInfo
->pNamesArray
[Index
];
163 CInterfaceProxyVtbl
*vtbl
= ProxyInfo
->pProxyVtblList
[Index
];
165 TRACE("(%p,%p,%p,%p,%p) %s\n", pUnkOuter
, vtbl
, pPSFactory
, ppProxy
, ppvObj
, name
);
167 /* TableVersion = 2 means it is the stubless version of CInterfaceProxyVtbl */
168 if (ProxyInfo
->TableVersion
> 1) {
169 ULONG count
= ProxyInfo
->pStubVtblList
[Index
]->header
.DispatchTableCount
;
170 vtbl
= (CInterfaceProxyVtbl
*)((const void **)vtbl
+ 1);
171 TRACE("stubless vtbl %p: count=%d\n", vtbl
->Vtbl
, count
);
172 fill_stubless_table( (IUnknownVtbl
*)vtbl
->Vtbl
, count
);
175 TRACE("iid=%s vtbl=%p\n", debugstr_guid(vtbl
->header
.piid
), vtbl
->Vtbl
);
177 if (!IsEqualGUID(vtbl
->header
.piid
, riid
)) {
178 ERR("IID mismatch during proxy creation\n");
179 return RPC_E_UNEXPECTED
;
182 This
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(StdProxyImpl
));
183 if (!This
) return E_OUTOFMEMORY
;
185 if (!pUnkOuter
) pUnkOuter
= (IUnknown
*)This
;
186 This
->lpVtbl
= &StdProxy_Vtbl
;
187 This
->PVtbl
= vtbl
->Vtbl
;
188 /* one reference for the proxy */
190 This
->piid
= vtbl
->header
.piid
;
191 This
->pUnkOuter
= pUnkOuter
;
193 This
->pPSFactory
= pPSFactory
;
194 This
->pChannel
= NULL
;
195 *ppProxy
= (LPRPCPROXYBUFFER
)&This
->lpVtbl
;
196 *ppvObj
= &This
->PVtbl
;
197 IUnknown_AddRef((IUnknown
*)*ppvObj
);
198 IPSFactoryBuffer_AddRef(pPSFactory
);
203 static void StdProxy_Destruct(LPRPCPROXYBUFFER iface
)
205 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
208 IRpcProxyBuffer_Disconnect(iface
);
210 IPSFactoryBuffer_Release(This
->pPSFactory
);
211 HeapFree(GetProcessHeap(),0,This
);
214 static HRESULT WINAPI
StdProxy_QueryInterface(LPRPCPROXYBUFFER iface
,
218 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
219 TRACE("(%p)->QueryInterface(%s,%p)\n",This
,debugstr_guid(riid
),obj
);
221 if (IsEqualGUID(&IID_IUnknown
,riid
) ||
222 IsEqualGUID(This
->piid
,riid
)) {
224 InterlockedIncrement(&This
->RefCount
);
228 if (IsEqualGUID(&IID_IRpcProxyBuffer
,riid
)) {
229 *obj
= &This
->lpVtbl
;
230 InterlockedIncrement(&This
->RefCount
);
234 return E_NOINTERFACE
;
237 static ULONG WINAPI
StdProxy_AddRef(LPRPCPROXYBUFFER iface
)
239 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
240 TRACE("(%p)->AddRef()\n",This
);
242 return InterlockedIncrement(&This
->RefCount
);
245 static ULONG WINAPI
StdProxy_Release(LPRPCPROXYBUFFER iface
)
248 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
249 TRACE("(%p)->Release()\n",This
);
251 refs
= InterlockedDecrement(&This
->RefCount
);
253 StdProxy_Destruct((LPRPCPROXYBUFFER
)&This
->lpVtbl
);
257 static HRESULT WINAPI
StdProxy_Connect(LPRPCPROXYBUFFER iface
,
258 LPRPCCHANNELBUFFER pChannel
)
260 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
261 TRACE("(%p)->Connect(%p)\n",This
,pChannel
);
263 This
->pChannel
= pChannel
;
264 IRpcChannelBuffer_AddRef(pChannel
);
268 static VOID WINAPI
StdProxy_Disconnect(LPRPCPROXYBUFFER iface
)
270 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
271 TRACE("(%p)->Disconnect()\n",This
);
273 IRpcChannelBuffer_Release(This
->pChannel
);
274 This
->pChannel
= NULL
;
277 static const IRpcProxyBufferVtbl StdProxy_Vtbl
=
279 StdProxy_QueryInterface
,
286 static void StdProxy_GetChannel(LPVOID iface
,
287 LPRPCCHANNELBUFFER
*ppChannel
)
289 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
290 TRACE("(%p)->GetChannel(%p) %s\n",This
,ppChannel
,This
->name
);
292 *ppChannel
= This
->pChannel
;
295 static void StdProxy_GetIID(LPVOID iface
,
298 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
299 TRACE("(%p)->GetIID(%p) %s\n",This
,ppiid
,This
->name
);
304 HRESULT WINAPI
IUnknown_QueryInterface_Proxy(LPUNKNOWN iface
,
308 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
309 TRACE("(%p)->QueryInterface(%s,%p) %s\n",This
,debugstr_guid(riid
),ppvObj
,This
->name
);
310 return IUnknown_QueryInterface(This
->pUnkOuter
,riid
,ppvObj
);
313 ULONG WINAPI
IUnknown_AddRef_Proxy(LPUNKNOWN iface
)
315 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
316 TRACE("(%p)->AddRef() %s\n",This
,This
->name
);
317 return IUnknown_AddRef(This
->pUnkOuter
);
320 ULONG WINAPI
IUnknown_Release_Proxy(LPUNKNOWN iface
)
322 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
323 TRACE("(%p)->Release() %s\n",This
,This
->name
);
324 return IUnknown_Release(This
->pUnkOuter
);
327 /***********************************************************************
328 * NdrProxyInitialize [RPCRT4.@]
330 void WINAPI
NdrProxyInitialize(void *This
,
331 PRPC_MESSAGE pRpcMsg
,
332 PMIDL_STUB_MESSAGE pStubMsg
,
333 PMIDL_STUB_DESC pStubDescriptor
,
334 unsigned int ProcNum
)
336 TRACE("(%p,%p,%p,%p,%d)\n", This
, pRpcMsg
, pStubMsg
, pStubDescriptor
, ProcNum
);
337 NdrClientInitializeNew(pRpcMsg
, pStubMsg
, pStubDescriptor
, ProcNum
);
338 StdProxy_GetChannel(This
, &pStubMsg
->pRpcChannelBuffer
);
339 IRpcChannelBuffer_GetDestCtx(pStubMsg
->pRpcChannelBuffer
,
340 &pStubMsg
->dwDestContext
,
341 &pStubMsg
->pvDestContext
);
342 TRACE("channel=%p\n", pStubMsg
->pRpcChannelBuffer
);
345 /***********************************************************************
346 * NdrProxyGetBuffer [RPCRT4.@]
348 void WINAPI
NdrProxyGetBuffer(void *This
,
349 PMIDL_STUB_MESSAGE pStubMsg
)
352 const IID
*riid
= NULL
;
354 TRACE("(%p,%p)\n", This
, pStubMsg
);
355 pStubMsg
->RpcMsg
->BufferLength
= pStubMsg
->BufferLength
;
356 pStubMsg
->dwStubPhase
= PROXY_GETBUFFER
;
357 StdProxy_GetIID(This
, &riid
);
358 hr
= IRpcChannelBuffer_GetBuffer(pStubMsg
->pRpcChannelBuffer
,
359 (RPCOLEMESSAGE
*)pStubMsg
->RpcMsg
,
363 RpcRaiseException(hr
);
366 pStubMsg
->fBufferValid
= TRUE
;
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 /* avoid sending uninitialised parts of the buffer on the wire */
392 pStubMsg
->RpcMsg
->BufferLength
= pStubMsg
->Buffer
- (unsigned char *)pStubMsg
->RpcMsg
->Buffer
;
393 hr
= IRpcChannelBuffer_SendReceive(pStubMsg
->pRpcChannelBuffer
,
394 (RPCOLEMESSAGE
*)pStubMsg
->RpcMsg
,
396 pStubMsg
->dwStubPhase
= PROXY_UNMARSHAL
;
397 pStubMsg
->BufferLength
= pStubMsg
->RpcMsg
->BufferLength
;
398 pStubMsg
->BufferStart
= pStubMsg
->RpcMsg
->Buffer
;
399 pStubMsg
->BufferEnd
= pStubMsg
->BufferStart
+ pStubMsg
->BufferLength
;
400 pStubMsg
->Buffer
= pStubMsg
->BufferStart
;
402 /* raise exception if call failed */
403 if (hr
== RPC_S_CALL_FAILED
) RpcRaiseException(*(DWORD
*)pStubMsg
->Buffer
);
404 else if (FAILED(hr
)) RpcRaiseException(hr
);
407 /***********************************************************************
408 * NdrProxyFreeBuffer [RPCRT4.@]
410 void WINAPI
NdrProxyFreeBuffer(void *This
,
411 PMIDL_STUB_MESSAGE pStubMsg
)
413 TRACE("(%p,%p)\n", This
, pStubMsg
);
415 if (pStubMsg
->fBufferValid
)
417 IRpcChannelBuffer_FreeBuffer(pStubMsg
->pRpcChannelBuffer
,
418 (RPCOLEMESSAGE
*)pStubMsg
->RpcMsg
);
419 pStubMsg
->fBufferValid
= TRUE
;
423 /***********************************************************************
424 * NdrProxyErrorHandler [RPCRT4.@]
426 HRESULT WINAPI
NdrProxyErrorHandler(DWORD dwExceptionCode
)
428 WARN("(0x%08x): a proxy call failed\n", dwExceptionCode
);
430 if (FAILED(dwExceptionCode
))
431 return dwExceptionCode
;
433 return HRESULT_FROM_WIN32(dwExceptionCode
);
437 CreateProxyFromTypeInfo( LPTYPEINFO pTypeInfo
, LPUNKNOWN pUnkOuter
, REFIID riid
,
438 LPRPCPROXYBUFFER
*ppProxy
, LPVOID
*ppv
)
440 typedef INT (WINAPI
*MessageBoxA
)(HWND
,LPCSTR
,LPCSTR
,UINT
);
441 HMODULE hUser32
= LoadLibraryA("user32");
442 MessageBoxA pMessageBoxA
= (void *)GetProcAddress(hUser32
, "MessageBoxA");
444 FIXME("%p %p %s %p %p\n", pTypeInfo
, pUnkOuter
, debugstr_guid(riid
), ppProxy
, ppv
);
448 "The native implementation of OLEAUT32.DLL cannot be used "
449 "with Wine's RPCRT4.DLL. Remove OLEAUT32.DLL and try again.\n",
450 "Wine: Unimplemented CreateProxyFromTypeInfo",