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
;
49 const MIDL_STUBLESS_PROXY_INFO
*stubless
;
53 LPPSFACTORYBUFFER pPSFactory
;
54 LPRPCCHANNELBUFFER pChannel
;
57 static const IRpcProxyBufferVtbl StdProxy_Vtbl
;
59 #define ICOM_THIS_MULTI(impl,field,iface) impl* const This=(impl*)((char*)(iface) - offsetof(impl,field))
74 extern void call_stubless_func(void);
75 __ASM_GLOBAL_FUNC(call_stubless_func
,
76 "pushl %esp\n\t" /* pointer to index */
77 "call " __ASM_NAME("ObjectStubless") "\n\t"
78 "popl %edx\n\t" /* args size */
79 "movl (%esp),%ecx\n\t" /* return address */
83 HRESULT WINAPI
ObjectStubless(DWORD
*args
)
85 DWORD index
= args
[0];
86 LPVOID iface
= (LPVOID
)args
[2];
88 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
90 const MIDL_STUBLESS_PROXY_INFO
*stubless
= This
->stubless
;
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
)
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
);
131 static BOOL
fill_stubless_table( IUnknownVtbl
*vtbl
, DWORD num
)
133 const void **entry
= (const void **)(vtbl
+ 1);
136 for (i
= 0; i
< (num
- 3 + BLOCK_SIZE
- 1) / BLOCK_SIZE
; i
++)
138 const struct thunk
*block
= method_blocks
[i
];
139 if (!block
&& !(block
= allocate_block( i
))) return FALSE
;
140 for (j
= 0; j
< BLOCK_SIZE
&& j
< num
- 3 - i
* BLOCK_SIZE
; j
++, entry
++)
141 if (*entry
== (LPVOID
)-1) *entry
= &block
[j
];
148 static BOOL
fill_stubless_table( IUnknownVtbl
*vtbl
, DWORD num
)
150 ERR("stubless proxies are not supported on this architecture\n");
154 #endif /* __i386__ */
156 HRESULT
StdProxy_Construct(REFIID riid
,
158 const ProxyFileInfo
*ProxyInfo
,
160 LPPSFACTORYBUFFER pPSFactory
,
161 LPRPCPROXYBUFFER
*ppProxy
,
165 const MIDL_STUBLESS_PROXY_INFO
*stubless
= NULL
;
166 PCInterfaceName name
= ProxyInfo
->pNamesArray
[Index
];
167 CInterfaceProxyVtbl
*vtbl
= ProxyInfo
->pProxyVtblList
[Index
];
169 TRACE("(%p,%p,%p,%p,%p) %s\n", pUnkOuter
, vtbl
, pPSFactory
, ppProxy
, ppvObj
, name
);
171 /* TableVersion = 2 means it is the stubless version of CInterfaceProxyVtbl */
172 if (ProxyInfo
->TableVersion
> 1) {
173 ULONG count
= ProxyInfo
->pStubVtblList
[Index
]->header
.DispatchTableCount
;
174 stubless
= *(const void **)vtbl
;
175 vtbl
= (CInterfaceProxyVtbl
*)((const void **)vtbl
+ 1);
176 TRACE("stubless %p vtbl %p: count=%d\n", stubless
, vtbl
->Vtbl
, count
);
177 fill_stubless_table( (IUnknownVtbl
*)vtbl
->Vtbl
, count
);
180 TRACE("iid=%s vtbl=%p\n", debugstr_guid(vtbl
->header
.piid
), vtbl
->Vtbl
);
182 if (!IsEqualGUID(vtbl
->header
.piid
, riid
)) {
183 ERR("IID mismatch during proxy creation\n");
184 return RPC_E_UNEXPECTED
;
187 This
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(StdProxyImpl
));
188 if (!This
) return E_OUTOFMEMORY
;
190 if (!pUnkOuter
) pUnkOuter
= (IUnknown
*)This
;
191 This
->lpVtbl
= &StdProxy_Vtbl
;
192 This
->PVtbl
= vtbl
->Vtbl
;
193 /* one reference for the proxy */
195 This
->stubless
= stubless
;
196 This
->piid
= vtbl
->header
.piid
;
197 This
->pUnkOuter
= pUnkOuter
;
199 This
->pPSFactory
= pPSFactory
;
200 This
->pChannel
= NULL
;
201 *ppProxy
= (LPRPCPROXYBUFFER
)&This
->lpVtbl
;
202 *ppvObj
= &This
->PVtbl
;
203 IUnknown_AddRef((IUnknown
*)*ppvObj
);
204 IPSFactoryBuffer_AddRef(pPSFactory
);
209 static void StdProxy_Destruct(LPRPCPROXYBUFFER iface
)
211 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
214 IRpcProxyBuffer_Disconnect(iface
);
216 IPSFactoryBuffer_Release(This
->pPSFactory
);
217 HeapFree(GetProcessHeap(),0,This
);
220 static HRESULT WINAPI
StdProxy_QueryInterface(LPRPCPROXYBUFFER iface
,
224 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
225 TRACE("(%p)->QueryInterface(%s,%p)\n",This
,debugstr_guid(riid
),obj
);
227 if (IsEqualGUID(&IID_IUnknown
,riid
) ||
228 IsEqualGUID(This
->piid
,riid
)) {
230 InterlockedIncrement(&This
->RefCount
);
234 if (IsEqualGUID(&IID_IRpcProxyBuffer
,riid
)) {
235 *obj
= &This
->lpVtbl
;
236 InterlockedIncrement(&This
->RefCount
);
240 return E_NOINTERFACE
;
243 static ULONG WINAPI
StdProxy_AddRef(LPRPCPROXYBUFFER iface
)
245 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
246 TRACE("(%p)->AddRef()\n",This
);
248 return InterlockedIncrement(&This
->RefCount
);
251 static ULONG WINAPI
StdProxy_Release(LPRPCPROXYBUFFER iface
)
254 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
255 TRACE("(%p)->Release()\n",This
);
257 refs
= InterlockedDecrement(&This
->RefCount
);
259 StdProxy_Destruct((LPRPCPROXYBUFFER
)&This
->lpVtbl
);
263 static HRESULT WINAPI
StdProxy_Connect(LPRPCPROXYBUFFER iface
,
264 LPRPCCHANNELBUFFER pChannel
)
266 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
267 TRACE("(%p)->Connect(%p)\n",This
,pChannel
);
269 This
->pChannel
= pChannel
;
270 IRpcChannelBuffer_AddRef(pChannel
);
274 static VOID WINAPI
StdProxy_Disconnect(LPRPCPROXYBUFFER iface
)
276 ICOM_THIS_MULTI(StdProxyImpl
,lpVtbl
,iface
);
277 TRACE("(%p)->Disconnect()\n",This
);
279 IRpcChannelBuffer_Release(This
->pChannel
);
280 This
->pChannel
= NULL
;
283 static const IRpcProxyBufferVtbl StdProxy_Vtbl
=
285 StdProxy_QueryInterface
,
292 static void StdProxy_GetChannel(LPVOID iface
,
293 LPRPCCHANNELBUFFER
*ppChannel
)
295 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
296 TRACE("(%p)->GetChannel(%p) %s\n",This
,ppChannel
,This
->name
);
298 *ppChannel
= This
->pChannel
;
301 static void StdProxy_GetIID(LPVOID iface
,
304 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
305 TRACE("(%p)->GetIID(%p) %s\n",This
,ppiid
,This
->name
);
310 HRESULT WINAPI
IUnknown_QueryInterface_Proxy(LPUNKNOWN iface
,
314 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
315 TRACE("(%p)->QueryInterface(%s,%p) %s\n",This
,debugstr_guid(riid
),ppvObj
,This
->name
);
316 return IUnknown_QueryInterface(This
->pUnkOuter
,riid
,ppvObj
);
319 ULONG WINAPI
IUnknown_AddRef_Proxy(LPUNKNOWN iface
)
321 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
322 TRACE("(%p)->AddRef() %s\n",This
,This
->name
);
323 return IUnknown_AddRef(This
->pUnkOuter
);
326 ULONG WINAPI
IUnknown_Release_Proxy(LPUNKNOWN iface
)
328 ICOM_THIS_MULTI(StdProxyImpl
,PVtbl
,iface
);
329 TRACE("(%p)->Release() %s\n",This
,This
->name
);
330 return IUnknown_Release(This
->pUnkOuter
);
333 /***********************************************************************
334 * NdrProxyInitialize [RPCRT4.@]
336 void WINAPI
NdrProxyInitialize(void *This
,
337 PRPC_MESSAGE pRpcMsg
,
338 PMIDL_STUB_MESSAGE pStubMsg
,
339 PMIDL_STUB_DESC pStubDescriptor
,
340 unsigned int ProcNum
)
342 TRACE("(%p,%p,%p,%p,%d)\n", This
, pRpcMsg
, pStubMsg
, pStubDescriptor
, ProcNum
);
343 NdrClientInitializeNew(pRpcMsg
, pStubMsg
, pStubDescriptor
, ProcNum
);
344 StdProxy_GetChannel(This
, &pStubMsg
->pRpcChannelBuffer
);
345 IRpcChannelBuffer_GetDestCtx(pStubMsg
->pRpcChannelBuffer
,
346 &pStubMsg
->dwDestContext
,
347 &pStubMsg
->pvDestContext
);
348 TRACE("channel=%p\n", pStubMsg
->pRpcChannelBuffer
);
351 /***********************************************************************
352 * NdrProxyGetBuffer [RPCRT4.@]
354 void WINAPI
NdrProxyGetBuffer(void *This
,
355 PMIDL_STUB_MESSAGE pStubMsg
)
358 const IID
*riid
= NULL
;
360 TRACE("(%p,%p)\n", This
, pStubMsg
);
361 pStubMsg
->RpcMsg
->BufferLength
= pStubMsg
->BufferLength
;
362 pStubMsg
->dwStubPhase
= PROXY_GETBUFFER
;
363 StdProxy_GetIID(This
, &riid
);
364 hr
= IRpcChannelBuffer_GetBuffer(pStubMsg
->pRpcChannelBuffer
,
365 (RPCOLEMESSAGE
*)pStubMsg
->RpcMsg
,
369 RpcRaiseException(hr
);
372 pStubMsg
->fBufferValid
= TRUE
;
373 pStubMsg
->BufferStart
= pStubMsg
->RpcMsg
->Buffer
;
374 pStubMsg
->BufferEnd
= pStubMsg
->BufferStart
+ pStubMsg
->BufferLength
;
375 pStubMsg
->Buffer
= pStubMsg
->BufferStart
;
376 pStubMsg
->dwStubPhase
= PROXY_MARSHAL
;
379 /***********************************************************************
380 * NdrProxySendReceive [RPCRT4.@]
382 void WINAPI
NdrProxySendReceive(void *This
,
383 PMIDL_STUB_MESSAGE pStubMsg
)
388 TRACE("(%p,%p)\n", This
, pStubMsg
);
390 if (!pStubMsg
->pRpcChannelBuffer
)
392 WARN("Trying to use disconnected proxy %p\n", This
);
393 RpcRaiseException(RPC_E_DISCONNECTED
);
396 pStubMsg
->dwStubPhase
= PROXY_SENDRECEIVE
;
397 /* avoid sending uninitialised parts of the buffer on the wire */
398 pStubMsg
->RpcMsg
->BufferLength
= pStubMsg
->Buffer
- (unsigned char *)pStubMsg
->RpcMsg
->Buffer
;
399 hr
= IRpcChannelBuffer_SendReceive(pStubMsg
->pRpcChannelBuffer
,
400 (RPCOLEMESSAGE
*)pStubMsg
->RpcMsg
,
402 pStubMsg
->dwStubPhase
= PROXY_UNMARSHAL
;
403 pStubMsg
->BufferLength
= pStubMsg
->RpcMsg
->BufferLength
;
404 pStubMsg
->BufferStart
= pStubMsg
->RpcMsg
->Buffer
;
405 pStubMsg
->BufferEnd
= pStubMsg
->BufferStart
+ pStubMsg
->BufferLength
;
406 pStubMsg
->Buffer
= pStubMsg
->BufferStart
;
408 /* raise exception if call failed */
409 if (hr
== RPC_S_CALL_FAILED
) RpcRaiseException(*(DWORD
*)pStubMsg
->Buffer
);
410 else if (FAILED(hr
)) RpcRaiseException(hr
);
413 /***********************************************************************
414 * NdrProxyFreeBuffer [RPCRT4.@]
416 void WINAPI
NdrProxyFreeBuffer(void *This
,
417 PMIDL_STUB_MESSAGE pStubMsg
)
419 TRACE("(%p,%p)\n", This
, pStubMsg
);
421 if (pStubMsg
->fBufferValid
)
423 IRpcChannelBuffer_FreeBuffer(pStubMsg
->pRpcChannelBuffer
,
424 (RPCOLEMESSAGE
*)pStubMsg
->RpcMsg
);
425 pStubMsg
->fBufferValid
= TRUE
;
429 /***********************************************************************
430 * NdrProxyErrorHandler [RPCRT4.@]
432 HRESULT WINAPI
NdrProxyErrorHandler(DWORD dwExceptionCode
)
434 WARN("(0x%08x): a proxy call failed\n", dwExceptionCode
);
436 if (FAILED(dwExceptionCode
))
437 return dwExceptionCode
;
439 return HRESULT_FROM_WIN32(dwExceptionCode
);
443 CreateProxyFromTypeInfo( LPTYPEINFO pTypeInfo
, LPUNKNOWN pUnkOuter
, REFIID riid
,
444 LPRPCPROXYBUFFER
*ppProxy
, LPVOID
*ppv
)
446 typedef INT (WINAPI
*MessageBoxA
)(HWND
,LPCSTR
,LPCSTR
,UINT
);
447 HMODULE hUser32
= LoadLibraryA("user32");
448 MessageBoxA pMessageBoxA
= (void *)GetProcAddress(hUser32
, "MessageBoxA");
450 FIXME("%p %p %s %p %p\n", pTypeInfo
, pUnkOuter
, debugstr_guid(riid
), ppProxy
, ppv
);
454 "The native implementation of OLEAUT32.DLL cannot be used "
455 "with Wine's RPCRT4.DLL. Remove OLEAUT32.DLL and try again.\n",
456 "Wine: Unimplemented CreateProxyFromTypeInfo",