shdocvw: Add German translation.
[wine/multimedia.git] / dlls / ole32 / git.c
blob3800de0072f16e2aa50b0c6f3a6f20a12f6815bf
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include <stdarg.h>
29 #define COBJMACROS
30 #define NONAMELESSUNION
31 #define NONAMELESSSTRUCT
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winuser.h"
36 #include "objbase.h"
37 #include "ole2.h"
38 #include "winerror.h"
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
57 DWORD cookie;
58 IID iid; /* IID of the interface */
59 IStream* stream; /* Holds the marshalled interface */
61 struct list entry;
62 } StdGITEntry;
64 /* Class data */
65 typedef struct StdGlobalInterfaceTableImpl
67 const IGlobalInterfaceTableVtbl *lpVtbl;
69 ULONG ref;
70 struct list list;
71 ULONG nextCookie;
73 } StdGlobalInterfaceTableImpl;
75 void* StdGlobalInterfaceTableInstance;
77 static CRITICAL_SECTION git_section;
78 static CRITICAL_SECTION_DEBUG critsect_debug =
80 0, 0, &git_section,
81 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
82 0, 0, { (DWORD_PTR)(__FILE__ ": global interface table") }
84 static CRITICAL_SECTION git_section = { &critsect_debug, -1, 0, 0, 0, 0 };
87 /** This destroys it again. It should revoke all the held interfaces first **/
88 static void StdGlobalInterfaceTable_Destroy(void* self)
90 TRACE("(%p)\n", self);
91 FIXME("Revoke held interfaces here\n");
93 HeapFree(GetProcessHeap(), 0, self);
94 StdGlobalInterfaceTableInstance = NULL;
97 /***
98 * A helper function to traverse the list and find the entry that matches the cookie.
99 * Returns NULL if not found. Must be called inside git_section critical section.
101 static StdGITEntry*
102 StdGlobalInterfaceTable_FindEntry(IGlobalInterfaceTable* iface, DWORD cookie)
104 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
105 StdGITEntry* e;
107 TRACE("iface=%p, cookie=0x%x\n", iface, cookie);
109 LIST_FOR_EACH_ENTRY(e, &self->list, StdGITEntry, entry) {
110 if (e->cookie == cookie)
111 return e;
114 TRACE("Entry not found\n");
115 return NULL;
118 /***
119 * Here's the boring boilerplate stuff for IUnknown
122 static HRESULT WINAPI
123 StdGlobalInterfaceTable_QueryInterface(IGlobalInterfaceTable* iface,
124 REFIID riid, void** ppvObject)
126 /* Make sure silly coders can't crash us */
127 if (ppvObject == 0) return E_INVALIDARG;
129 *ppvObject = 0; /* assume we don't have the interface */
131 /* Do we implement that interface? */
132 if (IsEqualIID(&IID_IUnknown, riid) ||
133 IsEqualIID(&IID_IGlobalInterfaceTable, riid))
134 *ppvObject = iface;
135 else
136 return E_NOINTERFACE;
138 /* Now inc the refcount */
139 IGlobalInterfaceTable_AddRef(iface);
140 return S_OK;
143 static ULONG WINAPI
144 StdGlobalInterfaceTable_AddRef(IGlobalInterfaceTable* iface)
146 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
148 /* InterlockedIncrement(&self->ref); */
149 return self->ref;
152 static ULONG WINAPI
153 StdGlobalInterfaceTable_Release(IGlobalInterfaceTable* iface)
155 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
157 /* InterlockedDecrement(&self->ref); */
158 if (self->ref == 0) {
159 /* Hey ho, it's time to go, so long again 'till next weeks show! */
160 StdGlobalInterfaceTable_Destroy(self);
161 return 0;
164 return self->ref;
167 /***
168 * Now implement the actual IGlobalInterfaceTable interface
171 static HRESULT WINAPI
172 StdGlobalInterfaceTable_RegisterInterfaceInGlobal(
173 IGlobalInterfaceTable* iface, IUnknown* pUnk,
174 REFIID riid, DWORD* pdwCookie)
176 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
177 IStream* stream = NULL;
178 HRESULT hres;
179 StdGITEntry* entry;
180 LARGE_INTEGER zero;
182 TRACE("iface=%p, pUnk=%p, riid=%s, pdwCookie=0x%p\n", iface, pUnk, debugstr_guid(riid), pdwCookie);
184 if (pUnk == NULL) return E_INVALIDARG;
186 /* marshal the interface */
187 TRACE("About to marshal the interface\n");
189 hres = CreateStreamOnHGlobal(0, TRUE, &stream);
190 if (hres != S_OK) return hres;
191 hres = CoMarshalInterface(stream, riid, pUnk, MSHCTX_INPROC, NULL, MSHLFLAGS_TABLESTRONG);
192 if (hres != S_OK)
194 IStream_Release(stream);
195 return hres;
198 zero.QuadPart = 0;
199 IStream_Seek(stream, zero, STREAM_SEEK_SET, NULL);
201 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(StdGITEntry));
202 if (entry == NULL) return E_OUTOFMEMORY;
204 EnterCriticalSection(&git_section);
206 entry->iid = *riid;
207 entry->stream = stream;
208 entry->cookie = self->nextCookie;
209 self->nextCookie++; /* inc the cookie count */
211 /* insert the new entry at the end of the list */
212 list_add_tail(&self->list, &entry->entry);
214 /* and return the cookie */
215 *pdwCookie = entry->cookie;
217 LeaveCriticalSection(&git_section);
219 TRACE("Cookie is 0x%x\n", entry->cookie);
220 return S_OK;
223 static HRESULT WINAPI
224 StdGlobalInterfaceTable_RevokeInterfaceFromGlobal(
225 IGlobalInterfaceTable* iface, DWORD dwCookie)
227 StdGITEntry* entry;
228 HRESULT hr;
230 TRACE("iface=%p, dwCookie=0x%x\n", iface, dwCookie);
232 EnterCriticalSection(&git_section);
234 entry = StdGlobalInterfaceTable_FindEntry(iface, dwCookie);
235 if (entry == NULL) {
236 TRACE("Entry not found\n");
237 LeaveCriticalSection(&git_section);
238 return E_INVALIDARG; /* not found */
241 list_remove(&entry->entry);
243 LeaveCriticalSection(&git_section);
245 /* Free the stream */
246 hr = CoReleaseMarshalData(entry->stream);
247 if (hr != S_OK)
249 WARN("Failed to release marshal data, hr = 0x%08x\n", hr);
250 return hr;
252 IStream_Release(entry->stream);
254 HeapFree(GetProcessHeap(), 0, entry);
255 return S_OK;
258 static HRESULT WINAPI
259 StdGlobalInterfaceTable_GetInterfaceFromGlobal(
260 IGlobalInterfaceTable* iface, DWORD dwCookie,
261 REFIID riid, void **ppv)
263 StdGITEntry* entry;
264 HRESULT hres;
265 IStream *stream;
267 TRACE("dwCookie=0x%x, riid=%s, ppv=%p\n", dwCookie, debugstr_guid(riid), ppv);
269 EnterCriticalSection(&git_section);
271 entry = StdGlobalInterfaceTable_FindEntry(iface, dwCookie);
272 if (entry == NULL) {
273 WARN("Entry for cookie 0x%x not found\n", dwCookie);
274 LeaveCriticalSection(&git_section);
275 return E_INVALIDARG;
278 TRACE("entry=%p\n", entry);
280 hres = IStream_Clone(entry->stream, &stream);
282 LeaveCriticalSection(&git_section);
284 if (hres != S_OK) {
285 WARN("Failed to clone stream with error 0x%08x\n", hres);
286 return hres;
289 /* unmarshal the interface */
290 hres = CoUnmarshalInterface(stream, riid, ppv);
291 IStream_Release(stream);
293 if (hres) {
294 WARN("Failed to unmarshal stream\n");
295 return hres;
298 TRACE("ppv=%p\n", *ppv);
299 return S_OK;
302 /* Classfactory definition - despite what MSDN says, some programs need this */
304 static HRESULT WINAPI
305 GITCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid, LPVOID *ppv)
307 *ppv = NULL;
308 if (IsEqualIID(riid,&IID_IUnknown) ||
309 IsEqualIID(riid,&IID_IGlobalInterfaceTable))
311 *ppv = iface;
312 return S_OK;
314 return E_NOINTERFACE;
317 static ULONG WINAPI GITCF_AddRef(LPCLASSFACTORY iface)
319 return 2;
322 static ULONG WINAPI GITCF_Release(LPCLASSFACTORY iface)
324 return 1;
327 static HRESULT WINAPI
328 GITCF_CreateInstance(LPCLASSFACTORY iface, LPUNKNOWN pUnk,
329 REFIID riid, LPVOID *ppv)
331 if (IsEqualIID(riid,&IID_IGlobalInterfaceTable)) {
332 if (StdGlobalInterfaceTableInstance == NULL)
333 StdGlobalInterfaceTableInstance = StdGlobalInterfaceTable_Construct();
334 return IGlobalInterfaceTable_QueryInterface( (IGlobalInterfaceTable*) StdGlobalInterfaceTableInstance, riid, ppv);
337 FIXME("(%s), not supported.\n",debugstr_guid(riid));
338 return E_NOINTERFACE;
341 static HRESULT WINAPI GITCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
343 FIXME("(%d), stub!\n",fLock);
344 return S_OK;
347 static const IClassFactoryVtbl GITClassFactoryVtbl = {
348 GITCF_QueryInterface,
349 GITCF_AddRef,
350 GITCF_Release,
351 GITCF_CreateInstance,
352 GITCF_LockServer
355 static const IClassFactoryVtbl *PGITClassFactoryVtbl = &GITClassFactoryVtbl;
357 HRESULT StdGlobalInterfaceTable_GetFactory(LPVOID *ppv)
359 *ppv = &PGITClassFactoryVtbl;
360 TRACE("Returning GIT classfactory\n");
361 return S_OK;
364 /* Virtual function table */
365 static const IGlobalInterfaceTableVtbl StdGlobalInterfaceTableImpl_Vtbl =
367 StdGlobalInterfaceTable_QueryInterface,
368 StdGlobalInterfaceTable_AddRef,
369 StdGlobalInterfaceTable_Release,
370 StdGlobalInterfaceTable_RegisterInterfaceInGlobal,
371 StdGlobalInterfaceTable_RevokeInterfaceFromGlobal,
372 StdGlobalInterfaceTable_GetInterfaceFromGlobal
375 /** This function constructs the GIT. It should only be called once **/
376 void* StdGlobalInterfaceTable_Construct(void)
378 StdGlobalInterfaceTableImpl* newGIT;
380 newGIT = HeapAlloc(GetProcessHeap(), 0, sizeof(StdGlobalInterfaceTableImpl));
381 if (newGIT == 0) return newGIT;
383 newGIT->lpVtbl = &StdGlobalInterfaceTableImpl_Vtbl;
384 newGIT->ref = 1; /* Initialise the reference count */
385 list_init(&newGIT->list);
386 newGIT->nextCookie = 0xf100; /* that's where windows starts, so that's where we start */
387 TRACE("Created the GIT at %p\n", newGIT);
389 return (void*)newGIT;