- Implement CoDisconnectObject.
[wine] / dlls / 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         IUnknownVtbl *lpVtbl;
41         DWORD ref;
42         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 HRESULT WINAPI IiFTMUnknown_fnQueryInterface (IUnknown * iface, REFIID riid, LPVOID * ppv)
55 {
56
57     FTMarshalImpl *This = (FTMarshalImpl *)iface;
58
59     TRACE ("\n");
60     *ppv = NULL;
61
62     if (IsEqualIID (&IID_IUnknown, riid))
63         *ppv = _IFTMUnknown_ (This);
64     else if (IsEqualIID (&IID_IMarshal, riid))
65         *ppv = _IFTMarshal_ (This);
66     else {
67         FIXME ("No interface for %s.\n", debugstr_guid (riid));
68         return E_NOINTERFACE;
69     }
70     IUnknown_AddRef ((IUnknown *) * ppv);
71     return S_OK;
72 }
73
74 ULONG WINAPI IiFTMUnknown_fnAddRef (IUnknown * iface)
75 {
76
77     FTMarshalImpl *This = (FTMarshalImpl *)iface;
78
79     TRACE ("\n");
80     return InterlockedIncrement (&This->ref);
81 }
82
83 ULONG WINAPI IiFTMUnknown_fnRelease (IUnknown * iface)
84 {
85
86     FTMarshalImpl *This = (FTMarshalImpl *)iface;
87
88     TRACE ("\n");
89     if (InterlockedDecrement (&This->ref))
90         return This->ref;
91     HeapFree (GetProcessHeap (), 0, This);
92     return 0;
93 }
94
95 static IUnknownVtbl iunkvt =
96 {
97         IiFTMUnknown_fnQueryInterface,
98         IiFTMUnknown_fnAddRef,
99         IiFTMUnknown_fnRelease
100 };
101
102 HRESULT WINAPI FTMarshalImpl_QueryInterface (LPMARSHAL iface, REFIID riid, LPVOID * ppv)
103 {
104
105     _ICOM_THIS_From_IFTMarshal (FTMarshalImpl, iface);
106
107     TRACE ("(%p)->(\n\tIID:\t%s,%p)\n", This, debugstr_guid (riid), ppv);
108     return IUnknown_QueryInterface (This->pUnkOuter, riid, ppv);
109 }
110
111 ULONG WINAPI FTMarshalImpl_AddRef (LPMARSHAL iface)
112 {
113
114     _ICOM_THIS_From_IFTMarshal (FTMarshalImpl, iface);
115
116     TRACE ("\n");
117     return IUnknown_AddRef (This->pUnkOuter);
118 }
119
120 ULONG WINAPI FTMarshalImpl_Release (LPMARSHAL iface)
121 {
122
123     _ICOM_THIS_From_IFTMarshal (FTMarshalImpl, iface);
124
125     TRACE ("\n");
126     return IUnknown_Release (This->pUnkOuter);
127 }
128
129 HRESULT WINAPI FTMarshalImpl_GetUnmarshalClass (LPMARSHAL iface, REFIID riid, void *pv, DWORD dwDestContext,
130                                                 void *pvDestContext, DWORD mshlflags, CLSID * pCid)
131 {
132     FIXME ("(), stub!\n");
133     return S_OK;
134 }
135
136 HRESULT WINAPI FTMarshalImpl_GetMarshalSizeMax (LPMARSHAL iface, REFIID riid, void *pv, DWORD dwDestContext,
137                                                 void *pvDestContext, DWORD mshlflags, DWORD * pSize)
138 {
139
140     IMarshal *pMarshal = NULL;
141     HRESULT hres;
142
143     _ICOM_THIS_From_IFTMarshal (FTMarshalImpl, iface);
144
145     FIXME ("(), stub!\n");
146
147     /* if the marshalling happens inside the same process the interface pointer is
148        copied between the apartments */
149     if (dwDestContext == MSHCTX_INPROC || dwDestContext == MSHCTX_CROSSCTX) {
150         *pSize = sizeof (This);
151         return S_OK;
152     }
153
154     /* use the standard marshaller to handle all other cases */
155     CoGetStandardMarshal (riid, pv, dwDestContext, pvDestContext, mshlflags, &pMarshal);
156     hres = IMarshal_GetMarshalSizeMax (pMarshal, riid, pv, dwDestContext, pvDestContext, mshlflags, pSize);
157     IMarshal_Release (pMarshal);
158     return hres;
159 }
160
161 HRESULT WINAPI FTMarshalImpl_MarshalInterface (LPMARSHAL iface, IStream * pStm, REFIID riid, void *pv,
162                                                DWORD dwDestContext, void *pvDestContext, DWORD mshlflags)
163 {
164
165     IMarshal *pMarshal = NULL;
166     HRESULT hres;
167
168     _ICOM_THIS_From_IFTMarshal (FTMarshalImpl, iface);
169
170     FIXME ("(), stub!\n");
171
172     /* if the marshalling happens inside the same process the interface pointer is
173        copied between the apartments */
174     if (dwDestContext == MSHCTX_INPROC || dwDestContext == MSHCTX_CROSSCTX) {
175         return IStream_Write (pStm, This, sizeof (This), 0);
176     }
177
178     /* use the standard marshaler to handle all other cases */
179     CoGetStandardMarshal (riid, pv, dwDestContext, pvDestContext, mshlflags, &pMarshal);
180     hres = IMarshal_MarshalInterface (pMarshal, pStm, riid, pv, dwDestContext, pvDestContext, mshlflags);
181     IMarshal_Release (pMarshal);
182     return hres;
183 }
184
185 HRESULT WINAPI FTMarshalImpl_UnmarshalInterface (LPMARSHAL iface, IStream * pStm, REFIID riid, void **ppv)
186 {
187     FIXME ("(), stub!\n");
188     return S_OK;
189 }
190
191 HRESULT WINAPI FTMarshalImpl_ReleaseMarshalData (LPMARSHAL iface, IStream * pStm)
192 {
193     FIXME ("(), stub!\n");
194     return S_OK;
195 }
196
197 HRESULT WINAPI FTMarshalImpl_DisconnectObject (LPMARSHAL iface, DWORD dwReserved)
198 {
199     FIXME ("(), stub!\n");
200     return S_OK;
201 }
202
203 IMarshalVtbl ftmvtbl =
204 {
205         FTMarshalImpl_QueryInterface,
206         FTMarshalImpl_AddRef,
207         FTMarshalImpl_Release,
208         FTMarshalImpl_GetUnmarshalClass,
209         FTMarshalImpl_GetMarshalSizeMax,
210         FTMarshalImpl_MarshalInterface,
211         FTMarshalImpl_UnmarshalInterface,
212         FTMarshalImpl_ReleaseMarshalData,
213         FTMarshalImpl_DisconnectObject
214 };
215
216 /***********************************************************************
217  *          CoCreateFreeThreadedMarshaler [OLE32.@]
218  *
219  */
220 HRESULT WINAPI CoCreateFreeThreadedMarshaler (LPUNKNOWN punkOuter, LPUNKNOWN * ppunkMarshal)
221 {
222
223     FTMarshalImpl *ftm;
224
225     TRACE ("(%p %p)\n", punkOuter, ppunkMarshal);
226
227     ftm = (FTMarshalImpl *) HeapAlloc (GetProcessHeap (), 0, sizeof (FTMarshalImpl));
228     if (!ftm)
229         return E_OUTOFMEMORY;
230
231     ftm->lpVtbl = &iunkvt;
232     ftm->lpvtblFTM = &ftmvtbl;
233     ftm->ref = 1;
234     ftm->pUnkOuter = punkOuter;
235
236     *ppunkMarshal = _IFTMUnknown_ (ftm);
237     return S_OK;
238 }