2003-03-11 02:45:38 +01:00
|
|
|
/*
|
|
|
|
* Implementation of the StdGlobalInterfaceTable object
|
|
|
|
*
|
|
|
|
* The GlobalInterfaceTable (GIT) object is used to marshal interfaces between
|
|
|
|
* threading apartments (contexts). When you want to pass an interface but not
|
|
|
|
* as a parameter, it wouldn't get marshalled automatically, so you can use this
|
|
|
|
* object to insert the interface into a table, and you get back a cookie.
|
|
|
|
* Then when it's retrieved, it'll be unmarshalled into the right apartment.
|
|
|
|
*
|
2003-03-20 02:31:16 +01:00
|
|
|
* Copyright 2003 Mike Hearn <mike@theoretic.com>
|
2003-03-11 02:45:38 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2003-03-11 02:45:38 +01:00
|
|
|
*/
|
|
|
|
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
2003-03-11 02:45:38 +01:00
|
|
|
|
2004-10-07 05:06:48 +02:00
|
|
|
#define COBJMACROS
|
|
|
|
|
2003-03-11 02:45:38 +01:00
|
|
|
#include "windef.h"
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "winbase.h"
|
2003-09-09 21:39:31 +02:00
|
|
|
#include "winuser.h"
|
2003-03-11 02:45:38 +01:00
|
|
|
#include "objbase.h"
|
|
|
|
#include "ole2.h"
|
|
|
|
#include "winerror.h"
|
|
|
|
|
|
|
|
#include "compobj_private.h"
|
|
|
|
|
2007-01-09 18:15:06 +01:00
|
|
|
#include "wine/list.h"
|
2003-03-11 02:45:38 +01:00
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(ole);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* StdGlobalInterfaceTable definition
|
|
|
|
*
|
|
|
|
* This class implements IGlobalInterfaceTable and is a process-wide singleton
|
|
|
|
* used for marshalling interfaces between threading apartments using cookies.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Each entry in the linked list of GIT entries */
|
|
|
|
typedef struct StdGITEntry
|
|
|
|
{
|
|
|
|
DWORD cookie;
|
2003-07-22 03:01:25 +02:00
|
|
|
IID iid; /* IID of the interface */
|
2003-03-11 02:45:38 +01:00
|
|
|
IStream* stream; /* Holds the marshalled interface */
|
|
|
|
|
2007-01-09 18:15:06 +01:00
|
|
|
struct list entry;
|
2003-03-11 02:45:38 +01:00
|
|
|
} StdGITEntry;
|
|
|
|
|
|
|
|
/* Class data */
|
|
|
|
typedef struct StdGlobalInterfaceTableImpl
|
|
|
|
{
|
2010-12-22 10:36:50 +01:00
|
|
|
IGlobalInterfaceTable IGlobalInterfaceTable_iface;
|
2003-03-11 02:45:38 +01:00
|
|
|
|
2007-01-09 18:15:06 +01:00
|
|
|
struct list list;
|
2003-03-11 02:45:38 +01:00
|
|
|
ULONG nextCookie;
|
|
|
|
|
|
|
|
} StdGlobalInterfaceTableImpl;
|
|
|
|
|
2013-08-18 07:10:18 +02:00
|
|
|
static IGlobalInterfaceTable *std_git;
|
2003-06-17 05:57:18 +02:00
|
|
|
|
2003-08-23 02:45:49 +02:00
|
|
|
static CRITICAL_SECTION git_section;
|
|
|
|
static CRITICAL_SECTION_DEBUG critsect_debug =
|
|
|
|
{
|
|
|
|
0, 0, &git_section,
|
|
|
|
{ &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
|
2005-09-09 12:19:44 +02:00
|
|
|
0, 0, { (DWORD_PTR)(__FILE__ ": global interface table") }
|
2003-08-23 02:45:49 +02:00
|
|
|
};
|
|
|
|
static CRITICAL_SECTION git_section = { &critsect_debug, -1, 0, 0, 0, 0 };
|
|
|
|
|
|
|
|
|
2010-12-22 10:36:50 +01:00
|
|
|
static inline StdGlobalInterfaceTableImpl *impl_from_IGlobalInterfaceTable(IGlobalInterfaceTable *iface)
|
|
|
|
{
|
|
|
|
return CONTAINING_RECORD(iface, StdGlobalInterfaceTableImpl, IGlobalInterfaceTable_iface);
|
|
|
|
}
|
|
|
|
|
2003-03-11 02:45:38 +01:00
|
|
|
/***
|
|
|
|
* A helper function to traverse the list and find the entry that matches the cookie.
|
2007-05-22 11:31:11 +02:00
|
|
|
* Returns NULL if not found. Must be called inside git_section critical section.
|
2003-03-11 02:45:38 +01:00
|
|
|
*/
|
2010-12-22 10:36:50 +01:00
|
|
|
static StdGITEntry* StdGlobalInterfaceTable_FindEntry(StdGlobalInterfaceTableImpl* This,
|
|
|
|
DWORD cookie)
|
2005-05-13 19:45:50 +02:00
|
|
|
{
|
2003-03-11 02:45:38 +01:00
|
|
|
StdGITEntry* e;
|
|
|
|
|
2022-02-08 21:09:20 +01:00
|
|
|
TRACE("%p, %#lx.\n", This, cookie);
|
2003-08-23 02:45:49 +02:00
|
|
|
|
2010-12-22 10:36:50 +01:00
|
|
|
LIST_FOR_EACH_ENTRY(e, &This->list, StdGITEntry, entry) {
|
2007-05-22 11:31:11 +02:00
|
|
|
if (e->cookie == cookie)
|
2003-08-23 02:45:49 +02:00
|
|
|
return e;
|
2003-03-11 02:45:38 +01:00
|
|
|
}
|
2010-12-22 10:36:50 +01:00
|
|
|
|
2003-06-17 05:57:18 +02:00
|
|
|
TRACE("Entry not found\n");
|
2003-03-11 02:45:38 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Here's the boring boilerplate stuff for IUnknown
|
|
|
|
*/
|
|
|
|
|
2005-05-13 19:45:50 +02:00
|
|
|
static HRESULT WINAPI
|
|
|
|
StdGlobalInterfaceTable_QueryInterface(IGlobalInterfaceTable* iface,
|
|
|
|
REFIID riid, void** ppvObject)
|
|
|
|
{
|
2003-03-11 02:45:38 +01:00
|
|
|
/* Make sure silly coders can't crash us */
|
|
|
|
if (ppvObject == 0) return E_INVALIDARG;
|
|
|
|
|
|
|
|
*ppvObject = 0; /* assume we don't have the interface */
|
|
|
|
|
|
|
|
/* Do we implement that interface? */
|
2005-05-13 19:45:50 +02:00
|
|
|
if (IsEqualIID(&IID_IUnknown, riid) ||
|
|
|
|
IsEqualIID(&IID_IGlobalInterfaceTable, riid))
|
2018-07-10 11:45:46 +02:00
|
|
|
{
|
2005-05-13 19:45:50 +02:00
|
|
|
*ppvObject = iface;
|
2018-07-10 11:45:46 +02:00
|
|
|
}
|
2005-05-13 19:45:50 +02:00
|
|
|
else
|
2018-07-10 11:45:46 +02:00
|
|
|
{
|
|
|
|
FIXME("(%s), not supported.\n", debugstr_guid(riid));
|
2005-05-13 19:45:50 +02:00
|
|
|
return E_NOINTERFACE;
|
2018-07-10 11:45:46 +02:00
|
|
|
}
|
2003-03-11 02:45:38 +01:00
|
|
|
|
|
|
|
/* Now inc the refcount */
|
2005-05-13 19:45:50 +02:00
|
|
|
IGlobalInterfaceTable_AddRef(iface);
|
2003-03-11 02:45:38 +01:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2005-05-13 19:45:50 +02:00
|
|
|
static ULONG WINAPI
|
|
|
|
StdGlobalInterfaceTable_AddRef(IGlobalInterfaceTable* iface)
|
|
|
|
{
|
2013-08-23 08:34:48 +02:00
|
|
|
return 1;
|
2003-03-11 02:45:38 +01:00
|
|
|
}
|
|
|
|
|
2005-05-13 19:45:50 +02:00
|
|
|
static ULONG WINAPI
|
|
|
|
StdGlobalInterfaceTable_Release(IGlobalInterfaceTable* iface)
|
|
|
|
{
|
2013-08-23 08:34:48 +02:00
|
|
|
return 1;
|
2003-03-11 02:45:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Now implement the actual IGlobalInterfaceTable interface
|
|
|
|
*/
|
|
|
|
|
2005-05-13 19:45:50 +02:00
|
|
|
static HRESULT WINAPI
|
|
|
|
StdGlobalInterfaceTable_RegisterInterfaceInGlobal(
|
|
|
|
IGlobalInterfaceTable* iface, IUnknown* pUnk,
|
|
|
|
REFIID riid, DWORD* pdwCookie)
|
|
|
|
{
|
2010-12-22 10:36:50 +01:00
|
|
|
StdGlobalInterfaceTableImpl* const This = impl_from_IGlobalInterfaceTable(iface);
|
2003-03-11 02:45:38 +01:00
|
|
|
IStream* stream = NULL;
|
|
|
|
HRESULT hres;
|
|
|
|
StdGITEntry* entry;
|
2005-05-13 19:45:50 +02:00
|
|
|
LARGE_INTEGER zero;
|
2003-03-11 02:45:38 +01:00
|
|
|
|
2003-06-17 05:57:18 +02:00
|
|
|
TRACE("iface=%p, pUnk=%p, riid=%s, pdwCookie=0x%p\n", iface, pUnk, debugstr_guid(riid), pdwCookie);
|
2003-03-11 02:45:38 +01:00
|
|
|
|
|
|
|
if (pUnk == NULL) return E_INVALIDARG;
|
|
|
|
|
|
|
|
/* marshal the interface */
|
2003-07-22 03:01:25 +02:00
|
|
|
TRACE("About to marshal the interface\n");
|
2005-02-15 16:47:44 +01:00
|
|
|
|
|
|
|
hres = CreateStreamOnHGlobal(0, TRUE, &stream);
|
2010-07-21 12:07:03 +02:00
|
|
|
if (hres != S_OK) return hres;
|
2005-02-15 16:47:44 +01:00
|
|
|
hres = CoMarshalInterface(stream, riid, pUnk, MSHCTX_INPROC, NULL, MSHLFLAGS_TABLESTRONG);
|
2010-07-21 12:07:03 +02:00
|
|
|
if (hres != S_OK)
|
2005-02-15 16:47:44 +01:00
|
|
|
{
|
|
|
|
IStream_Release(stream);
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2005-05-13 19:45:50 +02:00
|
|
|
zero.QuadPart = 0;
|
2007-01-09 18:15:59 +01:00
|
|
|
IStream_Seek(stream, zero, STREAM_SEEK_SET, NULL);
|
2005-02-15 16:47:44 +01:00
|
|
|
|
2003-03-11 02:45:38 +01:00
|
|
|
entry = HeapAlloc(GetProcessHeap(), 0, sizeof(StdGITEntry));
|
2013-08-25 13:40:36 +02:00
|
|
|
if (!entry)
|
|
|
|
{
|
|
|
|
CoReleaseMarshalData(stream);
|
|
|
|
IStream_Release(stream);
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
2003-08-23 02:45:49 +02:00
|
|
|
|
|
|
|
EnterCriticalSection(&git_section);
|
2003-03-11 02:45:38 +01:00
|
|
|
|
|
|
|
entry->iid = *riid;
|
|
|
|
entry->stream = stream;
|
2010-12-22 10:36:50 +01:00
|
|
|
entry->cookie = This->nextCookie;
|
|
|
|
This->nextCookie++; /* inc the cookie count */
|
2003-03-11 02:45:38 +01:00
|
|
|
|
|
|
|
/* insert the new entry at the end of the list */
|
2010-12-22 10:36:50 +01:00
|
|
|
list_add_tail(&This->list, &entry->entry);
|
2003-03-11 02:45:38 +01:00
|
|
|
|
|
|
|
/* and return the cookie */
|
|
|
|
*pdwCookie = entry->cookie;
|
2003-08-23 02:45:49 +02:00
|
|
|
|
|
|
|
LeaveCriticalSection(&git_section);
|
|
|
|
|
2022-02-08 21:09:20 +01:00
|
|
|
TRACE("Cookie is %#lx\n", entry->cookie);
|
2003-03-11 02:45:38 +01:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2005-05-13 19:45:50 +02:00
|
|
|
static HRESULT WINAPI
|
|
|
|
StdGlobalInterfaceTable_RevokeInterfaceFromGlobal(
|
|
|
|
IGlobalInterfaceTable* iface, DWORD dwCookie)
|
|
|
|
{
|
2010-12-22 10:36:50 +01:00
|
|
|
StdGlobalInterfaceTableImpl* This = impl_from_IGlobalInterfaceTable(iface);
|
2003-03-11 02:45:38 +01:00
|
|
|
StdGITEntry* entry;
|
2005-02-15 16:47:44 +01:00
|
|
|
HRESULT hr;
|
2003-03-11 02:45:38 +01:00
|
|
|
|
2022-02-08 21:09:20 +01:00
|
|
|
TRACE("%p, %#lx.\n", iface, dwCookie);
|
2007-05-22 11:31:11 +02:00
|
|
|
|
|
|
|
EnterCriticalSection(&git_section);
|
|
|
|
|
2010-12-22 10:36:50 +01:00
|
|
|
entry = StdGlobalInterfaceTable_FindEntry(This, dwCookie);
|
2003-03-11 02:45:38 +01:00
|
|
|
if (entry == NULL) {
|
|
|
|
TRACE("Entry not found\n");
|
2007-05-22 11:31:11 +02:00
|
|
|
LeaveCriticalSection(&git_section);
|
2003-03-11 02:45:38 +01:00
|
|
|
return E_INVALIDARG; /* not found */
|
|
|
|
}
|
2007-05-22 11:31:11 +02:00
|
|
|
|
|
|
|
list_remove(&entry->entry);
|
|
|
|
|
|
|
|
LeaveCriticalSection(&git_section);
|
2003-08-23 02:45:49 +02:00
|
|
|
|
|
|
|
/* Free the stream */
|
2005-02-15 16:47:44 +01:00
|
|
|
hr = CoReleaseMarshalData(entry->stream);
|
|
|
|
if (hr != S_OK)
|
|
|
|
{
|
2022-02-08 21:09:20 +01:00
|
|
|
WARN("Failed to release marshal data, hr = %#lx\n", hr);
|
2005-02-15 16:47:44 +01:00
|
|
|
return hr;
|
|
|
|
}
|
2003-08-23 02:45:49 +02:00
|
|
|
IStream_Release(entry->stream);
|
|
|
|
|
2003-03-11 02:45:38 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, entry);
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2005-05-13 19:45:50 +02:00
|
|
|
static HRESULT WINAPI
|
|
|
|
StdGlobalInterfaceTable_GetInterfaceFromGlobal(
|
|
|
|
IGlobalInterfaceTable* iface, DWORD dwCookie,
|
|
|
|
REFIID riid, void **ppv)
|
|
|
|
{
|
2010-12-22 10:36:50 +01:00
|
|
|
StdGlobalInterfaceTableImpl* This = impl_from_IGlobalInterfaceTable(iface);
|
2003-03-11 02:45:38 +01:00
|
|
|
StdGITEntry* entry;
|
|
|
|
HRESULT hres;
|
2007-05-22 11:31:11 +02:00
|
|
|
IStream *stream;
|
|
|
|
|
2022-02-08 21:09:20 +01:00
|
|
|
TRACE("%#lx, %s, %p.\n", dwCookie, debugstr_guid(riid), ppv);
|
2007-05-22 11:31:11 +02:00
|
|
|
|
|
|
|
EnterCriticalSection(&git_section);
|
|
|
|
|
2010-12-22 10:36:50 +01:00
|
|
|
entry = StdGlobalInterfaceTable_FindEntry(This, dwCookie);
|
2007-05-22 11:31:11 +02:00
|
|
|
if (entry == NULL) {
|
2022-02-08 21:09:20 +01:00
|
|
|
WARN("Entry for cookie %#lx not found\n", dwCookie);
|
2007-05-22 11:31:11 +02:00
|
|
|
LeaveCriticalSection(&git_section);
|
|
|
|
return E_INVALIDARG;
|
|
|
|
}
|
2003-08-23 02:45:49 +02:00
|
|
|
|
2003-07-22 03:01:25 +02:00
|
|
|
TRACE("entry=%p\n", entry);
|
2007-05-22 11:31:11 +02:00
|
|
|
|
|
|
|
hres = IStream_Clone(entry->stream, &stream);
|
|
|
|
|
|
|
|
LeaveCriticalSection(&git_section);
|
|
|
|
|
2010-07-21 12:07:03 +02:00
|
|
|
if (hres != S_OK) {
|
2022-02-08 21:09:20 +01:00
|
|
|
WARN("Failed to clone stream with error %#lx.\n", hres);
|
2007-05-22 11:31:11 +02:00
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2003-03-11 02:45:38 +01:00
|
|
|
/* unmarshal the interface */
|
2007-05-22 11:31:11 +02:00
|
|
|
hres = CoUnmarshalInterface(stream, riid, ppv);
|
|
|
|
IStream_Release(stream);
|
2003-08-23 02:45:49 +02:00
|
|
|
|
2013-08-18 07:14:20 +02:00
|
|
|
if (hres != S_OK) {
|
2006-06-14 14:20:14 +02:00
|
|
|
WARN("Failed to unmarshal stream\n");
|
|
|
|
return hres;
|
|
|
|
}
|
|
|
|
|
2003-08-23 02:45:49 +02:00
|
|
|
TRACE("ppv=%p\n", *ppv);
|
2003-03-11 02:45:38 +01:00
|
|
|
return S_OK;
|
|
|
|
}
|
2003-06-17 05:57:18 +02:00
|
|
|
|
2005-06-06 21:50:35 +02:00
|
|
|
static const IGlobalInterfaceTableVtbl StdGlobalInterfaceTableImpl_Vtbl =
|
2005-05-13 19:45:50 +02:00
|
|
|
{
|
2020-08-13 09:29:31 +02:00
|
|
|
StdGlobalInterfaceTable_QueryInterface,
|
|
|
|
StdGlobalInterfaceTable_AddRef,
|
|
|
|
StdGlobalInterfaceTable_Release,
|
|
|
|
StdGlobalInterfaceTable_RegisterInterfaceInGlobal,
|
|
|
|
StdGlobalInterfaceTable_RevokeInterfaceFromGlobal,
|
|
|
|
StdGlobalInterfaceTable_GetInterfaceFromGlobal
|
2005-05-13 19:45:50 +02:00
|
|
|
};
|
|
|
|
|
2020-08-13 09:29:31 +02:00
|
|
|
HRESULT WINAPI GlobalInterfaceTable_CreateInstance(IClassFactory *iface, IUnknown *outer, REFIID riid, void **obj)
|
2005-05-13 19:45:50 +02:00
|
|
|
{
|
2020-08-13 09:29:31 +02:00
|
|
|
StdGlobalInterfaceTableImpl *git;
|
2013-08-18 07:10:18 +02:00
|
|
|
|
2020-08-13 09:29:31 +02:00
|
|
|
if (!std_git)
|
2013-08-18 07:10:18 +02:00
|
|
|
{
|
2020-08-13 09:29:31 +02:00
|
|
|
git = heap_alloc(sizeof(*git));
|
|
|
|
if (!git) return E_OUTOFMEMORY;
|
|
|
|
|
|
|
|
git->IGlobalInterfaceTable_iface.lpVtbl = &StdGlobalInterfaceTableImpl_Vtbl;
|
|
|
|
list_init(&git->list);
|
|
|
|
git->nextCookie = 0xf100; /* that's where windows starts, so that's where we start */
|
|
|
|
|
|
|
|
if (InterlockedCompareExchangePointer((void **)&std_git, &git->IGlobalInterfaceTable_iface, NULL))
|
|
|
|
{
|
|
|
|
heap_free(git);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
TRACE("Created the GIT %p\n", git);
|
2013-08-18 07:10:18 +02:00
|
|
|
}
|
2005-05-13 19:45:50 +02:00
|
|
|
|
2020-08-13 09:29:31 +02:00
|
|
|
return IGlobalInterfaceTable_QueryInterface(std_git, riid, obj);
|
2005-05-13 19:45:50 +02:00
|
|
|
}
|
2013-08-23 08:34:48 +02:00
|
|
|
|
|
|
|
void release_std_git(void)
|
|
|
|
{
|
|
|
|
StdGlobalInterfaceTableImpl *git;
|
|
|
|
StdGITEntry *entry, *entry2;
|
|
|
|
|
|
|
|
if (!std_git) return;
|
|
|
|
|
|
|
|
git = impl_from_IGlobalInterfaceTable(std_git);
|
|
|
|
LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &git->list, StdGITEntry, entry)
|
|
|
|
{
|
|
|
|
list_remove(&entry->entry);
|
|
|
|
|
|
|
|
CoReleaseMarshalData(entry->stream);
|
|
|
|
IStream_Release(entry->stream);
|
|
|
|
HeapFree(GetProcessHeap(), 0, entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, git);
|
|
|
|
}
|