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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #define NONAMELESSUNION
31 #define NONAMELESSSTRUCT
40 #include "compobj_private.h"
42 #include "wine/list.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 */
65 typedef struct StdGlobalInterfaceTableImpl
67 IGlobalInterfaceTable IGlobalInterfaceTable_iface
;
72 } StdGlobalInterfaceTableImpl
;
74 static IGlobalInterfaceTable
*std_git
;
76 static CRITICAL_SECTION git_section
;
77 static CRITICAL_SECTION_DEBUG critsect_debug
=
80 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
81 0, 0, { (DWORD_PTR
)(__FILE__
": global interface table") }
83 static CRITICAL_SECTION git_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
86 static inline StdGlobalInterfaceTableImpl
*impl_from_IGlobalInterfaceTable(IGlobalInterfaceTable
*iface
)
88 return CONTAINING_RECORD(iface
, StdGlobalInterfaceTableImpl
, IGlobalInterfaceTable_iface
);
92 * A helper function to traverse the list and find the entry that matches the cookie.
93 * Returns NULL if not found. Must be called inside git_section critical section.
95 static StdGITEntry
* StdGlobalInterfaceTable_FindEntry(StdGlobalInterfaceTableImpl
* This
,
100 TRACE("This=%p, cookie=0x%x\n", This
, cookie
);
102 LIST_FOR_EACH_ENTRY(e
, &This
->list
, StdGITEntry
, entry
) {
103 if (e
->cookie
== cookie
)
107 TRACE("Entry not found\n");
112 * Here's the boring boilerplate stuff for IUnknown
115 static HRESULT WINAPI
116 StdGlobalInterfaceTable_QueryInterface(IGlobalInterfaceTable
* iface
,
117 REFIID riid
, void** ppvObject
)
119 /* Make sure silly coders can't crash us */
120 if (ppvObject
== 0) return E_INVALIDARG
;
122 *ppvObject
= 0; /* assume we don't have the interface */
124 /* Do we implement that interface? */
125 if (IsEqualIID(&IID_IUnknown
, riid
) ||
126 IsEqualIID(&IID_IGlobalInterfaceTable
, riid
))
129 return E_NOINTERFACE
;
131 /* Now inc the refcount */
132 IGlobalInterfaceTable_AddRef(iface
);
137 StdGlobalInterfaceTable_AddRef(IGlobalInterfaceTable
* iface
)
143 StdGlobalInterfaceTable_Release(IGlobalInterfaceTable
* iface
)
149 * Now implement the actual IGlobalInterfaceTable interface
152 static HRESULT WINAPI
153 StdGlobalInterfaceTable_RegisterInterfaceInGlobal(
154 IGlobalInterfaceTable
* iface
, IUnknown
* pUnk
,
155 REFIID riid
, DWORD
* pdwCookie
)
157 StdGlobalInterfaceTableImpl
* const This
= impl_from_IGlobalInterfaceTable(iface
);
158 IStream
* stream
= NULL
;
163 TRACE("iface=%p, pUnk=%p, riid=%s, pdwCookie=0x%p\n", iface
, pUnk
, debugstr_guid(riid
), pdwCookie
);
165 if (pUnk
== NULL
) return E_INVALIDARG
;
167 /* marshal the interface */
168 TRACE("About to marshal the interface\n");
170 hres
= CreateStreamOnHGlobal(0, TRUE
, &stream
);
171 if (hres
!= S_OK
) return hres
;
172 hres
= CoMarshalInterface(stream
, riid
, pUnk
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_TABLESTRONG
);
175 IStream_Release(stream
);
180 IStream_Seek(stream
, zero
, STREAM_SEEK_SET
, NULL
);
182 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(StdGITEntry
));
185 CoReleaseMarshalData(stream
);
186 IStream_Release(stream
);
187 return E_OUTOFMEMORY
;
190 EnterCriticalSection(&git_section
);
193 entry
->stream
= stream
;
194 entry
->cookie
= This
->nextCookie
;
195 This
->nextCookie
++; /* inc the cookie count */
197 /* insert the new entry at the end of the list */
198 list_add_tail(&This
->list
, &entry
->entry
);
200 /* and return the cookie */
201 *pdwCookie
= entry
->cookie
;
203 LeaveCriticalSection(&git_section
);
205 TRACE("Cookie is 0x%x\n", entry
->cookie
);
209 static HRESULT WINAPI
210 StdGlobalInterfaceTable_RevokeInterfaceFromGlobal(
211 IGlobalInterfaceTable
* iface
, DWORD dwCookie
)
213 StdGlobalInterfaceTableImpl
* This
= impl_from_IGlobalInterfaceTable(iface
);
217 TRACE("iface=%p, dwCookie=0x%x\n", iface
, dwCookie
);
219 EnterCriticalSection(&git_section
);
221 entry
= StdGlobalInterfaceTable_FindEntry(This
, dwCookie
);
223 TRACE("Entry not found\n");
224 LeaveCriticalSection(&git_section
);
225 return E_INVALIDARG
; /* not found */
228 list_remove(&entry
->entry
);
230 LeaveCriticalSection(&git_section
);
232 /* Free the stream */
233 hr
= CoReleaseMarshalData(entry
->stream
);
236 WARN("Failed to release marshal data, hr = 0x%08x\n", hr
);
239 IStream_Release(entry
->stream
);
241 HeapFree(GetProcessHeap(), 0, entry
);
245 static HRESULT WINAPI
246 StdGlobalInterfaceTable_GetInterfaceFromGlobal(
247 IGlobalInterfaceTable
* iface
, DWORD dwCookie
,
248 REFIID riid
, void **ppv
)
250 StdGlobalInterfaceTableImpl
* This
= impl_from_IGlobalInterfaceTable(iface
);
255 TRACE("dwCookie=0x%x, riid=%s, ppv=%p\n", dwCookie
, debugstr_guid(riid
), ppv
);
257 EnterCriticalSection(&git_section
);
259 entry
= StdGlobalInterfaceTable_FindEntry(This
, dwCookie
);
261 WARN("Entry for cookie 0x%x not found\n", dwCookie
);
262 LeaveCriticalSection(&git_section
);
266 TRACE("entry=%p\n", entry
);
268 hres
= IStream_Clone(entry
->stream
, &stream
);
270 LeaveCriticalSection(&git_section
);
273 WARN("Failed to clone stream with error 0x%08x\n", hres
);
277 /* unmarshal the interface */
278 hres
= CoUnmarshalInterface(stream
, riid
, ppv
);
279 IStream_Release(stream
);
282 WARN("Failed to unmarshal stream\n");
286 TRACE("ppv=%p\n", *ppv
);
290 /* Classfactory definition - despite what MSDN says, some programs need this */
292 static HRESULT WINAPI
293 GITCF_QueryInterface(LPCLASSFACTORY iface
,REFIID riid
, LPVOID
*ppv
)
296 if (IsEqualIID(riid
, &IID_IUnknown
) ||
297 IsEqualIID(riid
, &IID_IClassFactory
))
302 return E_NOINTERFACE
;
305 static ULONG WINAPI
GITCF_AddRef(LPCLASSFACTORY iface
)
310 static ULONG WINAPI
GITCF_Release(LPCLASSFACTORY iface
)
315 static HRESULT WINAPI
316 GITCF_CreateInstance(LPCLASSFACTORY iface
, LPUNKNOWN pUnk
,
317 REFIID riid
, LPVOID
*ppv
)
319 if (IsEqualIID(riid
,&IID_IGlobalInterfaceTable
)) {
320 IGlobalInterfaceTable
*git
= get_std_git();
321 return IGlobalInterfaceTable_QueryInterface(git
, riid
, ppv
);
324 FIXME("(%s), not supported.\n",debugstr_guid(riid
));
325 return E_NOINTERFACE
;
328 static HRESULT WINAPI
GITCF_LockServer(LPCLASSFACTORY iface
, BOOL fLock
)
330 FIXME("(%d), stub!\n",fLock
);
334 static const IClassFactoryVtbl GITClassFactoryVtbl
= {
335 GITCF_QueryInterface
,
338 GITCF_CreateInstance
,
342 static IClassFactory git_classfactory
= { &GITClassFactoryVtbl
};
344 HRESULT
StdGlobalInterfaceTable_GetFactory(LPVOID
*ppv
)
346 *ppv
= &git_classfactory
;
347 TRACE("Returning GIT classfactory\n");
351 /* Virtual function table */
352 static const IGlobalInterfaceTableVtbl StdGlobalInterfaceTableImpl_Vtbl
=
354 StdGlobalInterfaceTable_QueryInterface
,
355 StdGlobalInterfaceTable_AddRef
,
356 StdGlobalInterfaceTable_Release
,
357 StdGlobalInterfaceTable_RegisterInterfaceInGlobal
,
358 StdGlobalInterfaceTable_RevokeInterfaceFromGlobal
,
359 StdGlobalInterfaceTable_GetInterfaceFromGlobal
362 IGlobalInterfaceTable
* get_std_git(void)
366 StdGlobalInterfaceTableImpl
* newGIT
;
368 newGIT
= HeapAlloc(GetProcessHeap(), 0, sizeof(StdGlobalInterfaceTableImpl
));
369 if (!newGIT
) return NULL
;
371 newGIT
->IGlobalInterfaceTable_iface
.lpVtbl
= &StdGlobalInterfaceTableImpl_Vtbl
;
372 list_init(&newGIT
->list
);
373 newGIT
->nextCookie
= 0xf100; /* that's where windows starts, so that's where we start */
375 if (InterlockedCompareExchangePointer((void**)&std_git
, &newGIT
->IGlobalInterfaceTable_iface
, NULL
))
377 HeapFree(GetProcessHeap(), 0, newGIT
);
380 TRACE("Created the GIT at %p\n", newGIT
);
386 void release_std_git(void)
388 StdGlobalInterfaceTableImpl
*git
;
389 StdGITEntry
*entry
, *entry2
;
391 if (!std_git
) return;
393 git
= impl_from_IGlobalInterfaceTable(std_git
);
394 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &git
->list
, StdGITEntry
, entry
)
396 list_remove(&entry
->entry
);
398 CoReleaseMarshalData(entry
->stream
);
399 IStream_Release(entry
->stream
);
400 HeapFree(GetProcessHeap(), 0, entry
);
403 HeapFree(GetProcessHeap(), 0, git
);