user32: Delay registration of the builtin classes until the first window is created.
[wine.git] / dlls / ole32 / git.c
blobb83d415ce932d2b3314ee507796859db99677fdf
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 IGlobalInterfaceTable IGlobalInterfaceTable_iface;
69 struct list list;
70 ULONG nextCookie;
72 } StdGlobalInterfaceTableImpl;
74 static IGlobalInterfaceTable *std_git;
76 static CRITICAL_SECTION git_section;
77 static CRITICAL_SECTION_DEBUG critsect_debug =
79 0, 0, &git_section,
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);
91 /***
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,
96 DWORD cookie)
98 StdGITEntry* e;
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)
104 return e;
107 TRACE("Entry not found\n");
108 return NULL;
111 /***
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))
127 *ppvObject = iface;
128 else
129 return E_NOINTERFACE;
131 /* Now inc the refcount */
132 IGlobalInterfaceTable_AddRef(iface);
133 return S_OK;
136 static ULONG WINAPI
137 StdGlobalInterfaceTable_AddRef(IGlobalInterfaceTable* iface)
139 return 1;
142 static ULONG WINAPI
143 StdGlobalInterfaceTable_Release(IGlobalInterfaceTable* iface)
145 return 1;
148 /***
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;
159 HRESULT hres;
160 StdGITEntry* entry;
161 LARGE_INTEGER zero;
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);
173 if (hres != S_OK)
175 IStream_Release(stream);
176 return hres;
179 zero.QuadPart = 0;
180 IStream_Seek(stream, zero, STREAM_SEEK_SET, NULL);
182 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(StdGITEntry));
183 if (!entry)
185 CoReleaseMarshalData(stream);
186 IStream_Release(stream);
187 return E_OUTOFMEMORY;
190 EnterCriticalSection(&git_section);
192 entry->iid = *riid;
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);
206 return S_OK;
209 static HRESULT WINAPI
210 StdGlobalInterfaceTable_RevokeInterfaceFromGlobal(
211 IGlobalInterfaceTable* iface, DWORD dwCookie)
213 StdGlobalInterfaceTableImpl* This = impl_from_IGlobalInterfaceTable(iface);
214 StdGITEntry* entry;
215 HRESULT hr;
217 TRACE("iface=%p, dwCookie=0x%x\n", iface, dwCookie);
219 EnterCriticalSection(&git_section);
221 entry = StdGlobalInterfaceTable_FindEntry(This, dwCookie);
222 if (entry == NULL) {
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);
234 if (hr != S_OK)
236 WARN("Failed to release marshal data, hr = 0x%08x\n", hr);
237 return hr;
239 IStream_Release(entry->stream);
241 HeapFree(GetProcessHeap(), 0, entry);
242 return S_OK;
245 static HRESULT WINAPI
246 StdGlobalInterfaceTable_GetInterfaceFromGlobal(
247 IGlobalInterfaceTable* iface, DWORD dwCookie,
248 REFIID riid, void **ppv)
250 StdGlobalInterfaceTableImpl* This = impl_from_IGlobalInterfaceTable(iface);
251 StdGITEntry* entry;
252 HRESULT hres;
253 IStream *stream;
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);
260 if (entry == NULL) {
261 WARN("Entry for cookie 0x%x not found\n", dwCookie);
262 LeaveCriticalSection(&git_section);
263 return E_INVALIDARG;
266 TRACE("entry=%p\n", entry);
268 hres = IStream_Clone(entry->stream, &stream);
270 LeaveCriticalSection(&git_section);
272 if (hres != S_OK) {
273 WARN("Failed to clone stream with error 0x%08x\n", hres);
274 return hres;
277 /* unmarshal the interface */
278 hres = CoUnmarshalInterface(stream, riid, ppv);
279 IStream_Release(stream);
281 if (hres != S_OK) {
282 WARN("Failed to unmarshal stream\n");
283 return hres;
286 TRACE("ppv=%p\n", *ppv);
287 return S_OK;
290 /* Classfactory definition - despite what MSDN says, some programs need this */
292 static HRESULT WINAPI
293 GITCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid, LPVOID *ppv)
295 *ppv = NULL;
296 if (IsEqualIID(riid, &IID_IUnknown) ||
297 IsEqualIID(riid, &IID_IClassFactory))
299 *ppv = iface;
300 return S_OK;
302 return E_NOINTERFACE;
305 static ULONG WINAPI GITCF_AddRef(LPCLASSFACTORY iface)
307 return 2;
310 static ULONG WINAPI GITCF_Release(LPCLASSFACTORY iface)
312 return 1;
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);
331 return S_OK;
334 static const IClassFactoryVtbl GITClassFactoryVtbl = {
335 GITCF_QueryInterface,
336 GITCF_AddRef,
337 GITCF_Release,
338 GITCF_CreateInstance,
339 GITCF_LockServer
342 static IClassFactory git_classfactory = { &GITClassFactoryVtbl };
344 HRESULT StdGlobalInterfaceTable_GetFactory(LPVOID *ppv)
346 *ppv = &git_classfactory;
347 TRACE("Returning GIT classfactory\n");
348 return S_OK;
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)
364 if (!std_git)
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);
379 else
380 TRACE("Created the GIT at %p\n", newGIT);
383 return std_git;
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);