ole32/tests: Write-strings warnings fix.
[wine] / dlls / 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 #include "excpt.h"
29
30 #include "objbase.h"
31 #include "rpcproxy.h"
32
33 #include "wine/debug.h"
34 #include "wine/exception.h"
35
36 #include "cpsf.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(ole);
39
40 #define STUB_HEADER(This) (((CInterfaceStubHeader*)((This)->lpVtbl))[-1])
41
42 static WINE_EXCEPTION_FILTER(stub_filter)
43 {
44     if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
45         return EXCEPTION_CONTINUE_SEARCH;
46     return EXCEPTION_EXECUTE_HANDLER;
47 }
48
49 HRESULT WINAPI CStdStubBuffer_Construct(REFIID riid,
50                                        LPUNKNOWN pUnkServer,
51                                        PCInterfaceName name,
52                                        CInterfaceStubVtbl *vtbl,
53                                        LPPSFACTORYBUFFER pPSFactory,
54                                        LPRPCSTUBBUFFER *ppStub)
55 {
56   CStdStubBuffer *This;
57
58   TRACE("(%p,%p,%p,%p) %s\n", pUnkServer, vtbl, pPSFactory, ppStub, name);
59   TRACE("iid=%s\n", debugstr_guid(vtbl->header.piid));
60   TRACE("vtbl=%p\n", &vtbl->Vtbl);
61
62   if (!IsEqualGUID(vtbl->header.piid, riid)) {
63     ERR("IID mismatch during stub creation\n");
64     return RPC_E_UNEXPECTED;
65   }
66
67   This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CStdStubBuffer));
68   if (!This) return E_OUTOFMEMORY;
69
70   This->lpVtbl = &vtbl->Vtbl;
71   This->RefCount = 1;
72   This->pvServerObject = pUnkServer;
73   This->pPSFactory = pPSFactory;
74   *ppStub = (LPRPCSTUBBUFFER)This;
75
76   IUnknown_AddRef(This->pvServerObject);
77   IPSFactoryBuffer_AddRef(pPSFactory);
78   return S_OK;
79 }
80
81 HRESULT WINAPI CStdStubBuffer_QueryInterface(LPRPCSTUBBUFFER iface,
82                                             REFIID riid,
83                                             LPVOID *obj)
84 {
85   CStdStubBuffer *This = (CStdStubBuffer *)iface;
86   TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
87
88   if (IsEqualGUID(&IID_IUnknown,riid) ||
89       IsEqualGUID(&IID_IRpcStubBuffer,riid)) {
90     *obj = This;
91     This->RefCount++;
92     return S_OK;
93   }
94   return E_NOINTERFACE;
95 }
96
97 ULONG WINAPI CStdStubBuffer_AddRef(LPRPCSTUBBUFFER iface)
98 {
99   CStdStubBuffer *This = (CStdStubBuffer *)iface;
100   TRACE("(%p)->AddRef()\n",This);
101   return ++(This->RefCount);
102 }
103
104 ULONG WINAPI NdrCStdStubBuffer_Release(LPRPCSTUBBUFFER iface,
105                                       LPPSFACTORYBUFFER pPSF)
106 {
107   CStdStubBuffer *This = (CStdStubBuffer *)iface;
108   TRACE("(%p)->Release()\n",This);
109
110   if (!--(This->RefCount)) {
111     IRpcStubBuffer_Disconnect(iface);
112     if(This->pPSFactory)
113         IPSFactoryBuffer_Release(This->pPSFactory);
114     HeapFree(GetProcessHeap(),0,This);
115     return 0;
116   }
117   return This->RefCount;
118 }
119
120 ULONG WINAPI NdrCStdStubBuffer2_Release(LPRPCSTUBBUFFER iface,
121                                         LPPSFACTORYBUFFER pPSF)
122 {
123     FIXME("Not implemented\n");
124     return 0;
125 }
126
127 HRESULT WINAPI CStdStubBuffer_Connect(LPRPCSTUBBUFFER iface,
128                                      LPUNKNOWN lpUnkServer)
129 {
130   CStdStubBuffer *This = (CStdStubBuffer *)iface;
131   TRACE("(%p)->Connect(%p)\n",This,lpUnkServer);
132   This->pvServerObject = lpUnkServer;
133   return S_OK;
134 }
135
136 void WINAPI CStdStubBuffer_Disconnect(LPRPCSTUBBUFFER iface)
137 {
138   CStdStubBuffer *This = (CStdStubBuffer *)iface;
139   TRACE("(%p)->Disconnect()\n",This);
140   if (This->pvServerObject)
141   {
142     IUnknown_Release(This->pvServerObject);
143     This->pvServerObject = NULL;
144   }
145 }
146
147 HRESULT WINAPI CStdStubBuffer_Invoke(LPRPCSTUBBUFFER iface,
148                                     PRPCOLEMESSAGE pMsg,
149                                     LPRPCCHANNELBUFFER pChannel)
150 {
151   CStdStubBuffer *This = (CStdStubBuffer *)iface;
152   DWORD dwPhase = STUB_UNMARSHAL;
153   HRESULT hr = S_OK;
154
155   TRACE("(%p)->Invoke(%p,%p)\n",This,pMsg,pChannel);
156
157   __TRY
158   {
159     if (STUB_HEADER(This).pDispatchTable)
160       STUB_HEADER(This).pDispatchTable[pMsg->iMethod](iface, pChannel, (PRPC_MESSAGE)pMsg, &dwPhase);
161     else /* pure interpreted */
162       NdrStubCall2(iface, pChannel, (PRPC_MESSAGE)pMsg, &dwPhase);
163   }
164   __EXCEPT(stub_filter)
165   {
166     DWORD dwExceptionCode = GetExceptionCode();
167     WARN("a stub call failed with exception 0x%08lx (%ld)\n", dwExceptionCode, dwExceptionCode);
168     if (FAILED(dwExceptionCode))
169       hr = dwExceptionCode;
170     else
171       hr = HRESULT_FROM_WIN32(dwExceptionCode);
172   }
173   __ENDTRY
174
175   return hr;
176 }
177
178 LPRPCSTUBBUFFER WINAPI CStdStubBuffer_IsIIDSupported(LPRPCSTUBBUFFER iface,
179                                                     REFIID riid)
180 {
181   CStdStubBuffer *This = (CStdStubBuffer *)iface;
182   TRACE("(%p)->IsIIDSupported(%s)\n",This,debugstr_guid(riid));
183   return IsEqualGUID(STUB_HEADER(This).piid, riid) ? iface : NULL;
184 }
185
186 ULONG WINAPI CStdStubBuffer_CountRefs(LPRPCSTUBBUFFER iface)
187 {
188   CStdStubBuffer *This = (CStdStubBuffer *)iface;
189   TRACE("(%p)->CountRefs()\n",This);
190   return This->RefCount;
191 }
192
193 HRESULT WINAPI CStdStubBuffer_DebugServerQueryInterface(LPRPCSTUBBUFFER iface,
194                                                        LPVOID *ppv)
195 {
196   CStdStubBuffer *This = (CStdStubBuffer *)iface;
197   TRACE("(%p)->DebugServerQueryInterface(%p)\n",This,ppv);
198   return S_OK;
199 }
200
201 void WINAPI CStdStubBuffer_DebugServerRelease(LPRPCSTUBBUFFER iface,
202                                              LPVOID pv)
203 {
204   CStdStubBuffer *This = (CStdStubBuffer *)iface;
205   TRACE("(%p)->DebugServerRelease(%p)\n",This,pv);
206 }
207
208 const IRpcStubBufferVtbl CStdStubBuffer_Vtbl =
209 {
210     CStdStubBuffer_QueryInterface,
211     CStdStubBuffer_AddRef,
212     NULL,
213     CStdStubBuffer_Connect,
214     CStdStubBuffer_Disconnect,
215     CStdStubBuffer_Invoke,
216     CStdStubBuffer_IsIIDSupported,
217     CStdStubBuffer_CountRefs,
218     CStdStubBuffer_DebugServerQueryInterface,
219     CStdStubBuffer_DebugServerRelease
220 };
221
222 const MIDL_SERVER_INFO *CStdStubBuffer_GetServerInfo(IRpcStubBuffer *iface)
223 {
224   CStdStubBuffer *This = (CStdStubBuffer *)iface;
225   return STUB_HEADER(This).pServerInfo;
226 }