Release 20030408.
[wine/gsoc-2012-control.git] / dlls / ole32 / git.c
blob9a398f7b7f9ea078df96b54e8ac66fa85b7a7668
1 /*
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
27 #include "config.h"
29 #include <assert.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
34 #include "windef.h"
35 #include "objbase.h"
36 #include "ole2.h"
37 #include "winbase.h"
38 #include "winerror.h"
39 #include "winternl.h"
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
57 DWORD cookie;
58 IID iid; /* IID of the interface */
59 IStream* stream; /* Holds the marshalled interface */
61 struct StdGITEntry* next;
62 struct StdGITEntry* prev;
63 } StdGITEntry;
65 /* Class data */
66 typedef struct StdGlobalInterfaceTableImpl
68 ICOM_VFIELD(IGlobalInterfaceTable);
70 ULONG ref;
71 struct StdGITEntry* firstEntry;
72 struct StdGITEntry* lastEntry;
73 ULONG nextCookie;
75 } StdGlobalInterfaceTableImpl;
78 /* IUnknown */
79 static HRESULT WINAPI StdGlobalInterfaceTable_QueryInterface(IGlobalInterfaceTable* iface, REFIID riid, void** ppvObject);
80 static ULONG WINAPI StdGlobalInterfaceTable_AddRef(IGlobalInterfaceTable* iface);
81 static ULONG WINAPI StdGlobalInterfaceTable_Release(IGlobalInterfaceTable* iface);
82 /* IGlobalInterfaceTable */
83 static HRESULT WINAPI StdGlobalInterfaceTable_RegisterInterfaceInGlobal(IGlobalInterfaceTable* iface, IUnknown* pUnk, REFIID riid, DWORD* pdwCookie);
84 static HRESULT WINAPI StdGlobalInterfaceTable_RevokeInterfaceFromGlobal(IGlobalInterfaceTable* iface, DWORD dwCookie);
85 static HRESULT WINAPI StdGlobalInterfaceTable_GetInterfaceFromGlobal(IGlobalInterfaceTable* iface, DWORD dwCookie, REFIID riid, void **ppv);
87 /* Virtual function table */
88 static ICOM_VTABLE(IGlobalInterfaceTable) StdGlobalInterfaceTableImpl_Vtbl =
90 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
91 StdGlobalInterfaceTable_QueryInterface,
92 StdGlobalInterfaceTable_AddRef,
93 StdGlobalInterfaceTable_Release,
94 StdGlobalInterfaceTable_RegisterInterfaceInGlobal,
95 StdGlobalInterfaceTable_RevokeInterfaceFromGlobal,
96 StdGlobalInterfaceTable_GetInterfaceFromGlobal
99 /***
100 * Let's go! Here is the constructor and destructor for the class.
104 /** This function constructs the GIT. It should only be called once **/
105 void* StdGlobalInterfaceTable_Construct() {
106 StdGlobalInterfaceTableImpl* newGIT;
108 newGIT = HeapAlloc(GetProcessHeap(), 0, sizeof(StdGlobalInterfaceTableImpl));
109 if (newGIT == 0) return newGIT;
111 ICOM_VTBL(newGIT) = &StdGlobalInterfaceTableImpl_Vtbl;
113 newGIT->ref = 0; /* Initialise the reference count */
114 newGIT->firstEntry = NULL; /* we start with an empty table */
115 newGIT->lastEntry = NULL;
116 newGIT->nextCookie = 0xf100; /* that's where windows starts, so that's where we start */
117 TRACE("Created the GIT at %p\n", newGIT);
119 return (void*)newGIT;
122 /** This destroys it again. It should revoke all the held interfaces first **/
123 void StdGlobalInterfaceTable_Destroy(void* self) {
124 TRACE("(%p)\n", self);
125 FIXME("Revoke held interfaces here\n");
127 HeapFree(GetProcessHeap(), 0, self);
130 /***
131 * A helper function to traverse the list and find the entry that matches the cookie.
132 * Returns NULL if not found
134 StdGITEntry* StdGlobalInterfaceTable_FindEntry(IGlobalInterfaceTable* iface, DWORD cookie) {
135 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
136 StdGITEntry* e;
138 TRACE("iface=%p, cookie=0x%x\n", iface, (UINT)cookie);
140 e = self->firstEntry;
141 while (e != NULL) {
142 if (e->cookie == cookie) return e;
143 e = e->next;
145 return NULL;
148 /***
149 * Here's the boring boilerplate stuff for IUnknown
152 HRESULT WINAPI StdGlobalInterfaceTable_QueryInterface(IGlobalInterfaceTable* iface, REFIID riid, void** ppvObject) {
153 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
155 /* Make sure silly coders can't crash us */
156 if (ppvObject == 0) return E_INVALIDARG;
158 *ppvObject = 0; /* assume we don't have the interface */
160 /* Do we implement that interface? */
161 if (IsEqualIID(&IID_IUnknown, riid)) {
162 *ppvObject = (IGlobalInterfaceTable*) self;
163 } else if (IsEqualIID(&IID_IGlobalInterfaceTable, riid)) {
164 *ppvObject = (IGlobalInterfaceTable*) self;
165 } else return E_NOINTERFACE;
167 /* Now inc the refcount */
168 /* we don't use refcounts for now: StdGlobalInterfaceTable_AddRef(iface); */
169 return S_OK;
172 ULONG WINAPI StdGlobalInterfaceTable_AddRef(IGlobalInterfaceTable* iface) {
173 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
175 self->ref++;
176 return self->ref;
179 ULONG WINAPI StdGlobalInterfaceTable_Release(IGlobalInterfaceTable* iface) {
180 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
182 self->ref--;
183 if (self->ref == 0) {
184 /* Hey ho, it's time to go, so long again 'till next weeks show! */
185 StdGlobalInterfaceTable_Destroy(self);
186 return 0;
189 return self->ref;
192 /***
193 * Now implement the actual IGlobalInterfaceTable interface
196 HRESULT WINAPI StdGlobalInterfaceTable_RegisterInterfaceInGlobal(IGlobalInterfaceTable* iface, IUnknown* pUnk, REFIID riid, DWORD* pdwCookie) {
197 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
198 IStream* stream = NULL;
199 HRESULT hres;
200 StdGITEntry* entry;
202 TRACE("iface=%p, pUnk=%p, riid=%s, pdwCookie=%p\n", iface, pUnk, debugstr_guid(riid), pdwCookie);
204 if (pUnk == NULL) return E_INVALIDARG;
206 /* marshal the interface */
207 hres = CoMarshalInterThreadInterfaceInStream(riid, pUnk, &stream);
208 if (hres) return hres;
209 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(StdGITEntry));
210 if (entry == NULL) return E_OUTOFMEMORY;
212 entry->iid = *riid;
213 entry->stream = stream;
214 entry->cookie = self->nextCookie;
215 self->nextCookie++; /* inc the cookie count */
217 /* insert the new entry at the end of the list */
218 entry->next = NULL;
219 entry->prev = self->lastEntry;
220 if (entry->prev) entry->prev->next = entry;
221 else self->firstEntry = entry;
222 self->lastEntry = entry;
224 /* and return the cookie */
225 *pdwCookie = entry->cookie;
226 return S_OK;
229 HRESULT WINAPI StdGlobalInterfaceTable_RevokeInterfaceFromGlobal(IGlobalInterfaceTable* iface, DWORD dwCookie) {
230 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
231 StdGITEntry* entry;
233 TRACE("iface=%p, dwCookie=0x%x\n", iface, (UINT)dwCookie);
235 entry = StdGlobalInterfaceTable_FindEntry(iface, dwCookie);
236 if (entry == NULL) {
237 TRACE("Entry not found\n");
238 return E_INVALIDARG; /* not found */
241 /* chop entry out of the list, and free the memory */
242 if (entry->prev) entry->prev->next = entry->next;
243 else self->firstEntry = entry->next;
244 if (entry->next) entry->next->prev = entry->prev;
245 else self->lastEntry = entry->prev;
246 HeapFree(GetProcessHeap(), 0, entry);
247 return S_OK;
250 HRESULT WINAPI StdGlobalInterfaceTable_GetInterfaceFromGlobal(IGlobalInterfaceTable* iface, DWORD dwCookie, REFIID riid, void **ppv) {
251 StdGITEntry* entry;
252 HRESULT hres;
254 entry = StdGlobalInterfaceTable_FindEntry(iface, dwCookie);
255 if (entry == NULL) return E_INVALIDARG;
256 if (!IsEqualIID(&entry->iid, &riid)) return E_INVALIDARG;
258 /* unmarshal the interface */
259 hres = CoGetInterfaceAndReleaseStream(entry->stream, riid, *ppv);
260 if (hres) return hres;
262 return S_OK;