0a17b3489dec793a04f9c06c154a3d25165b7db0
[reactos.git] / reactos / lib / ole32 / ftmarshal.c
1 /*
2 * free threaded marshaller
3 *
4 * Copyright 2002 Juergen Schmied
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 "config.h"
22
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <assert.h>
28
29 #define COBJMACROS
30
31 #include "windef.h"
32 #include "winbase.h"
33 #include "objbase.h"
34
35 #include "wine/debug.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(ole);
38
39 typedef struct _FTMarshalImpl {
40 const IUnknownVtbl *lpVtbl;
41 DWORD ref;
42 const IMarshalVtbl *lpvtblFTM;
43
44 IUnknown *pUnkOuter;
45 } FTMarshalImpl;
46
47 #define _IFTMUnknown_(This)(IUnknown*)&(This->lpVtbl)
48 #define _IFTMarshal_(This) (IMarshal*)&(This->lpvtblFTM)
49
50 #define _IFTMarshall_Offset ((int)(&(((FTMarshalImpl*)0)->lpvtblFTM)))
51 #define _ICOM_THIS_From_IFTMarshal(class, name) class* This = (class*)(((char*)name)-_IFTMarshall_Offset);
52
53 /* inner IUnknown to handle aggregation */
54 static HRESULT WINAPI
55 IiFTMUnknown_fnQueryInterface (IUnknown * iface, REFIID riid, LPVOID * ppv)
56 {
57
58 FTMarshalImpl *This = (FTMarshalImpl *)iface;
59
60 TRACE ("\n");
61 *ppv = NULL;
62
63 if (IsEqualIID (&IID_IUnknown, riid))
64 *ppv = _IFTMUnknown_ (This);
65 else if (IsEqualIID (&IID_IMarshal, riid))
66 *ppv = _IFTMarshal_ (This);
67 else {
68 FIXME ("No interface for %s.\n", debugstr_guid (riid));
69 return E_NOINTERFACE;
70 }
71 IUnknown_AddRef ((IUnknown *) * ppv);
72 return S_OK;
73 }
74
75 static ULONG WINAPI IiFTMUnknown_fnAddRef (IUnknown * iface)
76 {
77
78 FTMarshalImpl *This = (FTMarshalImpl *)iface;
79
80 TRACE ("\n");
81 return InterlockedIncrement (&This->ref);
82 }
83
84 static ULONG WINAPI IiFTMUnknown_fnRelease (IUnknown * iface)
85 {
86
87 FTMarshalImpl *This = (FTMarshalImpl *)iface;
88
89 TRACE ("\n");
90 if (InterlockedDecrement (&This->ref))
91 return This->ref;
92 HeapFree (GetProcessHeap (), 0, This);
93 return 0;
94 }
95
96 static const IUnknownVtbl iunkvt =
97 {
98 IiFTMUnknown_fnQueryInterface,
99 IiFTMUnknown_fnAddRef,
100 IiFTMUnknown_fnRelease
101 };
102
103 static HRESULT WINAPI
104 FTMarshalImpl_QueryInterface (LPMARSHAL iface, REFIID riid, LPVOID * ppv)
105 {
106
107 _ICOM_THIS_From_IFTMarshal (FTMarshalImpl, iface);
108
109 TRACE ("(%p)->(\n\tIID:\t%s,%p)\n", This, debugstr_guid (riid), ppv);
110 return IUnknown_QueryInterface (This->pUnkOuter, riid, ppv);
111 }
112
113 static ULONG WINAPI
114 FTMarshalImpl_AddRef (LPMARSHAL iface)
115 {
116
117 _ICOM_THIS_From_IFTMarshal (FTMarshalImpl, iface);
118
119 TRACE ("\n");
120 return IUnknown_AddRef (This->pUnkOuter);
121 }
122
123 static ULONG WINAPI
124 FTMarshalImpl_Release (LPMARSHAL iface)
125 {
126
127 _ICOM_THIS_From_IFTMarshal (FTMarshalImpl, iface);
128
129 TRACE ("\n");
130 return IUnknown_Release (This->pUnkOuter);
131 }
132
133 static HRESULT WINAPI
134 FTMarshalImpl_GetUnmarshalClass (LPMARSHAL iface, REFIID riid, void *pv, DWORD dwDestContext,
135 void *pvDestContext, DWORD mshlflags, CLSID * pCid)
136 {
137 FIXME ("(), stub!\n");
138 return S_OK;
139 }
140
141 static HRESULT WINAPI
142 FTMarshalImpl_GetMarshalSizeMax (LPMARSHAL iface, REFIID riid, void *pv, DWORD dwDestContext,
143 void *pvDestContext, DWORD mshlflags, DWORD * pSize)
144 {
145
146 IMarshal *pMarshal = NULL;
147 HRESULT hres;
148
149 _ICOM_THIS_From_IFTMarshal (FTMarshalImpl, iface);
150
151 FIXME ("(), stub!\n");
152
153 /* if the marshalling happens inside the same process the interface pointer is
154 copied between the apartments */
155 if (dwDestContext == MSHCTX_INPROC || dwDestContext == MSHCTX_CROSSCTX) {
156 *pSize = sizeof (This);
157 return S_OK;
158 }
159
160 /* use the standard marshaller to handle all other cases */
161 CoGetStandardMarshal (riid, pv, dwDestContext, pvDestContext, mshlflags, &pMarshal);
162 hres = IMarshal_GetMarshalSizeMax (pMarshal, riid, pv, dwDestContext, pvDestContext, mshlflags, pSize);
163 IMarshal_Release (pMarshal);
164 return hres;
165 }
166
167 static HRESULT WINAPI
168 FTMarshalImpl_MarshalInterface (LPMARSHAL iface, IStream * pStm, REFIID riid, void *pv,
169 DWORD dwDestContext, void *pvDestContext, DWORD mshlflags)
170 {
171
172 IMarshal *pMarshal = NULL;
173 HRESULT hres;
174
175 _ICOM_THIS_From_IFTMarshal (FTMarshalImpl, iface);
176
177 FIXME ("(), stub!\n");
178
179 /* if the marshalling happens inside the same process the interface pointer is
180 copied between the apartments */
181 if (dwDestContext == MSHCTX_INPROC || dwDestContext == MSHCTX_CROSSCTX) {
182 return IStream_Write (pStm, This, sizeof (This), 0);
183 }
184
185 /* use the standard marshaler to handle all other cases */
186 CoGetStandardMarshal (riid, pv, dwDestContext, pvDestContext, mshlflags, &pMarshal);
187 hres = IMarshal_MarshalInterface (pMarshal, pStm, riid, pv, dwDestContext, pvDestContext, mshlflags);
188 IMarshal_Release (pMarshal);
189 return hres;
190 }
191
192 static HRESULT WINAPI
193 FTMarshalImpl_UnmarshalInterface (LPMARSHAL iface, IStream * pStm, REFIID riid, void **ppv)
194 {
195 FIXME ("(), stub!\n");
196 return S_OK;
197 }
198
199 static HRESULT WINAPI FTMarshalImpl_ReleaseMarshalData (LPMARSHAL iface, IStream * pStm)
200 {
201 FIXME ("(), stub!\n");
202 return S_OK;
203 }
204
205 static HRESULT WINAPI FTMarshalImpl_DisconnectObject (LPMARSHAL iface, DWORD dwReserved)
206 {
207 FIXME ("(), stub!\n");
208 return S_OK;
209 }
210
211 static const IMarshalVtbl ftmvtbl =
212 {
213 FTMarshalImpl_QueryInterface,
214 FTMarshalImpl_AddRef,
215 FTMarshalImpl_Release,
216 FTMarshalImpl_GetUnmarshalClass,
217 FTMarshalImpl_GetMarshalSizeMax,
218 FTMarshalImpl_MarshalInterface,
219 FTMarshalImpl_UnmarshalInterface,
220 FTMarshalImpl_ReleaseMarshalData,
221 FTMarshalImpl_DisconnectObject
222 };
223
224 /***********************************************************************
225 * CoCreateFreeThreadedMarshaler [OLE32.@]
226 *
227 */
228 HRESULT WINAPI CoCreateFreeThreadedMarshaler (LPUNKNOWN punkOuter, LPUNKNOWN * ppunkMarshal)
229 {
230
231 FTMarshalImpl *ftm;
232
233 TRACE ("(%p %p)\n", punkOuter, ppunkMarshal);
234
235 ftm = HeapAlloc (GetProcessHeap (), 0, sizeof (FTMarshalImpl));
236 if (!ftm)
237 return E_OUTOFMEMORY;
238
239 ftm->lpVtbl = &iunkvt;
240 ftm->lpvtblFTM = &ftmvtbl;
241 ftm->ref = 1;
242 ftm->pUnkOuter = punkOuter;
243
244 *ppunkMarshal = _IFTMUnknown_ (ftm);
245 return S_OK;
246 }