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
36 #define NONAMELESSUNION
37 #define NONAMELESSSTRUCT
48 #include "compobj_private.h"
50 #include "wine/debug.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(ole);
54 /****************************************************************************
55 * StdGlobalInterfaceTable definition
57 * This class implements IGlobalInterfaceTable and is a process-wide singleton
58 * used for marshalling interfaces between threading apartments using cookies.
61 /* Each entry in the linked list of GIT entries */
62 typedef struct StdGITEntry
65 IID iid; /* IID of the interface */
66 IStream* stream; /* Holds the marshalled interface */
68 struct StdGITEntry* next;
69 struct StdGITEntry* prev;
73 typedef struct StdGlobalInterfaceTableImpl
75 const IGlobalInterfaceTableVtbl *lpVtbl;
78 struct StdGITEntry* firstEntry;
79 struct StdGITEntry* lastEntry;
82 } StdGlobalInterfaceTableImpl;
84 void* StdGlobalInterfaceTableInstance;
86 static CRITICAL_SECTION git_section;
87 static CRITICAL_SECTION_DEBUG critsect_debug =
90 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
91 0, 0, { (DWORD_PTR)(__FILE__ ": global interface table") }
93 static CRITICAL_SECTION git_section = { &critsect_debug, -1, 0, 0, 0, 0 };
96 /** This destroys it again. It should revoke all the held interfaces first **/
97 void StdGlobalInterfaceTable_Destroy(void* self) {
98 TRACE("(%p)\n", self);
99 FIXME("Revoke held interfaces here\n");
101 HeapFree(GetProcessHeap(), 0, self);
102 StdGlobalInterfaceTableInstance = NULL;
106 * A helper function to traverse the list and find the entry that matches the cookie.
107 * Returns NULL if not found
110 StdGlobalInterfaceTable_FindEntry(IGlobalInterfaceTable* iface, DWORD cookie)
112 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
115 TRACE("iface=%p, cookie=0x%x\n", iface, (UINT)cookie);
117 EnterCriticalSection(&git_section);
118 e = self->firstEntry;
120 if (e->cookie == cookie) {
121 LeaveCriticalSection(&git_section);
126 LeaveCriticalSection(&git_section);
128 TRACE("Entry not found\n");
133 * Here's the boring boilerplate stuff for IUnknown
136 static HRESULT WINAPI
137 StdGlobalInterfaceTable_QueryInterface(IGlobalInterfaceTable* iface,
138 REFIID riid, void** ppvObject)
140 /* Make sure silly coders can't crash us */
141 if (ppvObject == 0) return E_INVALIDARG;
143 *ppvObject = 0; /* assume we don't have the interface */
145 /* Do we implement that interface? */
146 if (IsEqualIID(&IID_IUnknown, riid) ||
147 IsEqualIID(&IID_IGlobalInterfaceTable, riid))
150 return E_NOINTERFACE;
152 /* Now inc the refcount */
153 IGlobalInterfaceTable_AddRef(iface);
158 StdGlobalInterfaceTable_AddRef(IGlobalInterfaceTable* iface)
160 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
162 /* InterlockedIncrement(&self->ref); */
167 StdGlobalInterfaceTable_Release(IGlobalInterfaceTable* iface)
169 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
171 /* InterlockedDecrement(&self->ref); */
172 if (self->ref == 0) {
173 /* Hey ho, it's time to go, so long again 'till next weeks show! */
174 StdGlobalInterfaceTable_Destroy(self);
182 * Now implement the actual IGlobalInterfaceTable interface
185 static HRESULT WINAPI
186 StdGlobalInterfaceTable_RegisterInterfaceInGlobal(
187 IGlobalInterfaceTable* iface, IUnknown* pUnk,
188 REFIID riid, DWORD* pdwCookie)
190 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
191 IStream* stream = NULL;
196 TRACE("iface=%p, pUnk=%p, riid=%s, pdwCookie=0x%p\n", iface, pUnk, debugstr_guid(riid), pdwCookie);
198 if (pUnk == NULL) return E_INVALIDARG;
200 /* marshal the interface */
201 TRACE("About to marshal the interface\n");
203 hres = CreateStreamOnHGlobal(0, TRUE, &stream);
204 if (hres) return hres;
205 hres = CoMarshalInterface(stream, riid, pUnk, MSHCTX_INPROC, NULL, MSHLFLAGS_TABLESTRONG);
208 IStream_Release(stream);
213 IStream_Seek(stream, zero, SEEK_SET, NULL);
215 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(StdGITEntry));
216 if (entry == NULL) return E_OUTOFMEMORY;
218 EnterCriticalSection(&git_section);
221 entry->stream = stream;
222 entry->cookie = self->nextCookie;
223 self->nextCookie++; /* inc the cookie count */
225 /* insert the new entry at the end of the list */
227 entry->prev = self->lastEntry;
228 if (entry->prev) entry->prev->next = entry;
229 else self->firstEntry = entry;
230 self->lastEntry = entry;
232 /* and return the cookie */
233 *pdwCookie = entry->cookie;
235 LeaveCriticalSection(&git_section);
237 TRACE("Cookie is 0x%lx\n", entry->cookie);
241 static HRESULT WINAPI
242 StdGlobalInterfaceTable_RevokeInterfaceFromGlobal(
243 IGlobalInterfaceTable* iface, DWORD dwCookie)
245 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
249 TRACE("iface=%p, dwCookie=0x%x\n", iface, (UINT)dwCookie);
251 entry = StdGlobalInterfaceTable_FindEntry(iface, dwCookie);
253 TRACE("Entry not found\n");
254 return E_INVALIDARG; /* not found */
257 /* Free the stream */
258 hr = CoReleaseMarshalData(entry->stream);
261 WARN("Failed to release marshal data, hr = 0x%08lx\n", hr);
264 IStream_Release(entry->stream);
266 /* chop entry out of the list, and free the memory */
267 EnterCriticalSection(&git_section);
268 if (entry->prev) entry->prev->next = entry->next;
269 else self->firstEntry = entry->next;
270 if (entry->next) entry->next->prev = entry->prev;
271 else self->lastEntry = entry->prev;
272 LeaveCriticalSection(&git_section);
274 HeapFree(GetProcessHeap(), 0, entry);
278 static HRESULT WINAPI
279 StdGlobalInterfaceTable_GetInterfaceFromGlobal(
280 IGlobalInterfaceTable* iface, DWORD dwCookie,
281 REFIID riid, void **ppv)
288 TRACE("dwCookie=0x%lx, riid=%s, ppv=%p\n", dwCookie, debugstr_guid(riid), ppv);
290 entry = StdGlobalInterfaceTable_FindEntry(iface, dwCookie);
291 if (entry == NULL) return E_INVALIDARG;
293 if (!IsEqualIID(&entry->iid, riid)) {
294 WARN("entry->iid (%s) != riid\n", debugstr_guid(&entry->iid));
297 TRACE("entry=%p\n", entry);
299 /* unmarshal the interface */
300 hres = CoUnmarshalInterface(entry->stream, riid, ppv);
302 WARN("Failed to unmarshal stream\n");
306 /* rewind stream, in case it's used again */
309 IStream_Seek(entry->stream, move, STREAM_SEEK_SET, NULL);
313 IUnknown_AddRef(lpUnk);
314 TRACE("ppv=%p\n", *ppv);
318 /* Classfactory definition - despite what MSDN says, some programs need this */
320 static HRESULT WINAPI
321 GITCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid, LPVOID *ppv)
324 if (IsEqualIID(riid,&IID_IUnknown) ||
325 IsEqualIID(riid,&IID_IGlobalInterfaceTable))
327 *ppv = (LPVOID)iface;
330 return E_NOINTERFACE;
333 static ULONG WINAPI GITCF_AddRef(LPCLASSFACTORY iface)
338 static ULONG WINAPI GITCF_Release(LPCLASSFACTORY iface)
343 static HRESULT WINAPI
344 GITCF_CreateInstance(LPCLASSFACTORY iface, LPUNKNOWN pUnk,
345 REFIID riid, LPVOID *ppv)
347 if (IsEqualIID(riid,&IID_IGlobalInterfaceTable)) {
348 if (StdGlobalInterfaceTableInstance == NULL)
349 StdGlobalInterfaceTableInstance = StdGlobalInterfaceTable_Construct();
350 return IGlobalInterfaceTable_QueryInterface( (IGlobalInterfaceTable*) StdGlobalInterfaceTableInstance, riid, ppv);
353 FIXME("(%s), not supported.\n",debugstr_guid(riid));
354 return E_NOINTERFACE;
357 static HRESULT WINAPI GITCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
359 FIXME("(%d), stub!\n",fLock);
363 static const IClassFactoryVtbl GITClassFactoryVtbl = {
364 GITCF_QueryInterface,
367 GITCF_CreateInstance,
371 static const IClassFactoryVtbl *PGITClassFactoryVtbl = &GITClassFactoryVtbl;
373 HRESULT StdGlobalInterfaceTable_GetFactory(LPVOID *ppv)
375 *ppv = &PGITClassFactoryVtbl;
376 TRACE("Returning GIT classfactory\n");
380 /* Virtual function table */
381 static const IGlobalInterfaceTableVtbl StdGlobalInterfaceTableImpl_Vtbl =
383 StdGlobalInterfaceTable_QueryInterface,
384 StdGlobalInterfaceTable_AddRef,
385 StdGlobalInterfaceTable_Release,
386 StdGlobalInterfaceTable_RegisterInterfaceInGlobal,
387 StdGlobalInterfaceTable_RevokeInterfaceFromGlobal,
388 StdGlobalInterfaceTable_GetInterfaceFromGlobal
391 /** This function constructs the GIT. It should only be called once **/
392 void* StdGlobalInterfaceTable_Construct()
394 StdGlobalInterfaceTableImpl* newGIT;
396 newGIT = HeapAlloc(GetProcessHeap(), 0, sizeof(StdGlobalInterfaceTableImpl));
397 if (newGIT == 0) return newGIT;
399 newGIT->lpVtbl = &StdGlobalInterfaceTableImpl_Vtbl;
400 newGIT->ref = 1; /* Initialise the reference count */
401 newGIT->firstEntry = NULL; /* we start with an empty table */
402 newGIT->lastEntry = NULL;
403 newGIT->nextCookie = 0xf100; /* that's where windows starts, so that's where we start */
404 TRACE("Created the GIT at %p\n", newGIT);
406 return (void*)newGIT;