Add support for HCBT_SYSCOMMAND hook, add logging for HCBT_SYSCOMMAND
[wine.git] / dlls / ole32 / git.c
blobd2b753ff3d64c7ed0bf4c67f8e4a4908302c9d2c
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 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
31 #include <assert.h>
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <string.h>
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winuser.h"
40 #include "objbase.h"
41 #include "ole2.h"
42 #include "winerror.h"
43 #include "winreg.h"
44 #include "winternl.h"
46 #include "compobj_private.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(ole);
52 /****************************************************************************
53 * StdGlobalInterfaceTable definition
55 * This class implements IGlobalInterfaceTable and is a process-wide singleton
56 * used for marshalling interfaces between threading apartments using cookies.
59 /* Each entry in the linked list of GIT entries */
60 typedef struct StdGITEntry
62 DWORD cookie;
63 IID iid; /* IID of the interface */
64 IStream* stream; /* Holds the marshalled interface */
66 struct StdGITEntry* next;
67 struct StdGITEntry* prev;
68 } StdGITEntry;
70 /* Class data */
71 typedef struct StdGlobalInterfaceTableImpl
73 IGlobalInterfaceTableVtbl *lpVtbl;
75 ULONG ref;
76 struct StdGITEntry* firstEntry;
77 struct StdGITEntry* lastEntry;
78 ULONG nextCookie;
80 } StdGlobalInterfaceTableImpl;
82 void* StdGlobalInterfaceTableInstance;
85 /* IUnknown */
86 static HRESULT WINAPI StdGlobalInterfaceTable_QueryInterface(IGlobalInterfaceTable* iface, REFIID riid, void** ppvObject);
87 static ULONG WINAPI StdGlobalInterfaceTable_AddRef(IGlobalInterfaceTable* iface);
88 static ULONG WINAPI StdGlobalInterfaceTable_Release(IGlobalInterfaceTable* iface);
89 /* IGlobalInterfaceTable */
90 static HRESULT WINAPI StdGlobalInterfaceTable_RegisterInterfaceInGlobal(IGlobalInterfaceTable* iface, IUnknown* pUnk, REFIID riid, DWORD* pdwCookie);
91 static HRESULT WINAPI StdGlobalInterfaceTable_RevokeInterfaceFromGlobal(IGlobalInterfaceTable* iface, DWORD dwCookie);
92 static HRESULT WINAPI StdGlobalInterfaceTable_GetInterfaceFromGlobal(IGlobalInterfaceTable* iface, DWORD dwCookie, REFIID riid, void **ppv);
94 /* Virtual function table */
95 static IGlobalInterfaceTableVtbl StdGlobalInterfaceTableImpl_Vtbl =
97 StdGlobalInterfaceTable_QueryInterface,
98 StdGlobalInterfaceTable_AddRef,
99 StdGlobalInterfaceTable_Release,
100 StdGlobalInterfaceTable_RegisterInterfaceInGlobal,
101 StdGlobalInterfaceTable_RevokeInterfaceFromGlobal,
102 StdGlobalInterfaceTable_GetInterfaceFromGlobal
105 static CRITICAL_SECTION git_section;
106 static CRITICAL_SECTION_DEBUG critsect_debug =
108 0, 0, &git_section,
109 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
110 0, 0, { 0, (DWORD)(__FILE__ ": global interface table") }
112 static CRITICAL_SECTION git_section = { &critsect_debug, -1, 0, 0, 0, 0 };
115 /***
116 * Let's go! Here is the constructor and destructor for the class.
120 /** This function constructs the GIT. It should only be called once **/
121 void* StdGlobalInterfaceTable_Construct() {
122 StdGlobalInterfaceTableImpl* newGIT;
124 newGIT = HeapAlloc(GetProcessHeap(), 0, sizeof(StdGlobalInterfaceTableImpl));
125 if (newGIT == 0) return newGIT;
127 newGIT->lpVtbl = &StdGlobalInterfaceTableImpl_Vtbl;
128 newGIT->ref = 1; /* Initialise the reference count */
129 newGIT->firstEntry = NULL; /* we start with an empty table */
130 newGIT->lastEntry = NULL;
131 newGIT->nextCookie = 0xf100; /* that's where windows starts, so that's where we start */
132 TRACE("Created the GIT at %p\n", newGIT);
134 return (void*)newGIT;
137 /** This destroys it again. It should revoke all the held interfaces first **/
138 void StdGlobalInterfaceTable_Destroy(void* self) {
139 TRACE("(%p)\n", self);
140 FIXME("Revoke held interfaces here\n");
142 HeapFree(GetProcessHeap(), 0, self);
143 StdGlobalInterfaceTableInstance = NULL;
146 /***
147 * A helper function to traverse the list and find the entry that matches the cookie.
148 * Returns NULL if not found
150 StdGITEntry* StdGlobalInterfaceTable_FindEntry(IGlobalInterfaceTable* iface, DWORD cookie) {
151 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
152 StdGITEntry* e;
154 TRACE("iface=%p, cookie=0x%x\n", iface, (UINT)cookie);
156 EnterCriticalSection(&git_section);
157 e = self->firstEntry;
158 while (e != NULL) {
159 if (e->cookie == cookie) {
160 LeaveCriticalSection(&git_section);
161 return e;
163 e = e->next;
165 LeaveCriticalSection(&git_section);
167 TRACE("Entry not found\n");
168 return NULL;
171 /***
172 * Here's the boring boilerplate stuff for IUnknown
175 HRESULT WINAPI StdGlobalInterfaceTable_QueryInterface(IGlobalInterfaceTable* iface, REFIID riid, void** ppvObject) {
176 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
178 /* Make sure silly coders can't crash us */
179 if (ppvObject == 0) return E_INVALIDARG;
181 *ppvObject = 0; /* assume we don't have the interface */
183 /* Do we implement that interface? */
184 if (IsEqualIID(&IID_IUnknown, riid)) {
185 *ppvObject = (IGlobalInterfaceTable*) self;
186 } else if (IsEqualIID(&IID_IGlobalInterfaceTable, riid)) {
187 *ppvObject = (IGlobalInterfaceTable*) self;
188 } else return E_NOINTERFACE;
190 /* Now inc the refcount */
191 StdGlobalInterfaceTable_AddRef(iface);
192 return S_OK;
195 ULONG WINAPI StdGlobalInterfaceTable_AddRef(IGlobalInterfaceTable* iface) {
196 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
198 /* InterlockedIncrement(&self->ref); */
199 return self->ref;
202 ULONG WINAPI StdGlobalInterfaceTable_Release(IGlobalInterfaceTable* iface) {
203 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
205 /* InterlockedDecrement(&self->ref); */
206 if (self->ref == 0) {
207 /* Hey ho, it's time to go, so long again 'till next weeks show! */
208 StdGlobalInterfaceTable_Destroy(self);
209 return 0;
212 return self->ref;
215 /***
216 * Now implement the actual IGlobalInterfaceTable interface
219 HRESULT WINAPI StdGlobalInterfaceTable_RegisterInterfaceInGlobal(IGlobalInterfaceTable* iface, IUnknown* pUnk, REFIID riid, DWORD* pdwCookie) {
220 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
221 IStream* stream = NULL;
222 HRESULT hres;
223 StdGITEntry* entry;
225 TRACE("iface=%p, pUnk=%p, riid=%s, pdwCookie=0x%p\n", iface, pUnk, debugstr_guid(riid), pdwCookie);
227 if (pUnk == NULL) return E_INVALIDARG;
229 /* marshal the interface */
230 TRACE("About to marshal the interface\n");
231 hres = CoMarshalInterThreadInterfaceInStream(riid, pUnk, &stream);
232 if (hres) return hres;
233 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(StdGITEntry));
234 if (entry == NULL) return E_OUTOFMEMORY;
236 EnterCriticalSection(&git_section);
238 entry->iid = *riid;
239 entry->stream = stream;
240 entry->cookie = self->nextCookie;
241 self->nextCookie++; /* inc the cookie count */
243 /* insert the new entry at the end of the list */
244 entry->next = NULL;
245 entry->prev = self->lastEntry;
246 if (entry->prev) entry->prev->next = entry;
247 else self->firstEntry = entry;
248 self->lastEntry = entry;
250 /* and return the cookie */
251 *pdwCookie = entry->cookie;
253 LeaveCriticalSection(&git_section);
255 TRACE("Cookie is 0x%lx\n", entry->cookie);
256 return S_OK;
259 HRESULT WINAPI StdGlobalInterfaceTable_RevokeInterfaceFromGlobal(IGlobalInterfaceTable* iface, DWORD dwCookie) {
260 StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
261 StdGITEntry* entry;
263 TRACE("iface=%p, dwCookie=0x%x\n", iface, (UINT)dwCookie);
265 entry = StdGlobalInterfaceTable_FindEntry(iface, dwCookie);
266 if (entry == NULL) {
267 TRACE("Entry not found\n");
268 return E_INVALIDARG; /* not found */
271 /* Free the stream */
272 IStream_Release(entry->stream);
274 /* chop entry out of the list, and free the memory */
275 EnterCriticalSection(&git_section);
276 if (entry->prev) entry->prev->next = entry->next;
277 else self->firstEntry = entry->next;
278 if (entry->next) entry->next->prev = entry->prev;
279 else self->lastEntry = entry->prev;
280 LeaveCriticalSection(&git_section);
282 HeapFree(GetProcessHeap(), 0, entry);
283 return S_OK;
286 HRESULT WINAPI StdGlobalInterfaceTable_GetInterfaceFromGlobal(IGlobalInterfaceTable* iface, DWORD dwCookie, REFIID riid, void **ppv) {
287 StdGITEntry* entry;
288 HRESULT hres;
289 LARGE_INTEGER move;
290 LPUNKNOWN lpUnk;
292 TRACE("dwCookie=0x%lx, riid=%s, ppv=%p\n", dwCookie, debugstr_guid(riid), ppv);
294 entry = StdGlobalInterfaceTable_FindEntry(iface, dwCookie);
295 if (entry == NULL) return E_INVALIDARG;
297 if (!IsEqualIID(&entry->iid, riid)) {
298 WARN("entry->iid (%s) != riid\n", debugstr_guid(&entry->iid));
299 return E_INVALIDARG;
301 TRACE("entry=%p\n", entry);
303 /* unmarshal the interface */
304 hres = CoUnmarshalInterface(entry->stream, riid, ppv);
305 if (hres) {
306 WARN("Failed to unmarshal stream\n");
307 return hres;
310 /* rewind stream, in case it's used again */
311 move.u.LowPart = 0;
312 move.u.HighPart = 0;
313 IStream_Seek(entry->stream, move, STREAM_SEEK_SET, NULL);
315 /* addref it */
316 lpUnk = *ppv;
317 IUnknown_AddRef(lpUnk);
318 TRACE("ppv=%p\n", *ppv);
319 return S_OK;
322 /* Classfactory definition - despite what MSDN says, some programs need this */
324 static HRESULT WINAPI GITCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid, LPVOID *ppv) {
325 *ppv = NULL;
326 if (IsEqualIID(riid,&IID_IUnknown) || IsEqualIID(riid,&IID_IGlobalInterfaceTable)) {
327 *ppv = (LPVOID)iface;
328 return S_OK;
330 return E_NOINTERFACE;
332 static ULONG WINAPI GITCF_AddRef(LPCLASSFACTORY iface) { return 2; }
333 static ULONG WINAPI GITCF_Release(LPCLASSFACTORY iface) { return 1; }
335 static HRESULT WINAPI GITCF_CreateInstance(LPCLASSFACTORY iface, LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv) {
336 if (IsEqualIID(riid,&IID_IGlobalInterfaceTable)) {
337 if (StdGlobalInterfaceTableInstance == NULL)
338 StdGlobalInterfaceTableInstance = StdGlobalInterfaceTable_Construct();
339 return IGlobalInterfaceTable_QueryInterface( (IGlobalInterfaceTable*) StdGlobalInterfaceTableInstance, riid, ppv);
342 FIXME("(%s), not supported.\n",debugstr_guid(riid));
343 return E_NOINTERFACE;
346 static HRESULT WINAPI GITCF_LockServer(LPCLASSFACTORY iface, BOOL fLock) {
347 FIXME("(%d), stub!\n",fLock);
348 return S_OK;
351 static IClassFactoryVtbl GITClassFactoryVtbl = {
352 GITCF_QueryInterface,
353 GITCF_AddRef,
354 GITCF_Release,
355 GITCF_CreateInstance,
356 GITCF_LockServer
358 static IClassFactoryVtbl *PGITClassFactoryVtbl = &GITClassFactoryVtbl;
360 HRESULT StdGlobalInterfaceTable_GetFactory(LPVOID *ppv) {
361 *ppv = &PGITClassFactoryVtbl;
362 TRACE("Returning GIT classfactory\n");
363 return S_OK;