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
46 #include "compobj_private.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
52 /****************************************************************************
53 * StdGlobalInterfaceTable definition
55 * This class implements IGlobalInterfaceTable and is a process-wide singleton
56 * used for marshalling interfaces between threading apartments using cookies.
59 /* Each entry in the linked list of GIT entries */
60 typedef struct StdGITEntry
63 IID iid
; /* IID of the interface */
64 IStream
* stream
; /* Holds the marshalled interface */
66 struct StdGITEntry
* next
;
67 struct StdGITEntry
* prev
;
71 typedef struct StdGlobalInterfaceTableImpl
73 ICOM_VFIELD(IGlobalInterfaceTable
);
76 struct StdGITEntry
* firstEntry
;
77 struct StdGITEntry
* lastEntry
;
80 } StdGlobalInterfaceTableImpl
;
82 void* StdGlobalInterfaceTableInstance
;
86 static HRESULT WINAPI
StdGlobalInterfaceTable_QueryInterface(IGlobalInterfaceTable
* iface
, REFIID riid
, void** ppvObject
);
87 static ULONG WINAPI
StdGlobalInterfaceTable_AddRef(IGlobalInterfaceTable
* iface
);
88 static ULONG WINAPI
StdGlobalInterfaceTable_Release(IGlobalInterfaceTable
* iface
);
89 /* IGlobalInterfaceTable */
90 static HRESULT WINAPI
StdGlobalInterfaceTable_RegisterInterfaceInGlobal(IGlobalInterfaceTable
* iface
, IUnknown
* pUnk
, REFIID riid
, DWORD
* pdwCookie
);
91 static HRESULT WINAPI
StdGlobalInterfaceTable_RevokeInterfaceFromGlobal(IGlobalInterfaceTable
* iface
, DWORD dwCookie
);
92 static HRESULT WINAPI
StdGlobalInterfaceTable_GetInterfaceFromGlobal(IGlobalInterfaceTable
* iface
, DWORD dwCookie
, REFIID riid
, void **ppv
);
94 /* Virtual function table */
95 static ICOM_VTABLE(IGlobalInterfaceTable
) StdGlobalInterfaceTableImpl_Vtbl
=
97 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
98 StdGlobalInterfaceTable_QueryInterface
,
99 StdGlobalInterfaceTable_AddRef
,
100 StdGlobalInterfaceTable_Release
,
101 StdGlobalInterfaceTable_RegisterInterfaceInGlobal
,
102 StdGlobalInterfaceTable_RevokeInterfaceFromGlobal
,
103 StdGlobalInterfaceTable_GetInterfaceFromGlobal
106 static CRITICAL_SECTION git_section
;
107 static CRITICAL_SECTION_DEBUG critsect_debug
=
110 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
111 0, 0, { 0, (DWORD
)(__FILE__
": global interface table") }
113 static CRITICAL_SECTION git_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
117 * Let's go! Here is the constructor and destructor for the class.
121 /** This function constructs the GIT. It should only be called once **/
122 void* StdGlobalInterfaceTable_Construct() {
123 StdGlobalInterfaceTableImpl
* newGIT
;
125 newGIT
= HeapAlloc(GetProcessHeap(), 0, sizeof(StdGlobalInterfaceTableImpl
));
126 if (newGIT
== 0) return newGIT
;
128 newGIT
->lpVtbl
= &StdGlobalInterfaceTableImpl_Vtbl
;
129 newGIT
->ref
= 1; /* Initialise the reference count */
130 newGIT
->firstEntry
= NULL
; /* we start with an empty table */
131 newGIT
->lastEntry
= NULL
;
132 newGIT
->nextCookie
= 0xf100; /* that's where windows starts, so that's where we start */
133 TRACE("Created the GIT at %p\n", newGIT
);
135 return (void*)newGIT
;
138 /** This destroys it again. It should revoke all the held interfaces first **/
139 void StdGlobalInterfaceTable_Destroy(void* self
) {
140 TRACE("(%p)\n", self
);
141 FIXME("Revoke held interfaces here\n");
143 HeapFree(GetProcessHeap(), 0, self
);
144 StdGlobalInterfaceTableInstance
= NULL
;
148 * A helper function to traverse the list and find the entry that matches the cookie.
149 * Returns NULL if not found
151 StdGITEntry
* StdGlobalInterfaceTable_FindEntry(IGlobalInterfaceTable
* iface
, DWORD cookie
) {
152 StdGlobalInterfaceTableImpl
* const self
= (StdGlobalInterfaceTableImpl
*) iface
;
155 TRACE("iface=%p, cookie=0x%x\n", iface
, (UINT
)cookie
);
157 EnterCriticalSection(&git_section
);
158 e
= self
->firstEntry
;
160 if (e
->cookie
== cookie
) {
161 LeaveCriticalSection(&git_section
);
166 LeaveCriticalSection(&git_section
);
168 TRACE("Entry not found\n");
173 * Here's the boring boilerplate stuff for IUnknown
176 HRESULT WINAPI
StdGlobalInterfaceTable_QueryInterface(IGlobalInterfaceTable
* iface
, REFIID riid
, void** ppvObject
) {
177 StdGlobalInterfaceTableImpl
* const self
= (StdGlobalInterfaceTableImpl
*) iface
;
179 /* Make sure silly coders can't crash us */
180 if (ppvObject
== 0) return E_INVALIDARG
;
182 *ppvObject
= 0; /* assume we don't have the interface */
184 /* Do we implement that interface? */
185 if (IsEqualIID(&IID_IUnknown
, riid
)) {
186 *ppvObject
= (IGlobalInterfaceTable
*) self
;
187 } else if (IsEqualIID(&IID_IGlobalInterfaceTable
, riid
)) {
188 *ppvObject
= (IGlobalInterfaceTable
*) self
;
189 } else return E_NOINTERFACE
;
191 /* Now inc the refcount */
192 StdGlobalInterfaceTable_AddRef(iface
);
196 ULONG WINAPI
StdGlobalInterfaceTable_AddRef(IGlobalInterfaceTable
* iface
) {
197 StdGlobalInterfaceTableImpl
* const self
= (StdGlobalInterfaceTableImpl
*) iface
;
199 /* InterlockedIncrement(&self->ref); */
203 ULONG WINAPI
StdGlobalInterfaceTable_Release(IGlobalInterfaceTable
* iface
) {
204 StdGlobalInterfaceTableImpl
* const self
= (StdGlobalInterfaceTableImpl
*) iface
;
206 /* InterlockedDecrement(&self->ref); */
207 if (self
->ref
== 0) {
208 /* Hey ho, it's time to go, so long again 'till next weeks show! */
209 StdGlobalInterfaceTable_Destroy(self
);
217 * Now implement the actual IGlobalInterfaceTable interface
220 HRESULT WINAPI
StdGlobalInterfaceTable_RegisterInterfaceInGlobal(IGlobalInterfaceTable
* iface
, IUnknown
* pUnk
, REFIID riid
, DWORD
* pdwCookie
) {
221 StdGlobalInterfaceTableImpl
* const self
= (StdGlobalInterfaceTableImpl
*) iface
;
222 IStream
* stream
= NULL
;
226 TRACE("iface=%p, pUnk=%p, riid=%s, pdwCookie=0x%p\n", iface
, pUnk
, debugstr_guid(riid
), pdwCookie
);
228 if (pUnk
== NULL
) return E_INVALIDARG
;
230 /* marshal the interface */
231 TRACE("About to marshal the interface\n");
232 hres
= CoMarshalInterThreadInterfaceInStream(riid
, pUnk
, &stream
);
233 if (hres
) return hres
;
234 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(StdGITEntry
));
235 if (entry
== NULL
) return E_OUTOFMEMORY
;
237 EnterCriticalSection(&git_section
);
240 entry
->stream
= stream
;
241 entry
->cookie
= self
->nextCookie
;
242 self
->nextCookie
++; /* inc the cookie count */
244 /* insert the new entry at the end of the list */
246 entry
->prev
= self
->lastEntry
;
247 if (entry
->prev
) entry
->prev
->next
= entry
;
248 else self
->firstEntry
= entry
;
249 self
->lastEntry
= entry
;
251 /* and return the cookie */
252 *pdwCookie
= entry
->cookie
;
254 LeaveCriticalSection(&git_section
);
256 TRACE("Cookie is 0x%lx\n", entry
->cookie
);
260 HRESULT WINAPI
StdGlobalInterfaceTable_RevokeInterfaceFromGlobal(IGlobalInterfaceTable
* iface
, DWORD dwCookie
) {
261 StdGlobalInterfaceTableImpl
* const self
= (StdGlobalInterfaceTableImpl
*) iface
;
264 TRACE("iface=%p, dwCookie=0x%x\n", iface
, (UINT
)dwCookie
);
266 entry
= StdGlobalInterfaceTable_FindEntry(iface
, dwCookie
);
268 TRACE("Entry not found\n");
269 return E_INVALIDARG
; /* not found */
272 /* Free the stream */
273 IStream_Release(entry
->stream
);
275 /* chop entry out of the list, and free the memory */
276 EnterCriticalSection(&git_section
);
277 if (entry
->prev
) entry
->prev
->next
= entry
->next
;
278 else self
->firstEntry
= entry
->next
;
279 if (entry
->next
) entry
->next
->prev
= entry
->prev
;
280 else self
->lastEntry
= entry
->prev
;
281 LeaveCriticalSection(&git_section
);
283 HeapFree(GetProcessHeap(), 0, entry
);
287 HRESULT WINAPI
StdGlobalInterfaceTable_GetInterfaceFromGlobal(IGlobalInterfaceTable
* iface
, DWORD dwCookie
, REFIID riid
, void **ppv
) {
293 TRACE("dwCookie=0x%lx, riid=%s, ppv=%p\n", dwCookie
, debugstr_guid(riid
), ppv
);
295 entry
= StdGlobalInterfaceTable_FindEntry(iface
, dwCookie
);
296 if (entry
== NULL
) return E_INVALIDARG
;
298 if (!IsEqualIID(&entry
->iid
, riid
)) {
299 WARN("entry->iid (%s) != riid\n", debugstr_guid(&entry
->iid
));
302 TRACE("entry=%p\n", entry
);
304 /* unmarshal the interface */
305 hres
= CoUnmarshalInterface(entry
->stream
, riid
, ppv
);
307 WARN("Failed to unmarshal stream\n");
311 /* rewind stream, in case it's used again */
314 IStream_Seek(entry
->stream
, move
, STREAM_SEEK_SET
, NULL
);
318 IUnknown_AddRef(lpUnk
);
319 TRACE("ppv=%p\n", *ppv
);
323 /* Classfactory definition - despite what MSDN says, some programs need this */
325 static HRESULT WINAPI
GITCF_QueryInterface(LPCLASSFACTORY iface
,REFIID riid
, LPVOID
*ppv
) {
327 if (IsEqualIID(riid
,&IID_IUnknown
) || IsEqualIID(riid
,&IID_IGlobalInterfaceTable
)) {
328 *ppv
= (LPVOID
)iface
;
331 return E_NOINTERFACE
;
333 static ULONG WINAPI
GITCF_AddRef(LPCLASSFACTORY iface
) { return 2; }
334 static ULONG WINAPI
GITCF_Release(LPCLASSFACTORY iface
) { return 1; }
336 static HRESULT WINAPI
GITCF_CreateInstance(LPCLASSFACTORY iface
, LPUNKNOWN pUnk
, REFIID riid
, LPVOID
*ppv
) {
337 if (IsEqualIID(riid
,&IID_IGlobalInterfaceTable
)) {
338 if (StdGlobalInterfaceTableInstance
== NULL
)
339 StdGlobalInterfaceTableInstance
= StdGlobalInterfaceTable_Construct();
340 return IGlobalInterfaceTable_QueryInterface( (IGlobalInterfaceTable
*) StdGlobalInterfaceTableInstance
, riid
, ppv
);
343 FIXME("(%s), not supported.\n",debugstr_guid(riid
));
344 return E_NOINTERFACE
;
347 static HRESULT WINAPI
GITCF_LockServer(LPCLASSFACTORY iface
, BOOL fLock
) {
348 FIXME("(%d), stub!\n",fLock
);
352 static ICOM_VTABLE(IClassFactory
) GITClassFactoryVtbl
= {
353 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
354 GITCF_QueryInterface
,
357 GITCF_CreateInstance
,
360 static ICOM_VTABLE(IClassFactory
) *PGITClassFactoryVtbl
= &GITClassFactoryVtbl
;
362 HRESULT
StdGlobalInterfaceTable_GetFactory(LPVOID
*ppv
) {
363 *ppv
= &PGITClassFactoryVtbl
;
364 TRACE("Returning GIT classfactory\n");