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 IGlobalInterfaceTableVtbl
*lpVtbl
;
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 IGlobalInterfaceTableVtbl StdGlobalInterfaceTableImpl_Vtbl
=
97 StdGlobalInterfaceTable_QueryInterface
,
98 StdGlobalInterfaceTable_AddRef
,
99 StdGlobalInterfaceTable_Release
,
100 StdGlobalInterfaceTable_RegisterInterfaceInGlobal
,
101 StdGlobalInterfaceTable_RevokeInterfaceFromGlobal
,
102 StdGlobalInterfaceTable_GetInterfaceFromGlobal
105 static CRITICAL_SECTION git_section
;
106 static CRITICAL_SECTION_DEBUG critsect_debug
=
109 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
110 0, 0, { 0, (DWORD
)(__FILE__
": global interface table") }
112 static CRITICAL_SECTION git_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
116 * Let's go! Here is the constructor and destructor for the class.
120 /** This function constructs the GIT. It should only be called once **/
121 void* StdGlobalInterfaceTable_Construct() {
122 StdGlobalInterfaceTableImpl
* newGIT
;
124 newGIT
= HeapAlloc(GetProcessHeap(), 0, sizeof(StdGlobalInterfaceTableImpl
));
125 if (newGIT
== 0) return newGIT
;
127 newGIT
->lpVtbl
= &StdGlobalInterfaceTableImpl_Vtbl
;
128 newGIT
->ref
= 1; /* Initialise the reference count */
129 newGIT
->firstEntry
= NULL
; /* we start with an empty table */
130 newGIT
->lastEntry
= NULL
;
131 newGIT
->nextCookie
= 0xf100; /* that's where windows starts, so that's where we start */
132 TRACE("Created the GIT at %p\n", newGIT
);
134 return (void*)newGIT
;
137 /** This destroys it again. It should revoke all the held interfaces first **/
138 void StdGlobalInterfaceTable_Destroy(void* self
) {
139 TRACE("(%p)\n", self
);
140 FIXME("Revoke held interfaces here\n");
142 HeapFree(GetProcessHeap(), 0, self
);
143 StdGlobalInterfaceTableInstance
= NULL
;
147 * A helper function to traverse the list and find the entry that matches the cookie.
148 * Returns NULL if not found
150 StdGITEntry
* StdGlobalInterfaceTable_FindEntry(IGlobalInterfaceTable
* iface
, DWORD cookie
) {
151 StdGlobalInterfaceTableImpl
* const self
= (StdGlobalInterfaceTableImpl
*) iface
;
154 TRACE("iface=%p, cookie=0x%x\n", iface
, (UINT
)cookie
);
156 EnterCriticalSection(&git_section
);
157 e
= self
->firstEntry
;
159 if (e
->cookie
== cookie
) {
160 LeaveCriticalSection(&git_section
);
165 LeaveCriticalSection(&git_section
);
167 TRACE("Entry not found\n");
172 * Here's the boring boilerplate stuff for IUnknown
175 HRESULT WINAPI
StdGlobalInterfaceTable_QueryInterface(IGlobalInterfaceTable
* iface
, REFIID riid
, void** ppvObject
) {
176 StdGlobalInterfaceTableImpl
* const self
= (StdGlobalInterfaceTableImpl
*) iface
;
178 /* Make sure silly coders can't crash us */
179 if (ppvObject
== 0) return E_INVALIDARG
;
181 *ppvObject
= 0; /* assume we don't have the interface */
183 /* Do we implement that interface? */
184 if (IsEqualIID(&IID_IUnknown
, riid
)) {
185 *ppvObject
= (IGlobalInterfaceTable
*) self
;
186 } else if (IsEqualIID(&IID_IGlobalInterfaceTable
, riid
)) {
187 *ppvObject
= (IGlobalInterfaceTable
*) self
;
188 } else return E_NOINTERFACE
;
190 /* Now inc the refcount */
191 StdGlobalInterfaceTable_AddRef(iface
);
195 ULONG WINAPI
StdGlobalInterfaceTable_AddRef(IGlobalInterfaceTable
* iface
) {
196 StdGlobalInterfaceTableImpl
* const self
= (StdGlobalInterfaceTableImpl
*) iface
;
198 /* InterlockedIncrement(&self->ref); */
202 ULONG WINAPI
StdGlobalInterfaceTable_Release(IGlobalInterfaceTable
* iface
) {
203 StdGlobalInterfaceTableImpl
* const self
= (StdGlobalInterfaceTableImpl
*) iface
;
205 /* InterlockedDecrement(&self->ref); */
206 if (self
->ref
== 0) {
207 /* Hey ho, it's time to go, so long again 'till next weeks show! */
208 StdGlobalInterfaceTable_Destroy(self
);
216 * Now implement the actual IGlobalInterfaceTable interface
219 HRESULT WINAPI
StdGlobalInterfaceTable_RegisterInterfaceInGlobal(IGlobalInterfaceTable
* iface
, IUnknown
* pUnk
, REFIID riid
, DWORD
* pdwCookie
) {
220 StdGlobalInterfaceTableImpl
* const self
= (StdGlobalInterfaceTableImpl
*) iface
;
221 IStream
* stream
= NULL
;
225 TRACE("iface=%p, pUnk=%p, riid=%s, pdwCookie=0x%p\n", iface
, pUnk
, debugstr_guid(riid
), pdwCookie
);
227 if (pUnk
== NULL
) return E_INVALIDARG
;
229 /* marshal the interface */
230 TRACE("About to marshal the interface\n");
231 hres
= CoMarshalInterThreadInterfaceInStream(riid
, pUnk
, &stream
);
232 if (hres
) return hres
;
233 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(StdGITEntry
));
234 if (entry
== NULL
) return E_OUTOFMEMORY
;
236 EnterCriticalSection(&git_section
);
239 entry
->stream
= stream
;
240 entry
->cookie
= self
->nextCookie
;
241 self
->nextCookie
++; /* inc the cookie count */
243 /* insert the new entry at the end of the list */
245 entry
->prev
= self
->lastEntry
;
246 if (entry
->prev
) entry
->prev
->next
= entry
;
247 else self
->firstEntry
= entry
;
248 self
->lastEntry
= entry
;
250 /* and return the cookie */
251 *pdwCookie
= entry
->cookie
;
253 LeaveCriticalSection(&git_section
);
255 TRACE("Cookie is 0x%lx\n", entry
->cookie
);
259 HRESULT WINAPI
StdGlobalInterfaceTable_RevokeInterfaceFromGlobal(IGlobalInterfaceTable
* iface
, DWORD dwCookie
) {
260 StdGlobalInterfaceTableImpl
* const self
= (StdGlobalInterfaceTableImpl
*) iface
;
263 TRACE("iface=%p, dwCookie=0x%x\n", iface
, (UINT
)dwCookie
);
265 entry
= StdGlobalInterfaceTable_FindEntry(iface
, dwCookie
);
267 TRACE("Entry not found\n");
268 return E_INVALIDARG
; /* not found */
271 /* Free the stream */
272 IStream_Release(entry
->stream
);
274 /* chop entry out of the list, and free the memory */
275 EnterCriticalSection(&git_section
);
276 if (entry
->prev
) entry
->prev
->next
= entry
->next
;
277 else self
->firstEntry
= entry
->next
;
278 if (entry
->next
) entry
->next
->prev
= entry
->prev
;
279 else self
->lastEntry
= entry
->prev
;
280 LeaveCriticalSection(&git_section
);
282 HeapFree(GetProcessHeap(), 0, entry
);
286 HRESULT WINAPI
StdGlobalInterfaceTable_GetInterfaceFromGlobal(IGlobalInterfaceTable
* iface
, DWORD dwCookie
, REFIID riid
, void **ppv
) {
292 TRACE("dwCookie=0x%lx, riid=%s, ppv=%p\n", dwCookie
, debugstr_guid(riid
), ppv
);
294 entry
= StdGlobalInterfaceTable_FindEntry(iface
, dwCookie
);
295 if (entry
== NULL
) return E_INVALIDARG
;
297 if (!IsEqualIID(&entry
->iid
, riid
)) {
298 WARN("entry->iid (%s) != riid\n", debugstr_guid(&entry
->iid
));
301 TRACE("entry=%p\n", entry
);
303 /* unmarshal the interface */
304 hres
= CoUnmarshalInterface(entry
->stream
, riid
, ppv
);
306 WARN("Failed to unmarshal stream\n");
310 /* rewind stream, in case it's used again */
313 IStream_Seek(entry
->stream
, move
, STREAM_SEEK_SET
, NULL
);
317 IUnknown_AddRef(lpUnk
);
318 TRACE("ppv=%p\n", *ppv
);
322 /* Classfactory definition - despite what MSDN says, some programs need this */
324 static HRESULT WINAPI
GITCF_QueryInterface(LPCLASSFACTORY iface
,REFIID riid
, LPVOID
*ppv
) {
326 if (IsEqualIID(riid
,&IID_IUnknown
) || IsEqualIID(riid
,&IID_IGlobalInterfaceTable
)) {
327 *ppv
= (LPVOID
)iface
;
330 return E_NOINTERFACE
;
332 static ULONG WINAPI
GITCF_AddRef(LPCLASSFACTORY iface
) { return 2; }
333 static ULONG WINAPI
GITCF_Release(LPCLASSFACTORY iface
) { return 1; }
335 static HRESULT WINAPI
GITCF_CreateInstance(LPCLASSFACTORY iface
, LPUNKNOWN pUnk
, REFIID riid
, LPVOID
*ppv
) {
336 if (IsEqualIID(riid
,&IID_IGlobalInterfaceTable
)) {
337 if (StdGlobalInterfaceTableInstance
== NULL
)
338 StdGlobalInterfaceTableInstance
= StdGlobalInterfaceTable_Construct();
339 return IGlobalInterfaceTable_QueryInterface( (IGlobalInterfaceTable
*) StdGlobalInterfaceTableInstance
, riid
, ppv
);
342 FIXME("(%s), not supported.\n",debugstr_guid(riid
));
343 return E_NOINTERFACE
;
346 static HRESULT WINAPI
GITCF_LockServer(LPCLASSFACTORY iface
, BOOL fLock
) {
347 FIXME("(%d), stub!\n",fLock
);
351 static IClassFactoryVtbl GITClassFactoryVtbl
= {
352 GITCF_QueryInterface
,
355 GITCF_CreateInstance
,
358 static IClassFactoryVtbl
*PGITClassFactoryVtbl
= &GITClassFactoryVtbl
;
360 HRESULT
StdGlobalInterfaceTable_GetFactory(LPVOID
*ppv
) {
361 *ppv
= &PGITClassFactoryVtbl
;
362 TRACE("Returning GIT classfactory\n");