574f63b3e35247112a38c32c5406b10ce6dc7cd7
[reactos.git] / reactos / lib / rpcrt4 / cstub.c
1 /*
2 * COM stub (CStdStubBuffer) implementation
3 *
4 * Copyright 2001 Ove Kåven, TransGaming Technologies
5 *
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.
10 *
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.
15 *
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winerror.h"
28
29 #include "objbase.h"
30
31 #include "rpcproxy.h"
32
33 #include "wine/debug.h"
34
35 #include "cpsf.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(ole);
38
39 #define STUB_HEADER(This) (((CInterfaceStubHeader*)((This)->lpVtbl))[-1])
40
41 HRESULT WINAPI CStdStubBuffer_Construct(REFIID riid,
42 LPUNKNOWN pUnkServer,
43 PCInterfaceName name,
44 CInterfaceStubVtbl *vtbl,
45 LPPSFACTORYBUFFER pPSFactory,
46 LPRPCSTUBBUFFER *ppStub)
47 {
48 CStdStubBuffer *This;
49
50 TRACE("(%p,%p,%p,%p) %s\n", pUnkServer, vtbl, pPSFactory, ppStub, name);
51 TRACE("iid=%s\n", debugstr_guid(vtbl->header.piid));
52 TRACE("vtbl=%p\n", &vtbl->Vtbl);
53
54 if (!IsEqualGUID(vtbl->header.piid, riid)) {
55 ERR("IID mismatch during stub creation\n");
56 return RPC_E_UNEXPECTED;
57 }
58
59 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CStdStubBuffer));
60 if (!This) return E_OUTOFMEMORY;
61
62 This->lpVtbl = &vtbl->Vtbl;
63 This->RefCount = 1;
64 This->pvServerObject = pUnkServer;
65 This->pPSFactory = pPSFactory;
66 *ppStub = (LPRPCSTUBBUFFER)This;
67
68 IUnknown_AddRef(This->pvServerObject);
69 IPSFactoryBuffer_AddRef(pPSFactory);
70 return S_OK;
71 }
72
73 HRESULT WINAPI CStdStubBuffer_QueryInterface(LPRPCSTUBBUFFER iface,
74 REFIID riid,
75 LPVOID *obj)
76 {
77 CStdStubBuffer *This = (CStdStubBuffer *)iface;
78 TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
79
80 if (IsEqualGUID(&IID_IUnknown,riid) ||
81 IsEqualGUID(&IID_IRpcStubBuffer,riid)) {
82 *obj = This;
83 This->RefCount++;
84 return S_OK;
85 }
86 return E_NOINTERFACE;
87 }
88
89 ULONG WINAPI CStdStubBuffer_AddRef(LPRPCSTUBBUFFER iface)
90 {
91 CStdStubBuffer *This = (CStdStubBuffer *)iface;
92 TRACE("(%p)->AddRef()\n",This);
93 return ++(This->RefCount);
94 }
95
96 ULONG WINAPI NdrCStdStubBuffer_Release(LPRPCSTUBBUFFER iface,
97 LPPSFACTORYBUFFER pPSF)
98 {
99 CStdStubBuffer *This = (CStdStubBuffer *)iface;
100 TRACE("(%p)->Release()\n",This);
101
102 if (!--(This->RefCount)) {
103 if(This->pvServerObject)
104 IUnknown_Release(This->pvServerObject);
105 if(This->pPSFactory)
106 IPSFactoryBuffer_Release(This->pPSFactory);
107 HeapFree(GetProcessHeap(),0,This);
108 return 0;
109 }
110 return This->RefCount;
111 }
112
113 HRESULT WINAPI CStdStubBuffer_Connect(LPRPCSTUBBUFFER iface,
114 LPUNKNOWN lpUnkServer)
115 {
116 CStdStubBuffer *This = (CStdStubBuffer *)iface;
117 TRACE("(%p)->Connect(%p)\n",This,lpUnkServer);
118 This->pvServerObject = lpUnkServer;
119 return S_OK;
120 }
121
122 void WINAPI CStdStubBuffer_Disconnect(LPRPCSTUBBUFFER iface)
123 {
124 CStdStubBuffer *This = (CStdStubBuffer *)iface;
125 TRACE("(%p)->Disconnect()\n",This);
126 This->pvServerObject = NULL;
127 }
128
129 HRESULT WINAPI CStdStubBuffer_Invoke(LPRPCSTUBBUFFER iface,
130 PRPCOLEMESSAGE pMsg,
131 LPRPCCHANNELBUFFER pChannel)
132 {
133 CStdStubBuffer *This = (CStdStubBuffer *)iface;
134 DWORD dwPhase = STUB_UNMARSHAL;
135 TRACE("(%p)->Invoke(%p,%p)\n",This,pMsg,pChannel);
136
137 STUB_HEADER(This).pDispatchTable[pMsg->iMethod](iface, pChannel, (PRPC_MESSAGE)pMsg, &dwPhase);
138 return S_OK;
139 }
140
141 LPRPCSTUBBUFFER WINAPI CStdStubBuffer_IsIIDSupported(LPRPCSTUBBUFFER iface,
142 REFIID riid)
143 {
144 CStdStubBuffer *This = (CStdStubBuffer *)iface;
145 TRACE("(%p)->IsIIDSupported(%s)\n",This,debugstr_guid(riid));
146 return IsEqualGUID(STUB_HEADER(This).piid, riid) ? iface : NULL;
147 }
148
149 ULONG WINAPI CStdStubBuffer_CountRefs(LPRPCSTUBBUFFER iface)
150 {
151 CStdStubBuffer *This = (CStdStubBuffer *)iface;
152 TRACE("(%p)->CountRefs()\n",This);
153 return This->RefCount;
154 }
155
156 HRESULT WINAPI CStdStubBuffer_DebugServerQueryInterface(LPRPCSTUBBUFFER iface,
157 LPVOID *ppv)
158 {
159 CStdStubBuffer *This = (CStdStubBuffer *)iface;
160 TRACE("(%p)->DebugServerQueryInterface(%p)\n",This,ppv);
161 return S_OK;
162 }
163
164 void WINAPI CStdStubBuffer_DebugServerRelease(LPRPCSTUBBUFFER iface,
165 LPVOID pv)
166 {
167 CStdStubBuffer *This = (CStdStubBuffer *)iface;
168 TRACE("(%p)->DebugServerRelease(%p)\n",This,pv);
169 }