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
29 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
43 #include "compobj_private.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(ole);
49 /****************************************************************************
50 * StdGlobalInterfaceTable definition
52 * This class implements IGlobalInterfaceTable and is a process-wide singleton
53 * used for marshalling interfaces between threading apartments using cookies.
56 /* Each entry in the linked list of GIT entries */
57 typedef struct StdGITEntry
60 IID iid; /* IID of the interface */
61 IStream* stream; /* Holds the marshalled interface */
63 struct StdGITEntry* next;
64 struct StdGITEntry* prev;
68 typedef struct StdGlobalInterfaceTableImpl
70 ICOM_VFIELD(IGlobalInterfaceTable);
73 struct StdGITEntry* firstEntry;
74 struct StdGITEntry* lastEntry;
77 } StdGlobalInterfaceTableImpl;
79 void* StdGlobalInterfaceTableInstance;
83 static HRESULT WINAPI StdGlobalInterfaceTable_QueryInterface(IGlobalInterfaceTable* iface, REFIID riid, void** ppvObject);
84 static ULONG WINAPI StdGlobalInterfaceTable_AddRef(IGlobalInterfaceTable* iface);
85 static ULONG WINAPI StdGlobalInterfaceTable_Release(IGlobalInterfaceTable* iface);
86 /* IGlobalInterfaceTable */
87 static HRESULT WINAPI StdGlobalInterfaceTable_RegisterInterfaceInGlobal(IGlobalInterfaceTable* iface, IUnknown* pUnk, REFIID riid, DWORD* pdwCookie);
88 static HRESULT WINAPI StdGlobalInterfaceTable_RevokeInterfaceFromGlobal(IGlobalInterfaceTable* iface, DWORD dwCookie);
89 static HRESULT WINAPI StdGlobalInterfaceTable_GetInterfaceFromGlobal(IGlobalInterfaceTable* iface, DWORD dwCookie, REFIID riid, void **ppv);
91 /* Virtual function table */
92 static ICOM_VTABLE(IGlobalInterfaceTable) StdGlobalInterfaceTableImpl_Vtbl =
94 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
95 StdGlobalInterfaceTable_QueryInterface,
96 StdGlobalInterfaceTable_AddRef,
97 StdGlobalInterfaceTable_Release,
98 StdGlobalInterfaceTable_RegisterInterfaceInGlobal,
99 StdGlobalInterfaceTable_RevokeInterfaceFromGlobal,
100 StdGlobalInterfaceTable_GetInterfaceFromGlobal
103 static CRITICAL_SECTION git_section;
104 static CRITICAL_SECTION_DEBUG critsect_debug =
107 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
108 0, 0, { 0, (DWORD)(__FILE__ ": global interface table") }
110 static CRITICAL_SECTION git_section = { &critsect_debug, -1, 0, 0, 0, 0 };
114 * Let's go! Here is the constructor and destructor for the class.
118 /** This function constructs the GIT. It should only be called once **/
119 void* StdGlobalInterfaceTable_Construct() {
120 StdGlobalInterfaceTableImpl* newGIT;
122 newGIT = HeapAlloc(GetProcessHeap(), 0, sizeof(StdGlobalInterfaceTableImpl));
123 if (newGIT == 0) return newGIT;
125 newGIT->lpVtbl = &StdGlobalInterfaceTableImpl_Vtbl;
126 newGIT->ref = 1; /* Initialise the reference count */
127 newGIT->firstEntry = NULL; /* we start with an empty table */
128 newGIT->lastEntry = NULL;
129 newGIT->nextCookie = 0xf100; /* that's where windows starts, so that's where we start */
130 TRACE("Created the GIT at %p\n", newGIT);
132 return (void*)newGIT;
135 /** This destroys it again. It should revoke all the held interfaces first **/
136 void StdGlobalInterfaceTable_Destroy(void* self) {
137 TRACE("(%p)\n", self);
138 FIXME("Revoke held interfaces here\n");
140 HeapFree(GetProcessHeap(), 0, self);
141 StdGlobalInterfaceTableInstance = NULL;
145 * A helper function to traverse the list and find the entry that matches the cookie.
146 * Returns NULL if not found
148 StdGITEntry* StdGlobalInterfaceTable_FindEntry(IGlobalInterfaceTable* iface, DWORD cookie) {
149 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
152 TRACE("iface=%p, cookie=0x%x\n", iface, (UINT)cookie);
154 EnterCriticalSection(&git_section);
155 e = self->firstEntry;
157 if (e->cookie == cookie) {
158 LeaveCriticalSection(&git_section);
163 LeaveCriticalSection(&git_section);
165 TRACE("Entry not found\n");
170 * Here's the boring boilerplate stuff for IUnknown
173 HRESULT WINAPI StdGlobalInterfaceTable_QueryInterface(IGlobalInterfaceTable* iface, REFIID riid, void** ppvObject) {
174 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
176 /* Make sure silly coders can't crash us */
177 if (ppvObject == 0) return E_INVALIDARG;
179 *ppvObject = 0; /* assume we don't have the interface */
181 /* Do we implement that interface? */
182 if (IsEqualIID(&IID_IUnknown, riid)) {
183 *ppvObject = (IGlobalInterfaceTable*) self;
184 } else if (IsEqualIID(&IID_IGlobalInterfaceTable, riid)) {
185 *ppvObject = (IGlobalInterfaceTable*) self;
186 } else return E_NOINTERFACE;
188 /* Now inc the refcount */
189 StdGlobalInterfaceTable_AddRef(iface);
193 ULONG WINAPI StdGlobalInterfaceTable_AddRef(IGlobalInterfaceTable* iface) {
194 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
196 /* InterlockedIncrement(&self->ref); */
200 ULONG WINAPI StdGlobalInterfaceTable_Release(IGlobalInterfaceTable* iface) {
201 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
203 /* InterlockedDecrement(&self->ref); */
204 if (self->ref == 0) {
205 /* Hey ho, it's time to go, so long again 'till next weeks show! */
206 StdGlobalInterfaceTable_Destroy(self);
214 * Now implement the actual IGlobalInterfaceTable interface
217 HRESULT WINAPI StdGlobalInterfaceTable_RegisterInterfaceInGlobal(IGlobalInterfaceTable* iface, IUnknown* pUnk, REFIID riid, DWORD* pdwCookie) {
218 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
219 IStream* stream = NULL;
223 TRACE("iface=%p, pUnk=%p, riid=%s, pdwCookie=0x%p\n", iface, pUnk, debugstr_guid(riid), pdwCookie);
225 if (pUnk == NULL) return E_INVALIDARG;
227 /* marshal the interface */
228 TRACE("About to marshal the interface\n");
229 hres = CoMarshalInterThreadInterfaceInStream(riid, pUnk, &stream);
230 if (hres) return hres;
231 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(StdGITEntry));
232 if (entry == NULL) return E_OUTOFMEMORY;
234 EnterCriticalSection(&git_section);
237 entry->stream = stream;
238 entry->cookie = self->nextCookie;
239 self->nextCookie++; /* inc the cookie count */
241 /* insert the new entry at the end of the list */
243 entry->prev = self->lastEntry;
244 if (entry->prev) entry->prev->next = entry;
245 else self->firstEntry = entry;
246 self->lastEntry = entry;
248 /* and return the cookie */
249 *pdwCookie = entry->cookie;
251 LeaveCriticalSection(&git_section);
253 TRACE("Cookie is 0x%lx\n", entry->cookie);
257 HRESULT WINAPI StdGlobalInterfaceTable_RevokeInterfaceFromGlobal(IGlobalInterfaceTable* iface, DWORD dwCookie) {
258 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
261 TRACE("iface=%p, dwCookie=0x%x\n", iface, (UINT)dwCookie);
263 entry = StdGlobalInterfaceTable_FindEntry(iface, dwCookie);
265 TRACE("Entry not found\n");
266 return E_INVALIDARG; /* not found */
269 /* Free the stream */
270 IStream_Release(entry->stream);
272 /* chop entry out of the list, and free the memory */
273 EnterCriticalSection(&git_section);
274 if (entry->prev) entry->prev->next = entry->next;
275 else self->firstEntry = entry->next;
276 if (entry->next) entry->next->prev = entry->prev;
277 else self->lastEntry = entry->prev;
278 LeaveCriticalSection(&git_section);
280 HeapFree(GetProcessHeap(), 0, entry);
284 HRESULT WINAPI StdGlobalInterfaceTable_GetInterfaceFromGlobal(IGlobalInterfaceTable* iface, DWORD dwCookie, REFIID riid, void **ppv) {
290 TRACE("dwCookie=0x%lx, riid=%s, ppv=%p\n", dwCookie, debugstr_guid(riid), ppv);
292 entry = StdGlobalInterfaceTable_FindEntry(iface, dwCookie);
293 if (entry == NULL) return E_INVALIDARG;
295 if (!IsEqualIID(&entry->iid, riid)) {
296 WARN("entry->iid (%s) != riid\n", debugstr_guid(&entry->iid));
299 TRACE("entry=%p\n", entry);
301 /* unmarshal the interface */
302 hres = CoUnmarshalInterface(entry->stream, riid, ppv);
304 WARN("Failed to unmarshal stream\n");
308 /* rewind stream, in case it's used again */
311 IStream_Seek(entry->stream, move, STREAM_SEEK_SET, NULL);
315 IUnknown_AddRef(lpUnk);
316 TRACE("ppv=%p\n", *ppv);
320 /* Classfactory definition - despite what MSDN says, some programs need this */
322 static HRESULT WINAPI GITCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid, LPVOID *ppv) {
324 if (IsEqualIID(riid,&IID_IUnknown) || IsEqualIID(riid,&IID_IGlobalInterfaceTable)) {
325 *ppv = (LPVOID)iface;
328 return E_NOINTERFACE;
330 static ULONG WINAPI GITCF_AddRef(LPCLASSFACTORY iface) { return 2; }
331 static ULONG WINAPI GITCF_Release(LPCLASSFACTORY iface) { return 1; }
333 static HRESULT WINAPI GITCF_CreateInstance(LPCLASSFACTORY iface, LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv) {
334 if (IsEqualIID(riid,&IID_IGlobalInterfaceTable)) {
335 if (StdGlobalInterfaceTableInstance == NULL)
336 StdGlobalInterfaceTableInstance = StdGlobalInterfaceTable_Construct();
337 return IGlobalInterfaceTable_QueryInterface( (IGlobalInterfaceTable*) StdGlobalInterfaceTableInstance, riid, ppv);
340 FIXME("(%s), not supported.\n",debugstr_guid(riid));
341 return E_NOINTERFACE;
344 static HRESULT WINAPI GITCF_LockServer(LPCLASSFACTORY iface, BOOL fLock) {
345 FIXME("(%d), stub!\n",fLock);
349 static ICOM_VTABLE(IClassFactory) GITClassFactoryVtbl = {
350 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
351 GITCF_QueryInterface,
354 GITCF_CreateInstance,
357 static ICOM_VTABLE(IClassFactory) *PGITClassFactoryVtbl = &GITClassFactoryVtbl;
359 HRESULT StdGlobalInterfaceTable_GetFactory(LPVOID *ppv) {
360 *ppv = &PGITClassFactoryVtbl;
361 TRACE("Returning GIT classfactory\n");