secur32/tests: Make sure return values are used (LLVM/Clang).
[wine.git] / dlls / ole32 / compobj.c
bloba637439a27a1e38d2c633c01520809bee27c622f
1 /*
2 * COMPOBJ library
4 * Copyright 1995 Martin von Loewis
5 * Copyright 1998 Justin Bradford
6 * Copyright 1999 Francis Beaudet
7 * Copyright 1999 Sylvain St-Germain
8 * Copyright 2002 Marcus Meissner
9 * Copyright 2004 Mike Hearn
10 * Copyright 2005-2006 Robert Shearman (for CodeWeavers)
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
26 * Note
27 * 1. COINIT_MULTITHREADED is 0; it is the lack of COINIT_APARTMENTTHREADED
28 * Therefore do not test against COINIT_MULTITHREADED
30 * TODO list: (items bunched together depend on each other)
32 * - Implement the service control manager (in rpcss) to keep track
33 * of registered class objects: ISCM::ServerRegisterClsid et al
34 * - Implement the OXID resolver so we don't need magic endpoint names for
35 * clients and servers to meet up
39 #include "config.h"
41 #include <stdarg.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <assert.h>
46 #define COBJMACROS
47 #define NONAMELESSUNION
48 #define NONAMELESSSTRUCT
50 #include "windef.h"
51 #include "winbase.h"
52 #include "winerror.h"
53 #include "winreg.h"
54 #include "winuser.h"
55 #define USE_COM_CONTEXT_DEF
56 #include "objbase.h"
57 #include "ole2.h"
58 #include "ole2ver.h"
59 #include "ctxtcall.h"
60 #include "dde.h"
62 #include "initguid.h"
63 #include "compobj_private.h"
64 #include "moniker.h"
66 #include "wine/unicode.h"
67 #include "wine/debug.h"
69 WINE_DEFAULT_DEBUG_CHANNEL(ole);
71 #define ARRAYSIZE(array) (sizeof(array)/sizeof((array)[0]))
73 /****************************************************************************
74 * This section defines variables internal to the COM module.
77 static APARTMENT *MTA; /* protected by csApartment */
78 static APARTMENT *MainApartment; /* the first STA apartment */
79 static struct list apts = LIST_INIT( apts ); /* protected by csApartment */
81 static CRITICAL_SECTION csApartment;
82 static CRITICAL_SECTION_DEBUG critsect_debug =
84 0, 0, &csApartment,
85 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
86 0, 0, { (DWORD_PTR)(__FILE__ ": csApartment") }
88 static CRITICAL_SECTION csApartment = { &critsect_debug, -1, 0, 0, 0, 0 };
90 struct registered_psclsid
92 struct list entry;
93 IID iid;
94 CLSID clsid;
98 * This lock count counts the number of times CoInitialize is called. It is
99 * decreased every time CoUninitialize is called. When it hits 0, the COM
100 * libraries are freed
102 static LONG s_COMLockCount = 0;
103 /* Reference count used by CoAddRefServerProcess/CoReleaseServerProcess */
104 static LONG s_COMServerProcessReferences = 0;
107 * This linked list contains the list of registered class objects. These
108 * are mostly used to register the factories for out-of-proc servers of OLE
109 * objects.
111 * TODO: Make this data structure aware of inter-process communication. This
112 * means that parts of this will be exported to rpcss.
114 typedef struct tagRegisteredClass
116 struct list entry;
117 CLSID classIdentifier;
118 OXID apartment_id;
119 LPUNKNOWN classObject;
120 DWORD runContext;
121 DWORD connectFlags;
122 DWORD dwCookie;
123 LPSTREAM pMarshaledData; /* FIXME: only really need to store OXID and IPID */
124 void *RpcRegistration;
125 } RegisteredClass;
127 static struct list RegisteredClassList = LIST_INIT(RegisteredClassList);
129 static CRITICAL_SECTION csRegisteredClassList;
130 static CRITICAL_SECTION_DEBUG class_cs_debug =
132 0, 0, &csRegisteredClassList,
133 { &class_cs_debug.ProcessLocksList, &class_cs_debug.ProcessLocksList },
134 0, 0, { (DWORD_PTR)(__FILE__ ": csRegisteredClassList") }
136 static CRITICAL_SECTION csRegisteredClassList = { &class_cs_debug, -1, 0, 0, 0, 0 };
138 /*****************************************************************************
139 * This section contains OpenDllList definitions
141 * The OpenDllList contains only handles of dll loaded by CoGetClassObject or
142 * other functions that do LoadLibrary _without_ giving back a HMODULE.
143 * Without this list these handles would never be freed.
145 * FIXME: a DLL that says OK when asked for unloading is unloaded in the
146 * next unload-call but not before 600 sec.
149 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid, REFIID iid, LPVOID *ppv);
150 typedef HRESULT (WINAPI *DllCanUnloadNowFunc)(void);
152 typedef struct tagOpenDll
154 LONG refs;
155 LPWSTR library_name;
156 HANDLE library;
157 DllGetClassObjectFunc DllGetClassObject;
158 DllCanUnloadNowFunc DllCanUnloadNow;
159 struct list entry;
160 } OpenDll;
162 static struct list openDllList = LIST_INIT(openDllList);
164 static CRITICAL_SECTION csOpenDllList;
165 static CRITICAL_SECTION_DEBUG dll_cs_debug =
167 0, 0, &csOpenDllList,
168 { &dll_cs_debug.ProcessLocksList, &dll_cs_debug.ProcessLocksList },
169 0, 0, { (DWORD_PTR)(__FILE__ ": csOpenDllList") }
171 static CRITICAL_SECTION csOpenDllList = { &dll_cs_debug, -1, 0, 0, 0, 0 };
173 struct apartment_loaded_dll
175 struct list entry;
176 OpenDll *dll;
177 DWORD unload_time;
178 BOOL multi_threaded;
181 static const WCHAR wszAptWinClass[] = {'O','l','e','M','a','i','n','T','h','r','e','a','d','W','n','d','C','l','a','s','s',' ',
182 '0','x','#','#','#','#','#','#','#','#',' ',0};
184 /*****************************************************************************
185 * This section contains OpenDllList implementation
188 static OpenDll *COMPOBJ_DllList_Get(LPCWSTR library_name)
190 OpenDll *ptr;
191 OpenDll *ret = NULL;
192 EnterCriticalSection(&csOpenDllList);
193 LIST_FOR_EACH_ENTRY(ptr, &openDllList, OpenDll, entry)
195 if (!strcmpiW(library_name, ptr->library_name) &&
196 (InterlockedIncrement(&ptr->refs) != 1) /* entry is being destroy if == 1 */)
198 ret = ptr;
199 break;
202 LeaveCriticalSection(&csOpenDllList);
203 return ret;
206 /* caller must ensure that library_name is not already in the open dll list */
207 static HRESULT COMPOBJ_DllList_Add(LPCWSTR library_name, OpenDll **ret)
209 OpenDll *entry;
210 int len;
211 HRESULT hr = S_OK;
212 HANDLE hLibrary;
213 DllCanUnloadNowFunc DllCanUnloadNow;
214 DllGetClassObjectFunc DllGetClassObject;
216 TRACE("\n");
218 *ret = COMPOBJ_DllList_Get(library_name);
219 if (*ret) return S_OK;
221 /* do this outside the csOpenDllList to avoid creating a lock dependency on
222 * the loader lock */
223 hLibrary = LoadLibraryExW(library_name, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
224 if (!hLibrary)
226 ERR("couldn't load in-process dll %s\n", debugstr_w(library_name));
227 /* failure: DLL could not be loaded */
228 return E_ACCESSDENIED; /* FIXME: or should this be CO_E_DLLNOTFOUND? */
231 DllCanUnloadNow = (void *)GetProcAddress(hLibrary, "DllCanUnloadNow");
232 /* Note: failing to find DllCanUnloadNow is not a failure */
233 DllGetClassObject = (void *)GetProcAddress(hLibrary, "DllGetClassObject");
234 if (!DllGetClassObject)
236 /* failure: the dll did not export DllGetClassObject */
237 ERR("couldn't find function DllGetClassObject in %s\n", debugstr_w(library_name));
238 FreeLibrary(hLibrary);
239 return CO_E_DLLNOTFOUND;
242 EnterCriticalSection( &csOpenDllList );
244 *ret = COMPOBJ_DllList_Get(library_name);
245 if (*ret)
247 /* another caller to this function already added the dll while we
248 * weren't in the critical section */
249 FreeLibrary(hLibrary);
251 else
253 len = strlenW(library_name);
254 entry = HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
255 if (entry)
256 entry->library_name = HeapAlloc(GetProcessHeap(), 0, (len + 1)*sizeof(WCHAR));
257 if (entry && entry->library_name)
259 memcpy(entry->library_name, library_name, (len + 1)*sizeof(WCHAR));
260 entry->library = hLibrary;
261 entry->refs = 1;
262 entry->DllCanUnloadNow = DllCanUnloadNow;
263 entry->DllGetClassObject = DllGetClassObject;
264 list_add_tail(&openDllList, &entry->entry);
266 else
268 HeapFree(GetProcessHeap(), 0, entry);
269 hr = E_OUTOFMEMORY;
270 FreeLibrary(hLibrary);
272 *ret = entry;
275 LeaveCriticalSection( &csOpenDllList );
277 return hr;
280 /* pass FALSE for free_entry to release a reference without destroying the
281 * entry if it reaches zero or TRUE otherwise */
282 static void COMPOBJ_DllList_ReleaseRef(OpenDll *entry, BOOL free_entry)
284 if (!InterlockedDecrement(&entry->refs) && free_entry)
286 EnterCriticalSection(&csOpenDllList);
287 list_remove(&entry->entry);
288 LeaveCriticalSection(&csOpenDllList);
290 TRACE("freeing %p\n", entry->library);
291 FreeLibrary(entry->library);
293 HeapFree(GetProcessHeap(), 0, entry->library_name);
294 HeapFree(GetProcessHeap(), 0, entry);
298 /* frees memory associated with active dll list */
299 static void COMPOBJ_DllList_Free(void)
301 OpenDll *entry, *cursor2;
302 EnterCriticalSection(&csOpenDllList);
303 LIST_FOR_EACH_ENTRY_SAFE(entry, cursor2, &openDllList, OpenDll, entry)
305 list_remove(&entry->entry);
307 HeapFree(GetProcessHeap(), 0, entry->library_name);
308 HeapFree(GetProcessHeap(), 0, entry);
310 LeaveCriticalSection(&csOpenDllList);
313 /******************************************************************************
314 * Manage apartments.
317 static DWORD apartment_addref(struct apartment *apt)
319 DWORD refs = InterlockedIncrement(&apt->refs);
320 TRACE("%s: before = %d\n", wine_dbgstr_longlong(apt->oxid), refs - 1);
321 return refs;
324 /* allocates memory and fills in the necessary fields for a new apartment
325 * object. must be called inside apartment cs */
326 static APARTMENT *apartment_construct(DWORD model)
328 APARTMENT *apt;
330 TRACE("creating new apartment, model=%d\n", model);
332 apt = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*apt));
333 apt->tid = GetCurrentThreadId();
335 list_init(&apt->proxies);
336 list_init(&apt->stubmgrs);
337 list_init(&apt->psclsids);
338 list_init(&apt->loaded_dlls);
339 apt->ipidc = 0;
340 apt->refs = 1;
341 apt->remunk_exported = FALSE;
342 apt->oidc = 1;
343 InitializeCriticalSection(&apt->cs);
344 DEBUG_SET_CRITSEC_NAME(&apt->cs, "apartment");
346 apt->multi_threaded = !(model & COINIT_APARTMENTTHREADED);
348 if (apt->multi_threaded)
350 /* FIXME: should be randomly generated by in an RPC call to rpcss */
351 apt->oxid = ((OXID)GetCurrentProcessId() << 32) | 0xcafe;
353 else
355 /* FIXME: should be randomly generated by in an RPC call to rpcss */
356 apt->oxid = ((OXID)GetCurrentProcessId() << 32) | GetCurrentThreadId();
359 TRACE("Created apartment on OXID %s\n", wine_dbgstr_longlong(apt->oxid));
361 list_add_head(&apts, &apt->entry);
363 return apt;
366 /* gets and existing apartment if one exists or otherwise creates an apartment
367 * structure which stores OLE apartment-local information and stores a pointer
368 * to it in the thread-local storage */
369 static APARTMENT *apartment_get_or_create(DWORD model)
371 APARTMENT *apt = COM_CurrentApt();
373 if (!apt)
375 if (model & COINIT_APARTMENTTHREADED)
377 EnterCriticalSection(&csApartment);
379 apt = apartment_construct(model);
380 if (!MainApartment)
382 MainApartment = apt;
383 apt->main = TRUE;
384 TRACE("Created main-threaded apartment with OXID %s\n", wine_dbgstr_longlong(apt->oxid));
387 LeaveCriticalSection(&csApartment);
389 if (apt->main)
390 apartment_createwindowifneeded(apt);
392 else
394 EnterCriticalSection(&csApartment);
396 /* The multi-threaded apartment (MTA) contains zero or more threads interacting
397 * with free threaded (ie thread safe) COM objects. There is only ever one MTA
398 * in a process */
399 if (MTA)
401 TRACE("entering the multithreaded apartment %s\n", wine_dbgstr_longlong(MTA->oxid));
402 apartment_addref(MTA);
404 else
405 MTA = apartment_construct(model);
407 apt = MTA;
409 LeaveCriticalSection(&csApartment);
411 COM_CurrentInfo()->apt = apt;
414 return apt;
417 static inline BOOL apartment_is_model(const APARTMENT *apt, DWORD model)
419 return (apt->multi_threaded == !(model & COINIT_APARTMENTTHREADED));
422 static void COM_RevokeRegisteredClassObject(RegisteredClass *curClass)
424 list_remove(&curClass->entry);
426 if (curClass->runContext & CLSCTX_LOCAL_SERVER)
427 RPC_StopLocalServer(curClass->RpcRegistration);
430 * Release the reference to the class object.
432 IUnknown_Release(curClass->classObject);
434 if (curClass->pMarshaledData)
436 LARGE_INTEGER zero;
437 memset(&zero, 0, sizeof(zero));
438 IStream_Seek(curClass->pMarshaledData, zero, STREAM_SEEK_SET, NULL);
439 CoReleaseMarshalData(curClass->pMarshaledData);
440 IStream_Release(curClass->pMarshaledData);
443 HeapFree(GetProcessHeap(), 0, curClass);
446 static void COM_RevokeAllClasses(const struct apartment *apt)
448 RegisteredClass *curClass, *cursor;
450 EnterCriticalSection( &csRegisteredClassList );
452 LIST_FOR_EACH_ENTRY_SAFE(curClass, cursor, &RegisteredClassList, RegisteredClass, entry)
454 if (curClass->apartment_id == apt->oxid)
455 COM_RevokeRegisteredClassObject(curClass);
458 LeaveCriticalSection( &csRegisteredClassList );
461 /***********************************************************************
462 * CoRevokeClassObject [OLE32.@]
464 * Removes a class object from the class registry.
466 * PARAMS
467 * dwRegister [I] Cookie returned from CoRegisterClassObject().
469 * RETURNS
470 * Success: S_OK.
471 * Failure: HRESULT code.
473 * NOTES
474 * Must be called from the same apartment that called CoRegisterClassObject(),
475 * otherwise it will fail with RPC_E_WRONG_THREAD.
477 * SEE ALSO
478 * CoRegisterClassObject
480 HRESULT WINAPI CoRevokeClassObject(
481 DWORD dwRegister)
483 HRESULT hr = E_INVALIDARG;
484 RegisteredClass *curClass;
485 APARTMENT *apt;
487 TRACE("(%08x)\n",dwRegister);
489 apt = COM_CurrentApt();
490 if (!apt)
492 ERR("COM was not initialized\n");
493 return CO_E_NOTINITIALIZED;
496 EnterCriticalSection( &csRegisteredClassList );
498 LIST_FOR_EACH_ENTRY(curClass, &RegisteredClassList, RegisteredClass, entry)
501 * Check if we have a match on the cookie.
503 if (curClass->dwCookie == dwRegister)
505 if (curClass->apartment_id == apt->oxid)
507 COM_RevokeRegisteredClassObject(curClass);
508 hr = S_OK;
510 else
512 ERR("called from wrong apartment, should be called from %s\n",
513 wine_dbgstr_longlong(curClass->apartment_id));
514 hr = RPC_E_WRONG_THREAD;
516 break;
520 LeaveCriticalSection( &csRegisteredClassList );
522 return hr;
525 /* frees unused libraries loaded by apartment_getclassobject by calling the
526 * DLL's DllCanUnloadNow entry point */
527 static void apartment_freeunusedlibraries(struct apartment *apt, DWORD delay)
529 struct apartment_loaded_dll *entry, *next;
530 EnterCriticalSection(&apt->cs);
531 LIST_FOR_EACH_ENTRY_SAFE(entry, next, &apt->loaded_dlls, struct apartment_loaded_dll, entry)
533 if (entry->dll->DllCanUnloadNow && (entry->dll->DllCanUnloadNow() == S_OK))
535 DWORD real_delay = delay;
537 if (real_delay == INFINITE)
539 /* DLLs that return multi-threaded objects aren't unloaded
540 * straight away to cope for programs that have races between
541 * last object destruction and threads in the DLLs that haven't
542 * finished, despite DllCanUnloadNow returning S_OK */
543 if (entry->multi_threaded)
544 real_delay = 10 * 60 * 1000; /* 10 minutes */
545 else
546 real_delay = 0;
549 if (!real_delay || (entry->unload_time && (entry->unload_time < GetTickCount())))
551 list_remove(&entry->entry);
552 COMPOBJ_DllList_ReleaseRef(entry->dll, TRUE);
553 HeapFree(GetProcessHeap(), 0, entry);
555 else
556 entry->unload_time = GetTickCount() + real_delay;
558 else if (entry->unload_time)
559 entry->unload_time = 0;
561 LeaveCriticalSection(&apt->cs);
564 DWORD apartment_release(struct apartment *apt)
566 DWORD ret;
568 EnterCriticalSection(&csApartment);
570 ret = InterlockedDecrement(&apt->refs);
571 TRACE("%s: after = %d\n", wine_dbgstr_longlong(apt->oxid), ret);
572 /* destruction stuff that needs to happen under csApartment CS */
573 if (ret == 0)
575 if (apt == MTA) MTA = NULL;
576 else if (apt == MainApartment) MainApartment = NULL;
577 list_remove(&apt->entry);
580 LeaveCriticalSection(&csApartment);
582 if (ret == 0)
584 struct list *cursor, *cursor2;
586 TRACE("destroying apartment %p, oxid %s\n", apt, wine_dbgstr_longlong(apt->oxid));
588 /* Release the references to the registered class objects */
589 COM_RevokeAllClasses(apt);
591 /* no locking is needed for this apartment, because no other thread
592 * can access it at this point */
594 apartment_disconnectproxies(apt);
596 if (apt->win) DestroyWindow(apt->win);
597 if (apt->host_apt_tid) PostThreadMessageW(apt->host_apt_tid, WM_QUIT, 0, 0);
599 LIST_FOR_EACH_SAFE(cursor, cursor2, &apt->stubmgrs)
601 struct stub_manager *stubmgr = LIST_ENTRY(cursor, struct stub_manager, entry);
602 /* release the implicit reference given by the fact that the
603 * stub has external references (it must do since it is in the
604 * stub manager list in the apartment and all non-apartment users
605 * must have a ref on the apartment and so it cannot be destroyed).
607 stub_manager_int_release(stubmgr);
610 LIST_FOR_EACH_SAFE(cursor, cursor2, &apt->psclsids)
612 struct registered_psclsid *registered_psclsid =
613 LIST_ENTRY(cursor, struct registered_psclsid, entry);
615 list_remove(&registered_psclsid->entry);
616 HeapFree(GetProcessHeap(), 0, registered_psclsid);
619 /* if this assert fires, then another thread took a reference to a
620 * stub manager without taking a reference to the containing
621 * apartment, which it must do. */
622 assert(list_empty(&apt->stubmgrs));
624 if (apt->filter) IUnknown_Release(apt->filter);
626 /* free as many unused libraries as possible... */
627 apartment_freeunusedlibraries(apt, 0);
629 /* ... and free the memory for the apartment loaded dll entry and
630 * release the dll list reference without freeing the library for the
631 * rest */
632 while ((cursor = list_head(&apt->loaded_dlls)))
634 struct apartment_loaded_dll *apartment_loaded_dll = LIST_ENTRY(cursor, struct apartment_loaded_dll, entry);
635 COMPOBJ_DllList_ReleaseRef(apartment_loaded_dll->dll, FALSE);
636 list_remove(cursor);
637 HeapFree(GetProcessHeap(), 0, apartment_loaded_dll);
640 DEBUG_CLEAR_CRITSEC_NAME(&apt->cs);
641 DeleteCriticalSection(&apt->cs);
643 HeapFree(GetProcessHeap(), 0, apt);
646 return ret;
649 /* The given OXID must be local to this process:
651 * The ref parameter is here mostly to ensure people remember that
652 * they get one, you should normally take a ref for thread safety.
654 APARTMENT *apartment_findfromoxid(OXID oxid, BOOL ref)
656 APARTMENT *result = NULL;
657 struct list *cursor;
659 EnterCriticalSection(&csApartment);
660 LIST_FOR_EACH( cursor, &apts )
662 struct apartment *apt = LIST_ENTRY( cursor, struct apartment, entry );
663 if (apt->oxid == oxid)
665 result = apt;
666 if (ref) apartment_addref(result);
667 break;
670 LeaveCriticalSection(&csApartment);
672 return result;
675 /* gets the apartment which has a given creator thread ID. The caller must
676 * release the reference from the apartment as soon as the apartment pointer
677 * is no longer required. */
678 APARTMENT *apartment_findfromtid(DWORD tid)
680 APARTMENT *result = NULL;
681 struct list *cursor;
683 EnterCriticalSection(&csApartment);
684 LIST_FOR_EACH( cursor, &apts )
686 struct apartment *apt = LIST_ENTRY( cursor, struct apartment, entry );
687 if (apt->tid == tid)
689 result = apt;
690 apartment_addref(result);
691 break;
694 LeaveCriticalSection(&csApartment);
696 return result;
699 /* gets the main apartment if it exists. The caller must
700 * release the reference from the apartment as soon as the apartment pointer
701 * is no longer required. */
702 static APARTMENT *apartment_findmain(void)
704 APARTMENT *result;
706 EnterCriticalSection(&csApartment);
708 result = MainApartment;
709 if (result) apartment_addref(result);
711 LeaveCriticalSection(&csApartment);
713 return result;
716 /* gets the multi-threaded apartment if it exists. The caller must
717 * release the reference from the apartment as soon as the apartment pointer
718 * is no longer required. */
719 static APARTMENT *apartment_find_multi_threaded(void)
721 APARTMENT *result = NULL;
722 struct list *cursor;
724 EnterCriticalSection(&csApartment);
726 LIST_FOR_EACH( cursor, &apts )
728 struct apartment *apt = LIST_ENTRY( cursor, struct apartment, entry );
729 if (apt->multi_threaded)
731 result = apt;
732 apartment_addref(result);
733 break;
737 LeaveCriticalSection(&csApartment);
738 return result;
741 /* gets the specified class object by loading the appropriate DLL, if
742 * necessary and calls the DllGetClassObject function for the DLL */
743 static HRESULT apartment_getclassobject(struct apartment *apt, LPCWSTR dllpath,
744 BOOL apartment_threaded,
745 REFCLSID rclsid, REFIID riid, void **ppv)
747 static const WCHAR wszOle32[] = {'o','l','e','3','2','.','d','l','l',0};
748 HRESULT hr = S_OK;
749 BOOL found = FALSE;
750 struct apartment_loaded_dll *apartment_loaded_dll;
752 if (!strcmpiW(dllpath, wszOle32))
754 /* we don't need to control the lifetime of this dll, so use the local
755 * implementation of DllGetClassObject directly */
756 TRACE("calling ole32!DllGetClassObject\n");
757 hr = DllGetClassObject(rclsid, riid, ppv);
759 if (hr != S_OK)
760 ERR("DllGetClassObject returned error 0x%08x\n", hr);
762 return hr;
765 EnterCriticalSection(&apt->cs);
767 LIST_FOR_EACH_ENTRY(apartment_loaded_dll, &apt->loaded_dlls, struct apartment_loaded_dll, entry)
768 if (!strcmpiW(dllpath, apartment_loaded_dll->dll->library_name))
770 TRACE("found %s already loaded\n", debugstr_w(dllpath));
771 found = TRUE;
772 break;
775 if (!found)
777 apartment_loaded_dll = HeapAlloc(GetProcessHeap(), 0, sizeof(*apartment_loaded_dll));
778 if (!apartment_loaded_dll)
779 hr = E_OUTOFMEMORY;
780 if (SUCCEEDED(hr))
782 apartment_loaded_dll->unload_time = 0;
783 apartment_loaded_dll->multi_threaded = FALSE;
784 hr = COMPOBJ_DllList_Add( dllpath, &apartment_loaded_dll->dll );
785 if (FAILED(hr))
786 HeapFree(GetProcessHeap(), 0, apartment_loaded_dll);
788 if (SUCCEEDED(hr))
790 TRACE("added new loaded dll %s\n", debugstr_w(dllpath));
791 list_add_tail(&apt->loaded_dlls, &apartment_loaded_dll->entry);
795 LeaveCriticalSection(&apt->cs);
797 if (SUCCEEDED(hr))
799 /* one component being multi-threaded overrides any number of
800 * apartment-threaded components */
801 if (!apartment_threaded)
802 apartment_loaded_dll->multi_threaded = TRUE;
804 TRACE("calling DllGetClassObject %p\n", apartment_loaded_dll->dll->DllGetClassObject);
805 /* OK: get the ClassObject */
806 hr = apartment_loaded_dll->dll->DllGetClassObject(rclsid, riid, ppv);
808 if (hr != S_OK)
809 ERR("DllGetClassObject returned error 0x%08x\n", hr);
812 return hr;
815 /***********************************************************************
816 * COM_RegReadPath [internal]
818 * Reads a registry value and expands it when necessary
820 static DWORD COM_RegReadPath(HKEY hkeyroot, const WCHAR *keyname, const WCHAR *valuename, WCHAR * dst, DWORD dstlen)
822 DWORD ret;
823 HKEY key;
824 DWORD keytype;
825 WCHAR src[MAX_PATH];
826 DWORD dwLength = dstlen * sizeof(WCHAR);
828 if((ret = RegOpenKeyExW(hkeyroot, keyname, 0, KEY_READ, &key)) == ERROR_SUCCESS) {
829 if( (ret = RegQueryValueExW(key, NULL, NULL, &keytype, (LPBYTE)src, &dwLength)) == ERROR_SUCCESS ) {
830 if (keytype == REG_EXPAND_SZ) {
831 if (dstlen <= ExpandEnvironmentStringsW(src, dst, dstlen)) ret = ERROR_MORE_DATA;
832 } else {
833 const WCHAR *quote_start;
834 quote_start = strchrW(src, '\"');
835 if (quote_start) {
836 const WCHAR *quote_end = strchrW(quote_start + 1, '\"');
837 if (quote_end) {
838 memmove(src, quote_start + 1,
839 (quote_end - quote_start - 1) * sizeof(WCHAR));
840 src[quote_end - quote_start - 1] = '\0';
843 lstrcpynW(dst, src, dstlen);
846 RegCloseKey (key);
848 return ret;
851 struct host_object_params
853 HKEY hkeydll;
854 CLSID clsid; /* clsid of object to marshal */
855 IID iid; /* interface to marshal */
856 HANDLE event; /* event signalling when ready for multi-threaded case */
857 HRESULT hr; /* result for multi-threaded case */
858 IStream *stream; /* stream that the object will be marshaled into */
859 BOOL apartment_threaded; /* is the component purely apartment-threaded? */
862 static HRESULT apartment_hostobject(struct apartment *apt,
863 const struct host_object_params *params)
865 IUnknown *object;
866 HRESULT hr;
867 static const LARGE_INTEGER llZero;
868 WCHAR dllpath[MAX_PATH+1];
870 TRACE("clsid %s, iid %s\n", debugstr_guid(&params->clsid), debugstr_guid(&params->iid));
872 if (COM_RegReadPath(params->hkeydll, NULL, NULL, dllpath, ARRAYSIZE(dllpath)) != ERROR_SUCCESS)
874 /* failure: CLSID is not found in registry */
875 WARN("class %s not registered inproc\n", debugstr_guid(&params->clsid));
876 return REGDB_E_CLASSNOTREG;
879 hr = apartment_getclassobject(apt, dllpath, params->apartment_threaded,
880 &params->clsid, &params->iid, (void **)&object);
881 if (FAILED(hr))
882 return hr;
884 hr = CoMarshalInterface(params->stream, &params->iid, object, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
885 if (FAILED(hr))
886 IUnknown_Release(object);
887 IStream_Seek(params->stream, llZero, STREAM_SEEK_SET, NULL);
889 return hr;
892 static LRESULT CALLBACK apartment_wndproc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
894 switch (msg)
896 case DM_EXECUTERPC:
897 RPC_ExecuteCall((struct dispatch_params *)lParam);
898 return 0;
899 case DM_HOSTOBJECT:
900 return apartment_hostobject(COM_CurrentApt(), (const struct host_object_params *)lParam);
901 default:
902 return DefWindowProcW(hWnd, msg, wParam, lParam);
906 struct host_thread_params
908 COINIT threading_model;
909 HANDLE ready_event;
910 HWND apartment_hwnd;
913 /* thread for hosting an object to allow an object to appear to be created in
914 * an apartment with an incompatible threading model */
915 static DWORD CALLBACK apartment_hostobject_thread(LPVOID p)
917 struct host_thread_params *params = p;
918 MSG msg;
919 HRESULT hr;
920 struct apartment *apt;
922 TRACE("\n");
924 hr = CoInitializeEx(NULL, params->threading_model);
925 if (FAILED(hr)) return hr;
927 apt = COM_CurrentApt();
928 if (params->threading_model == COINIT_APARTMENTTHREADED)
930 apartment_createwindowifneeded(apt);
931 params->apartment_hwnd = apartment_getwindow(apt);
933 else
934 params->apartment_hwnd = NULL;
936 /* force the message queue to be created before signaling parent thread */
937 PeekMessageW(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
939 SetEvent(params->ready_event);
940 params = NULL; /* can't touch params after here as it may be invalid */
942 while (GetMessageW(&msg, NULL, 0, 0))
944 if (!msg.hwnd && (msg.message == DM_HOSTOBJECT))
946 struct host_object_params *obj_params = (struct host_object_params *)msg.lParam;
947 obj_params->hr = apartment_hostobject(apt, obj_params);
948 SetEvent(obj_params->event);
950 else
952 TranslateMessage(&msg);
953 DispatchMessageW(&msg);
957 TRACE("exiting\n");
959 CoUninitialize();
961 return S_OK;
964 /* finds or creates a host apartment, creates the object inside it and returns
965 * a proxy to it so that the object can be used in the apartment of the
966 * caller of this function */
967 static HRESULT apartment_hostobject_in_hostapt(
968 struct apartment *apt, BOOL multi_threaded, BOOL main_apartment,
969 HKEY hkeydll, REFCLSID rclsid, REFIID riid, void **ppv)
971 struct host_object_params params;
972 HWND apartment_hwnd = NULL;
973 DWORD apartment_tid = 0;
974 HRESULT hr;
976 if (!multi_threaded && main_apartment)
978 APARTMENT *host_apt = apartment_findmain();
979 if (host_apt)
981 apartment_hwnd = apartment_getwindow(host_apt);
982 apartment_release(host_apt);
986 if (!apartment_hwnd)
988 EnterCriticalSection(&apt->cs);
990 if (!apt->host_apt_tid)
992 struct host_thread_params thread_params;
993 HANDLE handles[2];
994 DWORD wait_value;
996 thread_params.threading_model = multi_threaded ? COINIT_MULTITHREADED : COINIT_APARTMENTTHREADED;
997 handles[0] = thread_params.ready_event = CreateEventW(NULL, FALSE, FALSE, NULL);
998 thread_params.apartment_hwnd = NULL;
999 handles[1] = CreateThread(NULL, 0, apartment_hostobject_thread, &thread_params, 0, &apt->host_apt_tid);
1000 if (!handles[1])
1002 CloseHandle(handles[0]);
1003 LeaveCriticalSection(&apt->cs);
1004 return E_OUTOFMEMORY;
1006 wait_value = WaitForMultipleObjects(2, handles, FALSE, INFINITE);
1007 CloseHandle(handles[0]);
1008 CloseHandle(handles[1]);
1009 if (wait_value == WAIT_OBJECT_0)
1010 apt->host_apt_hwnd = thread_params.apartment_hwnd;
1011 else
1013 LeaveCriticalSection(&apt->cs);
1014 return E_OUTOFMEMORY;
1018 if (multi_threaded || !main_apartment)
1020 apartment_hwnd = apt->host_apt_hwnd;
1021 apartment_tid = apt->host_apt_tid;
1024 LeaveCriticalSection(&apt->cs);
1027 /* another thread may have become the main apartment in the time it took
1028 * us to create the thread for the host apartment */
1029 if (!apartment_hwnd && !multi_threaded && main_apartment)
1031 APARTMENT *host_apt = apartment_findmain();
1032 if (host_apt)
1034 apartment_hwnd = apartment_getwindow(host_apt);
1035 apartment_release(host_apt);
1039 params.hkeydll = hkeydll;
1040 params.clsid = *rclsid;
1041 params.iid = *riid;
1042 hr = CreateStreamOnHGlobal(NULL, TRUE, &params.stream);
1043 if (FAILED(hr))
1044 return hr;
1045 params.apartment_threaded = !multi_threaded;
1046 if (multi_threaded)
1048 params.hr = S_OK;
1049 params.event = CreateEventW(NULL, FALSE, FALSE, NULL);
1050 if (!PostThreadMessageW(apartment_tid, DM_HOSTOBJECT, 0, (LPARAM)&params))
1051 hr = E_OUTOFMEMORY;
1052 else
1054 WaitForSingleObject(params.event, INFINITE);
1055 hr = params.hr;
1057 CloseHandle(params.event);
1059 else
1061 if (!apartment_hwnd)
1063 ERR("host apartment didn't create window\n");
1064 hr = E_OUTOFMEMORY;
1066 else
1067 hr = SendMessageW(apartment_hwnd, DM_HOSTOBJECT, 0, (LPARAM)&params);
1069 if (SUCCEEDED(hr))
1070 hr = CoUnmarshalInterface(params.stream, riid, ppv);
1071 IStream_Release(params.stream);
1072 return hr;
1075 /* create a window for the apartment or return the current one if one has
1076 * already been created */
1077 HRESULT apartment_createwindowifneeded(struct apartment *apt)
1079 if (apt->multi_threaded)
1080 return S_OK;
1082 if (!apt->win)
1084 HWND hwnd = CreateWindowW(wszAptWinClass, NULL, 0,
1085 0, 0, 0, 0,
1086 HWND_MESSAGE, 0, hProxyDll, NULL);
1087 if (!hwnd)
1089 ERR("CreateWindow failed with error %d\n", GetLastError());
1090 return HRESULT_FROM_WIN32(GetLastError());
1092 if (InterlockedCompareExchangePointer((PVOID *)&apt->win, hwnd, NULL))
1093 /* someone beat us to it */
1094 DestroyWindow(hwnd);
1097 return S_OK;
1100 /* retrieves the window for the main- or apartment-threaded apartment */
1101 HWND apartment_getwindow(const struct apartment *apt)
1103 assert(!apt->multi_threaded);
1104 return apt->win;
1107 void apartment_joinmta(void)
1109 apartment_addref(MTA);
1110 COM_CurrentInfo()->apt = MTA;
1113 static void COMPOBJ_InitProcess( void )
1115 WNDCLASSW wclass;
1117 /* Dispatching to the correct thread in an apartment is done through
1118 * window messages rather than RPC transports. When an interface is
1119 * marshalled into another apartment in the same process, a window of the
1120 * following class is created. The *caller* of CoMarshalInterface (i.e., the
1121 * application) is responsible for pumping the message loop in that thread.
1122 * The WM_USER messages which point to the RPCs are then dispatched to
1123 * apartment_wndproc by the user's code from the apartment in which the
1124 * interface was unmarshalled.
1126 memset(&wclass, 0, sizeof(wclass));
1127 wclass.lpfnWndProc = apartment_wndproc;
1128 wclass.hInstance = hProxyDll;
1129 wclass.lpszClassName = wszAptWinClass;
1130 RegisterClassW(&wclass);
1133 static void COMPOBJ_UninitProcess( void )
1135 UnregisterClassW(wszAptWinClass, hProxyDll);
1138 static void COM_TlsDestroy(void)
1140 struct oletls *info = NtCurrentTeb()->ReservedForOle;
1141 if (info)
1143 if (info->apt) apartment_release(info->apt);
1144 if (info->errorinfo) IErrorInfo_Release(info->errorinfo);
1145 if (info->state) IUnknown_Release(info->state);
1146 if (info->spy) IUnknown_Release(info->spy);
1147 if (info->context_token) IObjContext_Release(info->context_token);
1148 HeapFree(GetProcessHeap(), 0, info);
1149 NtCurrentTeb()->ReservedForOle = NULL;
1153 /******************************************************************************
1154 * CoBuildVersion [OLE32.@]
1156 * Gets the build version of the DLL.
1158 * PARAMS
1160 * RETURNS
1161 * Current build version, hiword is majornumber, loword is minornumber
1163 DWORD WINAPI CoBuildVersion(void)
1165 TRACE("Returning version %d, build %d.\n", rmm, rup);
1166 return (rmm<<16)+rup;
1169 /******************************************************************************
1170 * CoRegisterInitializeSpy [OLE32.@]
1172 * Add a Spy that watches CoInitializeEx calls
1174 * PARAMS
1175 * spy [I] Pointer to IUnknown interface that will be QueryInterface'd.
1176 * cookie [II] cookie receiver
1178 * RETURNS
1179 * Success: S_OK if not already initialized, S_FALSE otherwise.
1180 * Failure: HRESULT code.
1182 * SEE ALSO
1183 * CoInitializeEx
1185 HRESULT WINAPI CoRegisterInitializeSpy(IInitializeSpy *spy, ULARGE_INTEGER *cookie)
1187 struct oletls *info = COM_CurrentInfo();
1188 HRESULT hr;
1190 TRACE("(%p, %p)\n", spy, cookie);
1192 if (!spy || !cookie || !info)
1194 if (!info)
1195 WARN("Could not allocate tls\n");
1196 return E_INVALIDARG;
1199 if (info->spy)
1201 FIXME("Already registered?\n");
1202 return E_UNEXPECTED;
1205 hr = IUnknown_QueryInterface(spy, &IID_IInitializeSpy, (void **) &info->spy);
1206 if (SUCCEEDED(hr))
1208 cookie->QuadPart = (DWORD_PTR)spy;
1209 return S_OK;
1211 return hr;
1214 /******************************************************************************
1215 * CoRevokeInitializeSpy [OLE32.@]
1217 * Remove a spy that previously watched CoInitializeEx calls
1219 * PARAMS
1220 * cookie [I] The cookie obtained from a previous CoRegisterInitializeSpy call
1222 * RETURNS
1223 * Success: S_OK if a spy is removed
1224 * Failure: E_INVALIDARG
1226 * SEE ALSO
1227 * CoInitializeEx
1229 HRESULT WINAPI CoRevokeInitializeSpy(ULARGE_INTEGER cookie)
1231 struct oletls *info = COM_CurrentInfo();
1232 TRACE("(%s)\n", wine_dbgstr_longlong(cookie.QuadPart));
1234 if (!info || !info->spy || cookie.QuadPart != (DWORD_PTR)info->spy)
1235 return E_INVALIDARG;
1237 IUnknown_Release(info->spy);
1238 info->spy = NULL;
1239 return S_OK;
1243 /******************************************************************************
1244 * CoInitialize [OLE32.@]
1246 * Initializes the COM libraries by calling CoInitializeEx with
1247 * COINIT_APARTMENTTHREADED, ie it enters a STA thread.
1249 * PARAMS
1250 * lpReserved [I] Pointer to IMalloc interface (obsolete, should be NULL).
1252 * RETURNS
1253 * Success: S_OK if not already initialized, S_FALSE otherwise.
1254 * Failure: HRESULT code.
1256 * SEE ALSO
1257 * CoInitializeEx
1259 HRESULT WINAPI CoInitialize(LPVOID lpReserved)
1262 * Just delegate to the newer method.
1264 return CoInitializeEx(lpReserved, COINIT_APARTMENTTHREADED);
1267 /******************************************************************************
1268 * CoInitializeEx [OLE32.@]
1270 * Initializes the COM libraries.
1272 * PARAMS
1273 * lpReserved [I] Pointer to IMalloc interface (obsolete, should be NULL).
1274 * dwCoInit [I] One or more flags from the COINIT enumeration. See notes.
1276 * RETURNS
1277 * S_OK if successful,
1278 * S_FALSE if this function was called already.
1279 * RPC_E_CHANGED_MODE if a previous call to CoInitializeEx specified another
1280 * threading model.
1282 * NOTES
1284 * The behavior used to set the IMalloc used for memory management is
1285 * obsolete.
1286 * The dwCoInit parameter must specify one of the following apartment
1287 * threading models:
1288 *| COINIT_APARTMENTTHREADED - A single-threaded apartment (STA).
1289 *| COINIT_MULTITHREADED - A multi-threaded apartment (MTA).
1290 * The parameter may also specify zero or more of the following flags:
1291 *| COINIT_DISABLE_OLE1DDE - Don't use DDE for OLE1 support.
1292 *| COINIT_SPEED_OVER_MEMORY - Trade memory for speed.
1294 * SEE ALSO
1295 * CoUninitialize
1297 HRESULT WINAPI CoInitializeEx(LPVOID lpReserved, DWORD dwCoInit)
1299 struct oletls *info = COM_CurrentInfo();
1300 HRESULT hr = S_OK;
1301 APARTMENT *apt;
1303 TRACE("(%p, %x)\n", lpReserved, (int)dwCoInit);
1305 if (lpReserved!=NULL)
1307 ERR("(%p, %x) - Bad parameter passed-in %p, must be an old Windows Application\n", lpReserved, (int)dwCoInit, lpReserved);
1311 * Check the lock count. If this is the first time going through the initialize
1312 * process, we have to initialize the libraries.
1314 * And crank-up that lock count.
1316 if (InterlockedExchangeAdd(&s_COMLockCount,1)==0)
1319 * Initialize the various COM libraries and data structures.
1321 TRACE("() - Initializing the COM libraries\n");
1323 /* we may need to defer this until after apartment initialisation */
1324 RunningObjectTableImpl_Initialize();
1327 if (info->spy)
1328 IInitializeSpy_PreInitialize(info->spy, dwCoInit, info->inits);
1330 if (!(apt = info->apt))
1332 apt = apartment_get_or_create(dwCoInit);
1333 if (!apt) return E_OUTOFMEMORY;
1335 else if (!apartment_is_model(apt, dwCoInit))
1337 /* Changing the threading model after it's been set is illegal. If this warning is triggered by Wine
1338 code then we are probably using the wrong threading model to implement that API. */
1339 ERR("Attempt to change threading model of this apartment from %s to %s\n",
1340 apt->multi_threaded ? "multi-threaded" : "apartment threaded",
1341 dwCoInit & COINIT_APARTMENTTHREADED ? "apartment threaded" : "multi-threaded");
1342 return RPC_E_CHANGED_MODE;
1344 else
1345 hr = S_FALSE;
1347 info->inits++;
1349 if (info->spy)
1350 IInitializeSpy_PostInitialize(info->spy, hr, dwCoInit, info->inits);
1352 return hr;
1355 /***********************************************************************
1356 * CoUninitialize [OLE32.@]
1358 * This method will decrement the refcount on the current apartment, freeing
1359 * the resources associated with it if it is the last thread in the apartment.
1360 * If the last apartment is freed, the function will additionally release
1361 * any COM resources associated with the process.
1363 * PARAMS
1365 * RETURNS
1366 * Nothing.
1368 * SEE ALSO
1369 * CoInitializeEx
1371 void WINAPI CoUninitialize(void)
1373 struct oletls * info = COM_CurrentInfo();
1374 LONG lCOMRefCnt;
1376 TRACE("()\n");
1378 /* will only happen on OOM */
1379 if (!info) return;
1381 if (info->spy)
1382 IInitializeSpy_PreUninitialize(info->spy, info->inits);
1384 /* sanity check */
1385 if (!info->inits)
1387 ERR("Mismatched CoUninitialize\n");
1389 if (info->spy)
1390 IInitializeSpy_PostUninitialize(info->spy, info->inits);
1391 return;
1394 if (!--info->inits)
1396 apartment_release(info->apt);
1397 info->apt = NULL;
1401 * Decrease the reference count.
1402 * If we are back to 0 locks on the COM library, make sure we free
1403 * all the associated data structures.
1405 lCOMRefCnt = InterlockedExchangeAdd(&s_COMLockCount,-1);
1406 if (lCOMRefCnt==1)
1408 TRACE("() - Releasing the COM libraries\n");
1410 RunningObjectTableImpl_UnInitialize();
1412 else if (lCOMRefCnt<1) {
1413 ERR( "CoUninitialize() - not CoInitialized.\n" );
1414 InterlockedExchangeAdd(&s_COMLockCount,1); /* restore the lock count. */
1416 if (info->spy)
1417 IInitializeSpy_PostUninitialize(info->spy, info->inits);
1420 /******************************************************************************
1421 * CoDisconnectObject [OLE32.@]
1423 * Disconnects all connections to this object from remote processes. Dispatches
1424 * pending RPCs while blocking new RPCs from occurring, and then calls
1425 * IMarshal::DisconnectObject on the given object.
1427 * Typically called when the object server is forced to shut down, for instance by
1428 * the user.
1430 * PARAMS
1431 * lpUnk [I] The object whose stub should be disconnected.
1432 * reserved [I] Reserved. Should be set to 0.
1434 * RETURNS
1435 * Success: S_OK.
1436 * Failure: HRESULT code.
1438 * SEE ALSO
1439 * CoMarshalInterface, CoReleaseMarshalData, CoLockObjectExternal
1441 HRESULT WINAPI CoDisconnectObject( LPUNKNOWN lpUnk, DWORD reserved )
1443 HRESULT hr;
1444 IMarshal *marshal;
1445 APARTMENT *apt;
1447 TRACE("(%p, 0x%08x)\n", lpUnk, reserved);
1449 hr = IUnknown_QueryInterface(lpUnk, &IID_IMarshal, (void **)&marshal);
1450 if (hr == S_OK)
1452 hr = IMarshal_DisconnectObject(marshal, reserved);
1453 IMarshal_Release(marshal);
1454 return hr;
1457 apt = COM_CurrentApt();
1458 if (!apt)
1459 return CO_E_NOTINITIALIZED;
1461 apartment_disconnectobject(apt, lpUnk);
1463 /* Note: native is pretty broken here because it just silently
1464 * fails, without returning an appropriate error code if the object was
1465 * not found, making apps think that the object was disconnected, when
1466 * it actually wasn't */
1468 return S_OK;
1471 /******************************************************************************
1472 * CoCreateGuid [OLE32.@]
1474 * Simply forwards to UuidCreate in RPCRT4.
1476 * PARAMS
1477 * pguid [O] Points to the GUID to initialize.
1479 * RETURNS
1480 * Success: S_OK.
1481 * Failure: HRESULT code.
1483 * SEE ALSO
1484 * UuidCreate
1486 HRESULT WINAPI CoCreateGuid(GUID *pguid)
1488 DWORD status = UuidCreate(pguid);
1489 if (status == RPC_S_OK || status == RPC_S_UUID_LOCAL_ONLY) return S_OK;
1490 return HRESULT_FROM_WIN32( status );
1493 /******************************************************************************
1494 * CLSIDFromString [OLE32.@]
1495 * IIDFromString [OLE32.@]
1497 * Converts a unique identifier from its string representation into
1498 * the GUID struct.
1500 * PARAMS
1501 * idstr [I] The string representation of the GUID.
1502 * id [O] GUID converted from the string.
1504 * RETURNS
1505 * S_OK on success
1506 * CO_E_CLASSSTRING if idstr is not a valid CLSID
1508 * SEE ALSO
1509 * StringFromCLSID
1511 static HRESULT __CLSIDFromString(LPCWSTR s, LPCLSID id)
1513 int i;
1514 BYTE table[256];
1516 if (!s) {
1517 memset( id, 0, sizeof (CLSID) );
1518 return S_OK;
1521 /* validate the CLSID string */
1522 if (strlenW(s) != 38)
1523 return CO_E_CLASSSTRING;
1525 if ((s[0]!='{') || (s[9]!='-') || (s[14]!='-') || (s[19]!='-') || (s[24]!='-') || (s[37]!='}'))
1526 return CO_E_CLASSSTRING;
1528 for (i=1; i<37; i++) {
1529 if ((i == 9)||(i == 14)||(i == 19)||(i == 24)) continue;
1530 if (!(((s[i] >= '0') && (s[i] <= '9')) ||
1531 ((s[i] >= 'a') && (s[i] <= 'f')) ||
1532 ((s[i] >= 'A') && (s[i] <= 'F'))))
1533 return CO_E_CLASSSTRING;
1536 TRACE("%s -> %p\n", debugstr_w(s), id);
1538 /* quick lookup table */
1539 memset(table, 0, 256);
1541 for (i = 0; i < 10; i++) {
1542 table['0' + i] = i;
1544 for (i = 0; i < 6; i++) {
1545 table['A' + i] = i+10;
1546 table['a' + i] = i+10;
1549 /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
1551 id->Data1 = (table[s[1]] << 28 | table[s[2]] << 24 | table[s[3]] << 20 | table[s[4]] << 16 |
1552 table[s[5]] << 12 | table[s[6]] << 8 | table[s[7]] << 4 | table[s[8]]);
1553 id->Data2 = table[s[10]] << 12 | table[s[11]] << 8 | table[s[12]] << 4 | table[s[13]];
1554 id->Data3 = table[s[15]] << 12 | table[s[16]] << 8 | table[s[17]] << 4 | table[s[18]];
1556 /* these are just sequential bytes */
1557 id->Data4[0] = table[s[20]] << 4 | table[s[21]];
1558 id->Data4[1] = table[s[22]] << 4 | table[s[23]];
1559 id->Data4[2] = table[s[25]] << 4 | table[s[26]];
1560 id->Data4[3] = table[s[27]] << 4 | table[s[28]];
1561 id->Data4[4] = table[s[29]] << 4 | table[s[30]];
1562 id->Data4[5] = table[s[31]] << 4 | table[s[32]];
1563 id->Data4[6] = table[s[33]] << 4 | table[s[34]];
1564 id->Data4[7] = table[s[35]] << 4 | table[s[36]];
1566 return S_OK;
1569 /*****************************************************************************/
1571 HRESULT WINAPI CLSIDFromString(LPCOLESTR idstr, LPCLSID id )
1573 HRESULT ret;
1575 if (!id)
1576 return E_INVALIDARG;
1578 ret = __CLSIDFromString(idstr, id);
1579 if(ret != S_OK) { /* It appears a ProgID is also valid */
1580 ret = CLSIDFromProgID(idstr, id);
1582 return ret;
1586 /******************************************************************************
1587 * StringFromCLSID [OLE32.@]
1588 * StringFromIID [OLE32.@]
1590 * Converts a GUID into the respective string representation.
1591 * The target string is allocated using the OLE IMalloc.
1593 * PARAMS
1594 * id [I] the GUID to be converted.
1595 * idstr [O] A pointer to a to-be-allocated pointer pointing to the resulting string.
1597 * RETURNS
1598 * S_OK
1599 * E_FAIL
1601 * SEE ALSO
1602 * StringFromGUID2, CLSIDFromString
1604 HRESULT WINAPI StringFromCLSID(REFCLSID id, LPOLESTR *idstr)
1606 HRESULT ret;
1607 LPMALLOC mllc;
1609 if ((ret = CoGetMalloc(0,&mllc))) return ret;
1610 if (!(*idstr = IMalloc_Alloc( mllc, CHARS_IN_GUID * sizeof(WCHAR) ))) return E_OUTOFMEMORY;
1611 StringFromGUID2( id, *idstr, CHARS_IN_GUID );
1612 return S_OK;
1615 /******************************************************************************
1616 * StringFromGUID2 [OLE32.@]
1618 * Modified version of StringFromCLSID that allows you to specify max
1619 * buffer size.
1621 * PARAMS
1622 * id [I] GUID to convert to string.
1623 * str [O] Buffer where the result will be stored.
1624 * cmax [I] Size of the buffer in characters.
1626 * RETURNS
1627 * Success: The length of the resulting string in characters.
1628 * Failure: 0.
1630 INT WINAPI StringFromGUID2(REFGUID id, LPOLESTR str, INT cmax)
1632 static const WCHAR formatW[] = { '{','%','0','8','X','-','%','0','4','X','-',
1633 '%','0','4','X','-','%','0','2','X','%','0','2','X','-',
1634 '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
1635 '%','0','2','X','%','0','2','X','}',0 };
1636 if (!id || cmax < CHARS_IN_GUID) return 0;
1637 sprintfW( str, formatW, id->Data1, id->Data2, id->Data3,
1638 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
1639 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
1640 return CHARS_IN_GUID;
1643 /* open HKCR\\CLSID\\{string form of clsid}\\{keyname} key */
1644 HRESULT COM_OpenKeyForCLSID(REFCLSID clsid, LPCWSTR keyname, REGSAM access, HKEY *subkey)
1646 static const WCHAR wszCLSIDSlash[] = {'C','L','S','I','D','\\',0};
1647 WCHAR path[CHARS_IN_GUID + ARRAYSIZE(wszCLSIDSlash) - 1];
1648 LONG res;
1649 HKEY key;
1651 strcpyW(path, wszCLSIDSlash);
1652 StringFromGUID2(clsid, path + strlenW(wszCLSIDSlash), CHARS_IN_GUID);
1653 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, path, 0, keyname ? KEY_READ : access, &key);
1654 if (res == ERROR_FILE_NOT_FOUND)
1655 return REGDB_E_CLASSNOTREG;
1656 else if (res != ERROR_SUCCESS)
1657 return REGDB_E_READREGDB;
1659 if (!keyname)
1661 *subkey = key;
1662 return S_OK;
1665 res = RegOpenKeyExW(key, keyname, 0, access, subkey);
1666 RegCloseKey(key);
1667 if (res == ERROR_FILE_NOT_FOUND)
1668 return REGDB_E_KEYMISSING;
1669 else if (res != ERROR_SUCCESS)
1670 return REGDB_E_READREGDB;
1672 return S_OK;
1675 /* open HKCR\\AppId\\{string form of appid clsid} key */
1676 HRESULT COM_OpenKeyForAppIdFromCLSID(REFCLSID clsid, REGSAM access, HKEY *subkey)
1678 static const WCHAR szAppId[] = { 'A','p','p','I','d',0 };
1679 static const WCHAR szAppIdKey[] = { 'A','p','p','I','d','\\',0 };
1680 DWORD res;
1681 WCHAR buf[CHARS_IN_GUID];
1682 WCHAR keyname[ARRAYSIZE(szAppIdKey) + CHARS_IN_GUID];
1683 DWORD size;
1684 HKEY hkey;
1685 DWORD type;
1686 HRESULT hr;
1688 /* read the AppID value under the class's key */
1689 hr = COM_OpenKeyForCLSID(clsid, NULL, KEY_READ, &hkey);
1690 if (FAILED(hr))
1691 return hr;
1693 size = sizeof(buf);
1694 res = RegQueryValueExW(hkey, szAppId, NULL, &type, (LPBYTE)buf, &size);
1695 RegCloseKey(hkey);
1696 if (res == ERROR_FILE_NOT_FOUND)
1697 return REGDB_E_KEYMISSING;
1698 else if (res != ERROR_SUCCESS || type!=REG_SZ)
1699 return REGDB_E_READREGDB;
1701 strcpyW(keyname, szAppIdKey);
1702 strcatW(keyname, buf);
1703 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, access, subkey);
1704 if (res == ERROR_FILE_NOT_FOUND)
1705 return REGDB_E_KEYMISSING;
1706 else if (res != ERROR_SUCCESS)
1707 return REGDB_E_READREGDB;
1709 return S_OK;
1712 /******************************************************************************
1713 * ProgIDFromCLSID [OLE32.@]
1715 * Converts a class id into the respective program ID.
1717 * PARAMS
1718 * clsid [I] Class ID, as found in registry.
1719 * ppszProgID [O] Associated ProgID.
1721 * RETURNS
1722 * S_OK
1723 * E_OUTOFMEMORY
1724 * REGDB_E_CLASSNOTREG if the given clsid has no associated ProgID
1726 HRESULT WINAPI ProgIDFromCLSID(REFCLSID clsid, LPOLESTR *ppszProgID)
1728 static const WCHAR wszProgID[] = {'P','r','o','g','I','D',0};
1729 HKEY hkey;
1730 HRESULT ret;
1731 LONG progidlen = 0;
1733 if (!ppszProgID)
1735 ERR("ppszProgId isn't optional\n");
1736 return E_INVALIDARG;
1739 *ppszProgID = NULL;
1740 ret = COM_OpenKeyForCLSID(clsid, wszProgID, KEY_READ, &hkey);
1741 if (FAILED(ret))
1742 return ret;
1744 if (RegQueryValueW(hkey, NULL, NULL, &progidlen))
1745 ret = REGDB_E_CLASSNOTREG;
1747 if (ret == S_OK)
1749 *ppszProgID = CoTaskMemAlloc(progidlen * sizeof(WCHAR));
1750 if (*ppszProgID)
1752 if (RegQueryValueW(hkey, NULL, *ppszProgID, &progidlen))
1753 ret = REGDB_E_CLASSNOTREG;
1755 else
1756 ret = E_OUTOFMEMORY;
1759 RegCloseKey(hkey);
1760 return ret;
1763 /******************************************************************************
1764 * CLSIDFromProgID [OLE32.@]
1766 * Converts a program id into the respective GUID.
1768 * PARAMS
1769 * progid [I] Unicode program ID, as found in registry.
1770 * clsid [O] Associated CLSID.
1772 * RETURNS
1773 * Success: S_OK
1774 * Failure: CO_E_CLASSSTRING - the given ProgID cannot be found.
1776 HRESULT WINAPI CLSIDFromProgID(LPCOLESTR progid, LPCLSID clsid)
1778 static const WCHAR clsidW[] = { '\\','C','L','S','I','D',0 };
1779 WCHAR buf2[CHARS_IN_GUID];
1780 LONG buf2len = sizeof(buf2);
1781 HKEY xhkey;
1782 WCHAR *buf;
1784 if (!progid || !clsid)
1786 ERR("neither progid (%p) nor clsid (%p) are optional\n", progid, clsid);
1787 return E_INVALIDARG;
1790 /* initialise clsid in case of failure */
1791 memset(clsid, 0, sizeof(*clsid));
1793 buf = HeapAlloc( GetProcessHeap(),0,(strlenW(progid)+8) * sizeof(WCHAR) );
1794 strcpyW( buf, progid );
1795 strcatW( buf, clsidW );
1796 if (RegOpenKeyW(HKEY_CLASSES_ROOT,buf,&xhkey))
1798 HeapFree(GetProcessHeap(),0,buf);
1799 WARN("couldn't open key for ProgID %s\n", debugstr_w(progid));
1800 return CO_E_CLASSSTRING;
1802 HeapFree(GetProcessHeap(),0,buf);
1804 if (RegQueryValueW(xhkey,NULL,buf2,&buf2len))
1806 RegCloseKey(xhkey);
1807 WARN("couldn't query clsid value for ProgID %s\n", debugstr_w(progid));
1808 return CO_E_CLASSSTRING;
1810 RegCloseKey(xhkey);
1811 return __CLSIDFromString(buf2,clsid);
1815 /*****************************************************************************
1816 * CoGetPSClsid [OLE32.@]
1818 * Retrieves the CLSID of the proxy/stub factory that implements
1819 * IPSFactoryBuffer for the specified interface.
1821 * PARAMS
1822 * riid [I] Interface whose proxy/stub CLSID is to be returned.
1823 * pclsid [O] Where to store returned proxy/stub CLSID.
1825 * RETURNS
1826 * S_OK
1827 * E_OUTOFMEMORY
1828 * REGDB_E_IIDNOTREG if no PSFactoryBuffer is associated with the IID, or it could not be parsed
1830 * NOTES
1832 * The standard marshaller activates the object with the CLSID
1833 * returned and uses the CreateProxy and CreateStub methods on its
1834 * IPSFactoryBuffer interface to construct the proxies and stubs for a
1835 * given object.
1837 * CoGetPSClsid determines this CLSID by searching the
1838 * HKEY_CLASSES_ROOT\Interface\{string form of riid}\ProxyStubClsid32
1839 * in the registry and any interface id registered by
1840 * CoRegisterPSClsid within the current process.
1842 * BUGS
1844 * Native returns S_OK for interfaces with a key in HKCR\Interface, but
1845 * without a ProxyStubClsid32 key and leaves garbage in pclsid. This should be
1846 * considered a bug in native unless an application depends on this (unlikely).
1848 * SEE ALSO
1849 * CoRegisterPSClsid.
1851 HRESULT WINAPI CoGetPSClsid(REFIID riid, CLSID *pclsid)
1853 static const WCHAR wszInterface[] = {'I','n','t','e','r','f','a','c','e','\\',0};
1854 static const WCHAR wszPSC[] = {'\\','P','r','o','x','y','S','t','u','b','C','l','s','i','d','3','2',0};
1855 WCHAR path[ARRAYSIZE(wszInterface) - 1 + CHARS_IN_GUID - 1 + ARRAYSIZE(wszPSC)];
1856 WCHAR value[CHARS_IN_GUID];
1857 LONG len;
1858 HKEY hkey;
1859 APARTMENT *apt = COM_CurrentApt();
1860 struct registered_psclsid *registered_psclsid;
1862 TRACE("() riid=%s, pclsid=%p\n", debugstr_guid(riid), pclsid);
1864 if (!apt)
1866 ERR("apartment not initialised\n");
1867 return CO_E_NOTINITIALIZED;
1870 if (!pclsid)
1872 ERR("pclsid isn't optional\n");
1873 return E_INVALIDARG;
1876 EnterCriticalSection(&apt->cs);
1878 LIST_FOR_EACH_ENTRY(registered_psclsid, &apt->psclsids, struct registered_psclsid, entry)
1879 if (IsEqualIID(&registered_psclsid->iid, riid))
1881 *pclsid = registered_psclsid->clsid;
1882 LeaveCriticalSection(&apt->cs);
1883 return S_OK;
1886 LeaveCriticalSection(&apt->cs);
1888 /* Interface\\{string form of riid}\\ProxyStubClsid32 */
1889 strcpyW(path, wszInterface);
1890 StringFromGUID2(riid, path + ARRAYSIZE(wszInterface) - 1, CHARS_IN_GUID);
1891 strcpyW(path + ARRAYSIZE(wszInterface) - 1 + CHARS_IN_GUID - 1, wszPSC);
1893 /* Open the key.. */
1894 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, path, 0, KEY_READ, &hkey))
1896 WARN("No PSFactoryBuffer object is registered for IID %s\n", debugstr_guid(riid));
1897 return REGDB_E_IIDNOTREG;
1900 /* ... Once we have the key, query the registry to get the
1901 value of CLSID as a string, and convert it into a
1902 proper CLSID structure to be passed back to the app */
1903 len = sizeof(value);
1904 if (ERROR_SUCCESS != RegQueryValueW(hkey, NULL, value, &len))
1906 RegCloseKey(hkey);
1907 return REGDB_E_IIDNOTREG;
1909 RegCloseKey(hkey);
1911 /* We have the CLSID we want back from the registry as a string, so
1912 let's convert it into a CLSID structure */
1913 if (CLSIDFromString(value, pclsid) != NOERROR)
1914 return REGDB_E_IIDNOTREG;
1916 TRACE ("() Returning CLSID=%s\n", debugstr_guid(pclsid));
1917 return S_OK;
1920 /*****************************************************************************
1921 * CoRegisterPSClsid [OLE32.@]
1923 * Register a proxy/stub CLSID for the given interface in the current process
1924 * only.
1926 * PARAMS
1927 * riid [I] Interface whose proxy/stub CLSID is to be registered.
1928 * rclsid [I] CLSID of the proxy/stub.
1930 * RETURNS
1931 * Success: S_OK
1932 * Failure: E_OUTOFMEMORY
1934 * NOTES
1936 * This function does not add anything to the registry and the effects are
1937 * limited to the lifetime of the current process.
1939 * SEE ALSO
1940 * CoGetPSClsid.
1942 HRESULT WINAPI CoRegisterPSClsid(REFIID riid, REFCLSID rclsid)
1944 APARTMENT *apt = COM_CurrentApt();
1945 struct registered_psclsid *registered_psclsid;
1947 TRACE("(%s, %s)\n", debugstr_guid(riid), debugstr_guid(rclsid));
1949 if (!apt)
1951 ERR("apartment not initialised\n");
1952 return CO_E_NOTINITIALIZED;
1955 EnterCriticalSection(&apt->cs);
1957 LIST_FOR_EACH_ENTRY(registered_psclsid, &apt->psclsids, struct registered_psclsid, entry)
1958 if (IsEqualIID(&registered_psclsid->iid, riid))
1960 registered_psclsid->clsid = *rclsid;
1961 LeaveCriticalSection(&apt->cs);
1962 return S_OK;
1965 registered_psclsid = HeapAlloc(GetProcessHeap(), 0, sizeof(struct registered_psclsid));
1966 if (!registered_psclsid)
1968 LeaveCriticalSection(&apt->cs);
1969 return E_OUTOFMEMORY;
1972 registered_psclsid->iid = *riid;
1973 registered_psclsid->clsid = *rclsid;
1974 list_add_head(&apt->psclsids, &registered_psclsid->entry);
1976 LeaveCriticalSection(&apt->cs);
1978 return S_OK;
1982 /***
1983 * COM_GetRegisteredClassObject
1985 * This internal method is used to scan the registered class list to
1986 * find a class object.
1988 * Params:
1989 * rclsid Class ID of the class to find.
1990 * dwClsContext Class context to match.
1991 * ppv [out] returns a pointer to the class object. Complying
1992 * to normal COM usage, this method will increase the
1993 * reference count on this object.
1995 static HRESULT COM_GetRegisteredClassObject(const struct apartment *apt, REFCLSID rclsid,
1996 DWORD dwClsContext, LPUNKNOWN* ppUnk)
1998 HRESULT hr = S_FALSE;
1999 RegisteredClass *curClass;
2001 EnterCriticalSection( &csRegisteredClassList );
2003 LIST_FOR_EACH_ENTRY(curClass, &RegisteredClassList, RegisteredClass, entry)
2006 * Check if we have a match on the class ID and context.
2008 if ((apt->oxid == curClass->apartment_id) &&
2009 (dwClsContext & curClass->runContext) &&
2010 IsEqualGUID(&(curClass->classIdentifier), rclsid))
2013 * We have a match, return the pointer to the class object.
2015 *ppUnk = curClass->classObject;
2017 IUnknown_AddRef(curClass->classObject);
2019 hr = S_OK;
2020 break;
2024 LeaveCriticalSection( &csRegisteredClassList );
2026 return hr;
2029 /******************************************************************************
2030 * CoRegisterClassObject [OLE32.@]
2032 * Registers the class object for a given class ID. Servers housed in EXE
2033 * files use this method instead of exporting DllGetClassObject to allow
2034 * other code to connect to their objects.
2036 * PARAMS
2037 * rclsid [I] CLSID of the object to register.
2038 * pUnk [I] IUnknown of the object.
2039 * dwClsContext [I] CLSCTX flags indicating the context in which to run the executable.
2040 * flags [I] REGCLS flags indicating how connections are made.
2041 * lpdwRegister [I] A unique cookie that can be passed to CoRevokeClassObject.
2043 * RETURNS
2044 * S_OK on success,
2045 * E_INVALIDARG if lpdwRegister or pUnk are NULL,
2046 * CO_E_OBJISREG if the object is already registered. We should not return this.
2048 * SEE ALSO
2049 * CoRevokeClassObject, CoGetClassObject
2051 * NOTES
2052 * In-process objects are only registered for the current apartment.
2053 * CoGetClassObject() and CoCreateInstance() will not return objects registered
2054 * in other apartments.
2056 * BUGS
2057 * MSDN claims that multiple interface registrations are legal, but we
2058 * can't do that with our current implementation.
2060 HRESULT WINAPI CoRegisterClassObject(
2061 REFCLSID rclsid,
2062 LPUNKNOWN pUnk,
2063 DWORD dwClsContext,
2064 DWORD flags,
2065 LPDWORD lpdwRegister)
2067 static LONG next_cookie;
2068 RegisteredClass* newClass;
2069 LPUNKNOWN foundObject;
2070 HRESULT hr;
2071 APARTMENT *apt;
2073 TRACE("(%s,%p,0x%08x,0x%08x,%p)\n",
2074 debugstr_guid(rclsid),pUnk,dwClsContext,flags,lpdwRegister);
2076 if ( (lpdwRegister==0) || (pUnk==0) )
2077 return E_INVALIDARG;
2079 apt = COM_CurrentApt();
2080 if (!apt)
2082 ERR("COM was not initialized\n");
2083 return CO_E_NOTINITIALIZED;
2086 *lpdwRegister = 0;
2088 /* REGCLS_MULTIPLEUSE implies registering as inproc server. This is what
2089 * differentiates the flag from REGCLS_MULTI_SEPARATE. */
2090 if (flags & REGCLS_MULTIPLEUSE)
2091 dwClsContext |= CLSCTX_INPROC_SERVER;
2094 * First, check if the class is already registered.
2095 * If it is, this should cause an error.
2097 hr = COM_GetRegisteredClassObject(apt, rclsid, dwClsContext, &foundObject);
2098 if (hr == S_OK) {
2099 if (flags & REGCLS_MULTIPLEUSE) {
2100 if (dwClsContext & CLSCTX_LOCAL_SERVER)
2101 hr = CoLockObjectExternal(foundObject, TRUE, FALSE);
2102 IUnknown_Release(foundObject);
2103 return hr;
2105 IUnknown_Release(foundObject);
2106 ERR("object already registered for class %s\n", debugstr_guid(rclsid));
2107 return CO_E_OBJISREG;
2110 newClass = HeapAlloc(GetProcessHeap(), 0, sizeof(RegisteredClass));
2111 if ( newClass == NULL )
2112 return E_OUTOFMEMORY;
2114 newClass->classIdentifier = *rclsid;
2115 newClass->apartment_id = apt->oxid;
2116 newClass->runContext = dwClsContext;
2117 newClass->connectFlags = flags;
2118 newClass->pMarshaledData = NULL;
2119 newClass->RpcRegistration = NULL;
2121 if (!(newClass->dwCookie = InterlockedIncrement( &next_cookie )))
2122 newClass->dwCookie = InterlockedIncrement( &next_cookie );
2125 * Since we're making a copy of the object pointer, we have to increase its
2126 * reference count.
2128 newClass->classObject = pUnk;
2129 IUnknown_AddRef(newClass->classObject);
2131 EnterCriticalSection( &csRegisteredClassList );
2132 list_add_tail(&RegisteredClassList, &newClass->entry);
2133 LeaveCriticalSection( &csRegisteredClassList );
2135 *lpdwRegister = newClass->dwCookie;
2137 if (dwClsContext & CLSCTX_LOCAL_SERVER) {
2138 hr = CreateStreamOnHGlobal(0, TRUE, &newClass->pMarshaledData);
2139 if (hr) {
2140 FIXME("Failed to create stream on hglobal, %x\n", hr);
2141 return hr;
2143 hr = CoMarshalInterface(newClass->pMarshaledData, &IID_IUnknown,
2144 newClass->classObject, MSHCTX_LOCAL, NULL,
2145 MSHLFLAGS_TABLESTRONG);
2146 if (hr) {
2147 FIXME("CoMarshalInterface failed, %x!\n",hr);
2148 return hr;
2151 hr = RPC_StartLocalServer(&newClass->classIdentifier,
2152 newClass->pMarshaledData,
2153 flags & (REGCLS_MULTIPLEUSE|REGCLS_MULTI_SEPARATE),
2154 &newClass->RpcRegistration);
2156 return S_OK;
2159 static void get_threading_model(HKEY key, LPWSTR value, DWORD len)
2161 static const WCHAR wszThreadingModel[] = {'T','h','r','e','a','d','i','n','g','M','o','d','e','l',0};
2162 DWORD keytype;
2163 DWORD ret;
2164 DWORD dwLength = len * sizeof(WCHAR);
2166 ret = RegQueryValueExW(key, wszThreadingModel, NULL, &keytype, (LPBYTE)value, &dwLength);
2167 if ((ret != ERROR_SUCCESS) || (keytype != REG_SZ))
2168 value[0] = '\0';
2171 static HRESULT get_inproc_class_object(APARTMENT *apt, HKEY hkeydll,
2172 REFCLSID rclsid, REFIID riid,
2173 BOOL hostifnecessary, void **ppv)
2175 WCHAR dllpath[MAX_PATH+1];
2176 BOOL apartment_threaded;
2178 if (hostifnecessary)
2180 static const WCHAR wszApartment[] = {'A','p','a','r','t','m','e','n','t',0};
2181 static const WCHAR wszFree[] = {'F','r','e','e',0};
2182 static const WCHAR wszBoth[] = {'B','o','t','h',0};
2183 WCHAR threading_model[10 /* strlenW(L"apartment")+1 */];
2185 get_threading_model(hkeydll, threading_model, ARRAYSIZE(threading_model));
2186 /* "Apartment" */
2187 if (!strcmpiW(threading_model, wszApartment))
2189 apartment_threaded = TRUE;
2190 if (apt->multi_threaded)
2191 return apartment_hostobject_in_hostapt(apt, FALSE, FALSE, hkeydll, rclsid, riid, ppv);
2193 /* "Free" */
2194 else if (!strcmpiW(threading_model, wszFree))
2196 apartment_threaded = FALSE;
2197 if (!apt->multi_threaded)
2198 return apartment_hostobject_in_hostapt(apt, TRUE, FALSE, hkeydll, rclsid, riid, ppv);
2200 /* everything except "Apartment", "Free" and "Both" */
2201 else if (strcmpiW(threading_model, wszBoth))
2203 apartment_threaded = TRUE;
2204 /* everything else is main-threaded */
2205 if (threading_model[0])
2206 FIXME("unrecognised threading model %s for object %s, should be main-threaded?\n",
2207 debugstr_w(threading_model), debugstr_guid(rclsid));
2209 if (apt->multi_threaded || !apt->main)
2210 return apartment_hostobject_in_hostapt(apt, FALSE, TRUE, hkeydll, rclsid, riid, ppv);
2212 else
2213 apartment_threaded = FALSE;
2215 else
2216 apartment_threaded = !apt->multi_threaded;
2218 if (COM_RegReadPath(hkeydll, NULL, NULL, dllpath, ARRAYSIZE(dllpath)) != ERROR_SUCCESS)
2220 /* failure: CLSID is not found in registry */
2221 WARN("class %s not registered inproc\n", debugstr_guid(rclsid));
2222 return REGDB_E_CLASSNOTREG;
2225 return apartment_getclassobject(apt, dllpath, apartment_threaded,
2226 rclsid, riid, ppv);
2229 /***********************************************************************
2230 * CoGetClassObject [OLE32.@]
2232 * Creates an object of the specified class.
2234 * PARAMS
2235 * rclsid [I] Class ID to create an instance of.
2236 * dwClsContext [I] Flags to restrict the location of the created instance.
2237 * pServerInfo [I] Optional. Details for connecting to a remote server.
2238 * iid [I] The ID of the interface of the instance to return.
2239 * ppv [O] On returns, contains a pointer to the specified interface of the object.
2241 * RETURNS
2242 * Success: S_OK
2243 * Failure: HRESULT code.
2245 * NOTES
2246 * The dwClsContext parameter can be one or more of the following:
2247 *| CLSCTX_INPROC_SERVER - Use an in-process server, such as from a DLL.
2248 *| CLSCTX_INPROC_HANDLER - Use an in-process object which handles certain functions for an object running in another process.
2249 *| CLSCTX_LOCAL_SERVER - Connect to an object running in another process.
2250 *| CLSCTX_REMOTE_SERVER - Connect to an object running on another machine.
2252 * SEE ALSO
2253 * CoCreateInstance()
2255 HRESULT WINAPI CoGetClassObject(
2256 REFCLSID rclsid, DWORD dwClsContext, COSERVERINFO *pServerInfo,
2257 REFIID iid, LPVOID *ppv)
2259 LPUNKNOWN regClassObject;
2260 HRESULT hres = E_UNEXPECTED;
2261 APARTMENT *apt;
2262 BOOL release_apt = FALSE;
2264 TRACE("CLSID: %s,IID: %s\n", debugstr_guid(rclsid), debugstr_guid(iid));
2266 if (!ppv)
2267 return E_INVALIDARG;
2269 *ppv = NULL;
2271 if (!(apt = COM_CurrentApt()))
2273 if (!(apt = apartment_find_multi_threaded()))
2275 ERR("apartment not initialised\n");
2276 return CO_E_NOTINITIALIZED;
2278 release_apt = TRUE;
2281 if (pServerInfo) {
2282 FIXME("pServerInfo->name=%s pAuthInfo=%p\n",
2283 debugstr_w(pServerInfo->pwszName), pServerInfo->pAuthInfo);
2287 * First, try and see if we can't match the class ID with one of the
2288 * registered classes.
2290 if (S_OK == COM_GetRegisteredClassObject(apt, rclsid, dwClsContext,
2291 &regClassObject))
2293 /* Get the required interface from the retrieved pointer. */
2294 hres = IUnknown_QueryInterface(regClassObject, iid, ppv);
2297 * Since QI got another reference on the pointer, we want to release the
2298 * one we already have. If QI was unsuccessful, this will release the object. This
2299 * is good since we are not returning it in the "out" parameter.
2301 IUnknown_Release(regClassObject);
2302 if (release_apt) apartment_release(apt);
2303 return hres;
2306 /* First try in-process server */
2307 if (CLSCTX_INPROC_SERVER & dwClsContext)
2309 static const WCHAR wszInprocServer32[] = {'I','n','p','r','o','c','S','e','r','v','e','r','3','2',0};
2310 HKEY hkey;
2312 if (IsEqualCLSID(rclsid, &CLSID_InProcFreeMarshaler))
2314 if (release_apt) apartment_release(apt);
2315 return FTMarshalCF_Create(iid, ppv);
2318 hres = COM_OpenKeyForCLSID(rclsid, wszInprocServer32, KEY_READ, &hkey);
2319 if (FAILED(hres))
2321 if (hres == REGDB_E_CLASSNOTREG)
2322 ERR("class %s not registered\n", debugstr_guid(rclsid));
2323 else if (hres == REGDB_E_KEYMISSING)
2325 WARN("class %s not registered as in-proc server\n", debugstr_guid(rclsid));
2326 hres = REGDB_E_CLASSNOTREG;
2330 if (SUCCEEDED(hres))
2332 hres = get_inproc_class_object(apt, hkey, rclsid, iid,
2333 !(dwClsContext & WINE_CLSCTX_DONT_HOST), ppv);
2334 RegCloseKey(hkey);
2337 /* return if we got a class, otherwise fall through to one of the
2338 * other types */
2339 if (SUCCEEDED(hres))
2341 if (release_apt) apartment_release(apt);
2342 return hres;
2346 /* Next try in-process handler */
2347 if (CLSCTX_INPROC_HANDLER & dwClsContext)
2349 static const WCHAR wszInprocHandler32[] = {'I','n','p','r','o','c','H','a','n','d','l','e','r','3','2',0};
2350 HKEY hkey;
2352 hres = COM_OpenKeyForCLSID(rclsid, wszInprocHandler32, KEY_READ, &hkey);
2353 if (FAILED(hres))
2355 if (hres == REGDB_E_CLASSNOTREG)
2356 ERR("class %s not registered\n", debugstr_guid(rclsid));
2357 else if (hres == REGDB_E_KEYMISSING)
2359 WARN("class %s not registered in-proc handler\n", debugstr_guid(rclsid));
2360 hres = REGDB_E_CLASSNOTREG;
2364 if (SUCCEEDED(hres))
2366 hres = get_inproc_class_object(apt, hkey, rclsid, iid,
2367 !(dwClsContext & WINE_CLSCTX_DONT_HOST), ppv);
2368 RegCloseKey(hkey);
2371 /* return if we got a class, otherwise fall through to one of the
2372 * other types */
2373 if (SUCCEEDED(hres))
2375 if (release_apt) apartment_release(apt);
2376 return hres;
2379 if (release_apt) apartment_release(apt);
2381 /* Next try out of process */
2382 if (CLSCTX_LOCAL_SERVER & dwClsContext)
2384 hres = RPC_GetLocalClassObject(rclsid,iid,ppv);
2385 if (SUCCEEDED(hres))
2386 return hres;
2389 /* Finally try remote: this requires networked DCOM (a lot of work) */
2390 if (CLSCTX_REMOTE_SERVER & dwClsContext)
2392 FIXME ("CLSCTX_REMOTE_SERVER not supported\n");
2393 hres = REGDB_E_CLASSNOTREG;
2396 if (FAILED(hres))
2397 ERR("no class object %s could be created for context 0x%x\n",
2398 debugstr_guid(rclsid), dwClsContext);
2399 return hres;
2402 /***********************************************************************
2403 * CoResumeClassObjects (OLE32.@)
2405 * Resumes all class objects registered with REGCLS_SUSPENDED.
2407 * RETURNS
2408 * Success: S_OK.
2409 * Failure: HRESULT code.
2411 HRESULT WINAPI CoResumeClassObjects(void)
2413 FIXME("stub\n");
2414 return S_OK;
2417 /***********************************************************************
2418 * CoCreateInstance [OLE32.@]
2420 * Creates an instance of the specified class.
2422 * PARAMS
2423 * rclsid [I] Class ID to create an instance of.
2424 * pUnkOuter [I] Optional outer unknown to allow aggregation with another object.
2425 * dwClsContext [I] Flags to restrict the location of the created instance.
2426 * iid [I] The ID of the interface of the instance to return.
2427 * ppv [O] On returns, contains a pointer to the specified interface of the instance.
2429 * RETURNS
2430 * Success: S_OK
2431 * Failure: HRESULT code.
2433 * NOTES
2434 * The dwClsContext parameter can be one or more of the following:
2435 *| CLSCTX_INPROC_SERVER - Use an in-process server, such as from a DLL.
2436 *| CLSCTX_INPROC_HANDLER - Use an in-process object which handles certain functions for an object running in another process.
2437 *| CLSCTX_LOCAL_SERVER - Connect to an object running in another process.
2438 *| CLSCTX_REMOTE_SERVER - Connect to an object running on another machine.
2440 * Aggregation is the concept of deferring the IUnknown of an object to another
2441 * object. This allows a separate object to behave as though it was part of
2442 * the object and to allow this the pUnkOuter parameter can be set. Note that
2443 * not all objects support having an outer of unknown.
2445 * SEE ALSO
2446 * CoGetClassObject()
2448 HRESULT WINAPI CoCreateInstance(
2449 REFCLSID rclsid,
2450 LPUNKNOWN pUnkOuter,
2451 DWORD dwClsContext,
2452 REFIID iid,
2453 LPVOID *ppv)
2455 HRESULT hres;
2456 LPCLASSFACTORY lpclf = 0;
2457 APARTMENT *apt;
2459 TRACE("(rclsid=%s, pUnkOuter=%p, dwClsContext=%08x, riid=%s, ppv=%p)\n", debugstr_guid(rclsid),
2460 pUnkOuter, dwClsContext, debugstr_guid(iid), ppv);
2463 * Sanity check
2465 if (ppv==0)
2466 return E_POINTER;
2469 * Initialize the "out" parameter
2471 *ppv = 0;
2473 if (!(apt = COM_CurrentApt()))
2475 if (!(apt = apartment_find_multi_threaded()))
2477 ERR("apartment not initialised\n");
2478 return CO_E_NOTINITIALIZED;
2480 apartment_release(apt);
2484 * The Standard Global Interface Table (GIT) object is a process-wide singleton.
2485 * Rather than create a class factory, we can just check for it here
2487 if (IsEqualIID(rclsid, &CLSID_StdGlobalInterfaceTable)) {
2488 if (StdGlobalInterfaceTableInstance == NULL)
2489 StdGlobalInterfaceTableInstance = StdGlobalInterfaceTable_Construct();
2490 hres = IGlobalInterfaceTable_QueryInterface( (IGlobalInterfaceTable*) StdGlobalInterfaceTableInstance, iid, ppv);
2491 if (hres) return hres;
2493 TRACE("Retrieved GIT (%p)\n", *ppv);
2494 return S_OK;
2498 * Get a class factory to construct the object we want.
2500 hres = CoGetClassObject(rclsid,
2501 dwClsContext,
2502 NULL,
2503 &IID_IClassFactory,
2504 (LPVOID)&lpclf);
2506 if (FAILED(hres))
2507 return hres;
2510 * Create the object and don't forget to release the factory
2512 hres = IClassFactory_CreateInstance(lpclf, pUnkOuter, iid, ppv);
2513 IClassFactory_Release(lpclf);
2514 if(FAILED(hres))
2516 if (hres == CLASS_E_NOAGGREGATION && pUnkOuter)
2517 FIXME("Class %s does not support aggregation\n", debugstr_guid(rclsid));
2518 else
2519 FIXME("no instance created for interface %s of class %s, hres is 0x%08x\n", debugstr_guid(iid), debugstr_guid(rclsid),hres);
2522 return hres;
2525 /***********************************************************************
2526 * CoCreateInstanceEx [OLE32.@]
2528 HRESULT WINAPI CoCreateInstanceEx(
2529 REFCLSID rclsid,
2530 LPUNKNOWN pUnkOuter,
2531 DWORD dwClsContext,
2532 COSERVERINFO* pServerInfo,
2533 ULONG cmq,
2534 MULTI_QI* pResults)
2536 IUnknown* pUnk = NULL;
2537 HRESULT hr;
2538 ULONG index;
2539 ULONG successCount = 0;
2542 * Sanity check
2544 if ( (cmq==0) || (pResults==NULL))
2545 return E_INVALIDARG;
2547 if (pServerInfo!=NULL)
2548 FIXME("() non-NULL pServerInfo not supported!\n");
2551 * Initialize all the "out" parameters.
2553 for (index = 0; index < cmq; index++)
2555 pResults[index].pItf = NULL;
2556 pResults[index].hr = E_NOINTERFACE;
2560 * Get the object and get its IUnknown pointer.
2562 hr = CoCreateInstance(rclsid,
2563 pUnkOuter,
2564 dwClsContext,
2565 &IID_IUnknown,
2566 (VOID**)&pUnk);
2568 if (hr)
2569 return hr;
2572 * Then, query for all the interfaces requested.
2574 for (index = 0; index < cmq; index++)
2576 pResults[index].hr = IUnknown_QueryInterface(pUnk,
2577 pResults[index].pIID,
2578 (VOID**)&(pResults[index].pItf));
2580 if (pResults[index].hr == S_OK)
2581 successCount++;
2585 * Release our temporary unknown pointer.
2587 IUnknown_Release(pUnk);
2589 if (successCount == 0)
2590 return E_NOINTERFACE;
2592 if (successCount!=cmq)
2593 return CO_S_NOTALLINTERFACES;
2595 return S_OK;
2598 /***********************************************************************
2599 * CoLoadLibrary (OLE32.@)
2601 * Loads a library.
2603 * PARAMS
2604 * lpszLibName [I] Path to library.
2605 * bAutoFree [I] Whether the library should automatically be freed.
2607 * RETURNS
2608 * Success: Handle to loaded library.
2609 * Failure: NULL.
2611 * SEE ALSO
2612 * CoFreeLibrary, CoFreeAllLibraries, CoFreeUnusedLibraries
2614 HINSTANCE WINAPI CoLoadLibrary(LPOLESTR lpszLibName, BOOL bAutoFree)
2616 TRACE("(%s, %d)\n", debugstr_w(lpszLibName), bAutoFree);
2618 return LoadLibraryExW(lpszLibName, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
2621 /***********************************************************************
2622 * CoFreeLibrary [OLE32.@]
2624 * Unloads a library from memory.
2626 * PARAMS
2627 * hLibrary [I] Handle to library to unload.
2629 * RETURNS
2630 * Nothing
2632 * SEE ALSO
2633 * CoLoadLibrary, CoFreeAllLibraries, CoFreeUnusedLibraries
2635 void WINAPI CoFreeLibrary(HINSTANCE hLibrary)
2637 FreeLibrary(hLibrary);
2641 /***********************************************************************
2642 * CoFreeAllLibraries [OLE32.@]
2644 * Function for backwards compatibility only. Does nothing.
2646 * RETURNS
2647 * Nothing.
2649 * SEE ALSO
2650 * CoLoadLibrary, CoFreeLibrary, CoFreeUnusedLibraries
2652 void WINAPI CoFreeAllLibraries(void)
2654 /* NOP */
2657 /***********************************************************************
2658 * CoFreeUnusedLibrariesEx [OLE32.@]
2660 * Frees any previously unused libraries whose delay has expired and marks
2661 * currently unused libraries for unloading. Unused are identified as those that
2662 * return S_OK from their DllCanUnloadNow function.
2664 * PARAMS
2665 * dwUnloadDelay [I] Unload delay in milliseconds.
2666 * dwReserved [I] Reserved. Set to 0.
2668 * RETURNS
2669 * Nothing.
2671 * SEE ALSO
2672 * CoLoadLibrary, CoFreeAllLibraries, CoFreeLibrary
2674 void WINAPI CoFreeUnusedLibrariesEx(DWORD dwUnloadDelay, DWORD dwReserved)
2676 struct apartment *apt = COM_CurrentApt();
2677 if (!apt)
2679 ERR("apartment not initialised\n");
2680 return;
2683 apartment_freeunusedlibraries(apt, dwUnloadDelay);
2686 /***********************************************************************
2687 * CoFreeUnusedLibraries [OLE32.@]
2689 * Frees any unused libraries. Unused are identified as those that return
2690 * S_OK from their DllCanUnloadNow function.
2692 * RETURNS
2693 * Nothing.
2695 * SEE ALSO
2696 * CoLoadLibrary, CoFreeAllLibraries, CoFreeLibrary
2698 void WINAPI CoFreeUnusedLibraries(void)
2700 CoFreeUnusedLibrariesEx(INFINITE, 0);
2703 /***********************************************************************
2704 * CoFileTimeNow [OLE32.@]
2706 * Retrieves the current time in FILETIME format.
2708 * PARAMS
2709 * lpFileTime [O] The current time.
2711 * RETURNS
2712 * S_OK.
2714 HRESULT WINAPI CoFileTimeNow( FILETIME *lpFileTime )
2716 GetSystemTimeAsFileTime( lpFileTime );
2717 return S_OK;
2720 /******************************************************************************
2721 * CoLockObjectExternal [OLE32.@]
2723 * Increments or decrements the external reference count of a stub object.
2725 * PARAMS
2726 * pUnk [I] Stub object.
2727 * fLock [I] If TRUE then increments the external ref-count,
2728 * otherwise decrements.
2729 * fLastUnlockReleases [I] If TRUE then the last unlock has the effect of
2730 * calling CoDisconnectObject.
2732 * RETURNS
2733 * Success: S_OK.
2734 * Failure: HRESULT code.
2736 * NOTES
2737 * If fLock is TRUE and an object is passed in that doesn't have a stub
2738 * manager then a new stub manager is created for the object.
2740 HRESULT WINAPI CoLockObjectExternal(
2741 LPUNKNOWN pUnk,
2742 BOOL fLock,
2743 BOOL fLastUnlockReleases)
2745 struct stub_manager *stubmgr;
2746 struct apartment *apt;
2748 TRACE("pUnk=%p, fLock=%s, fLastUnlockReleases=%s\n",
2749 pUnk, fLock ? "TRUE" : "FALSE", fLastUnlockReleases ? "TRUE" : "FALSE");
2751 apt = COM_CurrentApt();
2752 if (!apt) return CO_E_NOTINITIALIZED;
2754 stubmgr = get_stub_manager_from_object(apt, pUnk);
2756 if (stubmgr)
2758 if (fLock)
2759 stub_manager_ext_addref(stubmgr, 1, FALSE);
2760 else
2761 stub_manager_ext_release(stubmgr, 1, FALSE, fLastUnlockReleases);
2763 stub_manager_int_release(stubmgr);
2765 return S_OK;
2767 else if (fLock)
2769 stubmgr = new_stub_manager(apt, pUnk);
2771 if (stubmgr)
2773 stub_manager_ext_addref(stubmgr, 1, FALSE);
2774 stub_manager_int_release(stubmgr);
2777 return S_OK;
2779 else
2781 WARN("stub object not found %p\n", pUnk);
2782 /* Note: native is pretty broken here because it just silently
2783 * fails, without returning an appropriate error code, making apps
2784 * think that the object was disconnected, when it actually wasn't */
2785 return S_OK;
2789 /***********************************************************************
2790 * CoInitializeWOW (OLE32.@)
2792 * WOW equivalent of CoInitialize?
2794 * PARAMS
2795 * x [I] Unknown.
2796 * y [I] Unknown.
2798 * RETURNS
2799 * Unknown.
2801 HRESULT WINAPI CoInitializeWOW(DWORD x,DWORD y)
2803 FIXME("(0x%08x,0x%08x),stub!\n",x,y);
2804 return 0;
2807 /***********************************************************************
2808 * CoGetState [OLE32.@]
2810 * Retrieves the thread state object previously stored by CoSetState().
2812 * PARAMS
2813 * ppv [I] Address where pointer to object will be stored.
2815 * RETURNS
2816 * Success: S_OK.
2817 * Failure: E_OUTOFMEMORY.
2819 * NOTES
2820 * Crashes on all invalid ppv addresses, including NULL.
2821 * If the function returns a non-NULL object then the caller must release its
2822 * reference on the object when the object is no longer required.
2824 * SEE ALSO
2825 * CoSetState().
2827 HRESULT WINAPI CoGetState(IUnknown ** ppv)
2829 struct oletls *info = COM_CurrentInfo();
2830 if (!info) return E_OUTOFMEMORY;
2832 *ppv = NULL;
2834 if (info->state)
2836 IUnknown_AddRef(info->state);
2837 *ppv = info->state;
2838 TRACE("apt->state=%p\n", info->state);
2841 return S_OK;
2844 /***********************************************************************
2845 * CoSetState [OLE32.@]
2847 * Sets the thread state object.
2849 * PARAMS
2850 * pv [I] Pointer to state object to be stored.
2852 * NOTES
2853 * The system keeps a reference on the object while the object stored.
2855 * RETURNS
2856 * Success: S_OK.
2857 * Failure: E_OUTOFMEMORY.
2859 HRESULT WINAPI CoSetState(IUnknown * pv)
2861 struct oletls *info = COM_CurrentInfo();
2862 if (!info) return E_OUTOFMEMORY;
2864 if (pv) IUnknown_AddRef(pv);
2866 if (info->state)
2868 TRACE("-- release %p now\n", info->state);
2869 IUnknown_Release(info->state);
2872 info->state = pv;
2874 return S_OK;
2878 /******************************************************************************
2879 * CoTreatAsClass [OLE32.@]
2881 * Sets the TreatAs value of a class.
2883 * PARAMS
2884 * clsidOld [I] Class to set TreatAs value on.
2885 * clsidNew [I] The class the clsidOld should be treated as.
2887 * RETURNS
2888 * Success: S_OK.
2889 * Failure: HRESULT code.
2891 * SEE ALSO
2892 * CoGetTreatAsClass
2894 HRESULT WINAPI CoTreatAsClass(REFCLSID clsidOld, REFCLSID clsidNew)
2896 static const WCHAR wszAutoTreatAs[] = {'A','u','t','o','T','r','e','a','t','A','s',0};
2897 static const WCHAR wszTreatAs[] = {'T','r','e','a','t','A','s',0};
2898 HKEY hkey = NULL;
2899 WCHAR szClsidNew[CHARS_IN_GUID];
2900 HRESULT res = S_OK;
2901 WCHAR auto_treat_as[CHARS_IN_GUID];
2902 LONG auto_treat_as_size = sizeof(auto_treat_as);
2903 CLSID id;
2905 res = COM_OpenKeyForCLSID(clsidOld, NULL, KEY_READ | KEY_WRITE, &hkey);
2906 if (FAILED(res))
2907 goto done;
2908 if (!memcmp( clsidOld, clsidNew, sizeof(*clsidOld) ))
2910 if (!RegQueryValueW(hkey, wszAutoTreatAs, auto_treat_as, &auto_treat_as_size) &&
2911 CLSIDFromString(auto_treat_as, &id) == S_OK)
2913 if (RegSetValueW(hkey, wszTreatAs, REG_SZ, auto_treat_as, sizeof(auto_treat_as)))
2915 res = REGDB_E_WRITEREGDB;
2916 goto done;
2919 else
2921 RegDeleteKeyW(hkey, wszTreatAs);
2922 goto done;
2925 else if (!StringFromGUID2(clsidNew, szClsidNew, ARRAYSIZE(szClsidNew)) &&
2926 !RegSetValueW(hkey, wszTreatAs, REG_SZ, szClsidNew, sizeof(szClsidNew)))
2928 res = REGDB_E_WRITEREGDB;
2929 goto done;
2932 done:
2933 if (hkey) RegCloseKey(hkey);
2934 return res;
2937 /******************************************************************************
2938 * CoGetTreatAsClass [OLE32.@]
2940 * Gets the TreatAs value of a class.
2942 * PARAMS
2943 * clsidOld [I] Class to get the TreatAs value of.
2944 * clsidNew [I] The class the clsidOld should be treated as.
2946 * RETURNS
2947 * Success: S_OK.
2948 * Failure: HRESULT code.
2950 * SEE ALSO
2951 * CoSetTreatAsClass
2953 HRESULT WINAPI CoGetTreatAsClass(REFCLSID clsidOld, LPCLSID clsidNew)
2955 static const WCHAR wszTreatAs[] = {'T','r','e','a','t','A','s',0};
2956 HKEY hkey = NULL;
2957 WCHAR szClsidNew[CHARS_IN_GUID];
2958 HRESULT res = S_OK;
2959 LONG len = sizeof(szClsidNew);
2961 TRACE("(%s,%p)\n", debugstr_guid(clsidOld), clsidNew);
2962 *clsidNew = *clsidOld; /* copy over old value */
2964 res = COM_OpenKeyForCLSID(clsidOld, wszTreatAs, KEY_READ, &hkey);
2965 if (FAILED(res))
2967 res = S_FALSE;
2968 goto done;
2970 if (RegQueryValueW(hkey, NULL, szClsidNew, &len))
2972 res = S_FALSE;
2973 goto done;
2975 res = CLSIDFromString(szClsidNew,clsidNew);
2976 if (FAILED(res))
2977 ERR("Failed CLSIDFromStringA(%s), hres 0x%08x\n", debugstr_w(szClsidNew), res);
2978 done:
2979 if (hkey) RegCloseKey(hkey);
2980 return res;
2983 /******************************************************************************
2984 * CoGetCurrentProcess [OLE32.@]
2986 * Gets the current process ID.
2988 * RETURNS
2989 * The current process ID.
2991 * NOTES
2992 * Is DWORD really the correct return type for this function?
2994 DWORD WINAPI CoGetCurrentProcess(void)
2996 return GetCurrentProcessId();
2999 /******************************************************************************
3000 * CoRegisterMessageFilter [OLE32.@]
3002 * Registers a message filter.
3004 * PARAMS
3005 * lpMessageFilter [I] Pointer to interface.
3006 * lplpMessageFilter [O] Indirect pointer to prior instance if non-NULL.
3008 * RETURNS
3009 * Success: S_OK.
3010 * Failure: HRESULT code.
3012 * NOTES
3013 * Both lpMessageFilter and lplpMessageFilter are optional. Passing in a NULL
3014 * lpMessageFilter removes the message filter.
3016 * If lplpMessageFilter is not NULL the previous message filter will be
3017 * returned in the memory pointer to this parameter and the caller is
3018 * responsible for releasing the object.
3020 * The current thread be in an apartment otherwise the function will crash.
3022 HRESULT WINAPI CoRegisterMessageFilter(
3023 LPMESSAGEFILTER lpMessageFilter,
3024 LPMESSAGEFILTER *lplpMessageFilter)
3026 struct apartment *apt;
3027 IMessageFilter *lpOldMessageFilter;
3029 TRACE("(%p, %p)\n", lpMessageFilter, lplpMessageFilter);
3031 apt = COM_CurrentApt();
3033 /* can't set a message filter in a multi-threaded apartment */
3034 if (!apt || apt->multi_threaded)
3036 WARN("can't set message filter in MTA or uninitialized apt\n");
3037 return CO_E_NOT_SUPPORTED;
3040 if (lpMessageFilter)
3041 IMessageFilter_AddRef(lpMessageFilter);
3043 EnterCriticalSection(&apt->cs);
3045 lpOldMessageFilter = apt->filter;
3046 apt->filter = lpMessageFilter;
3048 LeaveCriticalSection(&apt->cs);
3050 if (lplpMessageFilter)
3051 *lplpMessageFilter = lpOldMessageFilter;
3052 else if (lpOldMessageFilter)
3053 IMessageFilter_Release(lpOldMessageFilter);
3055 return S_OK;
3058 /***********************************************************************
3059 * CoIsOle1Class [OLE32.@]
3061 * Determines whether the specified class an OLE v1 class.
3063 * PARAMS
3064 * clsid [I] Class to test.
3066 * RETURNS
3067 * TRUE if the class is an OLE v1 class, or FALSE otherwise.
3069 BOOL WINAPI CoIsOle1Class(REFCLSID clsid)
3071 FIXME("%s\n", debugstr_guid(clsid));
3072 return FALSE;
3075 /***********************************************************************
3076 * IsEqualGUID [OLE32.@]
3078 * Compares two Unique Identifiers.
3080 * PARAMS
3081 * rguid1 [I] The first GUID to compare.
3082 * rguid2 [I] The other GUID to compare.
3084 * RETURNS
3085 * TRUE if equal
3087 #undef IsEqualGUID
3088 BOOL WINAPI IsEqualGUID(
3089 REFGUID rguid1,
3090 REFGUID rguid2)
3092 return !memcmp(rguid1,rguid2,sizeof(GUID));
3095 /***********************************************************************
3096 * CoInitializeSecurity [OLE32.@]
3098 HRESULT WINAPI CoInitializeSecurity(PSECURITY_DESCRIPTOR pSecDesc, LONG cAuthSvc,
3099 SOLE_AUTHENTICATION_SERVICE* asAuthSvc,
3100 void* pReserved1, DWORD dwAuthnLevel,
3101 DWORD dwImpLevel, void* pReserved2,
3102 DWORD dwCapabilities, void* pReserved3)
3104 FIXME("(%p,%d,%p,%p,%d,%d,%p,%d,%p) - stub!\n", pSecDesc, cAuthSvc,
3105 asAuthSvc, pReserved1, dwAuthnLevel, dwImpLevel, pReserved2,
3106 dwCapabilities, pReserved3);
3107 return S_OK;
3110 /***********************************************************************
3111 * CoSuspendClassObjects [OLE32.@]
3113 * Suspends all registered class objects to prevent further requests coming in
3114 * for those objects.
3116 * RETURNS
3117 * Success: S_OK.
3118 * Failure: HRESULT code.
3120 HRESULT WINAPI CoSuspendClassObjects(void)
3122 FIXME("\n");
3123 return S_OK;
3126 /***********************************************************************
3127 * CoAddRefServerProcess [OLE32.@]
3129 * Helper function for incrementing the reference count of a local-server
3130 * process.
3132 * RETURNS
3133 * New reference count.
3135 * SEE ALSO
3136 * CoReleaseServerProcess().
3138 ULONG WINAPI CoAddRefServerProcess(void)
3140 ULONG refs;
3142 TRACE("\n");
3144 EnterCriticalSection(&csRegisteredClassList);
3145 refs = ++s_COMServerProcessReferences;
3146 LeaveCriticalSection(&csRegisteredClassList);
3148 TRACE("refs before: %d\n", refs - 1);
3150 return refs;
3153 /***********************************************************************
3154 * CoReleaseServerProcess [OLE32.@]
3156 * Helper function for decrementing the reference count of a local-server
3157 * process.
3159 * RETURNS
3160 * New reference count.
3162 * NOTES
3163 * When reference count reaches 0, this function suspends all registered
3164 * classes so no new connections are accepted.
3166 * SEE ALSO
3167 * CoAddRefServerProcess(), CoSuspendClassObjects().
3169 ULONG WINAPI CoReleaseServerProcess(void)
3171 ULONG refs;
3173 TRACE("\n");
3175 EnterCriticalSection(&csRegisteredClassList);
3177 refs = --s_COMServerProcessReferences;
3178 /* FIXME: if (!refs) COM_SuspendClassObjects(); */
3180 LeaveCriticalSection(&csRegisteredClassList);
3182 TRACE("refs after: %d\n", refs);
3184 return refs;
3187 /***********************************************************************
3188 * CoIsHandlerConnected [OLE32.@]
3190 * Determines whether a proxy is connected to a remote stub.
3192 * PARAMS
3193 * pUnk [I] Pointer to object that may or may not be connected.
3195 * RETURNS
3196 * TRUE if pUnk is not a proxy or if pUnk is connected to a remote stub, or
3197 * FALSE otherwise.
3199 BOOL WINAPI CoIsHandlerConnected(IUnknown *pUnk)
3201 FIXME("%p\n", pUnk);
3203 return TRUE;
3206 /***********************************************************************
3207 * CoAllowSetForegroundWindow [OLE32.@]
3210 HRESULT WINAPI CoAllowSetForegroundWindow(IUnknown *pUnk, void *pvReserved)
3212 FIXME("(%p, %p): stub\n", pUnk, pvReserved);
3213 return S_OK;
3216 /***********************************************************************
3217 * CoQueryProxyBlanket [OLE32.@]
3219 * Retrieves the security settings being used by a proxy.
3221 * PARAMS
3222 * pProxy [I] Pointer to the proxy object.
3223 * pAuthnSvc [O] The type of authentication service.
3224 * pAuthzSvc [O] The type of authorization service.
3225 * ppServerPrincName [O] Optional. The server prinicple name.
3226 * pAuthnLevel [O] The authentication level.
3227 * pImpLevel [O] The impersonation level.
3228 * ppAuthInfo [O] Information specific to the authorization/authentication service.
3229 * pCapabilities [O] Flags affecting the security behaviour.
3231 * RETURNS
3232 * Success: S_OK.
3233 * Failure: HRESULT code.
3235 * SEE ALSO
3236 * CoCopyProxy, CoSetProxyBlanket.
3238 HRESULT WINAPI CoQueryProxyBlanket(IUnknown *pProxy, DWORD *pAuthnSvc,
3239 DWORD *pAuthzSvc, OLECHAR **ppServerPrincName, DWORD *pAuthnLevel,
3240 DWORD *pImpLevel, void **ppAuthInfo, DWORD *pCapabilities)
3242 IClientSecurity *pCliSec;
3243 HRESULT hr;
3245 TRACE("%p\n", pProxy);
3247 hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (void **)&pCliSec);
3248 if (SUCCEEDED(hr))
3250 hr = IClientSecurity_QueryBlanket(pCliSec, pProxy, pAuthnSvc,
3251 pAuthzSvc, ppServerPrincName,
3252 pAuthnLevel, pImpLevel, ppAuthInfo,
3253 pCapabilities);
3254 IClientSecurity_Release(pCliSec);
3257 if (FAILED(hr)) ERR("-- failed with 0x%08x\n", hr);
3258 return hr;
3261 /***********************************************************************
3262 * CoSetProxyBlanket [OLE32.@]
3264 * Sets the security settings for a proxy.
3266 * PARAMS
3267 * pProxy [I] Pointer to the proxy object.
3268 * AuthnSvc [I] The type of authentication service.
3269 * AuthzSvc [I] The type of authorization service.
3270 * pServerPrincName [I] The server prinicple name.
3271 * AuthnLevel [I] The authentication level.
3272 * ImpLevel [I] The impersonation level.
3273 * pAuthInfo [I] Information specific to the authorization/authentication service.
3274 * Capabilities [I] Flags affecting the security behaviour.
3276 * RETURNS
3277 * Success: S_OK.
3278 * Failure: HRESULT code.
3280 * SEE ALSO
3281 * CoQueryProxyBlanket, CoCopyProxy.
3283 HRESULT WINAPI CoSetProxyBlanket(IUnknown *pProxy, DWORD AuthnSvc,
3284 DWORD AuthzSvc, OLECHAR *pServerPrincName, DWORD AuthnLevel,
3285 DWORD ImpLevel, void *pAuthInfo, DWORD Capabilities)
3287 IClientSecurity *pCliSec;
3288 HRESULT hr;
3290 TRACE("%p\n", pProxy);
3292 hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (void **)&pCliSec);
3293 if (SUCCEEDED(hr))
3295 hr = IClientSecurity_SetBlanket(pCliSec, pProxy, AuthnSvc,
3296 AuthzSvc, pServerPrincName,
3297 AuthnLevel, ImpLevel, pAuthInfo,
3298 Capabilities);
3299 IClientSecurity_Release(pCliSec);
3302 if (FAILED(hr)) ERR("-- failed with 0x%08x\n", hr);
3303 return hr;
3306 /***********************************************************************
3307 * CoCopyProxy [OLE32.@]
3309 * Copies a proxy.
3311 * PARAMS
3312 * pProxy [I] Pointer to the proxy object.
3313 * ppCopy [O] Copy of the proxy.
3315 * RETURNS
3316 * Success: S_OK.
3317 * Failure: HRESULT code.
3319 * SEE ALSO
3320 * CoQueryProxyBlanket, CoSetProxyBlanket.
3322 HRESULT WINAPI CoCopyProxy(IUnknown *pProxy, IUnknown **ppCopy)
3324 IClientSecurity *pCliSec;
3325 HRESULT hr;
3327 TRACE("%p\n", pProxy);
3329 hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (void **)&pCliSec);
3330 if (SUCCEEDED(hr))
3332 hr = IClientSecurity_CopyProxy(pCliSec, pProxy, ppCopy);
3333 IClientSecurity_Release(pCliSec);
3336 if (FAILED(hr)) ERR("-- failed with 0x%08x\n", hr);
3337 return hr;
3341 /***********************************************************************
3342 * CoGetCallContext [OLE32.@]
3344 * Gets the context of the currently executing server call in the current
3345 * thread.
3347 * PARAMS
3348 * riid [I] Context interface to return.
3349 * ppv [O] Pointer to memory that will receive the context on return.
3351 * RETURNS
3352 * Success: S_OK.
3353 * Failure: HRESULT code.
3355 HRESULT WINAPI CoGetCallContext(REFIID riid, void **ppv)
3357 struct oletls *info = COM_CurrentInfo();
3359 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
3361 if (!info)
3362 return E_OUTOFMEMORY;
3364 if (!info->call_state)
3365 return RPC_E_CALL_COMPLETE;
3367 return IUnknown_QueryInterface(info->call_state, riid, ppv);
3370 /***********************************************************************
3371 * CoSwitchCallContext [OLE32.@]
3373 * Switches the context of the currently executing server call in the current
3374 * thread.
3376 * PARAMS
3377 * pObject [I] Pointer to new context object
3378 * ppOldObject [O] Pointer to memory that will receive old context object pointer
3380 * RETURNS
3381 * Success: S_OK.
3382 * Failure: HRESULT code.
3384 HRESULT WINAPI CoSwitchCallContext(IUnknown *pObject, IUnknown **ppOldObject)
3386 struct oletls *info = COM_CurrentInfo();
3388 TRACE("(%p, %p)\n", pObject, ppOldObject);
3390 if (!info)
3391 return E_OUTOFMEMORY;
3393 *ppOldObject = info->call_state;
3394 info->call_state = pObject; /* CoSwitchCallContext does not addref nor release objects */
3396 return S_OK;
3399 /***********************************************************************
3400 * CoQueryClientBlanket [OLE32.@]
3402 * Retrieves the authentication information about the client of the currently
3403 * executing server call in the current thread.
3405 * PARAMS
3406 * pAuthnSvc [O] Optional. The type of authentication service.
3407 * pAuthzSvc [O] Optional. The type of authorization service.
3408 * pServerPrincName [O] Optional. The server prinicple name.
3409 * pAuthnLevel [O] Optional. The authentication level.
3410 * pImpLevel [O] Optional. The impersonation level.
3411 * pPrivs [O] Optional. Information about the privileges of the client.
3412 * pCapabilities [IO] Optional. Flags affecting the security behaviour.
3414 * RETURNS
3415 * Success: S_OK.
3416 * Failure: HRESULT code.
3418 * SEE ALSO
3419 * CoImpersonateClient, CoRevertToSelf, CoGetCallContext.
3421 HRESULT WINAPI CoQueryClientBlanket(
3422 DWORD *pAuthnSvc,
3423 DWORD *pAuthzSvc,
3424 OLECHAR **pServerPrincName,
3425 DWORD *pAuthnLevel,
3426 DWORD *pImpLevel,
3427 RPC_AUTHZ_HANDLE *pPrivs,
3428 DWORD *pCapabilities)
3430 IServerSecurity *pSrvSec;
3431 HRESULT hr;
3433 TRACE("(%p, %p, %p, %p, %p, %p, %p)\n",
3434 pAuthnSvc, pAuthzSvc, pServerPrincName, pAuthnLevel, pImpLevel,
3435 pPrivs, pCapabilities);
3437 hr = CoGetCallContext(&IID_IServerSecurity, (void **)&pSrvSec);
3438 if (SUCCEEDED(hr))
3440 hr = IServerSecurity_QueryBlanket(
3441 pSrvSec, pAuthnSvc, pAuthzSvc, pServerPrincName, pAuthnLevel,
3442 pImpLevel, pPrivs, pCapabilities);
3443 IServerSecurity_Release(pSrvSec);
3446 return hr;
3449 /***********************************************************************
3450 * CoImpersonateClient [OLE32.@]
3452 * Impersonates the client of the currently executing server call in the
3453 * current thread.
3455 * PARAMS
3456 * None.
3458 * RETURNS
3459 * Success: S_OK.
3460 * Failure: HRESULT code.
3462 * NOTES
3463 * If this function fails then the current thread will not be impersonating
3464 * the client and all actions will take place on behalf of the server.
3465 * Therefore, it is important to check the return value from this function.
3467 * SEE ALSO
3468 * CoRevertToSelf, CoQueryClientBlanket, CoGetCallContext.
3470 HRESULT WINAPI CoImpersonateClient(void)
3472 IServerSecurity *pSrvSec;
3473 HRESULT hr;
3475 TRACE("\n");
3477 hr = CoGetCallContext(&IID_IServerSecurity, (void **)&pSrvSec);
3478 if (SUCCEEDED(hr))
3480 hr = IServerSecurity_ImpersonateClient(pSrvSec);
3481 IServerSecurity_Release(pSrvSec);
3484 return hr;
3487 /***********************************************************************
3488 * CoRevertToSelf [OLE32.@]
3490 * Ends the impersonation of the client of the currently executing server
3491 * call in the current thread.
3493 * PARAMS
3494 * None.
3496 * RETURNS
3497 * Success: S_OK.
3498 * Failure: HRESULT code.
3500 * SEE ALSO
3501 * CoImpersonateClient, CoQueryClientBlanket, CoGetCallContext.
3503 HRESULT WINAPI CoRevertToSelf(void)
3505 IServerSecurity *pSrvSec;
3506 HRESULT hr;
3508 TRACE("\n");
3510 hr = CoGetCallContext(&IID_IServerSecurity, (void **)&pSrvSec);
3511 if (SUCCEEDED(hr))
3513 hr = IServerSecurity_RevertToSelf(pSrvSec);
3514 IServerSecurity_Release(pSrvSec);
3517 return hr;
3520 static BOOL COM_PeekMessage(struct apartment *apt, MSG *msg)
3522 /* first try to retrieve messages for incoming COM calls to the apartment window */
3523 return PeekMessageW(msg, apt->win, WM_USER, WM_APP - 1, PM_REMOVE|PM_NOYIELD) ||
3524 /* next retrieve other messages necessary for the app to remain responsive */
3525 PeekMessageW(msg, NULL, WM_DDE_FIRST, WM_DDE_LAST, PM_REMOVE|PM_NOYIELD) ||
3526 PeekMessageW(msg, NULL, 0, 0, PM_QS_PAINT|PM_QS_SENDMESSAGE|PM_REMOVE|PM_NOYIELD);
3529 /***********************************************************************
3530 * CoWaitForMultipleHandles [OLE32.@]
3532 * Waits for one or more handles to become signaled.
3534 * PARAMS
3535 * dwFlags [I] Flags. See notes.
3536 * dwTimeout [I] Timeout in milliseconds.
3537 * cHandles [I] Number of handles pointed to by pHandles.
3538 * pHandles [I] Handles to wait for.
3539 * lpdwindex [O] Index of handle that was signaled.
3541 * RETURNS
3542 * Success: S_OK.
3543 * Failure: RPC_S_CALLPENDING on timeout.
3545 * NOTES
3547 * The dwFlags parameter can be zero or more of the following:
3548 *| COWAIT_WAITALL - Wait for all of the handles to become signaled.
3549 *| COWAIT_ALERTABLE - Allows a queued APC to run during the wait.
3551 * SEE ALSO
3552 * MsgWaitForMultipleObjects, WaitForMultipleObjects.
3554 HRESULT WINAPI CoWaitForMultipleHandles(DWORD dwFlags, DWORD dwTimeout,
3555 ULONG cHandles, LPHANDLE pHandles, LPDWORD lpdwindex)
3557 HRESULT hr = S_OK;
3558 DWORD start_time = GetTickCount();
3559 APARTMENT *apt = COM_CurrentApt();
3560 BOOL message_loop = apt && !apt->multi_threaded;
3562 TRACE("(0x%08x, 0x%08x, %d, %p, %p)\n", dwFlags, dwTimeout, cHandles,
3563 pHandles, lpdwindex);
3565 while (TRUE)
3567 DWORD now = GetTickCount();
3568 DWORD res;
3570 if (now - start_time > dwTimeout)
3572 hr = RPC_S_CALLPENDING;
3573 break;
3576 if (message_loop)
3578 DWORD wait_flags = ((dwFlags & COWAIT_WAITALL) ? MWMO_WAITALL : 0) |
3579 ((dwFlags & COWAIT_ALERTABLE ) ? MWMO_ALERTABLE : 0);
3581 TRACE("waiting for rpc completion or window message\n");
3583 res = MsgWaitForMultipleObjectsEx(cHandles, pHandles,
3584 (dwTimeout == INFINITE) ? INFINITE : start_time + dwTimeout - now,
3585 QS_ALLINPUT, wait_flags);
3587 if (res == WAIT_OBJECT_0 + cHandles) /* messages available */
3589 MSG msg;
3591 /* call message filter */
3593 if (COM_CurrentApt()->filter)
3595 PENDINGTYPE pendingtype =
3596 COM_CurrentInfo()->pending_call_count_server ?
3597 PENDINGTYPE_NESTED : PENDINGTYPE_TOPLEVEL;
3598 DWORD be_handled = IMessageFilter_MessagePending(
3599 COM_CurrentApt()->filter, 0 /* FIXME */,
3600 now - start_time, pendingtype);
3601 TRACE("IMessageFilter_MessagePending returned %d\n", be_handled);
3602 switch (be_handled)
3604 case PENDINGMSG_CANCELCALL:
3605 WARN("call canceled\n");
3606 hr = RPC_E_CALL_CANCELED;
3607 break;
3608 case PENDINGMSG_WAITNOPROCESS:
3609 case PENDINGMSG_WAITDEFPROCESS:
3610 default:
3611 /* FIXME: MSDN is very vague about the difference
3612 * between WAITNOPROCESS and WAITDEFPROCESS - there
3613 * appears to be none, so it is possibly a left-over
3614 * from the 16-bit world. */
3615 break;
3619 /* note: using "if" here instead of "while" might seem less
3620 * efficient, but only if we are optimising for quick delivery
3621 * of pending messages, rather than quick completion of the
3622 * COM call */
3623 if (COM_PeekMessage(apt, &msg))
3625 TRACE("received message whilst waiting for RPC: 0x%04x\n", msg.message);
3626 TranslateMessage(&msg);
3627 DispatchMessageW(&msg);
3628 if (msg.message == WM_QUIT)
3630 TRACE("resending WM_QUIT to outer message loop\n");
3631 PostQuitMessage(msg.wParam);
3632 /* no longer need to process messages */
3633 message_loop = FALSE;
3636 continue;
3639 else
3641 TRACE("waiting for rpc completion\n");
3643 res = WaitForMultipleObjectsEx(cHandles, pHandles,
3644 (dwFlags & COWAIT_WAITALL) ? TRUE : FALSE,
3645 (dwTimeout == INFINITE) ? INFINITE : start_time + dwTimeout - now,
3646 (dwFlags & COWAIT_ALERTABLE) ? TRUE : FALSE);
3649 switch (res)
3651 case WAIT_TIMEOUT:
3652 hr = RPC_S_CALLPENDING;
3653 break;
3654 case WAIT_FAILED:
3655 hr = HRESULT_FROM_WIN32( GetLastError() );
3656 break;
3657 default:
3658 *lpdwindex = res;
3659 break;
3661 break;
3663 TRACE("-- 0x%08x\n", hr);
3664 return hr;
3668 /***********************************************************************
3669 * CoGetObject [OLE32.@]
3671 * Gets the object named by converting the name to a moniker and binding to it.
3673 * PARAMS
3674 * pszName [I] String representing the object.
3675 * pBindOptions [I] Parameters affecting the binding to the named object.
3676 * riid [I] Interface to bind to on the objecct.
3677 * ppv [O] On output, the interface riid of the object represented
3678 * by pszName.
3680 * RETURNS
3681 * Success: S_OK.
3682 * Failure: HRESULT code.
3684 * SEE ALSO
3685 * MkParseDisplayName.
3687 HRESULT WINAPI CoGetObject(LPCWSTR pszName, BIND_OPTS *pBindOptions,
3688 REFIID riid, void **ppv)
3690 IBindCtx *pbc;
3691 HRESULT hr;
3693 *ppv = NULL;
3695 hr = CreateBindCtx(0, &pbc);
3696 if (SUCCEEDED(hr))
3698 if (pBindOptions)
3699 hr = IBindCtx_SetBindOptions(pbc, pBindOptions);
3701 if (SUCCEEDED(hr))
3703 ULONG chEaten;
3704 IMoniker *pmk;
3706 hr = MkParseDisplayName(pbc, pszName, &chEaten, &pmk);
3707 if (SUCCEEDED(hr))
3709 hr = IMoniker_BindToObject(pmk, pbc, NULL, riid, ppv);
3710 IMoniker_Release(pmk);
3714 IBindCtx_Release(pbc);
3716 return hr;
3719 /***********************************************************************
3720 * CoRegisterChannelHook [OLE32.@]
3722 * Registers a process-wide hook that is called during ORPC calls.
3724 * PARAMS
3725 * guidExtension [I] GUID of the channel hook to register.
3726 * pChannelHook [I] Channel hook object to register.
3728 * RETURNS
3729 * Success: S_OK.
3730 * Failure: HRESULT code.
3732 HRESULT WINAPI CoRegisterChannelHook(REFGUID guidExtension, IChannelHook *pChannelHook)
3734 TRACE("(%s, %p)\n", debugstr_guid(guidExtension), pChannelHook);
3736 return RPC_RegisterChannelHook(guidExtension, pChannelHook);
3739 typedef struct Context
3741 IComThreadingInfo IComThreadingInfo_iface;
3742 IContextCallback IContextCallback_iface;
3743 IObjContext IObjContext_iface;
3744 LONG refs;
3745 APTTYPE apttype;
3746 } Context;
3748 static inline Context *impl_from_IComThreadingInfo( IComThreadingInfo *iface )
3750 return CONTAINING_RECORD(iface, Context, IComThreadingInfo_iface);
3753 static inline Context *impl_from_IContextCallback( IContextCallback *iface )
3755 return CONTAINING_RECORD(iface, Context, IContextCallback_iface);
3758 static inline Context *impl_from_IObjContext( IObjContext *iface )
3760 return CONTAINING_RECORD(iface, Context, IObjContext_iface);
3763 static HRESULT Context_QueryInterface(Context *iface, REFIID riid, LPVOID *ppv)
3765 *ppv = NULL;
3767 if (IsEqualIID(riid, &IID_IComThreadingInfo) ||
3768 IsEqualIID(riid, &IID_IUnknown))
3770 *ppv = &iface->IComThreadingInfo_iface;
3772 else if (IsEqualIID(riid, &IID_IContextCallback))
3774 *ppv = &iface->IContextCallback_iface;
3776 else if (IsEqualIID(riid, &IID_IObjContext))
3778 *ppv = &iface->IObjContext_iface;
3781 if (*ppv)
3783 IUnknown_AddRef((IUnknown*)*ppv);
3784 return S_OK;
3787 FIXME("interface not implemented %s\n", debugstr_guid(riid));
3788 return E_NOINTERFACE;
3791 static ULONG Context_AddRef(Context *This)
3793 return InterlockedIncrement(&This->refs);
3796 static ULONG Context_Release(Context *This)
3798 ULONG refs = InterlockedDecrement(&This->refs);
3799 if (!refs)
3800 HeapFree(GetProcessHeap(), 0, This);
3801 return refs;
3804 static HRESULT WINAPI Context_CTI_QueryInterface(IComThreadingInfo *iface, REFIID riid, LPVOID *ppv)
3806 Context *This = impl_from_IComThreadingInfo(iface);
3807 return Context_QueryInterface(This, riid, ppv);
3810 static ULONG WINAPI Context_CTI_AddRef(IComThreadingInfo *iface)
3812 Context *This = impl_from_IComThreadingInfo(iface);
3813 return Context_AddRef(This);
3816 static ULONG WINAPI Context_CTI_Release(IComThreadingInfo *iface)
3818 Context *This = impl_from_IComThreadingInfo(iface);
3819 return Context_Release(This);
3822 static HRESULT WINAPI Context_CTI_GetCurrentApartmentType(IComThreadingInfo *iface, APTTYPE *apttype)
3824 Context *This = impl_from_IComThreadingInfo(iface);
3826 TRACE("(%p)\n", apttype);
3828 *apttype = This->apttype;
3829 return S_OK;
3832 static HRESULT WINAPI Context_CTI_GetCurrentThreadType(IComThreadingInfo *iface, THDTYPE *thdtype)
3834 Context *This = impl_from_IComThreadingInfo(iface);
3836 TRACE("(%p)\n", thdtype);
3838 switch (This->apttype)
3840 case APTTYPE_STA:
3841 case APTTYPE_MAINSTA:
3842 *thdtype = THDTYPE_PROCESSMESSAGES;
3843 break;
3844 default:
3845 *thdtype = THDTYPE_BLOCKMESSAGES;
3846 break;
3848 return S_OK;
3851 static HRESULT WINAPI Context_CTI_GetCurrentLogicalThreadId(IComThreadingInfo *iface, GUID *logical_thread_id)
3853 FIXME("(%p): stub\n", logical_thread_id);
3854 return E_NOTIMPL;
3857 static HRESULT WINAPI Context_CTI_SetCurrentLogicalThreadId(IComThreadingInfo *iface, REFGUID logical_thread_id)
3859 FIXME("(%s): stub\n", debugstr_guid(logical_thread_id));
3860 return E_NOTIMPL;
3863 static const IComThreadingInfoVtbl Context_Threading_Vtbl =
3865 Context_CTI_QueryInterface,
3866 Context_CTI_AddRef,
3867 Context_CTI_Release,
3868 Context_CTI_GetCurrentApartmentType,
3869 Context_CTI_GetCurrentThreadType,
3870 Context_CTI_GetCurrentLogicalThreadId,
3871 Context_CTI_SetCurrentLogicalThreadId
3874 static HRESULT WINAPI Context_CC_QueryInterface(IContextCallback *iface, REFIID riid, LPVOID *ppv)
3876 Context *This = impl_from_IContextCallback(iface);
3877 return Context_QueryInterface(This, riid, ppv);
3880 static ULONG WINAPI Context_CC_AddRef(IContextCallback *iface)
3882 Context *This = impl_from_IContextCallback(iface);
3883 return Context_AddRef(This);
3886 static ULONG WINAPI Context_CC_Release(IContextCallback *iface)
3888 Context *This = impl_from_IContextCallback(iface);
3889 return Context_Release(This);
3892 static HRESULT WINAPI Context_CC_ContextCallback(IContextCallback *iface, PFNCONTEXTCALL pCallback,
3893 ComCallData *param, REFIID riid, int method, IUnknown *punk)
3895 Context *This = impl_from_IContextCallback(iface);
3897 FIXME("(%p/%p)->(%p, %p, %s, %d, %p)\n", This, iface, pCallback, param, debugstr_guid(riid), method, punk);
3898 return E_NOTIMPL;
3901 static const IContextCallbackVtbl Context_Callback_Vtbl =
3903 Context_CC_QueryInterface,
3904 Context_CC_AddRef,
3905 Context_CC_Release,
3906 Context_CC_ContextCallback
3909 static HRESULT WINAPI Context_OC_QueryInterface(IObjContext *iface, REFIID riid, LPVOID *ppv)
3911 Context *This = impl_from_IObjContext(iface);
3912 return Context_QueryInterface(This, riid, ppv);
3915 static ULONG WINAPI Context_OC_AddRef(IObjContext *iface)
3917 Context *This = impl_from_IObjContext(iface);
3918 return Context_AddRef(This);
3921 static ULONG WINAPI Context_OC_Release(IObjContext *iface)
3923 Context *This = impl_from_IObjContext(iface);
3924 return Context_Release(This);
3927 static HRESULT WINAPI Context_OC_SetProperty(IObjContext *iface, REFGUID propid, CPFLAGS flags, IUnknown *punk)
3929 Context *This = impl_from_IObjContext(iface);
3931 FIXME("(%p/%p)->(%s, %x, %p)\n", This, iface, debugstr_guid(propid), flags, punk);
3932 return E_NOTIMPL;
3935 static HRESULT WINAPI Context_OC_RemoveProperty(IObjContext *iface, REFGUID propid)
3937 Context *This = impl_from_IObjContext(iface);
3939 FIXME("(%p/%p)->(%s)\n", This, iface, debugstr_guid(propid));
3940 return E_NOTIMPL;
3943 static HRESULT WINAPI Context_OC_GetProperty(IObjContext *iface, REFGUID propid, CPFLAGS *flags, IUnknown **punk)
3945 Context *This = impl_from_IObjContext(iface);
3947 FIXME("(%p/%p)->(%s, %p, %p)\n", This, iface, debugstr_guid(propid), flags, punk);
3948 return E_NOTIMPL;
3951 static HRESULT WINAPI Context_OC_EnumContextProps(IObjContext *iface, IEnumContextProps **props)
3953 Context *This = impl_from_IObjContext(iface);
3955 FIXME("(%p/%p)->(%p)\n", This, iface, props);
3956 return E_NOTIMPL;
3959 static void WINAPI Context_OC_Reserved1(IObjContext *iface)
3961 Context *This = impl_from_IObjContext(iface);
3962 FIXME("(%p/%p)\n", This, iface);
3965 static void WINAPI Context_OC_Reserved2(IObjContext *iface)
3967 Context *This = impl_from_IObjContext(iface);
3968 FIXME("(%p/%p)\n", This, iface);
3971 static void WINAPI Context_OC_Reserved3(IObjContext *iface)
3973 Context *This = impl_from_IObjContext(iface);
3974 FIXME("(%p/%p)\n", This, iface);
3977 static void WINAPI Context_OC_Reserved4(IObjContext *iface)
3979 Context *This = impl_from_IObjContext(iface);
3980 FIXME("(%p/%p)\n", This, iface);
3983 static void WINAPI Context_OC_Reserved5(IObjContext *iface)
3985 Context *This = impl_from_IObjContext(iface);
3986 FIXME("(%p/%p)\n", This, iface);
3989 static void WINAPI Context_OC_Reserved6(IObjContext *iface)
3991 Context *This = impl_from_IObjContext(iface);
3992 FIXME("(%p/%p)\n", This, iface);
3995 static void WINAPI Context_OC_Reserved7(IObjContext *iface)
3997 Context *This = impl_from_IObjContext(iface);
3998 FIXME("(%p/%p)\n", This, iface);
4001 static const IObjContextVtbl Context_Object_Vtbl =
4003 Context_OC_QueryInterface,
4004 Context_OC_AddRef,
4005 Context_OC_Release,
4006 Context_OC_SetProperty,
4007 Context_OC_RemoveProperty,
4008 Context_OC_GetProperty,
4009 Context_OC_EnumContextProps,
4010 Context_OC_Reserved1,
4011 Context_OC_Reserved2,
4012 Context_OC_Reserved3,
4013 Context_OC_Reserved4,
4014 Context_OC_Reserved5,
4015 Context_OC_Reserved6,
4016 Context_OC_Reserved7
4019 /***********************************************************************
4020 * CoGetObjectContext [OLE32.@]
4022 * Retrieves an object associated with the current context (i.e. apartment).
4024 * PARAMS
4025 * riid [I] ID of the interface of the object to retrieve.
4026 * ppv [O] Address where object will be stored on return.
4028 * RETURNS
4029 * Success: S_OK.
4030 * Failure: HRESULT code.
4032 HRESULT WINAPI CoGetObjectContext(REFIID riid, void **ppv)
4034 APARTMENT *apt = COM_CurrentApt();
4035 Context *context;
4036 HRESULT hr;
4038 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
4040 *ppv = NULL;
4041 if (!apt)
4043 if (!(apt = apartment_find_multi_threaded()))
4045 ERR("apartment not initialised\n");
4046 return CO_E_NOTINITIALIZED;
4048 apartment_release(apt);
4051 context = HeapAlloc(GetProcessHeap(), 0, sizeof(*context));
4052 if (!context)
4053 return E_OUTOFMEMORY;
4055 context->IComThreadingInfo_iface.lpVtbl = &Context_Threading_Vtbl;
4056 context->IContextCallback_iface.lpVtbl = &Context_Callback_Vtbl;
4057 context->IObjContext_iface.lpVtbl = &Context_Object_Vtbl;
4058 context->refs = 1;
4059 if (apt->multi_threaded)
4060 context->apttype = APTTYPE_MTA;
4061 else if (apt->main)
4062 context->apttype = APTTYPE_MAINSTA;
4063 else
4064 context->apttype = APTTYPE_STA;
4066 hr = IUnknown_QueryInterface((IUnknown *)&context->IComThreadingInfo_iface, riid, ppv);
4067 IUnknown_Release((IUnknown *)&context->IComThreadingInfo_iface);
4069 return hr;
4073 /***********************************************************************
4074 * CoGetContextToken [OLE32.@]
4076 HRESULT WINAPI CoGetContextToken( ULONG_PTR *token )
4078 struct oletls *info = COM_CurrentInfo();
4080 TRACE("(%p)\n", token);
4082 if (!info)
4083 return E_OUTOFMEMORY;
4085 if (!info->apt)
4087 APARTMENT *apt;
4088 if (!(apt = apartment_find_multi_threaded()))
4090 ERR("apartment not initialised\n");
4091 return CO_E_NOTINITIALIZED;
4093 apartment_release(apt);
4096 if (!token)
4097 return E_POINTER;
4099 if (!info->context_token)
4101 HRESULT hr;
4102 IObjContext *ctx;
4104 hr = CoGetObjectContext(&IID_IObjContext, (void **)&ctx);
4105 if (FAILED(hr)) return hr;
4106 info->context_token = ctx;
4109 *token = (ULONG_PTR)info->context_token;
4110 TRACE("apt->context_token=%p\n", info->context_token);
4112 return S_OK;
4115 HRESULT Handler_DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
4117 static const WCHAR wszInprocHandler32[] = {'I','n','p','r','o','c','H','a','n','d','l','e','r','3','2',0};
4118 HKEY hkey;
4119 HRESULT hres;
4121 hres = COM_OpenKeyForCLSID(rclsid, wszInprocHandler32, KEY_READ, &hkey);
4122 if (SUCCEEDED(hres))
4124 WCHAR dllpath[MAX_PATH+1];
4126 if (COM_RegReadPath(hkey, NULL, NULL, dllpath, ARRAYSIZE(dllpath)) == ERROR_SUCCESS)
4128 static const WCHAR wszOle32[] = {'o','l','e','3','2','.','d','l','l',0};
4129 if (!strcmpiW(dllpath, wszOle32))
4131 RegCloseKey(hkey);
4132 return HandlerCF_Create(rclsid, riid, ppv);
4135 else
4136 WARN("not creating object for inproc handler path %s\n", debugstr_w(dllpath));
4137 RegCloseKey(hkey);
4140 return CLASS_E_CLASSNOTAVAILABLE;
4143 /***********************************************************************
4144 * DllMain (OLE32.@)
4146 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID fImpLoad)
4148 TRACE("%p 0x%x %p\n", hinstDLL, fdwReason, fImpLoad);
4150 switch(fdwReason) {
4151 case DLL_PROCESS_ATTACH:
4152 hProxyDll = hinstDLL;
4153 COMPOBJ_InitProcess();
4154 break;
4156 case DLL_PROCESS_DETACH:
4157 COMPOBJ_UninitProcess();
4158 RPC_UnregisterAllChannelHooks();
4159 COMPOBJ_DllList_Free();
4160 break;
4162 case DLL_THREAD_DETACH:
4163 COM_TlsDestroy();
4164 break;
4166 return TRUE;
4169 /***********************************************************************
4170 * DllRegisterServer (OLE32.@)
4172 HRESULT WINAPI DllRegisterServer(void)
4174 return OLE32_DllRegisterServer();
4177 /***********************************************************************
4178 * DllUnregisterServer (OLE32.@)
4180 HRESULT WINAPI DllUnregisterServer(void)
4182 return OLE32_DllUnregisterServer();