2 * Implementation of the StdGlobalInterfaceTable object
4 * The GlobalInterfaceTable (GIT) object is used to marshal interfaces between
5 * threading apartments (contexts). When you want to pass an interface but not
6 * as a parameter, it wouldn't get marshalled automatically, so you can use this
7 * object to insert the interface into a table, and you get back a cookie.
8 * Then when it's retrieved, it'll be unmarshalled into the right apartment.
10 * Copyright 2003 Mike Hearn <mike@theoretic.com>
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #define NONAMELESSUNION
31 #define NONAMELESSSTRUCT
40 #include "compobj_private.h"
42 #include "wine/list.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
47 /****************************************************************************
48 * StdGlobalInterfaceTable definition
50 * This class implements IGlobalInterfaceTable and is a process-wide singleton
51 * used for marshalling interfaces between threading apartments using cookies.
54 /* Each entry in the linked list of GIT entries */
55 typedef struct StdGITEntry
58 IID iid
; /* IID of the interface */
59 IStream
* stream
; /* Holds the marshalled interface */
65 typedef struct StdGlobalInterfaceTableImpl
67 const IGlobalInterfaceTableVtbl
*lpVtbl
;
73 } StdGlobalInterfaceTableImpl
;
75 void* StdGlobalInterfaceTableInstance
;
77 static CRITICAL_SECTION git_section
;
78 static CRITICAL_SECTION_DEBUG critsect_debug
=
81 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
82 0, 0, { (DWORD_PTR
)(__FILE__
": global interface table") }
84 static CRITICAL_SECTION git_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
87 /** This destroys it again. It should revoke all the held interfaces first **/
88 static void StdGlobalInterfaceTable_Destroy(void* self
)
90 TRACE("(%p)\n", self
);
91 FIXME("Revoke held interfaces here\n");
93 HeapFree(GetProcessHeap(), 0, self
);
94 StdGlobalInterfaceTableInstance
= NULL
;
98 * A helper function to traverse the list and find the entry that matches the cookie.
99 * Returns NULL if not found. Must be called inside git_section critical section.
102 StdGlobalInterfaceTable_FindEntry(IGlobalInterfaceTable
* iface
, DWORD cookie
)
104 StdGlobalInterfaceTableImpl
* const self
= (StdGlobalInterfaceTableImpl
*) iface
;
107 TRACE("iface=%p, cookie=0x%x\n", iface
, (UINT
)cookie
);
109 LIST_FOR_EACH_ENTRY(e
, &self
->list
, StdGITEntry
, entry
) {
110 if (e
->cookie
== cookie
)
114 TRACE("Entry not found\n");
119 * Here's the boring boilerplate stuff for IUnknown
122 static HRESULT WINAPI
123 StdGlobalInterfaceTable_QueryInterface(IGlobalInterfaceTable
* iface
,
124 REFIID riid
, void** ppvObject
)
126 /* Make sure silly coders can't crash us */
127 if (ppvObject
== 0) return E_INVALIDARG
;
129 *ppvObject
= 0; /* assume we don't have the interface */
131 /* Do we implement that interface? */
132 if (IsEqualIID(&IID_IUnknown
, riid
) ||
133 IsEqualIID(&IID_IGlobalInterfaceTable
, riid
))
136 return E_NOINTERFACE
;
138 /* Now inc the refcount */
139 IGlobalInterfaceTable_AddRef(iface
);
144 StdGlobalInterfaceTable_AddRef(IGlobalInterfaceTable
* iface
)
146 StdGlobalInterfaceTableImpl
* const self
= (StdGlobalInterfaceTableImpl
*) iface
;
148 /* InterlockedIncrement(&self->ref); */
153 StdGlobalInterfaceTable_Release(IGlobalInterfaceTable
* iface
)
155 StdGlobalInterfaceTableImpl
* const self
= (StdGlobalInterfaceTableImpl
*) iface
;
157 /* InterlockedDecrement(&self->ref); */
158 if (self
->ref
== 0) {
159 /* Hey ho, it's time to go, so long again 'till next weeks show! */
160 StdGlobalInterfaceTable_Destroy(self
);
168 * Now implement the actual IGlobalInterfaceTable interface
171 static HRESULT WINAPI
172 StdGlobalInterfaceTable_RegisterInterfaceInGlobal(
173 IGlobalInterfaceTable
* iface
, IUnknown
* pUnk
,
174 REFIID riid
, DWORD
* pdwCookie
)
176 StdGlobalInterfaceTableImpl
* const self
= (StdGlobalInterfaceTableImpl
*) iface
;
177 IStream
* stream
= NULL
;
182 TRACE("iface=%p, pUnk=%p, riid=%s, pdwCookie=0x%p\n", iface
, pUnk
, debugstr_guid(riid
), pdwCookie
);
184 if (pUnk
== NULL
) return E_INVALIDARG
;
186 /* marshal the interface */
187 TRACE("About to marshal the interface\n");
189 hres
= CreateStreamOnHGlobal(0, TRUE
, &stream
);
190 if (hres
) return hres
;
191 hres
= CoMarshalInterface(stream
, riid
, pUnk
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_TABLESTRONG
);
194 IStream_Release(stream
);
199 IStream_Seek(stream
, zero
, STREAM_SEEK_SET
, NULL
);
201 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(StdGITEntry
));
202 if (entry
== NULL
) return E_OUTOFMEMORY
;
204 EnterCriticalSection(&git_section
);
207 entry
->stream
= stream
;
208 entry
->cookie
= self
->nextCookie
;
209 self
->nextCookie
++; /* inc the cookie count */
211 /* insert the new entry at the end of the list */
212 list_add_tail(&self
->list
, &entry
->entry
);
214 /* and return the cookie */
215 *pdwCookie
= entry
->cookie
;
217 LeaveCriticalSection(&git_section
);
219 TRACE("Cookie is 0x%x\n", entry
->cookie
);
223 static HRESULT WINAPI
224 StdGlobalInterfaceTable_RevokeInterfaceFromGlobal(
225 IGlobalInterfaceTable
* iface
, DWORD dwCookie
)
230 TRACE("iface=%p, dwCookie=0x%x\n", iface
, (UINT
)dwCookie
);
232 EnterCriticalSection(&git_section
);
234 entry
= StdGlobalInterfaceTable_FindEntry(iface
, dwCookie
);
236 TRACE("Entry not found\n");
237 LeaveCriticalSection(&git_section
);
238 return E_INVALIDARG
; /* not found */
241 list_remove(&entry
->entry
);
243 LeaveCriticalSection(&git_section
);
245 /* Free the stream */
246 hr
= CoReleaseMarshalData(entry
->stream
);
249 WARN("Failed to release marshal data, hr = 0x%08x\n", hr
);
252 IStream_Release(entry
->stream
);
254 HeapFree(GetProcessHeap(), 0, entry
);
258 static HRESULT WINAPI
259 StdGlobalInterfaceTable_GetInterfaceFromGlobal(
260 IGlobalInterfaceTable
* iface
, DWORD dwCookie
,
261 REFIID riid
, void **ppv
)
267 TRACE("dwCookie=0x%x, riid=%s, ppv=%p\n", dwCookie
, debugstr_guid(riid
), ppv
);
269 EnterCriticalSection(&git_section
);
271 entry
= StdGlobalInterfaceTable_FindEntry(iface
, dwCookie
);
273 WARN("Entry for cookie 0x%x not found\n", dwCookie
);
274 LeaveCriticalSection(&git_section
);
278 TRACE("entry=%p\n", entry
);
280 hres
= IStream_Clone(entry
->stream
, &stream
);
282 LeaveCriticalSection(&git_section
);
285 WARN("Failed to clone stream with error 0x%08x\n", hres
);
289 /* unmarshal the interface */
290 hres
= CoUnmarshalInterface(stream
, riid
, ppv
);
291 IStream_Release(stream
);
294 WARN("Failed to unmarshal stream\n");
298 TRACE("ppv=%p\n", *ppv
);
302 /* Classfactory definition - despite what MSDN says, some programs need this */
304 static HRESULT WINAPI
305 GITCF_QueryInterface(LPCLASSFACTORY iface
,REFIID riid
, LPVOID
*ppv
)
308 if (IsEqualIID(riid
,&IID_IUnknown
) ||
309 IsEqualIID(riid
,&IID_IGlobalInterfaceTable
))
311 *ppv
= (LPVOID
)iface
;
314 return E_NOINTERFACE
;
317 static ULONG WINAPI
GITCF_AddRef(LPCLASSFACTORY iface
)
322 static ULONG WINAPI
GITCF_Release(LPCLASSFACTORY iface
)
327 static HRESULT WINAPI
328 GITCF_CreateInstance(LPCLASSFACTORY iface
, LPUNKNOWN pUnk
,
329 REFIID riid
, LPVOID
*ppv
)
331 if (IsEqualIID(riid
,&IID_IGlobalInterfaceTable
)) {
332 if (StdGlobalInterfaceTableInstance
== NULL
)
333 StdGlobalInterfaceTableInstance
= StdGlobalInterfaceTable_Construct();
334 return IGlobalInterfaceTable_QueryInterface( (IGlobalInterfaceTable
*) StdGlobalInterfaceTableInstance
, riid
, ppv
);
337 FIXME("(%s), not supported.\n",debugstr_guid(riid
));
338 return E_NOINTERFACE
;
341 static HRESULT WINAPI
GITCF_LockServer(LPCLASSFACTORY iface
, BOOL fLock
)
343 FIXME("(%d), stub!\n",fLock
);
347 static const IClassFactoryVtbl GITClassFactoryVtbl
= {
348 GITCF_QueryInterface
,
351 GITCF_CreateInstance
,
355 static const IClassFactoryVtbl
*PGITClassFactoryVtbl
= &GITClassFactoryVtbl
;
357 HRESULT
StdGlobalInterfaceTable_GetFactory(LPVOID
*ppv
)
359 *ppv
= &PGITClassFactoryVtbl
;
360 TRACE("Returning GIT classfactory\n");
364 /* Virtual function table */
365 static const IGlobalInterfaceTableVtbl StdGlobalInterfaceTableImpl_Vtbl
=
367 StdGlobalInterfaceTable_QueryInterface
,
368 StdGlobalInterfaceTable_AddRef
,
369 StdGlobalInterfaceTable_Release
,
370 StdGlobalInterfaceTable_RegisterInterfaceInGlobal
,
371 StdGlobalInterfaceTable_RevokeInterfaceFromGlobal
,
372 StdGlobalInterfaceTable_GetInterfaceFromGlobal
375 /** This function constructs the GIT. It should only be called once **/
376 void* StdGlobalInterfaceTable_Construct(void)
378 StdGlobalInterfaceTableImpl
* newGIT
;
380 newGIT
= HeapAlloc(GetProcessHeap(), 0, sizeof(StdGlobalInterfaceTableImpl
));
381 if (newGIT
== 0) return newGIT
;
383 newGIT
->lpVtbl
= &StdGlobalInterfaceTableImpl_Vtbl
;
384 newGIT
->ref
= 1; /* Initialise the reference count */
385 list_init(&newGIT
->list
);
386 newGIT
->nextCookie
= 0xf100; /* that's where windows starts, so that's where we start */
387 TRACE("Created the GIT at %p\n", newGIT
);
389 return (void*)newGIT
;