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
;
226 static const LARGE_INTEGER zero
;
228 TRACE("iface=%p, pUnk=%p, riid=%s, pdwCookie=0x%p\n", iface
, pUnk
, debugstr_guid(riid
), pdwCookie
);
230 if (pUnk
== NULL
) return E_INVALIDARG
;
232 /* marshal the interface */
233 TRACE("About to marshal the interface\n");
235 hres
= CreateStreamOnHGlobal(0, TRUE
, &stream
);
236 if (hres
) return hres
;
237 hres
= CoMarshalInterface(stream
, riid
, pUnk
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_TABLESTRONG
);
240 IStream_Release(stream
);
244 IStream_Seek(stream
, zero
, SEEK_SET
, NULL
);
246 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(StdGITEntry
));
247 if (entry
== NULL
) return E_OUTOFMEMORY
;
249 EnterCriticalSection(&git_section
);
252 entry
->stream
= stream
;
253 entry
->cookie
= self
->nextCookie
;
254 self
->nextCookie
++; /* inc the cookie count */
256 /* insert the new entry at the end of the list */
258 entry
->prev
= self
->lastEntry
;
259 if (entry
->prev
) entry
->prev
->next
= entry
;
260 else self
->firstEntry
= entry
;
261 self
->lastEntry
= entry
;
263 /* and return the cookie */
264 *pdwCookie
= entry
->cookie
;
266 LeaveCriticalSection(&git_section
);
268 TRACE("Cookie is 0x%lx\n", entry
->cookie
);
272 HRESULT WINAPI
StdGlobalInterfaceTable_RevokeInterfaceFromGlobal(IGlobalInterfaceTable
* iface
, DWORD dwCookie
) {
273 StdGlobalInterfaceTableImpl
* const self
= (StdGlobalInterfaceTableImpl
*) iface
;
277 TRACE("iface=%p, dwCookie=0x%x\n", iface
, (UINT
)dwCookie
);
279 entry
= StdGlobalInterfaceTable_FindEntry(iface
, dwCookie
);
281 TRACE("Entry not found\n");
282 return E_INVALIDARG
; /* not found */
285 /* Free the stream */
286 hr
= CoReleaseMarshalData(entry
->stream
);
289 WARN("Failed to release marshal data, hr = 0x%08lx\n", hr
);
292 IStream_Release(entry
->stream
);
294 /* chop entry out of the list, and free the memory */
295 EnterCriticalSection(&git_section
);
296 if (entry
->prev
) entry
->prev
->next
= entry
->next
;
297 else self
->firstEntry
= entry
->next
;
298 if (entry
->next
) entry
->next
->prev
= entry
->prev
;
299 else self
->lastEntry
= entry
->prev
;
300 LeaveCriticalSection(&git_section
);
302 HeapFree(GetProcessHeap(), 0, entry
);
306 HRESULT WINAPI
StdGlobalInterfaceTable_GetInterfaceFromGlobal(IGlobalInterfaceTable
* iface
, DWORD dwCookie
, REFIID riid
, void **ppv
) {
312 TRACE("dwCookie=0x%lx, riid=%s, ppv=%p\n", dwCookie
, debugstr_guid(riid
), ppv
);
314 entry
= StdGlobalInterfaceTable_FindEntry(iface
, dwCookie
);
315 if (entry
== NULL
) return E_INVALIDARG
;
317 if (!IsEqualIID(&entry
->iid
, riid
)) {
318 WARN("entry->iid (%s) != riid\n", debugstr_guid(&entry
->iid
));
321 TRACE("entry=%p\n", entry
);
323 /* unmarshal the interface */
324 hres
= CoUnmarshalInterface(entry
->stream
, riid
, ppv
);
326 WARN("Failed to unmarshal stream\n");
330 /* rewind stream, in case it's used again */
333 IStream_Seek(entry
->stream
, move
, STREAM_SEEK_SET
, NULL
);
337 IUnknown_AddRef(lpUnk
);
338 TRACE("ppv=%p\n", *ppv
);
342 /* Classfactory definition - despite what MSDN says, some programs need this */
344 static HRESULT WINAPI
GITCF_QueryInterface(LPCLASSFACTORY iface
,REFIID riid
, LPVOID
*ppv
) {
346 if (IsEqualIID(riid
,&IID_IUnknown
) || IsEqualIID(riid
,&IID_IGlobalInterfaceTable
)) {
347 *ppv
= (LPVOID
)iface
;
350 return E_NOINTERFACE
;
352 static ULONG WINAPI
GITCF_AddRef(LPCLASSFACTORY iface
) { return 2; }
353 static ULONG WINAPI
GITCF_Release(LPCLASSFACTORY iface
) { return 1; }
355 static HRESULT WINAPI
GITCF_CreateInstance(LPCLASSFACTORY iface
, LPUNKNOWN pUnk
, REFIID riid
, LPVOID
*ppv
) {
356 if (IsEqualIID(riid
,&IID_IGlobalInterfaceTable
)) {
357 if (StdGlobalInterfaceTableInstance
== NULL
)
358 StdGlobalInterfaceTableInstance
= StdGlobalInterfaceTable_Construct();
359 return IGlobalInterfaceTable_QueryInterface( (IGlobalInterfaceTable
*) StdGlobalInterfaceTableInstance
, riid
, ppv
);
362 FIXME("(%s), not supported.\n",debugstr_guid(riid
));
363 return E_NOINTERFACE
;
366 static HRESULT WINAPI
GITCF_LockServer(LPCLASSFACTORY iface
, BOOL fLock
) {
367 FIXME("(%d), stub!\n",fLock
);
371 static IClassFactoryVtbl GITClassFactoryVtbl
= {
372 GITCF_QueryInterface
,
375 GITCF_CreateInstance
,
378 static IClassFactoryVtbl
*PGITClassFactoryVtbl
= &GITClassFactoryVtbl
;
380 HRESULT
StdGlobalInterfaceTable_GetFactory(LPVOID
*ppv
) {
381 *ppv
= &PGITClassFactoryVtbl
;
382 TRACE("Returning GIT classfactory\n");