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
41 #include "compobj_private.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 */
61 struct StdGITEntry
* next
;
62 struct StdGITEntry
* prev
;
66 typedef struct StdGlobalInterfaceTableImpl
68 ICOM_VFIELD(IGlobalInterfaceTable
);
71 struct StdGITEntry
* firstEntry
;
72 struct StdGITEntry
* lastEntry
;
75 } StdGlobalInterfaceTableImpl
;
77 void* StdGlobalInterfaceTableInstance
;
81 static HRESULT WINAPI
StdGlobalInterfaceTable_QueryInterface(IGlobalInterfaceTable
* iface
, REFIID riid
, void** ppvObject
);
82 static ULONG WINAPI
StdGlobalInterfaceTable_AddRef(IGlobalInterfaceTable
* iface
);
83 static ULONG WINAPI
StdGlobalInterfaceTable_Release(IGlobalInterfaceTable
* iface
);
84 /* IGlobalInterfaceTable */
85 static HRESULT WINAPI
StdGlobalInterfaceTable_RegisterInterfaceInGlobal(IGlobalInterfaceTable
* iface
, IUnknown
* pUnk
, REFIID riid
, DWORD
* pdwCookie
);
86 static HRESULT WINAPI
StdGlobalInterfaceTable_RevokeInterfaceFromGlobal(IGlobalInterfaceTable
* iface
, DWORD dwCookie
);
87 static HRESULT WINAPI
StdGlobalInterfaceTable_GetInterfaceFromGlobal(IGlobalInterfaceTable
* iface
, DWORD dwCookie
, REFIID riid
, void **ppv
);
89 /* Virtual function table */
90 static ICOM_VTABLE(IGlobalInterfaceTable
) StdGlobalInterfaceTableImpl_Vtbl
=
92 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
93 StdGlobalInterfaceTable_QueryInterface
,
94 StdGlobalInterfaceTable_AddRef
,
95 StdGlobalInterfaceTable_Release
,
96 StdGlobalInterfaceTable_RegisterInterfaceInGlobal
,
97 StdGlobalInterfaceTable_RevokeInterfaceFromGlobal
,
98 StdGlobalInterfaceTable_GetInterfaceFromGlobal
102 * Let's go! Here is the constructor and destructor for the class.
106 /** This function constructs the GIT. It should only be called once **/
107 void* StdGlobalInterfaceTable_Construct() {
108 StdGlobalInterfaceTableImpl
* newGIT
;
110 TRACE("constructing\n");
112 newGIT
= HeapAlloc(GetProcessHeap(), 0, sizeof(StdGlobalInterfaceTableImpl
));
113 if (newGIT
== 0) return newGIT
;
115 newGIT
->lpVtbl
= &StdGlobalInterfaceTableImpl_Vtbl
;
116 newGIT
->ref
= 0; /* Initialise the reference count */
117 newGIT
->firstEntry
= NULL
; /* we start with an empty table */
118 newGIT
->lastEntry
= NULL
;
119 newGIT
->nextCookie
= 0xf100; /* that's where windows starts, so that's where we start */
120 TRACE("Created the GIT at %p\n", newGIT
);
122 return (void*)newGIT
;
125 /** This destroys it again. It should revoke all the held interfaces first **/
126 void StdGlobalInterfaceTable_Destroy(void* self
) {
127 TRACE("(%p)\n", self
);
128 FIXME("Revoke held interfaces here\n");
130 HeapFree(GetProcessHeap(), 0, self
);
134 * A helper function to traverse the list and find the entry that matches the cookie.
135 * Returns NULL if not found
137 StdGITEntry
* StdGlobalInterfaceTable_FindEntry(IGlobalInterfaceTable
* iface
, DWORD cookie
) {
138 StdGlobalInterfaceTableImpl
* const self
= (StdGlobalInterfaceTableImpl
*) iface
;
141 TRACE("iface=%p, cookie=0x%x\n", iface
, (UINT
)cookie
);
143 e
= self
->firstEntry
;
145 if (e
->cookie
== cookie
) return e
;
148 TRACE("Entry not found\n");
153 * Here's the boring boilerplate stuff for IUnknown
156 HRESULT WINAPI
StdGlobalInterfaceTable_QueryInterface(IGlobalInterfaceTable
* iface
, REFIID riid
, void** ppvObject
) {
157 StdGlobalInterfaceTableImpl
* const self
= (StdGlobalInterfaceTableImpl
*) iface
;
159 /* Make sure silly coders can't crash us */
160 if (ppvObject
== 0) return E_INVALIDARG
;
162 *ppvObject
= 0; /* assume we don't have the interface */
164 /* Do we implement that interface? */
165 if (IsEqualIID(&IID_IUnknown
, riid
)) {
166 *ppvObject
= (IGlobalInterfaceTable
*) self
;
167 } else if (IsEqualIID(&IID_IGlobalInterfaceTable
, riid
)) {
168 *ppvObject
= (IGlobalInterfaceTable
*) self
;
169 } else return E_NOINTERFACE
;
171 /* Now inc the refcount */
172 /* we don't use refcounts for now: StdGlobalInterfaceTable_AddRef(iface); */
176 ULONG WINAPI
StdGlobalInterfaceTable_AddRef(IGlobalInterfaceTable
* iface
) {
177 StdGlobalInterfaceTableImpl
* const self
= (StdGlobalInterfaceTableImpl
*) iface
;
183 ULONG WINAPI
StdGlobalInterfaceTable_Release(IGlobalInterfaceTable
* iface
) {
184 StdGlobalInterfaceTableImpl
* const self
= (StdGlobalInterfaceTableImpl
*) iface
;
187 if (self
->ref
== 0) {
188 /* Hey ho, it's time to go, so long again 'till next weeks show! */
189 StdGlobalInterfaceTable_Destroy(self
);
197 * Now implement the actual IGlobalInterfaceTable interface
200 HRESULT WINAPI
StdGlobalInterfaceTable_RegisterInterfaceInGlobal(IGlobalInterfaceTable
* iface
, IUnknown
* pUnk
, REFIID riid
, DWORD
* pdwCookie
) {
201 StdGlobalInterfaceTableImpl
* const self
= (StdGlobalInterfaceTableImpl
*) iface
;
202 IStream
* stream
= NULL
;
206 TRACE("iface=%p, pUnk=%p, riid=%s, pdwCookie=0x%p\n", iface
, pUnk
, debugstr_guid(riid
), pdwCookie
);
208 if (pUnk
== NULL
) return E_INVALIDARG
;
210 /* marshal the interface */
211 hres
= CoMarshalInterThreadInterfaceInStream(riid
, pUnk
, &stream
);
212 if (hres
) return hres
;
213 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(StdGITEntry
));
214 if (entry
== NULL
) return E_OUTOFMEMORY
;
217 entry
->stream
= stream
;
218 entry
->cookie
= self
->nextCookie
;
219 self
->nextCookie
++; /* inc the cookie count */
221 /* insert the new entry at the end of the list */
223 entry
->prev
= self
->lastEntry
;
224 if (entry
->prev
) entry
->prev
->next
= entry
;
225 else self
->firstEntry
= entry
;
226 self
->lastEntry
= entry
;
228 /* and return the cookie */
229 *pdwCookie
= entry
->cookie
;
230 TRACE("Cookie is 0x%ld\n", entry
->cookie
);
234 HRESULT WINAPI
StdGlobalInterfaceTable_RevokeInterfaceFromGlobal(IGlobalInterfaceTable
* iface
, DWORD dwCookie
) {
235 StdGlobalInterfaceTableImpl
* const self
= (StdGlobalInterfaceTableImpl
*) iface
;
238 TRACE("iface=%p, dwCookie=0x%x\n", iface
, (UINT
)dwCookie
);
240 entry
= StdGlobalInterfaceTable_FindEntry(iface
, dwCookie
);
242 TRACE("Entry not found\n");
243 return E_INVALIDARG
; /* not found */
246 /* chop entry out of the list, and free the memory */
247 if (entry
->prev
) entry
->prev
->next
= entry
->next
;
248 else self
->firstEntry
= entry
->next
;
249 if (entry
->next
) entry
->next
->prev
= entry
->prev
;
250 else self
->lastEntry
= entry
->prev
;
251 HeapFree(GetProcessHeap(), 0, entry
);
255 HRESULT WINAPI
StdGlobalInterfaceTable_GetInterfaceFromGlobal(IGlobalInterfaceTable
* iface
, DWORD dwCookie
, REFIID riid
, void **ppv
) {
259 TRACE("dwCookie=0x%lx, riid=%s\n", dwCookie
, debugstr_guid(riid
));
261 entry
= StdGlobalInterfaceTable_FindEntry(iface
, dwCookie
);
262 if (entry
== NULL
) return E_INVALIDARG
;
263 if (!IsEqualIID(&entry
->iid
, &riid
)) return E_INVALIDARG
;
265 /* unmarshal the interface */
266 hres
= CoGetInterfaceAndReleaseStream(entry
->stream
, riid
, *ppv
);
267 if (hres
) return hres
;
272 /* Classfactory definition - despite what MSDN says, some programs need this */
274 static HRESULT WINAPI
GITCF_QueryInterface(LPCLASSFACTORY iface
,REFIID riid
, LPVOID
*ppv
) {
276 if (IsEqualIID(riid
,&IID_IUnknown
) || IsEqualIID(riid
,&IID_IGlobalInterfaceTable
)) {
277 *ppv
= (LPVOID
)iface
;
280 return E_NOINTERFACE
;
282 static ULONG WINAPI
GITCF_AddRef(LPCLASSFACTORY iface
) { return 2; }
283 static ULONG WINAPI
GITCF_Release(LPCLASSFACTORY iface
) { return 1; }
285 static HRESULT WINAPI
GITCF_CreateInstance(LPCLASSFACTORY iface
, LPUNKNOWN pUnk
, REFIID riid
, LPVOID
*ppv
) {
286 if (IsEqualIID(riid
,&IID_IGlobalInterfaceTable
)) {
287 if (StdGlobalInterfaceTableInstance
== NULL
)
288 StdGlobalInterfaceTableInstance
= StdGlobalInterfaceTable_Construct();
289 return IGlobalInterfaceTable_QueryInterface( (IGlobalInterfaceTable
*) StdGlobalInterfaceTableInstance
, riid
, ppv
);
292 FIXME("(%s), not supported.\n",debugstr_guid(riid
));
293 return E_NOINTERFACE
;
296 static HRESULT WINAPI
GITCF_LockServer(LPCLASSFACTORY iface
, BOOL fLock
) {
297 FIXME("(%d), stub!\n",fLock
);
301 static ICOM_VTABLE(IClassFactory
) GITClassFactoryVtbl
= {
302 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
303 GITCF_QueryInterface
,
306 GITCF_CreateInstance
,
309 static ICOM_VTABLE(IClassFactory
) *PGITClassFactoryVtbl
= &GITClassFactoryVtbl
;
311 HRESULT
StdGlobalInterfaceTable_GetFactory(LPVOID
*ppv
) {
312 *ppv
= &PGITClassFactoryVtbl
;
313 TRACE("Returning GIT classfactory\n");