2 * COM stub (CStdStubBuffer) 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
33 #include "wine/debug.h"
34 #include "wine/exception.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(ole);
40 #define STUB_HEADER(This) (((CInterfaceStubHeader*)((This)->lpVtbl))[-1])
42 static WINE_EXCEPTION_FILTER(stub_filter)
44 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
45 return EXCEPTION_CONTINUE_SEARCH;
46 return EXCEPTION_EXECUTE_HANDLER;
49 HRESULT WINAPI CStdStubBuffer_Construct(REFIID riid,
52 CInterfaceStubVtbl *vtbl,
53 LPPSFACTORYBUFFER pPSFactory,
54 LPRPCSTUBBUFFER *ppStub)
59 TRACE("(%p,%p,%p,%p) %s\n", pUnkServer, vtbl, pPSFactory, ppStub, name);
60 TRACE("iid=%s\n", debugstr_guid(vtbl->header.piid));
61 TRACE("vtbl=%p\n", &vtbl->Vtbl);
63 if (!IsEqualGUID(vtbl->header.piid, riid)) {
64 ERR("IID mismatch during stub creation\n");
65 return RPC_E_UNEXPECTED;
68 r = IUnknown_QueryInterface(pUnkServer, riid, (void**)&pvServer);
72 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CStdStubBuffer));
74 IUnknown_Release(pvServer);
78 This->lpVtbl = &vtbl->Vtbl;
80 This->pvServerObject = pvServer;
81 This->pPSFactory = pPSFactory;
82 *ppStub = (LPRPCSTUBBUFFER)This;
84 IPSFactoryBuffer_AddRef(pPSFactory);
88 HRESULT WINAPI CStdStubBuffer_QueryInterface(LPRPCSTUBBUFFER iface,
92 CStdStubBuffer *This = (CStdStubBuffer *)iface;
93 TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
95 if (IsEqualGUID(&IID_IUnknown,riid) ||
96 IsEqualGUID(&IID_IRpcStubBuffer,riid)) {
101 return E_NOINTERFACE;
104 ULONG WINAPI CStdStubBuffer_AddRef(LPRPCSTUBBUFFER iface)
106 CStdStubBuffer *This = (CStdStubBuffer *)iface;
107 TRACE("(%p)->AddRef()\n",This);
108 return ++(This->RefCount);
111 ULONG WINAPI NdrCStdStubBuffer_Release(LPRPCSTUBBUFFER iface,
112 LPPSFACTORYBUFFER pPSF)
114 CStdStubBuffer *This = (CStdStubBuffer *)iface;
115 TRACE("(%p)->Release()\n",This);
117 if (!--(This->RefCount)) {
118 IRpcStubBuffer_Disconnect(iface);
120 IPSFactoryBuffer_Release(This->pPSFactory);
121 HeapFree(GetProcessHeap(),0,This);
124 return This->RefCount;
127 ULONG WINAPI NdrCStdStubBuffer2_Release(LPRPCSTUBBUFFER iface,
128 LPPSFACTORYBUFFER pPSF)
130 FIXME("Not implemented\n");
134 HRESULT WINAPI CStdStubBuffer_Connect(LPRPCSTUBBUFFER iface,
135 LPUNKNOWN lpUnkServer)
137 CStdStubBuffer *This = (CStdStubBuffer *)iface;
139 IUnknown *new = NULL;
141 TRACE("(%p)->Connect(%p)\n",This,lpUnkServer);
143 r = IUnknown_QueryInterface(lpUnkServer, STUB_HEADER(This).piid, (void**)&new);
144 new = InterlockedExchangePointer((void**)&This->pvServerObject, new);
146 IUnknown_Release(new);
150 void WINAPI CStdStubBuffer_Disconnect(LPRPCSTUBBUFFER iface)
152 CStdStubBuffer *This = (CStdStubBuffer *)iface;
154 TRACE("(%p)->Disconnect()\n",This);
156 old = InterlockedExchangePointer((void**)&This->pvServerObject, NULL);
159 IUnknown_Release(old);
162 HRESULT WINAPI CStdStubBuffer_Invoke(LPRPCSTUBBUFFER iface,
164 LPRPCCHANNELBUFFER pChannel)
166 CStdStubBuffer *This = (CStdStubBuffer *)iface;
167 DWORD dwPhase = STUB_UNMARSHAL;
170 TRACE("(%p)->Invoke(%p,%p)\n",This,pMsg,pChannel);
174 if (STUB_HEADER(This).pDispatchTable)
175 STUB_HEADER(This).pDispatchTable[pMsg->iMethod](iface, pChannel, (PRPC_MESSAGE)pMsg, &dwPhase);
176 else /* pure interpreted */
177 NdrStubCall2(iface, pChannel, (PRPC_MESSAGE)pMsg, &dwPhase);
179 __EXCEPT(stub_filter)
181 DWORD dwExceptionCode = GetExceptionCode();
182 WARN("a stub call failed with exception 0x%08lx (%ld)\n", dwExceptionCode, dwExceptionCode);
183 if (FAILED(dwExceptionCode))
184 hr = dwExceptionCode;
186 hr = HRESULT_FROM_WIN32(dwExceptionCode);
193 LPRPCSTUBBUFFER WINAPI CStdStubBuffer_IsIIDSupported(LPRPCSTUBBUFFER iface,
196 CStdStubBuffer *This = (CStdStubBuffer *)iface;
197 TRACE("(%p)->IsIIDSupported(%s)\n",This,debugstr_guid(riid));
198 return IsEqualGUID(STUB_HEADER(This).piid, riid) ? iface : NULL;
201 ULONG WINAPI CStdStubBuffer_CountRefs(LPRPCSTUBBUFFER iface)
203 CStdStubBuffer *This = (CStdStubBuffer *)iface;
204 TRACE("(%p)->CountRefs()\n",This);
205 return This->RefCount;
208 HRESULT WINAPI CStdStubBuffer_DebugServerQueryInterface(LPRPCSTUBBUFFER iface,
211 CStdStubBuffer *This = (CStdStubBuffer *)iface;
212 TRACE("(%p)->DebugServerQueryInterface(%p)\n",This,ppv);
216 void WINAPI CStdStubBuffer_DebugServerRelease(LPRPCSTUBBUFFER iface,
219 CStdStubBuffer *This = (CStdStubBuffer *)iface;
220 TRACE("(%p)->DebugServerRelease(%p)\n",This,pv);
223 const IRpcStubBufferVtbl CStdStubBuffer_Vtbl =
225 CStdStubBuffer_QueryInterface,
226 CStdStubBuffer_AddRef,
228 CStdStubBuffer_Connect,
229 CStdStubBuffer_Disconnect,
230 CStdStubBuffer_Invoke,
231 CStdStubBuffer_IsIIDSupported,
232 CStdStubBuffer_CountRefs,
233 CStdStubBuffer_DebugServerQueryInterface,
234 CStdStubBuffer_DebugServerRelease
237 const MIDL_SERVER_INFO *CStdStubBuffer_GetServerInfo(IRpcStubBuffer *iface)
239 CStdStubBuffer *This = (CStdStubBuffer *)iface;
240 return STUB_HEADER(This).pServerInfo;
243 /************************************************************************
244 * NdrStubForwardingFunction [RPCRT4.@]
246 void __RPC_STUB NdrStubForwardingFunction( IRpcStubBuffer *This, IRpcChannelBuffer *pChannel,
247 PRPC_MESSAGE pMsg, DWORD *pdwStubPhase )
249 /* Once stub delegation is implemented, this should call
250 IRpcStubBuffer_Invoke on the stub's base interface. The
251 IRpcStubBuffer for this interface is stored at (void**)This-1.
252 The pChannel and pMsg parameters are passed intact
253 (RPCOLEMESSAGE is basically a RPC_MESSAGE). If Invoke returns
254 with a failure then an exception is raised (to see this, change
255 the return value in the test).
257 IRpcStubBuffer *base_this = *(IRpcStubBuffer**)((void**)This - 1);
258 HRESULT r = IRpcStubBuffer_Invoke(base_this, (RPCOLEMESSAGE*)pMsg, pChannel);
259 if(FAILED(r)) RpcRaiseException(r);
263 FIXME("Not implemented\n");