_open_osfhandle is expected to take the absence of either _O_TEXT or
[wine/gsoc_dplay.git] / dlls / ole32 / git.c
blobbc7c0f0d31763c2548475f50b06d61dc6e8566bd
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 newGIT->lpVtbl = &StdGlobalInterfaceTableImpl_Vtbl;
112 newGIT->ref = 0; /* Initialise the reference count */
113 newGIT->firstEntry = NULL; /* we start with an empty table */
114 newGIT->lastEntry = NULL;
115 newGIT->nextCookie = 0xf100; /* that's where windows starts, so that's where we start */
116 TRACE("Created the GIT at %p\n", newGIT);
118 return (void*)newGIT;
121 /** This destroys it again. It should revoke all the held interfaces first **/
122 void StdGlobalInterfaceTable_Destroy(void* self) {
123 TRACE("(%p)\n", self);
124 FIXME("Revoke held interfaces here\n");
126 HeapFree(GetProcessHeap(), 0, self);
129 /***
130 * A helper function to traverse the list and find the entry that matches the cookie.
131 * Returns NULL if not found
133 StdGITEntry* StdGlobalInterfaceTable_FindEntry(IGlobalInterfaceTable* iface, DWORD cookie) {
134 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
135 StdGITEntry* e;
137 TRACE("iface=%p, cookie=0x%x\n", iface, (UINT)cookie);
139 e = self->firstEntry;
140 while (e != NULL) {
141 if (e->cookie == cookie) return e;
142 e = e->next;
144 return NULL;
147 /***
148 * Here's the boring boilerplate stuff for IUnknown
151 HRESULT WINAPI StdGlobalInterfaceTable_QueryInterface(IGlobalInterfaceTable* iface, REFIID riid, void** ppvObject) {
152 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
154 /* Make sure silly coders can't crash us */
155 if (ppvObject == 0) return E_INVALIDARG;
157 *ppvObject = 0; /* assume we don't have the interface */
159 /* Do we implement that interface? */
160 if (IsEqualIID(&IID_IUnknown, riid)) {
161 *ppvObject = (IGlobalInterfaceTable*) self;
162 } else if (IsEqualIID(&IID_IGlobalInterfaceTable, riid)) {
163 *ppvObject = (IGlobalInterfaceTable*) self;
164 } else return E_NOINTERFACE;
166 /* Now inc the refcount */
167 /* we don't use refcounts for now: StdGlobalInterfaceTable_AddRef(iface); */
168 return S_OK;
171 ULONG WINAPI StdGlobalInterfaceTable_AddRef(IGlobalInterfaceTable* iface) {
172 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
174 self->ref++;
175 return self->ref;
178 ULONG WINAPI StdGlobalInterfaceTable_Release(IGlobalInterfaceTable* iface) {
179 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
181 self->ref--;
182 if (self->ref == 0) {
183 /* Hey ho, it's time to go, so long again 'till next weeks show! */
184 StdGlobalInterfaceTable_Destroy(self);
185 return 0;
188 return self->ref;
191 /***
192 * Now implement the actual IGlobalInterfaceTable interface
195 HRESULT WINAPI StdGlobalInterfaceTable_RegisterInterfaceInGlobal(IGlobalInterfaceTable* iface, IUnknown* pUnk, REFIID riid, DWORD* pdwCookie) {
196 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
197 IStream* stream = NULL;
198 HRESULT hres;
199 StdGITEntry* entry;
201 TRACE("iface=%p, pUnk=%p, riid=%s, pdwCookie=%p\n", iface, pUnk, debugstr_guid(riid), pdwCookie);
203 if (pUnk == NULL) return E_INVALIDARG;
205 /* marshal the interface */
206 hres = CoMarshalInterThreadInterfaceInStream(riid, pUnk, &stream);
207 if (hres) return hres;
208 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(StdGITEntry));
209 if (entry == NULL) return E_OUTOFMEMORY;
211 entry->iid = *riid;
212 entry->stream = stream;
213 entry->cookie = self->nextCookie;
214 self->nextCookie++; /* inc the cookie count */
216 /* insert the new entry at the end of the list */
217 entry->next = NULL;
218 entry->prev = self->lastEntry;
219 if (entry->prev) entry->prev->next = entry;
220 else self->firstEntry = entry;
221 self->lastEntry = entry;
223 /* and return the cookie */
224 *pdwCookie = entry->cookie;
225 return S_OK;
228 HRESULT WINAPI StdGlobalInterfaceTable_RevokeInterfaceFromGlobal(IGlobalInterfaceTable* iface, DWORD dwCookie) {
229 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
230 StdGITEntry* entry;
232 TRACE("iface=%p, dwCookie=0x%x\n", iface, (UINT)dwCookie);
234 entry = StdGlobalInterfaceTable_FindEntry(iface, dwCookie);
235 if (entry == NULL) {
236 TRACE("Entry not found\n");
237 return E_INVALIDARG; /* not found */
240 /* chop entry out of the list, and free the memory */
241 if (entry->prev) entry->prev->next = entry->next;
242 else self->firstEntry = entry->next;
243 if (entry->next) entry->next->prev = entry->prev;
244 else self->lastEntry = entry->prev;
245 HeapFree(GetProcessHeap(), 0, entry);
246 return S_OK;
249 HRESULT WINAPI StdGlobalInterfaceTable_GetInterfaceFromGlobal(IGlobalInterfaceTable* iface, DWORD dwCookie, REFIID riid, void **ppv) {
250 StdGITEntry* entry;
251 HRESULT hres;
253 entry = StdGlobalInterfaceTable_FindEntry(iface, dwCookie);
254 if (entry == NULL) return E_INVALIDARG;
255 if (!IsEqualIID(&entry->iid, &riid)) return E_INVALIDARG;
257 /* unmarshal the interface */
258 hres = CoGetInterfaceAndReleaseStream(entry->stream, riid, *ppv);
259 if (hres) return hres;
261 return S_OK;