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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 IGlobalInterfaceTableVtbl *lpVtbl;
78 struct StdGITEntry* firstEntry;
79 struct StdGITEntry* lastEntry;
82 } StdGlobalInterfaceTableImpl;
84 void* StdGlobalInterfaceTableInstance;
88 static HRESULT WINAPI StdGlobalInterfaceTable_QueryInterface(IGlobalInterfaceTable* iface, REFIID riid, void** ppvObject);
89 static ULONG WINAPI StdGlobalInterfaceTable_AddRef(IGlobalInterfaceTable* iface);
90 static ULONG WINAPI StdGlobalInterfaceTable_Release(IGlobalInterfaceTable* iface);
91 /* IGlobalInterfaceTable */
92 static HRESULT WINAPI StdGlobalInterfaceTable_RegisterInterfaceInGlobal(IGlobalInterfaceTable* iface, IUnknown* pUnk, REFIID riid, DWORD* pdwCookie);
93 static HRESULT WINAPI StdGlobalInterfaceTable_RevokeInterfaceFromGlobal(IGlobalInterfaceTable* iface, DWORD dwCookie);
94 static HRESULT WINAPI StdGlobalInterfaceTable_GetInterfaceFromGlobal(IGlobalInterfaceTable* iface, DWORD dwCookie, REFIID riid, void **ppv);
96 /* Virtual function table */
97 static IGlobalInterfaceTableVtbl StdGlobalInterfaceTableImpl_Vtbl =
99 StdGlobalInterfaceTable_QueryInterface,
100 StdGlobalInterfaceTable_AddRef,
101 StdGlobalInterfaceTable_Release,
102 StdGlobalInterfaceTable_RegisterInterfaceInGlobal,
103 StdGlobalInterfaceTable_RevokeInterfaceFromGlobal,
104 StdGlobalInterfaceTable_GetInterfaceFromGlobal
107 static CRITICAL_SECTION git_section;
108 static CRITICAL_SECTION_DEBUG critsect_debug =
111 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
112 0, 0, { 0, (DWORD)(__FILE__ ": global interface table") }
114 static CRITICAL_SECTION git_section = { &critsect_debug, -1, 0, 0, 0, 0 };
118 * Let's go! Here is the constructor and destructor for the class.
122 /** This function constructs the GIT. It should only be called once **/
123 void* StdGlobalInterfaceTable_Construct() {
124 StdGlobalInterfaceTableImpl* newGIT;
126 newGIT = HeapAlloc(GetProcessHeap(), 0, sizeof(StdGlobalInterfaceTableImpl));
127 if (newGIT == 0) return newGIT;
129 newGIT->lpVtbl = &StdGlobalInterfaceTableImpl_Vtbl;
130 newGIT->ref = 1; /* Initialise the reference count */
131 newGIT->firstEntry = NULL; /* we start with an empty table */
132 newGIT->lastEntry = NULL;
133 newGIT->nextCookie = 0xf100; /* that's where windows starts, so that's where we start */
134 TRACE("Created the GIT at %p\n", newGIT);
136 return (void*)newGIT;
139 /** This destroys it again. It should revoke all the held interfaces first **/
140 void StdGlobalInterfaceTable_Destroy(void* self) {
141 TRACE("(%p)\n", self);
142 FIXME("Revoke held interfaces here\n");
144 HeapFree(GetProcessHeap(), 0, self);
145 StdGlobalInterfaceTableInstance = NULL;
149 * A helper function to traverse the list and find the entry that matches the cookie.
150 * Returns NULL if not found
152 StdGITEntry* StdGlobalInterfaceTable_FindEntry(IGlobalInterfaceTable* iface, DWORD cookie) {
153 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
156 TRACE("iface=%p, cookie=0x%x\n", iface, (UINT)cookie);
158 EnterCriticalSection(&git_section);
159 e = self->firstEntry;
161 if (e->cookie == cookie) {
162 LeaveCriticalSection(&git_section);
167 LeaveCriticalSection(&git_section);
169 TRACE("Entry not found\n");
174 * Here's the boring boilerplate stuff for IUnknown
177 HRESULT WINAPI StdGlobalInterfaceTable_QueryInterface(IGlobalInterfaceTable* iface, REFIID riid, void** ppvObject) {
178 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
180 /* Make sure silly coders can't crash us */
181 if (ppvObject == 0) return E_INVALIDARG;
183 *ppvObject = 0; /* assume we don't have the interface */
185 /* Do we implement that interface? */
186 if (IsEqualIID(&IID_IUnknown, riid)) {
187 *ppvObject = (IGlobalInterfaceTable*) self;
188 } else if (IsEqualIID(&IID_IGlobalInterfaceTable, riid)) {
189 *ppvObject = (IGlobalInterfaceTable*) self;
190 } else return E_NOINTERFACE;
192 /* Now inc the refcount */
193 StdGlobalInterfaceTable_AddRef(iface);
197 ULONG WINAPI StdGlobalInterfaceTable_AddRef(IGlobalInterfaceTable* iface) {
198 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
200 /* InterlockedIncrement(&self->ref); */
204 ULONG WINAPI StdGlobalInterfaceTable_Release(IGlobalInterfaceTable* iface) {
205 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
207 /* InterlockedDecrement(&self->ref); */
208 if (self->ref == 0) {
209 /* Hey ho, it's time to go, so long again 'till next weeks show! */
210 StdGlobalInterfaceTable_Destroy(self);
218 * Now implement the actual IGlobalInterfaceTable interface
221 HRESULT WINAPI StdGlobalInterfaceTable_RegisterInterfaceInGlobal(IGlobalInterfaceTable* iface, IUnknown* pUnk, REFIID riid, DWORD* pdwCookie) {
222 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
223 IStream* stream = NULL;
227 TRACE("iface=%p, pUnk=%p, riid=%s, pdwCookie=0x%p\n", iface, pUnk, debugstr_guid(riid), pdwCookie);
229 if (pUnk == NULL) return E_INVALIDARG;
231 /* marshal the interface */
232 TRACE("About to marshal the interface\n");
233 hres = CoMarshalInterThreadInterfaceInStream(riid, pUnk, &stream);
234 if (hres) return hres;
235 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(StdGITEntry));
236 if (entry == NULL) return E_OUTOFMEMORY;
238 EnterCriticalSection(&git_section);
241 entry->stream = stream;
242 entry->cookie = self->nextCookie;
243 self->nextCookie++; /* inc the cookie count */
245 /* insert the new entry at the end of the list */
247 entry->prev = self->lastEntry;
248 if (entry->prev) entry->prev->next = entry;
249 else self->firstEntry = entry;
250 self->lastEntry = entry;
252 /* and return the cookie */
253 *pdwCookie = entry->cookie;
255 LeaveCriticalSection(&git_section);
257 TRACE("Cookie is 0x%lx\n", entry->cookie);
261 HRESULT WINAPI StdGlobalInterfaceTable_RevokeInterfaceFromGlobal(IGlobalInterfaceTable* iface, DWORD dwCookie) {
262 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
265 TRACE("iface=%p, dwCookie=0x%x\n", iface, (UINT)dwCookie);
267 entry = StdGlobalInterfaceTable_FindEntry(iface, dwCookie);
269 TRACE("Entry not found\n");
270 return E_INVALIDARG; /* not found */
273 /* Free the stream */
274 IStream_Release(entry->stream);
276 /* chop entry out of the list, and free the memory */
277 EnterCriticalSection(&git_section);
278 if (entry->prev) entry->prev->next = entry->next;
279 else self->firstEntry = entry->next;
280 if (entry->next) entry->next->prev = entry->prev;
281 else self->lastEntry = entry->prev;
282 LeaveCriticalSection(&git_section);
284 HeapFree(GetProcessHeap(), 0, entry);
288 HRESULT WINAPI StdGlobalInterfaceTable_GetInterfaceFromGlobal(IGlobalInterfaceTable* iface, DWORD dwCookie, REFIID riid, void **ppv) {
294 TRACE("dwCookie=0x%lx, riid=%s, ppv=%p\n", dwCookie, debugstr_guid(riid), ppv);
296 entry = StdGlobalInterfaceTable_FindEntry(iface, dwCookie);
297 if (entry == NULL) return E_INVALIDARG;
299 if (!IsEqualIID(&entry->iid, riid)) {
300 WARN("entry->iid (%s) != riid\n", debugstr_guid(&entry->iid));
303 TRACE("entry=%p\n", entry);
305 /* unmarshal the interface */
306 hres = CoUnmarshalInterface(entry->stream, riid, ppv);
308 WARN("Failed to unmarshal stream\n");
312 /* rewind stream, in case it's used again */
315 IStream_Seek(entry->stream, move, STREAM_SEEK_SET, NULL);
319 IUnknown_AddRef(lpUnk);
320 TRACE("ppv=%p\n", *ppv);
324 /* Classfactory definition - despite what MSDN says, some programs need this */
326 static HRESULT WINAPI GITCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid, LPVOID *ppv) {
328 if (IsEqualIID(riid,&IID_IUnknown) || IsEqualIID(riid,&IID_IGlobalInterfaceTable)) {
329 *ppv = (LPVOID)iface;
332 return E_NOINTERFACE;
334 static ULONG WINAPI GITCF_AddRef(LPCLASSFACTORY iface) { return 2; }
335 static ULONG WINAPI GITCF_Release(LPCLASSFACTORY iface) { return 1; }
337 static HRESULT WINAPI GITCF_CreateInstance(LPCLASSFACTORY iface, LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv) {
338 if (IsEqualIID(riid,&IID_IGlobalInterfaceTable)) {
339 if (StdGlobalInterfaceTableInstance == NULL)
340 StdGlobalInterfaceTableInstance = StdGlobalInterfaceTable_Construct();
341 return IGlobalInterfaceTable_QueryInterface( (IGlobalInterfaceTable*) StdGlobalInterfaceTableInstance, riid, ppv);
344 FIXME("(%s), not supported.\n",debugstr_guid(riid));
345 return E_NOINTERFACE;
348 static HRESULT WINAPI GITCF_LockServer(LPCLASSFACTORY iface, BOOL fLock) {
349 FIXME("(%d), stub!\n",fLock);
353 static IClassFactoryVtbl GITClassFactoryVtbl = {
354 GITCF_QueryInterface,
357 GITCF_CreateInstance,
360 static IClassFactoryVtbl *PGITClassFactoryVtbl = &GITClassFactoryVtbl;
362 HRESULT StdGlobalInterfaceTable_GetFactory(LPVOID *ppv) {
363 *ppv = &PGITClassFactoryVtbl;
364 TRACE("Returning GIT classfactory\n");