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
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
47 #define NONAMELESSUNION
48 #define NONAMELESSSTRUCT
59 #include "compobj_private.h"
61 #include "wine/unicode.h"
62 #include "wine/debug.h"
64 WINE_DEFAULT_DEBUG_CHANNEL(ole);
66 HINSTANCE OLE32_hInstance = 0;
68 #define ARRAYSIZE(array) (sizeof(array)/sizeof((array)[0]))
70 /****************************************************************************
71 * This section defines variables internal to the COM module.
74 static HRESULT COM_GetRegisteredClassObject(const struct apartment *apt, REFCLSID rclsid,
75 DWORD dwClsContext, LPUNKNOWN* ppUnk);
76 static void COM_RevokeAllClasses(const struct apartment *apt);
77 static HRESULT get_inproc_class_object(APARTMENT *apt, HKEY hkeydll, REFCLSID rclsid, REFIID riid, BOOL hostifnecessary, void **ppv);
79 static APARTMENT *MTA; /* protected by csApartment */
80 static APARTMENT *MainApartment; /* the first STA apartment */
81 static struct list apts = LIST_INIT( apts ); /* protected by csApartment */
83 static CRITICAL_SECTION csApartment;
84 static CRITICAL_SECTION_DEBUG critsect_debug =
87 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
88 0, 0, { (DWORD_PTR)(__FILE__ ": csApartment") }
90 static CRITICAL_SECTION csApartment = { &critsect_debug, -1, 0, 0, 0, 0 };
92 struct registered_psclsid
100 * This lock count counts the number of times CoInitialize is called. It is
101 * decreased every time CoUninitialize is called. When it hits 0, the COM
102 * libraries are freed
104 static LONG s_COMLockCount = 0;
105 /* Reference count used by CoAddRefServerProcess/CoReleaseServerProcess */
106 static LONG s_COMServerProcessReferences = 0;
109 * This linked list contains the list of registered class objects. These
110 * are mostly used to register the factories for out-of-proc servers of OLE
113 * TODO: Make this data structure aware of inter-process communication. This
114 * means that parts of this will be exported to rpcss.
116 typedef struct tagRegisteredClass
119 CLSID classIdentifier;
121 LPUNKNOWN classObject;
125 LPSTREAM pMarshaledData; /* FIXME: only really need to store OXID and IPID */
126 void *RpcRegistration;
129 static struct list RegisteredClassList = LIST_INIT(RegisteredClassList);
131 static CRITICAL_SECTION csRegisteredClassList;
132 static CRITICAL_SECTION_DEBUG class_cs_debug =
134 0, 0, &csRegisteredClassList,
135 { &class_cs_debug.ProcessLocksList, &class_cs_debug.ProcessLocksList },
136 0, 0, { (DWORD_PTR)(__FILE__ ": csRegisteredClassList") }
138 static CRITICAL_SECTION csRegisteredClassList = { &class_cs_debug, -1, 0, 0, 0, 0 };
140 /*****************************************************************************
141 * This section contains OpenDllList definitions
143 * The OpenDllList contains only handles of dll loaded by CoGetClassObject or
144 * other functions that do LoadLibrary _without_ giving back a HMODULE.
145 * Without this list these handles would never be freed.
147 * FIXME: a DLL that says OK when asked for unloading is unloaded in the
148 * next unload-call but not before 600 sec.
151 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid, REFIID iid, LPVOID *ppv);
152 typedef HRESULT (WINAPI *DllCanUnloadNowFunc)(void);
154 typedef struct tagOpenDll
159 DllGetClassObjectFunc DllGetClassObject;
160 DllCanUnloadNowFunc DllCanUnloadNow;
164 static struct list openDllList = LIST_INIT(openDllList);
166 static CRITICAL_SECTION csOpenDllList;
167 static CRITICAL_SECTION_DEBUG dll_cs_debug =
169 0, 0, &csOpenDllList,
170 { &dll_cs_debug.ProcessLocksList, &dll_cs_debug.ProcessLocksList },
171 0, 0, { (DWORD_PTR)(__FILE__ ": csOpenDllList") }
173 static CRITICAL_SECTION csOpenDllList = { &dll_cs_debug, -1, 0, 0, 0, 0 };
175 struct apartment_loaded_dll
183 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',' ',
184 '0','x','#','#','#','#','#','#','#','#',' ',0};
185 static LRESULT CALLBACK apartment_wndproc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
186 static HRESULT apartment_getclassobject(struct apartment *apt, LPCWSTR dllpath,
187 BOOL apartment_threaded,
188 REFCLSID rclsid, REFIID riid, void **ppv);
189 static void apartment_freeunusedlibraries(struct apartment *apt, DWORD delay);
191 static HRESULT COMPOBJ_DllList_Add(LPCWSTR library_name, OpenDll **ret);
192 static OpenDll *COMPOBJ_DllList_Get(LPCWSTR library_name);
193 static void COMPOBJ_DllList_ReleaseRef(OpenDll *entry, BOOL free_entry);
195 static DWORD COM_RegReadPath(HKEY hkeyroot, const WCHAR *keyname, const WCHAR *valuename, WCHAR * dst, DWORD dstlen);
197 static void COMPOBJ_InitProcess( void )
201 /* Dispatching to the correct thread in an apartment is done through
202 * window messages rather than RPC transports. When an interface is
203 * marshalled into another apartment in the same process, a window of the
204 * following class is created. The *caller* of CoMarshalInterface (i.e., the
205 * application) is responsible for pumping the message loop in that thread.
206 * The WM_USER messages which point to the RPCs are then dispatched to
207 * apartment_wndproc by the user's code from the apartment in which the
208 * interface was unmarshalled.
210 memset(&wclass, 0, sizeof(wclass));
211 wclass.lpfnWndProc = apartment_wndproc;
212 wclass.hInstance = OLE32_hInstance;
213 wclass.lpszClassName = wszAptWinClass;
214 RegisterClassW(&wclass);
217 static void COMPOBJ_UninitProcess( void )
219 UnregisterClassW(wszAptWinClass, OLE32_hInstance);
222 static void COM_TlsDestroy(void)
224 struct oletls *info = NtCurrentTeb()->ReservedForOle;
227 if (info->apt) apartment_release(info->apt);
228 if (info->errorinfo) IErrorInfo_Release(info->errorinfo);
229 if (info->state) IUnknown_Release(info->state);
230 if (info->spy) IUnknown_Release(info->spy);
231 HeapFree(GetProcessHeap(), 0, info);
232 NtCurrentTeb()->ReservedForOle = NULL;
236 /******************************************************************************
240 /* allocates memory and fills in the necessary fields for a new apartment
241 * object. must be called inside apartment cs */
242 static APARTMENT *apartment_construct(DWORD model)
246 TRACE("creating new apartment, model=%d\n", model);
248 apt = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*apt));
249 apt->tid = GetCurrentThreadId();
251 list_init(&apt->proxies);
252 list_init(&apt->stubmgrs);
253 list_init(&apt->psclsids);
254 list_init(&apt->loaded_dlls);
257 apt->remunk_exported = FALSE;
259 InitializeCriticalSection(&apt->cs);
260 DEBUG_SET_CRITSEC_NAME(&apt->cs, "apartment");
262 apt->multi_threaded = !(model & COINIT_APARTMENTTHREADED);
264 if (apt->multi_threaded)
266 /* FIXME: should be randomly generated by in an RPC call to rpcss */
267 apt->oxid = ((OXID)GetCurrentProcessId() << 32) | 0xcafe;
271 /* FIXME: should be randomly generated by in an RPC call to rpcss */
272 apt->oxid = ((OXID)GetCurrentProcessId() << 32) | GetCurrentThreadId();
275 TRACE("Created apartment on OXID %s\n", wine_dbgstr_longlong(apt->oxid));
277 list_add_head(&apts, &apt->entry);
282 /* gets and existing apartment if one exists or otherwise creates an apartment
283 * structure which stores OLE apartment-local information and stores a pointer
284 * to it in the thread-local storage */
285 static APARTMENT *apartment_get_or_create(DWORD model)
287 APARTMENT *apt = COM_CurrentApt();
291 if (model & COINIT_APARTMENTTHREADED)
293 EnterCriticalSection(&csApartment);
295 apt = apartment_construct(model);
300 TRACE("Created main-threaded apartment with OXID %s\n", wine_dbgstr_longlong(apt->oxid));
303 LeaveCriticalSection(&csApartment);
306 apartment_createwindowifneeded(apt);
310 EnterCriticalSection(&csApartment);
312 /* The multi-threaded apartment (MTA) contains zero or more threads interacting
313 * with free threaded (ie thread safe) COM objects. There is only ever one MTA
317 TRACE("entering the multithreaded apartment %s\n", wine_dbgstr_longlong(MTA->oxid));
318 apartment_addref(MTA);
321 MTA = apartment_construct(model);
325 LeaveCriticalSection(&csApartment);
327 COM_CurrentInfo()->apt = apt;
333 static inline BOOL apartment_is_model(const APARTMENT *apt, DWORD model)
335 return (apt->multi_threaded == !(model & COINIT_APARTMENTTHREADED));
338 DWORD apartment_addref(struct apartment *apt)
340 DWORD refs = InterlockedIncrement(&apt->refs);
341 TRACE("%s: before = %d\n", wine_dbgstr_longlong(apt->oxid), refs - 1);
345 DWORD apartment_release(struct apartment *apt)
349 EnterCriticalSection(&csApartment);
351 ret = InterlockedDecrement(&apt->refs);
352 TRACE("%s: after = %d\n", wine_dbgstr_longlong(apt->oxid), ret);
353 /* destruction stuff that needs to happen under csApartment CS */
356 if (apt == MTA) MTA = NULL;
357 else if (apt == MainApartment) MainApartment = NULL;
358 list_remove(&apt->entry);
361 LeaveCriticalSection(&csApartment);
365 struct list *cursor, *cursor2;
367 TRACE("destroying apartment %p, oxid %s\n", apt, wine_dbgstr_longlong(apt->oxid));
369 /* Release the references to the registered class objects */
370 COM_RevokeAllClasses(apt);
372 /* no locking is needed for this apartment, because no other thread
373 * can access it at this point */
375 apartment_disconnectproxies(apt);
377 if (apt->win) DestroyWindow(apt->win);
378 if (apt->host_apt_tid) PostThreadMessageW(apt->host_apt_tid, WM_QUIT, 0, 0);
380 LIST_FOR_EACH_SAFE(cursor, cursor2, &apt->stubmgrs)
382 struct stub_manager *stubmgr = LIST_ENTRY(cursor, struct stub_manager, entry);
383 /* release the implicit reference given by the fact that the
384 * stub has external references (it must do since it is in the
385 * stub manager list in the apartment and all non-apartment users
386 * must have a ref on the apartment and so it cannot be destroyed).
388 stub_manager_int_release(stubmgr);
391 LIST_FOR_EACH_SAFE(cursor, cursor2, &apt->psclsids)
393 struct registered_psclsid *registered_psclsid =
394 LIST_ENTRY(cursor, struct registered_psclsid, entry);
396 list_remove(®istered_psclsid->entry);
397 HeapFree(GetProcessHeap(), 0, registered_psclsid);
400 /* if this assert fires, then another thread took a reference to a
401 * stub manager without taking a reference to the containing
402 * apartment, which it must do. */
403 assert(list_empty(&apt->stubmgrs));
405 if (apt->filter) IUnknown_Release(apt->filter);
407 /* free as many unused libraries as possible... */
408 apartment_freeunusedlibraries(apt, 0);
410 /* ... and free the memory for the apartment loaded dll entry and
411 * release the dll list reference without freeing the library for the
413 while ((cursor = list_head(&apt->loaded_dlls)))
415 struct apartment_loaded_dll *apartment_loaded_dll = LIST_ENTRY(cursor, struct apartment_loaded_dll, entry);
416 COMPOBJ_DllList_ReleaseRef(apartment_loaded_dll->dll, FALSE);
418 HeapFree(GetProcessHeap(), 0, apartment_loaded_dll);
421 DEBUG_CLEAR_CRITSEC_NAME(&apt->cs);
422 DeleteCriticalSection(&apt->cs);
424 HeapFree(GetProcessHeap(), 0, apt);
430 /* The given OXID must be local to this process:
432 * The ref parameter is here mostly to ensure people remember that
433 * they get one, you should normally take a ref for thread safety.
435 APARTMENT *apartment_findfromoxid(OXID oxid, BOOL ref)
437 APARTMENT *result = NULL;
440 EnterCriticalSection(&csApartment);
441 LIST_FOR_EACH( cursor, &apts )
443 struct apartment *apt = LIST_ENTRY( cursor, struct apartment, entry );
444 if (apt->oxid == oxid)
447 if (ref) apartment_addref(result);
451 LeaveCriticalSection(&csApartment);
456 /* gets the apartment which has a given creator thread ID. The caller must
457 * release the reference from the apartment as soon as the apartment pointer
458 * is no longer required. */
459 APARTMENT *apartment_findfromtid(DWORD tid)
461 APARTMENT *result = NULL;
464 EnterCriticalSection(&csApartment);
465 LIST_FOR_EACH( cursor, &apts )
467 struct apartment *apt = LIST_ENTRY( cursor, struct apartment, entry );
471 apartment_addref(result);
475 LeaveCriticalSection(&csApartment);
480 /* gets the main apartment if it exists. The caller must
481 * release the reference from the apartment as soon as the apartment pointer
482 * is no longer required. */
483 static APARTMENT *apartment_findmain(void)
487 EnterCriticalSection(&csApartment);
489 result = MainApartment;
490 if (result) apartment_addref(result);
492 LeaveCriticalSection(&csApartment);
497 struct host_object_params
500 CLSID clsid; /* clsid of object to marshal */
501 IID iid; /* interface to marshal */
502 HANDLE event; /* event signalling when ready for multi-threaded case */
503 HRESULT hr; /* result for multi-threaded case */
504 IStream *stream; /* stream that the object will be marshaled into */
505 BOOL apartment_threaded; /* is the component purely apartment-threaded? */
508 static HRESULT apartment_hostobject(struct apartment *apt,
509 const struct host_object_params *params)
513 static const LARGE_INTEGER llZero;
514 WCHAR dllpath[MAX_PATH+1];
516 TRACE("clsid %s, iid %s\n", debugstr_guid(¶ms->clsid), debugstr_guid(¶ms->iid));
518 if (COM_RegReadPath(params->hkeydll, NULL, NULL, dllpath, ARRAYSIZE(dllpath)) != ERROR_SUCCESS)
520 /* failure: CLSID is not found in registry */
521 WARN("class %s not registered inproc\n", debugstr_guid(¶ms->clsid));
522 return REGDB_E_CLASSNOTREG;
525 hr = apartment_getclassobject(apt, dllpath, params->apartment_threaded,
526 ¶ms->clsid, ¶ms->iid, (void **)&object);
530 hr = CoMarshalInterface(params->stream, ¶ms->iid, object, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
532 IUnknown_Release(object);
533 IStream_Seek(params->stream, llZero, STREAM_SEEK_SET, NULL);
538 static LRESULT CALLBACK apartment_wndproc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
543 RPC_ExecuteCall((struct dispatch_params *)lParam);
546 return apartment_hostobject(COM_CurrentApt(), (const struct host_object_params *)lParam);
548 return DefWindowProcW(hWnd, msg, wParam, lParam);
552 struct host_thread_params
554 COINIT threading_model;
559 /* thread for hosting an object to allow an object to appear to be created in
560 * an apartment with an incompatible threading model */
561 static DWORD CALLBACK apartment_hostobject_thread(LPVOID p)
563 struct host_thread_params *params = p;
566 struct apartment *apt;
570 hr = CoInitializeEx(NULL, params->threading_model);
571 if (FAILED(hr)) return hr;
573 apt = COM_CurrentApt();
574 if (params->threading_model == COINIT_APARTMENTTHREADED)
576 apartment_createwindowifneeded(apt);
577 params->apartment_hwnd = apartment_getwindow(apt);
580 params->apartment_hwnd = NULL;
582 /* force the message queue to be created before signaling parent thread */
583 PeekMessageW(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
585 SetEvent(params->ready_event);
586 params = NULL; /* can't touch params after here as it may be invalid */
588 while (GetMessageW(&msg, NULL, 0, 0))
590 if (!msg.hwnd && (msg.message == DM_HOSTOBJECT))
592 struct host_object_params *obj_params = (struct host_object_params *)msg.lParam;
593 obj_params->hr = apartment_hostobject(apt, obj_params);
594 SetEvent(obj_params->event);
598 TranslateMessage(&msg);
599 DispatchMessageW(&msg);
610 /* finds or creates a host apartment, creates the object inside it and returns
611 * a proxy to it so that the object can be used in the apartment of the
612 * caller of this function */
613 static HRESULT apartment_hostobject_in_hostapt(
614 struct apartment *apt, BOOL multi_threaded, BOOL main_apartment,
615 HKEY hkeydll, REFCLSID rclsid, REFIID riid, void **ppv)
617 struct host_object_params params;
618 HWND apartment_hwnd = NULL;
619 DWORD apartment_tid = 0;
622 if (!multi_threaded && main_apartment)
624 APARTMENT *host_apt = apartment_findmain();
627 apartment_hwnd = apartment_getwindow(host_apt);
628 apartment_release(host_apt);
634 EnterCriticalSection(&apt->cs);
636 if (!apt->host_apt_tid)
638 struct host_thread_params thread_params;
642 thread_params.threading_model = multi_threaded ? COINIT_MULTITHREADED : COINIT_APARTMENTTHREADED;
643 handles[0] = thread_params.ready_event = CreateEventW(NULL, FALSE, FALSE, NULL);
644 thread_params.apartment_hwnd = NULL;
645 handles[1] = CreateThread(NULL, 0, apartment_hostobject_thread, &thread_params, 0, &apt->host_apt_tid);
648 CloseHandle(handles[0]);
649 LeaveCriticalSection(&apt->cs);
650 return E_OUTOFMEMORY;
652 wait_value = WaitForMultipleObjects(2, handles, FALSE, INFINITE);
653 CloseHandle(handles[0]);
654 CloseHandle(handles[1]);
655 if (wait_value == WAIT_OBJECT_0)
656 apt->host_apt_hwnd = thread_params.apartment_hwnd;
659 LeaveCriticalSection(&apt->cs);
660 return E_OUTOFMEMORY;
664 if (multi_threaded || !main_apartment)
666 apartment_hwnd = apt->host_apt_hwnd;
667 apartment_tid = apt->host_apt_tid;
670 LeaveCriticalSection(&apt->cs);
673 /* another thread may have become the main apartment in the time it took
674 * us to create the thread for the host apartment */
675 if (!apartment_hwnd && !multi_threaded && main_apartment)
677 APARTMENT *host_apt = apartment_findmain();
680 apartment_hwnd = apartment_getwindow(host_apt);
681 apartment_release(host_apt);
685 params.hkeydll = hkeydll;
686 params.clsid = *rclsid;
688 hr = CreateStreamOnHGlobal(NULL, TRUE, ¶ms.stream);
691 params.apartment_threaded = !multi_threaded;
695 params.event = CreateEventW(NULL, FALSE, FALSE, NULL);
696 if (!PostThreadMessageW(apartment_tid, DM_HOSTOBJECT, 0, (LPARAM)¶ms))
700 WaitForSingleObject(params.event, INFINITE);
703 CloseHandle(params.event);
709 ERR("host apartment didn't create window\n");
713 hr = SendMessageW(apartment_hwnd, DM_HOSTOBJECT, 0, (LPARAM)¶ms);
716 hr = CoUnmarshalInterface(params.stream, riid, ppv);
717 IStream_Release(params.stream);
721 /* create a window for the apartment or return the current one if one has
722 * already been created */
723 HRESULT apartment_createwindowifneeded(struct apartment *apt)
725 if (apt->multi_threaded)
730 HWND hwnd = CreateWindowW(wszAptWinClass, NULL, 0,
732 HWND_MESSAGE, 0, OLE32_hInstance, NULL);
735 ERR("CreateWindow failed with error %d\n", GetLastError());
736 return HRESULT_FROM_WIN32(GetLastError());
738 if (InterlockedCompareExchangePointer((PVOID *)&apt->win, hwnd, NULL))
739 /* someone beat us to it */
746 /* retrieves the window for the main- or apartment-threaded apartment */
747 HWND apartment_getwindow(const struct apartment *apt)
749 assert(!apt->multi_threaded);
753 void apartment_joinmta(void)
755 apartment_addref(MTA);
756 COM_CurrentInfo()->apt = MTA;
759 /* gets the specified class object by loading the appropriate DLL, if
760 * necessary and calls the DllGetClassObject function for the DLL */
761 static HRESULT apartment_getclassobject(struct apartment *apt, LPCWSTR dllpath,
762 BOOL apartment_threaded,
763 REFCLSID rclsid, REFIID riid, void **ppv)
765 static const WCHAR wszOle32[] = {'o','l','e','3','2','.','d','l','l',0};
768 struct apartment_loaded_dll *apartment_loaded_dll;
770 if (!strcmpiW(dllpath, wszOle32))
772 /* we don't need to control the lifetime of this dll, so use the local
773 * implementation of DllGetClassObject directly */
774 TRACE("calling ole32!DllGetClassObject\n");
775 hr = DllGetClassObject(rclsid, riid, ppv);
778 ERR("DllGetClassObject returned error 0x%08x\n", hr);
783 EnterCriticalSection(&apt->cs);
785 LIST_FOR_EACH_ENTRY(apartment_loaded_dll, &apt->loaded_dlls, struct apartment_loaded_dll, entry)
786 if (!strcmpiW(dllpath, apartment_loaded_dll->dll->library_name))
788 TRACE("found %s already loaded\n", debugstr_w(dllpath));
795 apartment_loaded_dll = HeapAlloc(GetProcessHeap(), 0, sizeof(*apartment_loaded_dll));
796 if (!apartment_loaded_dll)
800 apartment_loaded_dll->unload_time = 0;
801 apartment_loaded_dll->multi_threaded = FALSE;
802 hr = COMPOBJ_DllList_Add( dllpath, &apartment_loaded_dll->dll );
804 HeapFree(GetProcessHeap(), 0, apartment_loaded_dll);
808 TRACE("added new loaded dll %s\n", debugstr_w(dllpath));
809 list_add_tail(&apt->loaded_dlls, &apartment_loaded_dll->entry);
813 LeaveCriticalSection(&apt->cs);
817 /* one component being multi-threaded overrides any number of
818 * apartment-threaded components */
819 if (!apartment_threaded)
820 apartment_loaded_dll->multi_threaded = TRUE;
822 TRACE("calling DllGetClassObject %p\n", apartment_loaded_dll->dll->DllGetClassObject);
823 /* OK: get the ClassObject */
824 hr = apartment_loaded_dll->dll->DllGetClassObject(rclsid, riid, ppv);
827 ERR("DllGetClassObject returned error 0x%08x\n", hr);
833 /* frees unused libraries loaded by apartment_getclassobject by calling the
834 * DLL's DllCanUnloadNow entry point */
835 static void apartment_freeunusedlibraries(struct apartment *apt, DWORD delay)
837 struct apartment_loaded_dll *entry, *next;
838 EnterCriticalSection(&apt->cs);
839 LIST_FOR_EACH_ENTRY_SAFE(entry, next, &apt->loaded_dlls, struct apartment_loaded_dll, entry)
841 if (entry->dll->DllCanUnloadNow && (entry->dll->DllCanUnloadNow() == S_OK))
843 DWORD real_delay = delay;
845 if (real_delay == INFINITE)
847 /* DLLs that return multi-threaded objects aren't unloaded
848 * straight away to cope for programs that have races between
849 * last object destruction and threads in the DLLs that haven't
850 * finished, despite DllCanUnloadNow returning S_OK */
851 if (entry->multi_threaded)
852 real_delay = 10 * 60 * 1000; /* 10 minutes */
857 if (!real_delay || (entry->unload_time && (entry->unload_time < GetTickCount())))
859 list_remove(&entry->entry);
860 COMPOBJ_DllList_ReleaseRef(entry->dll, TRUE);
861 HeapFree(GetProcessHeap(), 0, entry);
864 entry->unload_time = GetTickCount() + real_delay;
866 else if (entry->unload_time)
867 entry->unload_time = 0;
869 LeaveCriticalSection(&apt->cs);
872 /*****************************************************************************
873 * This section contains OpenDllList implementation
876 /* caller must ensure that library_name is not already in the open dll list */
877 static HRESULT COMPOBJ_DllList_Add(LPCWSTR library_name, OpenDll **ret)
883 DllCanUnloadNowFunc DllCanUnloadNow;
884 DllGetClassObjectFunc DllGetClassObject;
888 *ret = COMPOBJ_DllList_Get(library_name);
889 if (*ret) return S_OK;
891 /* do this outside the csOpenDllList to avoid creating a lock dependency on
893 hLibrary = LoadLibraryExW(library_name, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
896 ERR("couldn't load in-process dll %s\n", debugstr_w(library_name));
897 /* failure: DLL could not be loaded */
898 return E_ACCESSDENIED; /* FIXME: or should this be CO_E_DLLNOTFOUND? */
901 DllCanUnloadNow = (void *)GetProcAddress(hLibrary, "DllCanUnloadNow");
902 /* Note: failing to find DllCanUnloadNow is not a failure */
903 DllGetClassObject = (void *)GetProcAddress(hLibrary, "DllGetClassObject");
904 if (!DllGetClassObject)
906 /* failure: the dll did not export DllGetClassObject */
907 ERR("couldn't find function DllGetClassObject in %s\n", debugstr_w(library_name));
908 FreeLibrary(hLibrary);
909 return CO_E_DLLNOTFOUND;
912 EnterCriticalSection( &csOpenDllList );
914 *ret = COMPOBJ_DllList_Get(library_name);
917 /* another caller to this function already added the dll while we
918 * weren't in the critical section */
919 FreeLibrary(hLibrary);
923 len = strlenW(library_name);
924 entry = HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
926 entry->library_name = HeapAlloc(GetProcessHeap(), 0, (len + 1)*sizeof(WCHAR));
927 if (entry && entry->library_name)
929 memcpy(entry->library_name, library_name, (len + 1)*sizeof(WCHAR));
930 entry->library = hLibrary;
932 entry->DllCanUnloadNow = DllCanUnloadNow;
933 entry->DllGetClassObject = DllGetClassObject;
934 list_add_tail(&openDllList, &entry->entry);
939 FreeLibrary(hLibrary);
944 LeaveCriticalSection( &csOpenDllList );
949 static OpenDll *COMPOBJ_DllList_Get(LPCWSTR library_name)
953 EnterCriticalSection(&csOpenDllList);
954 LIST_FOR_EACH_ENTRY(ptr, &openDllList, OpenDll, entry)
956 if (!strcmpiW(library_name, ptr->library_name) &&
957 (InterlockedIncrement(&ptr->refs) != 1) /* entry is being destroy if == 1 */)
963 LeaveCriticalSection(&csOpenDllList);
967 /* pass FALSE for free_entry to release a reference without destroying the
968 * entry if it reaches zero or TRUE otherwise */
969 static void COMPOBJ_DllList_ReleaseRef(OpenDll *entry, BOOL free_entry)
971 if (!InterlockedDecrement(&entry->refs) && free_entry)
973 EnterCriticalSection(&csOpenDllList);
974 list_remove(&entry->entry);
975 LeaveCriticalSection(&csOpenDllList);
977 TRACE("freeing %p\n", entry->library);
978 FreeLibrary(entry->library);
980 HeapFree(GetProcessHeap(), 0, entry->library_name);
981 HeapFree(GetProcessHeap(), 0, entry);
985 /* frees memory associated with active dll list */
986 static void COMPOBJ_DllList_Free(void)
988 OpenDll *entry, *cursor2;
989 EnterCriticalSection(&csOpenDllList);
990 LIST_FOR_EACH_ENTRY_SAFE(entry, cursor2, &openDllList, OpenDll, entry)
992 list_remove(&entry->entry);
994 HeapFree(GetProcessHeap(), 0, entry->library_name);
995 HeapFree(GetProcessHeap(), 0, entry);
997 LeaveCriticalSection(&csOpenDllList);
1000 /******************************************************************************
1001 * CoBuildVersion [OLE32.@]
1002 * CoBuildVersion [COMPOBJ.1]
1004 * Gets the build version of the DLL.
1009 * Current build version, hiword is majornumber, loword is minornumber
1011 DWORD WINAPI CoBuildVersion(void)
1013 TRACE("Returning version %d, build %d.\n", rmm, rup);
1014 return (rmm<<16)+rup;
1017 /******************************************************************************
1018 * CoRegisterInitializeSpy [OLE32.@]
1020 * Add a Spy that watches CoInitializeEx calls
1023 * spy [I] Pointer to IUnknown interface that will be QueryInterface'd.
1024 * cookie [II] cookie receiver
1027 * Success: S_OK if not already initialized, S_FALSE otherwise.
1028 * Failure: HRESULT code.
1033 HRESULT WINAPI CoRegisterInitializeSpy(IInitializeSpy *spy, ULARGE_INTEGER *cookie)
1035 struct oletls *info = COM_CurrentInfo();
1038 TRACE("(%p, %p)\n", spy, cookie);
1040 if (!spy || !cookie || !info)
1043 WARN("Could not allocate tls\n");
1044 return E_INVALIDARG;
1049 FIXME("Already registered?\n");
1050 return E_UNEXPECTED;
1053 hr = IUnknown_QueryInterface(spy, &IID_IInitializeSpy, (void **) &info->spy);
1056 cookie->QuadPart = (DWORD_PTR)spy;
1062 /******************************************************************************
1063 * CoRevokeInitializeSpy [OLE32.@]
1065 * Remove a spy that previously watched CoInitializeEx calls
1068 * cookie [I] The cookie obtained from a previous CoRegisterInitializeSpy call
1071 * Success: S_OK if a spy is removed
1072 * Failure: E_INVALIDARG
1077 HRESULT WINAPI CoRevokeInitializeSpy(ULARGE_INTEGER cookie)
1079 struct oletls *info = COM_CurrentInfo();
1080 TRACE("(%s)\n", wine_dbgstr_longlong(cookie.QuadPart));
1082 if (!info || !info->spy || cookie.QuadPart != (DWORD_PTR)info->spy)
1083 return E_INVALIDARG;
1085 IUnknown_Release(info->spy);
1091 /******************************************************************************
1092 * CoInitialize [OLE32.@]
1094 * Initializes the COM libraries by calling CoInitializeEx with
1095 * COINIT_APARTMENTTHREADED, ie it enters a STA thread.
1098 * lpReserved [I] Pointer to IMalloc interface (obsolete, should be NULL).
1101 * Success: S_OK if not already initialized, S_FALSE otherwise.
1102 * Failure: HRESULT code.
1107 HRESULT WINAPI CoInitialize(LPVOID lpReserved)
1110 * Just delegate to the newer method.
1112 return CoInitializeEx(lpReserved, COINIT_APARTMENTTHREADED);
1115 /******************************************************************************
1116 * CoInitializeEx [OLE32.@]
1118 * Initializes the COM libraries.
1121 * lpReserved [I] Pointer to IMalloc interface (obsolete, should be NULL).
1122 * dwCoInit [I] One or more flags from the COINIT enumeration. See notes.
1125 * S_OK if successful,
1126 * S_FALSE if this function was called already.
1127 * RPC_E_CHANGED_MODE if a previous call to CoInitializeEx specified another
1132 * The behavior used to set the IMalloc used for memory management is
1134 * The dwCoInit parameter must specify one of the following apartment
1136 *| COINIT_APARTMENTTHREADED - A single-threaded apartment (STA).
1137 *| COINIT_MULTITHREADED - A multi-threaded apartment (MTA).
1138 * The parameter may also specify zero or more of the following flags:
1139 *| COINIT_DISABLE_OLE1DDE - Don't use DDE for OLE1 support.
1140 *| COINIT_SPEED_OVER_MEMORY - Trade memory for speed.
1145 HRESULT WINAPI CoInitializeEx(LPVOID lpReserved, DWORD dwCoInit)
1147 struct oletls *info = COM_CurrentInfo();
1151 TRACE("(%p, %x)\n", lpReserved, (int)dwCoInit);
1153 if (lpReserved!=NULL)
1155 ERR("(%p, %x) - Bad parameter passed-in %p, must be an old Windows Application\n", lpReserved, (int)dwCoInit, lpReserved);
1159 * Check the lock count. If this is the first time going through the initialize
1160 * process, we have to initialize the libraries.
1162 * And crank-up that lock count.
1164 if (InterlockedExchangeAdd(&s_COMLockCount,1)==0)
1167 * Initialize the various COM libraries and data structures.
1169 TRACE("() - Initializing the COM libraries\n");
1171 /* we may need to defer this until after apartment initialisation */
1172 RunningObjectTableImpl_Initialize();
1176 IInitializeSpy_PreInitialize(info->spy, dwCoInit, info->inits);
1178 if (!(apt = info->apt))
1180 apt = apartment_get_or_create(dwCoInit);
1181 if (!apt) return E_OUTOFMEMORY;
1183 else if (!apartment_is_model(apt, dwCoInit))
1185 /* Changing the threading model after it's been set is illegal. If this warning is triggered by Wine
1186 code then we are probably using the wrong threading model to implement that API. */
1187 ERR("Attempt to change threading model of this apartment from %s to %s\n",
1188 apt->multi_threaded ? "multi-threaded" : "apartment threaded",
1189 dwCoInit & COINIT_APARTMENTTHREADED ? "apartment threaded" : "multi-threaded");
1190 return RPC_E_CHANGED_MODE;
1198 IInitializeSpy_PostInitialize(info->spy, hr, dwCoInit, info->inits);
1203 /***********************************************************************
1204 * CoUninitialize [OLE32.@]
1206 * This method will decrement the refcount on the current apartment, freeing
1207 * the resources associated with it if it is the last thread in the apartment.
1208 * If the last apartment is freed, the function will additionally release
1209 * any COM resources associated with the process.
1219 void WINAPI CoUninitialize(void)
1221 struct oletls * info = COM_CurrentInfo();
1226 /* will only happen on OOM */
1230 IInitializeSpy_PreUninitialize(info->spy, info->inits);
1235 ERR("Mismatched CoUninitialize\n");
1238 IInitializeSpy_PostUninitialize(info->spy, info->inits);
1244 apartment_release(info->apt);
1249 * Decrease the reference count.
1250 * If we are back to 0 locks on the COM library, make sure we free
1251 * all the associated data structures.
1253 lCOMRefCnt = InterlockedExchangeAdd(&s_COMLockCount,-1);
1256 TRACE("() - Releasing the COM libraries\n");
1258 RunningObjectTableImpl_UnInitialize();
1260 else if (lCOMRefCnt<1) {
1261 ERR( "CoUninitialize() - not CoInitialized.\n" );
1262 InterlockedExchangeAdd(&s_COMLockCount,1); /* restore the lock count. */
1265 IInitializeSpy_PostUninitialize(info->spy, info->inits);
1268 /******************************************************************************
1269 * CoDisconnectObject [OLE32.@]
1271 * Disconnects all connections to this object from remote processes. Dispatches
1272 * pending RPCs while blocking new RPCs from occurring, and then calls
1273 * IMarshal::DisconnectObject on the given object.
1275 * Typically called when the object server is forced to shut down, for instance by
1279 * lpUnk [I] The object whose stub should be disconnected.
1280 * reserved [I] Reserved. Should be set to 0.
1284 * Failure: HRESULT code.
1287 * CoMarshalInterface, CoReleaseMarshalData, CoLockObjectExternal
1289 HRESULT WINAPI CoDisconnectObject( LPUNKNOWN lpUnk, DWORD reserved )
1295 TRACE("(%p, 0x%08x)\n", lpUnk, reserved);
1297 hr = IUnknown_QueryInterface(lpUnk, &IID_IMarshal, (void **)&marshal);
1300 hr = IMarshal_DisconnectObject(marshal, reserved);
1301 IMarshal_Release(marshal);
1305 apt = COM_CurrentApt();
1307 return CO_E_NOTINITIALIZED;
1309 apartment_disconnectobject(apt, lpUnk);
1311 /* Note: native is pretty broken here because it just silently
1312 * fails, without returning an appropriate error code if the object was
1313 * not found, making apps think that the object was disconnected, when
1314 * it actually wasn't */
1319 /******************************************************************************
1320 * CoCreateGuid [OLE32.@]
1321 * CoCreateGuid [COMPOBJ.73]
1323 * Simply forwards to UuidCreate in RPCRT4.
1326 * pguid [O] Points to the GUID to initialize.
1330 * Failure: HRESULT code.
1335 HRESULT WINAPI CoCreateGuid(GUID *pguid)
1337 DWORD status = UuidCreate(pguid);
1338 if (status == RPC_S_OK || status == RPC_S_UUID_LOCAL_ONLY) return S_OK;
1339 return HRESULT_FROM_WIN32( status );
1342 /******************************************************************************
1343 * CLSIDFromString [OLE32.@]
1344 * IIDFromString [OLE32.@]
1346 * Converts a unique identifier from its string representation into
1350 * idstr [I] The string representation of the GUID.
1351 * id [O] GUID converted from the string.
1355 * CO_E_CLASSSTRING if idstr is not a valid CLSID
1360 static HRESULT __CLSIDFromString(LPCWSTR s, CLSID *id)
1366 memset( id, 0, sizeof (CLSID) );
1370 /* validate the CLSID string */
1371 if (strlenW(s) != 38)
1372 return CO_E_CLASSSTRING;
1374 if ((s[0]!='{') || (s[9]!='-') || (s[14]!='-') || (s[19]!='-') || (s[24]!='-') || (s[37]!='}'))
1375 return CO_E_CLASSSTRING;
1377 for (i=1; i<37; i++) {
1378 if ((i == 9)||(i == 14)||(i == 19)||(i == 24)) continue;
1379 if (!(((s[i] >= '0') && (s[i] <= '9')) ||
1380 ((s[i] >= 'a') && (s[i] <= 'f')) ||
1381 ((s[i] >= 'A') && (s[i] <= 'F'))))
1382 return CO_E_CLASSSTRING;
1385 TRACE("%s -> %p\n", debugstr_w(s), id);
1387 /* quick lookup table */
1388 memset(table, 0, 256);
1390 for (i = 0; i < 10; i++) {
1393 for (i = 0; i < 6; i++) {
1394 table['A' + i] = i+10;
1395 table['a' + i] = i+10;
1398 /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
1400 id->Data1 = (table[s[1]] << 28 | table[s[2]] << 24 | table[s[3]] << 20 | table[s[4]] << 16 |
1401 table[s[5]] << 12 | table[s[6]] << 8 | table[s[7]] << 4 | table[s[8]]);
1402 id->Data2 = table[s[10]] << 12 | table[s[11]] << 8 | table[s[12]] << 4 | table[s[13]];
1403 id->Data3 = table[s[15]] << 12 | table[s[16]] << 8 | table[s[17]] << 4 | table[s[18]];
1405 /* these are just sequential bytes */
1406 id->Data4[0] = table[s[20]] << 4 | table[s[21]];
1407 id->Data4[1] = table[s[22]] << 4 | table[s[23]];
1408 id->Data4[2] = table[s[25]] << 4 | table[s[26]];
1409 id->Data4[3] = table[s[27]] << 4 | table[s[28]];
1410 id->Data4[4] = table[s[29]] << 4 | table[s[30]];
1411 id->Data4[5] = table[s[31]] << 4 | table[s[32]];
1412 id->Data4[6] = table[s[33]] << 4 | table[s[34]];
1413 id->Data4[7] = table[s[35]] << 4 | table[s[36]];
1418 /*****************************************************************************/
1420 HRESULT WINAPI CLSIDFromString(LPOLESTR idstr, CLSID *id )
1425 return E_INVALIDARG;
1427 ret = __CLSIDFromString(idstr, id);
1428 if(ret != S_OK) { /* It appears a ProgID is also valid */
1429 ret = CLSIDFromProgID(idstr, id);
1434 /* Converts a GUID into the respective string representation. */
1435 HRESULT WINE_StringFromCLSID(
1436 const CLSID *id, /* [in] GUID to be converted */
1437 LPSTR idstr /* [out] pointer to buffer to contain converted guid */
1439 static const char hex[] = "0123456789ABCDEF";
1444 { ERR("called with id=Null\n");
1449 sprintf(idstr, "{%08X-%04X-%04X-%02X%02X-",
1450 id->Data1, id->Data2, id->Data3,
1451 id->Data4[0], id->Data4[1]);
1455 for (i = 2; i < 8; i++) {
1456 *s++ = hex[id->Data4[i]>>4];
1457 *s++ = hex[id->Data4[i] & 0xf];
1463 TRACE("%p->%s\n", id, idstr);
1469 /******************************************************************************
1470 * StringFromCLSID [OLE32.@]
1471 * StringFromIID [OLE32.@]
1473 * Converts a GUID into the respective string representation.
1474 * The target string is allocated using the OLE IMalloc.
1477 * id [I] the GUID to be converted.
1478 * idstr [O] A pointer to a to-be-allocated pointer pointing to the resulting string.
1485 * StringFromGUID2, CLSIDFromString
1487 HRESULT WINAPI StringFromCLSID(REFCLSID id, LPOLESTR *idstr)
1493 if ((ret = CoGetMalloc(0,&mllc)))
1496 ret=WINE_StringFromCLSID(id,buf);
1498 DWORD len = MultiByteToWideChar( CP_ACP, 0, buf, -1, NULL, 0 );
1499 *idstr = IMalloc_Alloc( mllc, len * sizeof(WCHAR) );
1500 MultiByteToWideChar( CP_ACP, 0, buf, -1, *idstr, len );
1505 /******************************************************************************
1506 * StringFromGUID2 [OLE32.@]
1507 * StringFromGUID2 [COMPOBJ.76]
1509 * Modified version of StringFromCLSID that allows you to specify max
1513 * id [I] GUID to convert to string.
1514 * str [O] Buffer where the result will be stored.
1515 * cmax [I] Size of the buffer in characters.
1518 * Success: The length of the resulting string in characters.
1521 INT WINAPI StringFromGUID2(REFGUID id, LPOLESTR str, INT cmax)
1525 if (WINE_StringFromCLSID(id,xguid))
1527 return MultiByteToWideChar( CP_ACP, 0, xguid, -1, str, cmax );
1530 /* open HKCR\\CLSID\\{string form of clsid}\\{keyname} key */
1531 HRESULT COM_OpenKeyForCLSID(REFCLSID clsid, LPCWSTR keyname, REGSAM access, HKEY *subkey)
1533 static const WCHAR wszCLSIDSlash[] = {'C','L','S','I','D','\\',0};
1534 WCHAR path[CHARS_IN_GUID + ARRAYSIZE(wszCLSIDSlash) - 1];
1538 strcpyW(path, wszCLSIDSlash);
1539 StringFromGUID2(clsid, path + strlenW(wszCLSIDSlash), CHARS_IN_GUID);
1540 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, path, 0, keyname ? KEY_READ : access, &key);
1541 if (res == ERROR_FILE_NOT_FOUND)
1542 return REGDB_E_CLASSNOTREG;
1543 else if (res != ERROR_SUCCESS)
1544 return REGDB_E_READREGDB;
1552 res = RegOpenKeyExW(key, keyname, 0, access, subkey);
1554 if (res == ERROR_FILE_NOT_FOUND)
1555 return REGDB_E_KEYMISSING;
1556 else if (res != ERROR_SUCCESS)
1557 return REGDB_E_READREGDB;
1562 /* open HKCR\\AppId\\{string form of appid clsid} key */
1563 HRESULT COM_OpenKeyForAppIdFromCLSID(REFCLSID clsid, REGSAM access, HKEY *subkey)
1565 static const WCHAR szAppId[] = { 'A','p','p','I','d',0 };
1566 static const WCHAR szAppIdKey[] = { 'A','p','p','I','d','\\',0 };
1568 WCHAR buf[CHARS_IN_GUID];
1569 WCHAR keyname[ARRAYSIZE(szAppIdKey) + CHARS_IN_GUID];
1575 /* read the AppID value under the class's key */
1576 hr = COM_OpenKeyForCLSID(clsid, NULL, KEY_READ, &hkey);
1581 res = RegQueryValueExW(hkey, szAppId, NULL, &type, (LPBYTE)buf, &size);
1583 if (res == ERROR_FILE_NOT_FOUND)
1584 return REGDB_E_KEYMISSING;
1585 else if (res != ERROR_SUCCESS || type!=REG_SZ)
1586 return REGDB_E_READREGDB;
1588 strcpyW(keyname, szAppIdKey);
1589 strcatW(keyname, buf);
1590 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, access, subkey);
1591 if (res == ERROR_FILE_NOT_FOUND)
1592 return REGDB_E_KEYMISSING;
1593 else if (res != ERROR_SUCCESS)
1594 return REGDB_E_READREGDB;
1599 /******************************************************************************
1600 * ProgIDFromCLSID [OLE32.@]
1602 * Converts a class id into the respective program ID.
1605 * clsid [I] Class ID, as found in registry.
1606 * ppszProgID [O] Associated ProgID.
1611 * REGDB_E_CLASSNOTREG if the given clsid has no associated ProgID
1613 HRESULT WINAPI ProgIDFromCLSID(REFCLSID clsid, LPOLESTR *ppszProgID)
1615 static const WCHAR wszProgID[] = {'P','r','o','g','I','D',0};
1622 ERR("ppszProgId isn't optional\n");
1623 return E_INVALIDARG;
1627 ret = COM_OpenKeyForCLSID(clsid, wszProgID, KEY_READ, &hkey);
1631 if (RegQueryValueW(hkey, NULL, NULL, &progidlen))
1632 ret = REGDB_E_CLASSNOTREG;
1636 *ppszProgID = CoTaskMemAlloc(progidlen * sizeof(WCHAR));
1639 if (RegQueryValueW(hkey, NULL, *ppszProgID, &progidlen))
1640 ret = REGDB_E_CLASSNOTREG;
1643 ret = E_OUTOFMEMORY;
1650 /******************************************************************************
1651 * CLSIDFromProgID [OLE32.@]
1653 * Converts a program id into the respective GUID.
1656 * progid [I] Unicode program ID, as found in registry.
1657 * clsid [O] Associated CLSID.
1661 * Failure: CO_E_CLASSSTRING - the given ProgID cannot be found.
1663 HRESULT WINAPI CLSIDFromProgID(LPCOLESTR progid, LPCLSID clsid)
1665 static const WCHAR clsidW[] = { '\\','C','L','S','I','D',0 };
1666 WCHAR buf2[CHARS_IN_GUID];
1667 LONG buf2len = sizeof(buf2);
1671 if (!progid || !clsid)
1673 ERR("neither progid (%p) nor clsid (%p) are optional\n", progid, clsid);
1674 return E_INVALIDARG;
1677 /* initialise clsid in case of failure */
1678 memset(clsid, 0, sizeof(*clsid));
1680 buf = HeapAlloc( GetProcessHeap(),0,(strlenW(progid)+8) * sizeof(WCHAR) );
1681 strcpyW( buf, progid );
1682 strcatW( buf, clsidW );
1683 if (RegOpenKeyW(HKEY_CLASSES_ROOT,buf,&xhkey))
1685 HeapFree(GetProcessHeap(),0,buf);
1686 WARN("couldn't open key for ProgID %s\n", debugstr_w(progid));
1687 return CO_E_CLASSSTRING;
1689 HeapFree(GetProcessHeap(),0,buf);
1691 if (RegQueryValueW(xhkey,NULL,buf2,&buf2len))
1694 WARN("couldn't query clsid value for ProgID %s\n", debugstr_w(progid));
1695 return CO_E_CLASSSTRING;
1698 return CLSIDFromString(buf2,clsid);
1702 /*****************************************************************************
1703 * CoGetPSClsid [OLE32.@]
1705 * Retrieves the CLSID of the proxy/stub factory that implements
1706 * IPSFactoryBuffer for the specified interface.
1709 * riid [I] Interface whose proxy/stub CLSID is to be returned.
1710 * pclsid [O] Where to store returned proxy/stub CLSID.
1715 * REGDB_E_IIDNOTREG if no PSFactoryBuffer is associated with the IID, or it could not be parsed
1719 * The standard marshaller activates the object with the CLSID
1720 * returned and uses the CreateProxy and CreateStub methods on its
1721 * IPSFactoryBuffer interface to construct the proxies and stubs for a
1724 * CoGetPSClsid determines this CLSID by searching the
1725 * HKEY_CLASSES_ROOT\Interface\{string form of riid}\ProxyStubClsid32
1726 * in the registry and any interface id registered by
1727 * CoRegisterPSClsid within the current process.
1731 * Native returns S_OK for interfaces with a key in HKCR\Interface, but
1732 * without a ProxyStubClsid32 key and leaves garbage in pclsid. This should be
1733 * considered a bug in native unless an application depends on this (unlikely).
1736 * CoRegisterPSClsid.
1738 HRESULT WINAPI CoGetPSClsid(REFIID riid, CLSID *pclsid)
1740 static const WCHAR wszInterface[] = {'I','n','t','e','r','f','a','c','e','\\',0};
1741 static const WCHAR wszPSC[] = {'\\','P','r','o','x','y','S','t','u','b','C','l','s','i','d','3','2',0};
1742 WCHAR path[ARRAYSIZE(wszInterface) - 1 + CHARS_IN_GUID - 1 + ARRAYSIZE(wszPSC)];
1743 WCHAR value[CHARS_IN_GUID];
1746 APARTMENT *apt = COM_CurrentApt();
1747 struct registered_psclsid *registered_psclsid;
1749 TRACE("() riid=%s, pclsid=%p\n", debugstr_guid(riid), pclsid);
1753 ERR("apartment not initialised\n");
1754 return CO_E_NOTINITIALIZED;
1759 ERR("pclsid isn't optional\n");
1760 return E_INVALIDARG;
1763 EnterCriticalSection(&apt->cs);
1765 LIST_FOR_EACH_ENTRY(registered_psclsid, &apt->psclsids, struct registered_psclsid, entry)
1766 if (IsEqualIID(®istered_psclsid->iid, riid))
1768 *pclsid = registered_psclsid->clsid;
1769 LeaveCriticalSection(&apt->cs);
1773 LeaveCriticalSection(&apt->cs);
1775 /* Interface\\{string form of riid}\\ProxyStubClsid32 */
1776 strcpyW(path, wszInterface);
1777 StringFromGUID2(riid, path + ARRAYSIZE(wszInterface) - 1, CHARS_IN_GUID);
1778 strcpyW(path + ARRAYSIZE(wszInterface) - 1 + CHARS_IN_GUID - 1, wszPSC);
1780 /* Open the key.. */
1781 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, path, 0, KEY_READ, &hkey))
1783 WARN("No PSFactoryBuffer object is registered for IID %s\n", debugstr_guid(riid));
1784 return REGDB_E_IIDNOTREG;
1787 /* ... Once we have the key, query the registry to get the
1788 value of CLSID as a string, and convert it into a
1789 proper CLSID structure to be passed back to the app */
1790 len = sizeof(value);
1791 if (ERROR_SUCCESS != RegQueryValueW(hkey, NULL, value, &len))
1794 return REGDB_E_IIDNOTREG;
1798 /* We have the CLSID we want back from the registry as a string, so
1799 let's convert it into a CLSID structure */
1800 if (CLSIDFromString(value, pclsid) != NOERROR)
1801 return REGDB_E_IIDNOTREG;
1803 TRACE ("() Returning CLSID=%s\n", debugstr_guid(pclsid));
1807 /*****************************************************************************
1808 * CoRegisterPSClsid [OLE32.@]
1810 * Register a proxy/stub CLSID for the given interface in the current process
1814 * riid [I] Interface whose proxy/stub CLSID is to be registered.
1815 * rclsid [I] CLSID of the proxy/stub.
1819 * Failure: E_OUTOFMEMORY
1823 * This function does not add anything to the registry and the effects are
1824 * limited to the lifetime of the current process.
1829 HRESULT WINAPI CoRegisterPSClsid(REFIID riid, REFCLSID rclsid)
1831 APARTMENT *apt = COM_CurrentApt();
1832 struct registered_psclsid *registered_psclsid;
1834 TRACE("(%s, %s)\n", debugstr_guid(riid), debugstr_guid(rclsid));
1838 ERR("apartment not initialised\n");
1839 return CO_E_NOTINITIALIZED;
1842 EnterCriticalSection(&apt->cs);
1844 LIST_FOR_EACH_ENTRY(registered_psclsid, &apt->psclsids, struct registered_psclsid, entry)
1845 if (IsEqualIID(®istered_psclsid->iid, riid))
1847 registered_psclsid->clsid = *rclsid;
1848 LeaveCriticalSection(&apt->cs);
1852 registered_psclsid = HeapAlloc(GetProcessHeap(), 0, sizeof(struct registered_psclsid));
1853 if (!registered_psclsid)
1855 LeaveCriticalSection(&apt->cs);
1856 return E_OUTOFMEMORY;
1859 registered_psclsid->iid = *riid;
1860 registered_psclsid->clsid = *rclsid;
1861 list_add_head(&apt->psclsids, ®istered_psclsid->entry);
1863 LeaveCriticalSection(&apt->cs);
1870 * COM_GetRegisteredClassObject
1872 * This internal method is used to scan the registered class list to
1873 * find a class object.
1876 * rclsid Class ID of the class to find.
1877 * dwClsContext Class context to match.
1878 * ppv [out] returns a pointer to the class object. Complying
1879 * to normal COM usage, this method will increase the
1880 * reference count on this object.
1882 static HRESULT COM_GetRegisteredClassObject(const struct apartment *apt, REFCLSID rclsid,
1883 DWORD dwClsContext, LPUNKNOWN* ppUnk)
1885 HRESULT hr = S_FALSE;
1886 RegisteredClass *curClass;
1888 EnterCriticalSection( &csRegisteredClassList );
1890 LIST_FOR_EACH_ENTRY(curClass, &RegisteredClassList, RegisteredClass, entry)
1893 * Check if we have a match on the class ID and context.
1895 if ((apt->oxid == curClass->apartment_id) &&
1896 (dwClsContext & curClass->runContext) &&
1897 IsEqualGUID(&(curClass->classIdentifier), rclsid))
1900 * We have a match, return the pointer to the class object.
1902 *ppUnk = curClass->classObject;
1904 IUnknown_AddRef(curClass->classObject);
1911 LeaveCriticalSection( &csRegisteredClassList );
1916 /******************************************************************************
1917 * CoRegisterClassObject [OLE32.@]
1919 * Registers the class object for a given class ID. Servers housed in EXE
1920 * files use this method instead of exporting DllGetClassObject to allow
1921 * other code to connect to their objects.
1924 * rclsid [I] CLSID of the object to register.
1925 * pUnk [I] IUnknown of the object.
1926 * dwClsContext [I] CLSCTX flags indicating the context in which to run the executable.
1927 * flags [I] REGCLS flags indicating how connections are made.
1928 * lpdwRegister [I] A unique cookie that can be passed to CoRevokeClassObject.
1932 * E_INVALIDARG if lpdwRegister or pUnk are NULL,
1933 * CO_E_OBJISREG if the object is already registered. We should not return this.
1936 * CoRevokeClassObject, CoGetClassObject
1939 * In-process objects are only registered for the current apartment.
1940 * CoGetClassObject() and CoCreateInstance() will not return objects registered
1941 * in other apartments.
1944 * MSDN claims that multiple interface registrations are legal, but we
1945 * can't do that with our current implementation.
1947 HRESULT WINAPI CoRegisterClassObject(
1952 LPDWORD lpdwRegister)
1954 RegisteredClass* newClass;
1955 LPUNKNOWN foundObject;
1959 TRACE("(%s,%p,0x%08x,0x%08x,%p)\n",
1960 debugstr_guid(rclsid),pUnk,dwClsContext,flags,lpdwRegister);
1962 if ( (lpdwRegister==0) || (pUnk==0) )
1963 return E_INVALIDARG;
1965 apt = COM_CurrentApt();
1968 ERR("COM was not initialized\n");
1969 return CO_E_NOTINITIALIZED;
1974 /* REGCLS_MULTIPLEUSE implies registering as inproc server. This is what
1975 * differentiates the flag from REGCLS_MULTI_SEPARATE. */
1976 if (flags & REGCLS_MULTIPLEUSE)
1977 dwClsContext |= CLSCTX_INPROC_SERVER;
1980 * First, check if the class is already registered.
1981 * If it is, this should cause an error.
1983 hr = COM_GetRegisteredClassObject(apt, rclsid, dwClsContext, &foundObject);
1985 if (flags & REGCLS_MULTIPLEUSE) {
1986 if (dwClsContext & CLSCTX_LOCAL_SERVER)
1987 hr = CoLockObjectExternal(foundObject, TRUE, FALSE);
1988 IUnknown_Release(foundObject);
1991 IUnknown_Release(foundObject);
1992 ERR("object already registered for class %s\n", debugstr_guid(rclsid));
1993 return CO_E_OBJISREG;
1996 newClass = HeapAlloc(GetProcessHeap(), 0, sizeof(RegisteredClass));
1997 if ( newClass == NULL )
1998 return E_OUTOFMEMORY;
2000 newClass->classIdentifier = *rclsid;
2001 newClass->apartment_id = apt->oxid;
2002 newClass->runContext = dwClsContext;
2003 newClass->connectFlags = flags;
2004 newClass->pMarshaledData = NULL;
2005 newClass->RpcRegistration = NULL;
2008 * Use the address of the chain node as the cookie since we are sure it's
2009 * unique. FIXME: not on 64-bit platforms.
2011 newClass->dwCookie = (DWORD)newClass;
2014 * Since we're making a copy of the object pointer, we have to increase its
2017 newClass->classObject = pUnk;
2018 IUnknown_AddRef(newClass->classObject);
2020 EnterCriticalSection( &csRegisteredClassList );
2021 list_add_tail(&RegisteredClassList, &newClass->entry);
2022 LeaveCriticalSection( &csRegisteredClassList );
2024 *lpdwRegister = newClass->dwCookie;
2026 if (dwClsContext & CLSCTX_LOCAL_SERVER) {
2027 hr = CreateStreamOnHGlobal(0, TRUE, &newClass->pMarshaledData);
2029 FIXME("Failed to create stream on hglobal, %x\n", hr);
2032 hr = CoMarshalInterface(newClass->pMarshaledData, &IID_IClassFactory,
2033 newClass->classObject, MSHCTX_LOCAL, NULL,
2034 MSHLFLAGS_TABLESTRONG);
2036 FIXME("CoMarshalInterface failed, %x!\n",hr);
2040 hr = RPC_StartLocalServer(&newClass->classIdentifier,
2041 newClass->pMarshaledData,
2042 flags & (REGCLS_MULTIPLEUSE|REGCLS_MULTI_SEPARATE),
2043 &newClass->RpcRegistration);
2048 static void COM_RevokeRegisteredClassObject(RegisteredClass *curClass)
2050 list_remove(&curClass->entry);
2052 if (curClass->runContext & CLSCTX_LOCAL_SERVER)
2053 RPC_StopLocalServer(curClass->RpcRegistration);
2056 * Release the reference to the class object.
2058 IUnknown_Release(curClass->classObject);
2060 if (curClass->pMarshaledData)
2063 memset(&zero, 0, sizeof(zero));
2064 IStream_Seek(curClass->pMarshaledData, zero, STREAM_SEEK_SET, NULL);
2065 CoReleaseMarshalData(curClass->pMarshaledData);
2066 IStream_Release(curClass->pMarshaledData);
2069 HeapFree(GetProcessHeap(), 0, curClass);
2072 static void COM_RevokeAllClasses(const struct apartment *apt)
2074 RegisteredClass *curClass, *cursor;
2076 EnterCriticalSection( &csRegisteredClassList );
2078 LIST_FOR_EACH_ENTRY_SAFE(curClass, cursor, &RegisteredClassList, RegisteredClass, entry)
2080 if (curClass->apartment_id == apt->oxid)
2081 COM_RevokeRegisteredClassObject(curClass);
2084 LeaveCriticalSection( &csRegisteredClassList );
2087 /***********************************************************************
2088 * CoRevokeClassObject [OLE32.@]
2090 * Removes a class object from the class registry.
2093 * dwRegister [I] Cookie returned from CoRegisterClassObject().
2097 * Failure: HRESULT code.
2100 * Must be called from the same apartment that called CoRegisterClassObject(),
2101 * otherwise it will fail with RPC_E_WRONG_THREAD.
2104 * CoRegisterClassObject
2106 HRESULT WINAPI CoRevokeClassObject(
2109 HRESULT hr = E_INVALIDARG;
2110 RegisteredClass *curClass;
2113 TRACE("(%08x)\n",dwRegister);
2115 apt = COM_CurrentApt();
2118 ERR("COM was not initialized\n");
2119 return CO_E_NOTINITIALIZED;
2122 EnterCriticalSection( &csRegisteredClassList );
2124 LIST_FOR_EACH_ENTRY(curClass, &RegisteredClassList, RegisteredClass, entry)
2127 * Check if we have a match on the cookie.
2129 if (curClass->dwCookie == dwRegister)
2131 if (curClass->apartment_id == apt->oxid)
2133 COM_RevokeRegisteredClassObject(curClass);
2138 ERR("called from wrong apartment, should be called from %s\n",
2139 wine_dbgstr_longlong(curClass->apartment_id));
2140 hr = RPC_E_WRONG_THREAD;
2146 LeaveCriticalSection( &csRegisteredClassList );
2151 /***********************************************************************
2152 * COM_RegReadPath [internal]
2154 * Reads a registry value and expands it when necessary
2156 static DWORD COM_RegReadPath(HKEY hkeyroot, const WCHAR *keyname, const WCHAR *valuename, WCHAR * dst, DWORD dstlen)
2161 WCHAR src[MAX_PATH];
2162 DWORD dwLength = dstlen * sizeof(WCHAR);
2164 if((ret = RegOpenKeyExW(hkeyroot, keyname, 0, KEY_READ, &key)) == ERROR_SUCCESS) {
2165 if( (ret = RegQueryValueExW(key, NULL, NULL, &keytype, (LPBYTE)src, &dwLength)) == ERROR_SUCCESS ) {
2166 if (keytype == REG_EXPAND_SZ) {
2167 if (dstlen <= ExpandEnvironmentStringsW(src, dst, dstlen)) ret = ERROR_MORE_DATA;
2169 lstrcpynW(dst, src, dstlen);
2177 static void get_threading_model(HKEY key, LPWSTR value, DWORD len)
2179 static const WCHAR wszThreadingModel[] = {'T','h','r','e','a','d','i','n','g','M','o','d','e','l',0};
2182 DWORD dwLength = len * sizeof(WCHAR);
2184 ret = RegQueryValueExW(key, wszThreadingModel, NULL, &keytype, (LPBYTE)value, &dwLength);
2185 if ((ret != ERROR_SUCCESS) || (keytype != REG_SZ))
2189 static HRESULT get_inproc_class_object(APARTMENT *apt, HKEY hkeydll,
2190 REFCLSID rclsid, REFIID riid,
2191 BOOL hostifnecessary, void **ppv)
2193 WCHAR dllpath[MAX_PATH+1];
2194 BOOL apartment_threaded;
2196 if (hostifnecessary)
2198 static const WCHAR wszApartment[] = {'A','p','a','r','t','m','e','n','t',0};
2199 static const WCHAR wszFree[] = {'F','r','e','e',0};
2200 static const WCHAR wszBoth[] = {'B','o','t','h',0};
2201 WCHAR threading_model[10 /* strlenW(L"apartment")+1 */];
2203 get_threading_model(hkeydll, threading_model, ARRAYSIZE(threading_model));
2205 if (!strcmpiW(threading_model, wszApartment))
2207 apartment_threaded = TRUE;
2208 if (apt->multi_threaded)
2209 return apartment_hostobject_in_hostapt(apt, FALSE, FALSE, hkeydll, rclsid, riid, ppv);
2212 else if (!strcmpiW(threading_model, wszFree))
2214 apartment_threaded = FALSE;
2215 if (!apt->multi_threaded)
2216 return apartment_hostobject_in_hostapt(apt, TRUE, FALSE, hkeydll, rclsid, riid, ppv);
2218 /* everything except "Apartment", "Free" and "Both" */
2219 else if (strcmpiW(threading_model, wszBoth))
2221 apartment_threaded = TRUE;
2222 /* everything else is main-threaded */
2223 if (threading_model[0])
2224 FIXME("unrecognised threading model %s for object %s, should be main-threaded?\n",
2225 debugstr_w(threading_model), debugstr_guid(rclsid));
2227 if (apt->multi_threaded || !apt->main)
2228 return apartment_hostobject_in_hostapt(apt, FALSE, TRUE, hkeydll, rclsid, riid, ppv);
2231 apartment_threaded = FALSE;
2234 apartment_threaded = !apt->multi_threaded;
2236 if (COM_RegReadPath(hkeydll, NULL, NULL, dllpath, ARRAYSIZE(dllpath)) != ERROR_SUCCESS)
2238 /* failure: CLSID is not found in registry */
2239 WARN("class %s not registered inproc\n", debugstr_guid(rclsid));
2240 return REGDB_E_CLASSNOTREG;
2243 return apartment_getclassobject(apt, dllpath, apartment_threaded,
2247 /***********************************************************************
2248 * CoGetClassObject [OLE32.@]
2250 * Creates an object of the specified class.
2253 * rclsid [I] Class ID to create an instance of.
2254 * dwClsContext [I] Flags to restrict the location of the created instance.
2255 * pServerInfo [I] Optional. Details for connecting to a remote server.
2256 * iid [I] The ID of the interface of the instance to return.
2257 * ppv [O] On returns, contains a pointer to the specified interface of the object.
2261 * Failure: HRESULT code.
2264 * The dwClsContext parameter can be one or more of the following:
2265 *| CLSCTX_INPROC_SERVER - Use an in-process server, such as from a DLL.
2266 *| CLSCTX_INPROC_HANDLER - Use an in-process object which handles certain functions for an object running in another process.
2267 *| CLSCTX_LOCAL_SERVER - Connect to an object running in another process.
2268 *| CLSCTX_REMOTE_SERVER - Connect to an object running on another machine.
2271 * CoCreateInstance()
2273 HRESULT WINAPI CoGetClassObject(
2274 REFCLSID rclsid, DWORD dwClsContext, COSERVERINFO *pServerInfo,
2275 REFIID iid, LPVOID *ppv)
2277 LPUNKNOWN regClassObject;
2278 HRESULT hres = E_UNEXPECTED;
2281 TRACE("\n\tCLSID:\t%s,\n\tIID:\t%s\n", debugstr_guid(rclsid), debugstr_guid(iid));
2284 return E_INVALIDARG;
2288 apt = COM_CurrentApt();
2291 ERR("apartment not initialised\n");
2292 return CO_E_NOTINITIALIZED;
2296 FIXME("\tpServerInfo: name=%s\n",debugstr_w(pServerInfo->pwszName));
2297 FIXME("\t\tpAuthInfo=%p\n",pServerInfo->pAuthInfo);
2301 * First, try and see if we can't match the class ID with one of the
2302 * registered classes.
2304 if (S_OK == COM_GetRegisteredClassObject(apt, rclsid, dwClsContext,
2307 /* Get the required interface from the retrieved pointer. */
2308 hres = IUnknown_QueryInterface(regClassObject, iid, ppv);
2311 * Since QI got another reference on the pointer, we want to release the
2312 * one we already have. If QI was unsuccessful, this will release the object. This
2313 * is good since we are not returning it in the "out" parameter.
2315 IUnknown_Release(regClassObject);
2320 /* First try in-process server */
2321 if (CLSCTX_INPROC_SERVER & dwClsContext)
2323 static const WCHAR wszInprocServer32[] = {'I','n','p','r','o','c','S','e','r','v','e','r','3','2',0};
2326 if (IsEqualCLSID(rclsid, &CLSID_InProcFreeMarshaler))
2327 return FTMarshalCF_Create(iid, ppv);
2329 hres = COM_OpenKeyForCLSID(rclsid, wszInprocServer32, KEY_READ, &hkey);
2332 if (hres == REGDB_E_CLASSNOTREG)
2333 ERR("class %s not registered\n", debugstr_guid(rclsid));
2334 else if (hres == REGDB_E_KEYMISSING)
2336 WARN("class %s not registered as in-proc server\n", debugstr_guid(rclsid));
2337 hres = REGDB_E_CLASSNOTREG;
2341 if (SUCCEEDED(hres))
2343 hres = get_inproc_class_object(apt, hkey, rclsid, iid,
2344 !(dwClsContext & WINE_CLSCTX_DONT_HOST), ppv);
2348 /* return if we got a class, otherwise fall through to one of the
2350 if (SUCCEEDED(hres))
2354 /* Next try in-process handler */
2355 if (CLSCTX_INPROC_HANDLER & dwClsContext)
2357 static const WCHAR wszInprocHandler32[] = {'I','n','p','r','o','c','H','a','n','d','l','e','r','3','2',0};
2360 hres = COM_OpenKeyForCLSID(rclsid, wszInprocHandler32, KEY_READ, &hkey);
2363 if (hres == REGDB_E_CLASSNOTREG)
2364 ERR("class %s not registered\n", debugstr_guid(rclsid));
2365 else if (hres == REGDB_E_KEYMISSING)
2367 WARN("class %s not registered in-proc handler\n", debugstr_guid(rclsid));
2368 hres = REGDB_E_CLASSNOTREG;
2372 if (SUCCEEDED(hres))
2374 hres = get_inproc_class_object(apt, hkey, rclsid, iid,
2375 !(dwClsContext & WINE_CLSCTX_DONT_HOST), ppv);
2379 /* return if we got a class, otherwise fall through to one of the
2381 if (SUCCEEDED(hres))
2385 /* Next try out of process */
2386 if (CLSCTX_LOCAL_SERVER & dwClsContext)
2388 hres = RPC_GetLocalClassObject(rclsid,iid,ppv);
2389 if (SUCCEEDED(hres))
2393 /* Finally try remote: this requires networked DCOM (a lot of work) */
2394 if (CLSCTX_REMOTE_SERVER & dwClsContext)
2396 FIXME ("CLSCTX_REMOTE_SERVER not supported\n");
2397 hres = E_NOINTERFACE;
2401 ERR("no class object %s could be created for context 0x%x\n",
2402 debugstr_guid(rclsid), dwClsContext);
2406 /***********************************************************************
2407 * CoResumeClassObjects (OLE32.@)
2409 * Resumes all class objects registered with REGCLS_SUSPENDED.
2413 * Failure: HRESULT code.
2415 HRESULT WINAPI CoResumeClassObjects(void)
2421 /***********************************************************************
2422 * CoCreateInstance [OLE32.@]
2424 * Creates an instance of the specified class.
2427 * rclsid [I] Class ID to create an instance of.
2428 * pUnkOuter [I] Optional outer unknown to allow aggregation with another object.
2429 * dwClsContext [I] Flags to restrict the location of the created instance.
2430 * iid [I] The ID of the interface of the instance to return.
2431 * ppv [O] On returns, contains a pointer to the specified interface of the instance.
2435 * Failure: HRESULT code.
2438 * The dwClsContext parameter can be one or more of the following:
2439 *| CLSCTX_INPROC_SERVER - Use an in-process server, such as from a DLL.
2440 *| CLSCTX_INPROC_HANDLER - Use an in-process object which handles certain functions for an object running in another process.
2441 *| CLSCTX_LOCAL_SERVER - Connect to an object running in another process.
2442 *| CLSCTX_REMOTE_SERVER - Connect to an object running on another machine.
2444 * Aggregation is the concept of deferring the IUnknown of an object to another
2445 * object. This allows a separate object to behave as though it was part of
2446 * the object and to allow this the pUnkOuter parameter can be set. Note that
2447 * not all objects support having an outer of unknown.
2450 * CoGetClassObject()
2452 HRESULT WINAPI CoCreateInstance(
2454 LPUNKNOWN pUnkOuter,
2460 LPCLASSFACTORY lpclf = 0;
2462 TRACE("(rclsid=%s, pUnkOuter=%p, dwClsContext=%08x, riid=%s, ppv=%p)\n", debugstr_guid(rclsid),
2463 pUnkOuter, dwClsContext, debugstr_guid(iid), ppv);
2472 * Initialize the "out" parameter
2476 if (!COM_CurrentApt())
2478 ERR("apartment not initialised\n");
2479 return CO_E_NOTINITIALIZED;
2483 * The Standard Global Interface Table (GIT) object is a process-wide singleton.
2484 * Rather than create a class factory, we can just check for it here
2486 if (IsEqualIID(rclsid, &CLSID_StdGlobalInterfaceTable)) {
2487 if (StdGlobalInterfaceTableInstance == NULL)
2488 StdGlobalInterfaceTableInstance = StdGlobalInterfaceTable_Construct();
2489 hres = IGlobalInterfaceTable_QueryInterface( (IGlobalInterfaceTable*) StdGlobalInterfaceTableInstance, iid, ppv);
2490 if (hres) return hres;
2492 TRACE("Retrieved GIT (%p)\n", *ppv);
2497 * Get a class factory to construct the object we want.
2499 hres = CoGetClassObject(rclsid,
2509 * Create the object and don't forget to release the factory
2511 hres = IClassFactory_CreateInstance(lpclf, pUnkOuter, iid, ppv);
2512 IClassFactory_Release(lpclf);
2515 if (hres == CLASS_E_NOAGGREGATION && pUnkOuter)
2516 FIXME("Class %s does not support aggregation\n", debugstr_guid(rclsid));
2518 FIXME("no instance created for interface %s of class %s, hres is 0x%08x\n", debugstr_guid(iid), debugstr_guid(rclsid),hres);
2524 /***********************************************************************
2525 * CoCreateInstanceEx [OLE32.@]
2527 HRESULT WINAPI CoCreateInstanceEx(
2529 LPUNKNOWN pUnkOuter,
2531 COSERVERINFO* pServerInfo,
2535 IUnknown* pUnk = NULL;
2538 ULONG successCount = 0;
2543 if ( (cmq==0) || (pResults==NULL))
2544 return E_INVALIDARG;
2546 if (pServerInfo!=NULL)
2547 FIXME("() non-NULL pServerInfo not supported!\n");
2550 * Initialize all the "out" parameters.
2552 for (index = 0; index < cmq; index++)
2554 pResults[index].pItf = NULL;
2555 pResults[index].hr = E_NOINTERFACE;
2559 * Get the object and get its IUnknown pointer.
2561 hr = CoCreateInstance(rclsid,
2571 * Then, query for all the interfaces requested.
2573 for (index = 0; index < cmq; index++)
2575 pResults[index].hr = IUnknown_QueryInterface(pUnk,
2576 pResults[index].pIID,
2577 (VOID**)&(pResults[index].pItf));
2579 if (pResults[index].hr == S_OK)
2584 * Release our temporary unknown pointer.
2586 IUnknown_Release(pUnk);
2588 if (successCount == 0)
2589 return E_NOINTERFACE;
2591 if (successCount!=cmq)
2592 return CO_S_NOTALLINTERFACES;
2597 /***********************************************************************
2598 * CoLoadLibrary (OLE32.@)
2603 * lpszLibName [I] Path to library.
2604 * bAutoFree [I] Whether the library should automatically be freed.
2607 * Success: Handle to loaded library.
2611 * CoFreeLibrary, CoFreeAllLibraries, CoFreeUnusedLibraries
2613 HINSTANCE WINAPI CoLoadLibrary(LPOLESTR lpszLibName, BOOL bAutoFree)
2615 TRACE("(%s, %d)\n", debugstr_w(lpszLibName), bAutoFree);
2617 return LoadLibraryExW(lpszLibName, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
2620 /***********************************************************************
2621 * CoFreeLibrary [OLE32.@]
2623 * Unloads a library from memory.
2626 * hLibrary [I] Handle to library to unload.
2632 * CoLoadLibrary, CoFreeAllLibraries, CoFreeUnusedLibraries
2634 void WINAPI CoFreeLibrary(HINSTANCE hLibrary)
2636 FreeLibrary(hLibrary);
2640 /***********************************************************************
2641 * CoFreeAllLibraries [OLE32.@]
2643 * Function for backwards compatibility only. Does nothing.
2649 * CoLoadLibrary, CoFreeLibrary, CoFreeUnusedLibraries
2651 void WINAPI CoFreeAllLibraries(void)
2656 /***********************************************************************
2657 * CoFreeUnusedLibrariesEx [OLE32.@]
2659 * Frees any previously unused libraries whose delay has expired and marks
2660 * currently unused libraries for unloading. Unused are identified as those that
2661 * return S_OK from their DllCanUnloadNow function.
2664 * dwUnloadDelay [I] Unload delay in milliseconds.
2665 * dwReserved [I] Reserved. Set to 0.
2671 * CoLoadLibrary, CoFreeAllLibraries, CoFreeLibrary
2673 void WINAPI CoFreeUnusedLibrariesEx(DWORD dwUnloadDelay, DWORD dwReserved)
2675 struct apartment *apt = COM_CurrentApt();
2678 ERR("apartment not initialised\n");
2682 apartment_freeunusedlibraries(apt, dwUnloadDelay);
2685 /***********************************************************************
2686 * CoFreeUnusedLibraries [OLE32.@]
2687 * CoFreeUnusedLibraries [COMPOBJ.17]
2689 * Frees any unused libraries. Unused are identified as those that return
2690 * S_OK from their DllCanUnloadNow function.
2696 * CoLoadLibrary, CoFreeAllLibraries, CoFreeLibrary
2698 void WINAPI CoFreeUnusedLibraries(void)
2700 CoFreeUnusedLibrariesEx(INFINITE, 0);
2703 /***********************************************************************
2704 * CoFileTimeNow [OLE32.@]
2705 * CoFileTimeNow [COMPOBJ.82]
2707 * Retrieves the current time in FILETIME format.
2710 * lpFileTime [O] The current time.
2715 HRESULT WINAPI CoFileTimeNow( FILETIME *lpFileTime )
2717 GetSystemTimeAsFileTime( lpFileTime );
2721 /******************************************************************************
2722 * CoLockObjectExternal [OLE32.@]
2724 * Increments or decrements the external reference count of a stub object.
2727 * pUnk [I] Stub object.
2728 * fLock [I] If TRUE then increments the external ref-count,
2729 * otherwise decrements.
2730 * fLastUnlockReleases [I] If TRUE then the last unlock has the effect of
2731 * calling CoDisconnectObject.
2735 * Failure: HRESULT code.
2738 * If fLock is TRUE and an object is passed in that doesn't have a stub
2739 * manager then a new stub manager is created for the object.
2741 HRESULT WINAPI CoLockObjectExternal(
2744 BOOL fLastUnlockReleases)
2746 struct stub_manager *stubmgr;
2747 struct apartment *apt;
2749 TRACE("pUnk=%p, fLock=%s, fLastUnlockReleases=%s\n",
2750 pUnk, fLock ? "TRUE" : "FALSE", fLastUnlockReleases ? "TRUE" : "FALSE");
2752 apt = COM_CurrentApt();
2753 if (!apt) return CO_E_NOTINITIALIZED;
2755 stubmgr = get_stub_manager_from_object(apt, pUnk);
2760 stub_manager_ext_addref(stubmgr, 1, FALSE);
2762 stub_manager_ext_release(stubmgr, 1, FALSE, fLastUnlockReleases);
2764 stub_manager_int_release(stubmgr);
2770 stubmgr = new_stub_manager(apt, pUnk);
2774 stub_manager_ext_addref(stubmgr, 1, FALSE);
2775 stub_manager_int_release(stubmgr);
2782 WARN("stub object not found %p\n", pUnk);
2783 /* Note: native is pretty broken here because it just silently
2784 * fails, without returning an appropriate error code, making apps
2785 * think that the object was disconnected, when it actually wasn't */
2790 /***********************************************************************
2791 * CoInitializeWOW (OLE32.@)
2793 * WOW equivalent of CoInitialize?
2802 HRESULT WINAPI CoInitializeWOW(DWORD x,DWORD y)
2804 FIXME("(0x%08x,0x%08x),stub!\n",x,y);
2808 /***********************************************************************
2809 * CoGetState [OLE32.@]
2811 * Retrieves the thread state object previously stored by CoSetState().
2814 * ppv [I] Address where pointer to object will be stored.
2818 * Failure: E_OUTOFMEMORY.
2821 * Crashes on all invalid ppv addresses, including NULL.
2822 * If the function returns a non-NULL object then the caller must release its
2823 * reference on the object when the object is no longer required.
2828 HRESULT WINAPI CoGetState(IUnknown ** ppv)
2830 struct oletls *info = COM_CurrentInfo();
2831 if (!info) return E_OUTOFMEMORY;
2837 IUnknown_AddRef(info->state);
2839 TRACE("apt->state=%p\n", info->state);
2845 /***********************************************************************
2846 * CoSetState [OLE32.@]
2848 * Sets the thread state object.
2851 * pv [I] Pointer to state object to be stored.
2854 * The system keeps a reference on the object while the object stored.
2858 * Failure: E_OUTOFMEMORY.
2860 HRESULT WINAPI CoSetState(IUnknown * pv)
2862 struct oletls *info = COM_CurrentInfo();
2863 if (!info) return E_OUTOFMEMORY;
2865 if (pv) IUnknown_AddRef(pv);
2869 TRACE("-- release %p now\n", info->state);
2870 IUnknown_Release(info->state);
2879 /******************************************************************************
2880 * CoTreatAsClass [OLE32.@]
2882 * Sets the TreatAs value of a class.
2885 * clsidOld [I] Class to set TreatAs value on.
2886 * clsidNew [I] The class the clsidOld should be treated as.
2890 * Failure: HRESULT code.
2895 HRESULT WINAPI CoTreatAsClass(REFCLSID clsidOld, REFCLSID clsidNew)
2897 static const WCHAR wszAutoTreatAs[] = {'A','u','t','o','T','r','e','a','t','A','s',0};
2898 static const WCHAR wszTreatAs[] = {'T','r','e','a','t','A','s',0};
2900 WCHAR szClsidNew[CHARS_IN_GUID];
2902 WCHAR auto_treat_as[CHARS_IN_GUID];
2903 LONG auto_treat_as_size = sizeof(auto_treat_as);
2906 res = COM_OpenKeyForCLSID(clsidOld, NULL, KEY_READ | KEY_WRITE, &hkey);
2909 if (!memcmp( clsidOld, clsidNew, sizeof(*clsidOld) ))
2911 if (!RegQueryValueW(hkey, wszAutoTreatAs, auto_treat_as, &auto_treat_as_size) &&
2912 CLSIDFromString(auto_treat_as, &id) == S_OK)
2914 if (RegSetValueW(hkey, wszTreatAs, REG_SZ, auto_treat_as, sizeof(auto_treat_as)))
2916 res = REGDB_E_WRITEREGDB;
2922 RegDeleteKeyW(hkey, wszTreatAs);
2926 else if (!StringFromGUID2(clsidNew, szClsidNew, ARRAYSIZE(szClsidNew)) &&
2927 !RegSetValueW(hkey, wszTreatAs, REG_SZ, szClsidNew, sizeof(szClsidNew)))
2929 res = REGDB_E_WRITEREGDB;
2934 if (hkey) RegCloseKey(hkey);
2938 /******************************************************************************
2939 * CoGetTreatAsClass [OLE32.@]
2941 * Gets the TreatAs value of a class.
2944 * clsidOld [I] Class to get the TreatAs value of.
2945 * clsidNew [I] The class the clsidOld should be treated as.
2949 * Failure: HRESULT code.
2954 HRESULT WINAPI CoGetTreatAsClass(REFCLSID clsidOld, LPCLSID clsidNew)
2956 static const WCHAR wszTreatAs[] = {'T','r','e','a','t','A','s',0};
2958 WCHAR szClsidNew[CHARS_IN_GUID];
2960 LONG len = sizeof(szClsidNew);
2962 FIXME("(%s,%p)\n", debugstr_guid(clsidOld), clsidNew);
2963 *clsidNew = *clsidOld; /* copy over old value */
2965 res = COM_OpenKeyForCLSID(clsidOld, wszTreatAs, KEY_READ, &hkey);
2968 if (RegQueryValueW(hkey, NULL, szClsidNew, &len))
2973 res = CLSIDFromString(szClsidNew,clsidNew);
2975 ERR("Failed CLSIDFromStringA(%s), hres 0x%08x\n", debugstr_w(szClsidNew), res);
2977 if (hkey) RegCloseKey(hkey);
2981 /******************************************************************************
2982 * CoGetCurrentProcess [OLE32.@]
2983 * CoGetCurrentProcess [COMPOBJ.34]
2985 * Gets the current process ID.
2988 * The current process ID.
2991 * Is DWORD really the correct return type for this function?
2993 DWORD WINAPI CoGetCurrentProcess(void)
2995 return GetCurrentProcessId();
2998 /******************************************************************************
2999 * CoRegisterMessageFilter [OLE32.@]
3001 * Registers a message filter.
3004 * lpMessageFilter [I] Pointer to interface.
3005 * lplpMessageFilter [O] Indirect pointer to prior instance if non-NULL.
3009 * Failure: HRESULT code.
3012 * Both lpMessageFilter and lplpMessageFilter are optional. Passing in a NULL
3013 * lpMessageFilter removes the message filter.
3015 * If lplpMessageFilter is not NULL the previous message filter will be
3016 * returned in the memory pointer to this parameter and the caller is
3017 * responsible for releasing the object.
3019 * The current thread be in an apartment otherwise the function will crash.
3021 HRESULT WINAPI CoRegisterMessageFilter(
3022 LPMESSAGEFILTER lpMessageFilter,
3023 LPMESSAGEFILTER *lplpMessageFilter)
3025 struct apartment *apt;
3026 IMessageFilter *lpOldMessageFilter;
3028 TRACE("(%p, %p)\n", lpMessageFilter, lplpMessageFilter);
3030 apt = COM_CurrentApt();
3032 /* can't set a message filter in a multi-threaded apartment */
3033 if (!apt || apt->multi_threaded)
3035 WARN("can't set message filter in MTA or uninitialized apt\n");
3036 return CO_E_NOT_SUPPORTED;
3039 if (lpMessageFilter)
3040 IMessageFilter_AddRef(lpMessageFilter);
3042 EnterCriticalSection(&apt->cs);
3044 lpOldMessageFilter = apt->filter;
3045 apt->filter = lpMessageFilter;
3047 LeaveCriticalSection(&apt->cs);
3049 if (lplpMessageFilter)
3050 *lplpMessageFilter = lpOldMessageFilter;
3051 else if (lpOldMessageFilter)
3052 IMessageFilter_Release(lpOldMessageFilter);
3057 /***********************************************************************
3058 * CoIsOle1Class [OLE32.@]
3060 * Determines whether the specified class an OLE v1 class.
3063 * clsid [I] Class to test.
3066 * TRUE if the class is an OLE v1 class, or FALSE otherwise.
3068 BOOL WINAPI CoIsOle1Class(REFCLSID clsid)
3070 FIXME("%s\n", debugstr_guid(clsid));
3074 /***********************************************************************
3075 * IsEqualGUID [OLE32.@]
3077 * Compares two Unique Identifiers.
3080 * rguid1 [I] The first GUID to compare.
3081 * rguid2 [I] The other GUID to compare.
3087 BOOL WINAPI IsEqualGUID(
3091 return !memcmp(rguid1,rguid2,sizeof(GUID));
3094 /***********************************************************************
3095 * CoInitializeSecurity [OLE32.@]
3097 HRESULT WINAPI CoInitializeSecurity(PSECURITY_DESCRIPTOR pSecDesc, LONG cAuthSvc,
3098 SOLE_AUTHENTICATION_SERVICE* asAuthSvc,
3099 void* pReserved1, DWORD dwAuthnLevel,
3100 DWORD dwImpLevel, void* pReserved2,
3101 DWORD dwCapabilities, void* pReserved3)
3103 FIXME("(%p,%d,%p,%p,%d,%d,%p,%d,%p) - stub!\n", pSecDesc, cAuthSvc,
3104 asAuthSvc, pReserved1, dwAuthnLevel, dwImpLevel, pReserved2,
3105 dwCapabilities, pReserved3);
3109 /***********************************************************************
3110 * CoSuspendClassObjects [OLE32.@]
3112 * Suspends all registered class objects to prevent further requests coming in
3113 * for those objects.
3117 * Failure: HRESULT code.
3119 HRESULT WINAPI CoSuspendClassObjects(void)
3125 /***********************************************************************
3126 * CoAddRefServerProcess [OLE32.@]
3128 * Helper function for incrementing the reference count of a local-server
3132 * New reference count.
3135 * CoReleaseServerProcess().
3137 ULONG WINAPI CoAddRefServerProcess(void)
3143 EnterCriticalSection(&csRegisteredClassList);
3144 refs = ++s_COMServerProcessReferences;
3145 LeaveCriticalSection(&csRegisteredClassList);
3147 TRACE("refs before: %d\n", refs - 1);
3152 /***********************************************************************
3153 * CoReleaseServerProcess [OLE32.@]
3155 * Helper function for decrementing the reference count of a local-server
3159 * New reference count.
3162 * When reference count reaches 0, this function suspends all registered
3163 * classes so no new connections are accepted.
3166 * CoAddRefServerProcess(), CoSuspendClassObjects().
3168 ULONG WINAPI CoReleaseServerProcess(void)
3174 EnterCriticalSection(&csRegisteredClassList);
3176 refs = --s_COMServerProcessReferences;
3177 /* FIXME: if (!refs) COM_SuspendClassObjects(); */
3179 LeaveCriticalSection(&csRegisteredClassList);
3181 TRACE("refs after: %d\n", refs);
3186 /***********************************************************************
3187 * CoIsHandlerConnected [OLE32.@]
3189 * Determines whether a proxy is connected to a remote stub.
3192 * pUnk [I] Pointer to object that may or may not be connected.
3195 * TRUE if pUnk is not a proxy or if pUnk is connected to a remote stub, or
3198 BOOL WINAPI CoIsHandlerConnected(IUnknown *pUnk)
3200 FIXME("%p\n", pUnk);
3205 /***********************************************************************
3206 * CoAllowSetForegroundWindow [OLE32.@]
3209 HRESULT WINAPI CoAllowSetForegroundWindow(IUnknown *pUnk, void *pvReserved)
3211 FIXME("(%p, %p): stub\n", pUnk, pvReserved);
3215 /***********************************************************************
3216 * CoQueryProxyBlanket [OLE32.@]
3218 * Retrieves the security settings being used by a proxy.
3221 * pProxy [I] Pointer to the proxy object.
3222 * pAuthnSvc [O] The type of authentication service.
3223 * pAuthzSvc [O] The type of authorization service.
3224 * ppServerPrincName [O] Optional. The server prinicple name.
3225 * pAuthnLevel [O] The authentication level.
3226 * pImpLevel [O] The impersonation level.
3227 * ppAuthInfo [O] Information specific to the authorization/authentication service.
3228 * pCapabilities [O] Flags affecting the security behaviour.
3232 * Failure: HRESULT code.
3235 * CoCopyProxy, CoSetProxyBlanket.
3237 HRESULT WINAPI CoQueryProxyBlanket(IUnknown *pProxy, DWORD *pAuthnSvc,
3238 DWORD *pAuthzSvc, OLECHAR **ppServerPrincName, DWORD *pAuthnLevel,
3239 DWORD *pImpLevel, void **ppAuthInfo, DWORD *pCapabilities)
3241 IClientSecurity *pCliSec;
3244 TRACE("%p\n", pProxy);
3246 hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (void **)&pCliSec);
3249 hr = IClientSecurity_QueryBlanket(pCliSec, pProxy, pAuthnSvc,
3250 pAuthzSvc, ppServerPrincName,
3251 pAuthnLevel, pImpLevel, ppAuthInfo,
3253 IClientSecurity_Release(pCliSec);
3256 if (FAILED(hr)) ERR("-- failed with 0x%08x\n", hr);
3260 /***********************************************************************
3261 * CoSetProxyBlanket [OLE32.@]
3263 * Sets the security settings for a proxy.
3266 * pProxy [I] Pointer to the proxy object.
3267 * AuthnSvc [I] The type of authentication service.
3268 * AuthzSvc [I] The type of authorization service.
3269 * pServerPrincName [I] The server prinicple name.
3270 * AuthnLevel [I] The authentication level.
3271 * ImpLevel [I] The impersonation level.
3272 * pAuthInfo [I] Information specific to the authorization/authentication service.
3273 * Capabilities [I] Flags affecting the security behaviour.
3277 * Failure: HRESULT code.
3280 * CoQueryProxyBlanket, CoCopyProxy.
3282 HRESULT WINAPI CoSetProxyBlanket(IUnknown *pProxy, DWORD AuthnSvc,
3283 DWORD AuthzSvc, OLECHAR *pServerPrincName, DWORD AuthnLevel,
3284 DWORD ImpLevel, void *pAuthInfo, DWORD Capabilities)
3286 IClientSecurity *pCliSec;
3289 TRACE("%p\n", pProxy);
3291 hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (void **)&pCliSec);
3294 hr = IClientSecurity_SetBlanket(pCliSec, pProxy, AuthnSvc,
3295 AuthzSvc, pServerPrincName,
3296 AuthnLevel, ImpLevel, pAuthInfo,
3298 IClientSecurity_Release(pCliSec);
3301 if (FAILED(hr)) ERR("-- failed with 0x%08x\n", hr);
3305 /***********************************************************************
3306 * CoCopyProxy [OLE32.@]
3311 * pProxy [I] Pointer to the proxy object.
3312 * ppCopy [O] Copy of the proxy.
3316 * Failure: HRESULT code.
3319 * CoQueryProxyBlanket, CoSetProxyBlanket.
3321 HRESULT WINAPI CoCopyProxy(IUnknown *pProxy, IUnknown **ppCopy)
3323 IClientSecurity *pCliSec;
3326 TRACE("%p\n", pProxy);
3328 hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (void **)&pCliSec);
3331 hr = IClientSecurity_CopyProxy(pCliSec, pProxy, ppCopy);
3332 IClientSecurity_Release(pCliSec);
3335 if (FAILED(hr)) ERR("-- failed with 0x%08x\n", hr);
3340 /***********************************************************************
3341 * CoGetCallContext [OLE32.@]
3343 * Gets the context of the currently executing server call in the current
3347 * riid [I] Context interface to return.
3348 * ppv [O] Pointer to memory that will receive the context on return.
3352 * Failure: HRESULT code.
3354 HRESULT WINAPI CoGetCallContext(REFIID riid, void **ppv)
3356 FIXME("(%s, %p): stub\n", debugstr_guid(riid), ppv);
3359 return E_NOINTERFACE;
3362 /***********************************************************************
3363 * CoQueryClientBlanket [OLE32.@]
3365 * Retrieves the authentication information about the client of the currently
3366 * executing server call in the current thread.
3369 * pAuthnSvc [O] Optional. The type of authentication service.
3370 * pAuthzSvc [O] Optional. The type of authorization service.
3371 * pServerPrincName [O] Optional. The server prinicple name.
3372 * pAuthnLevel [O] Optional. The authentication level.
3373 * pImpLevel [O] Optional. The impersonation level.
3374 * pPrivs [O] Optional. Information about the privileges of the client.
3375 * pCapabilities [IO] Optional. Flags affecting the security behaviour.
3379 * Failure: HRESULT code.
3382 * CoImpersonateClient, CoRevertToSelf, CoGetCallContext.
3384 HRESULT WINAPI CoQueryClientBlanket(
3387 OLECHAR **pServerPrincName,
3390 RPC_AUTHZ_HANDLE *pPrivs,
3391 DWORD *pCapabilities)
3393 IServerSecurity *pSrvSec;
3396 TRACE("(%p, %p, %p, %p, %p, %p, %p)\n",
3397 pAuthnSvc, pAuthzSvc, pServerPrincName, pAuthnLevel, pImpLevel,
3398 pPrivs, pCapabilities);
3400 hr = CoGetCallContext(&IID_IServerSecurity, (void **)&pSrvSec);
3403 hr = IServerSecurity_QueryBlanket(
3404 pSrvSec, pAuthnSvc, pAuthzSvc, pServerPrincName, pAuthnLevel,
3405 pImpLevel, pPrivs, pCapabilities);
3406 IServerSecurity_Release(pSrvSec);
3412 /***********************************************************************
3413 * CoImpersonateClient [OLE32.@]
3415 * Impersonates the client of the currently executing server call in the
3423 * Failure: HRESULT code.
3426 * If this function fails then the current thread will not be impersonating
3427 * the client and all actions will take place on behalf of the server.
3428 * Therefore, it is important to check the return value from this function.
3431 * CoRevertToSelf, CoQueryClientBlanket, CoGetCallContext.
3433 HRESULT WINAPI CoImpersonateClient(void)
3435 IServerSecurity *pSrvSec;
3440 hr = CoGetCallContext(&IID_IServerSecurity, (void **)&pSrvSec);
3443 hr = IServerSecurity_ImpersonateClient(pSrvSec);
3444 IServerSecurity_Release(pSrvSec);
3450 /***********************************************************************
3451 * CoRevertToSelf [OLE32.@]
3453 * Ends the impersonation of the client of the currently executing server
3454 * call in the current thread.
3461 * Failure: HRESULT code.
3464 * CoImpersonateClient, CoQueryClientBlanket, CoGetCallContext.
3466 HRESULT WINAPI CoRevertToSelf(void)
3468 IServerSecurity *pSrvSec;
3473 hr = CoGetCallContext(&IID_IServerSecurity, (void **)&pSrvSec);
3476 hr = IServerSecurity_RevertToSelf(pSrvSec);
3477 IServerSecurity_Release(pSrvSec);
3483 static BOOL COM_PeekMessage(struct apartment *apt, MSG *msg)
3485 /* first try to retrieve messages for incoming COM calls to the apartment window */
3486 return PeekMessageW(msg, apt->win, WM_USER, WM_APP - 1, PM_REMOVE|PM_NOYIELD) ||
3487 /* next retrieve other messages necessary for the app to remain responsive */
3488 PeekMessageW(msg, NULL, 0, 0, PM_QS_PAINT|PM_QS_POSTMESSAGE|PM_REMOVE|PM_NOYIELD);
3491 /***********************************************************************
3492 * CoWaitForMultipleHandles [OLE32.@]
3494 * Waits for one or more handles to become signaled.
3497 * dwFlags [I] Flags. See notes.
3498 * dwTimeout [I] Timeout in milliseconds.
3499 * cHandles [I] Number of handles pointed to by pHandles.
3500 * pHandles [I] Handles to wait for.
3501 * lpdwindex [O] Index of handle that was signaled.
3505 * Failure: RPC_S_CALLPENDING on timeout.
3509 * The dwFlags parameter can be zero or more of the following:
3510 *| COWAIT_WAITALL - Wait for all of the handles to become signaled.
3511 *| COWAIT_ALERTABLE - Allows a queued APC to run during the wait.
3514 * MsgWaitForMultipleObjects, WaitForMultipleObjects.
3516 HRESULT WINAPI CoWaitForMultipleHandles(DWORD dwFlags, DWORD dwTimeout,
3517 ULONG cHandles, LPHANDLE pHandles, LPDWORD lpdwindex)
3520 DWORD start_time = GetTickCount();
3521 APARTMENT *apt = COM_CurrentApt();
3522 BOOL message_loop = apt && !apt->multi_threaded;
3524 TRACE("(0x%08x, 0x%08x, %d, %p, %p)\n", dwFlags, dwTimeout, cHandles,
3525 pHandles, lpdwindex);
3529 DWORD now = GetTickCount();
3532 if (now - start_time > dwTimeout)
3534 hr = RPC_S_CALLPENDING;
3540 DWORD wait_flags = ((dwFlags & COWAIT_WAITALL) ? MWMO_WAITALL : 0) |
3541 ((dwFlags & COWAIT_ALERTABLE ) ? MWMO_ALERTABLE : 0);
3543 TRACE("waiting for rpc completion or window message\n");
3545 res = MsgWaitForMultipleObjectsEx(cHandles, pHandles,
3546 (dwTimeout == INFINITE) ? INFINITE : start_time + dwTimeout - now,
3547 QS_ALLINPUT, wait_flags);
3549 if (res == WAIT_OBJECT_0 + cHandles) /* messages available */
3553 /* call message filter */
3555 if (COM_CurrentApt()->filter)
3557 PENDINGTYPE pendingtype =
3558 COM_CurrentInfo()->pending_call_count_server ?
3559 PENDINGTYPE_NESTED : PENDINGTYPE_TOPLEVEL;
3560 DWORD be_handled = IMessageFilter_MessagePending(
3561 COM_CurrentApt()->filter, 0 /* FIXME */,
3562 now - start_time, pendingtype);
3563 TRACE("IMessageFilter_MessagePending returned %d\n", be_handled);
3566 case PENDINGMSG_CANCELCALL:
3567 WARN("call canceled\n");
3568 hr = RPC_E_CALL_CANCELED;
3570 case PENDINGMSG_WAITNOPROCESS:
3571 case PENDINGMSG_WAITDEFPROCESS:
3573 /* FIXME: MSDN is very vague about the difference
3574 * between WAITNOPROCESS and WAITDEFPROCESS - there
3575 * appears to be none, so it is possibly a left-over
3576 * from the 16-bit world. */
3581 /* note: using "if" here instead of "while" might seem less
3582 * efficient, but only if we are optimising for quick delivery
3583 * of pending messages, rather than quick completion of the
3585 if (COM_PeekMessage(apt, &msg))
3587 TRACE("received message whilst waiting for RPC: 0x%04x\n", msg.message);
3588 TranslateMessage(&msg);
3589 DispatchMessageW(&msg);
3590 if (msg.message == WM_QUIT)
3592 TRACE("resending WM_QUIT to outer message loop\n");
3593 PostQuitMessage(msg.wParam);
3594 /* no longer need to process messages */
3595 message_loop = FALSE;
3603 TRACE("waiting for rpc completion\n");
3605 res = WaitForMultipleObjectsEx(cHandles, pHandles,
3606 (dwFlags & COWAIT_WAITALL) ? TRUE : FALSE,
3607 (dwTimeout == INFINITE) ? INFINITE : start_time + dwTimeout - now,
3608 (dwFlags & COWAIT_ALERTABLE) ? TRUE : FALSE);
3611 if (res < WAIT_OBJECT_0 + cHandles)
3613 /* handle signaled, store index */
3614 *lpdwindex = (res - WAIT_OBJECT_0);
3617 else if (res == WAIT_TIMEOUT)
3619 hr = RPC_S_CALLPENDING;
3624 ERR("Unexpected wait termination: %d, %d\n", res, GetLastError());
3629 TRACE("-- 0x%08x\n", hr);
3634 /***********************************************************************
3635 * CoGetObject [OLE32.@]
3637 * Gets the object named by converting the name to a moniker and binding to it.
3640 * pszName [I] String representing the object.
3641 * pBindOptions [I] Parameters affecting the binding to the named object.
3642 * riid [I] Interface to bind to on the objecct.
3643 * ppv [O] On output, the interface riid of the object represented
3648 * Failure: HRESULT code.
3651 * MkParseDisplayName.
3653 HRESULT WINAPI CoGetObject(LPCWSTR pszName, BIND_OPTS *pBindOptions,
3654 REFIID riid, void **ppv)
3661 hr = CreateBindCtx(0, &pbc);
3665 hr = IBindCtx_SetBindOptions(pbc, pBindOptions);
3672 hr = MkParseDisplayName(pbc, pszName, &chEaten, &pmk);
3675 hr = IMoniker_BindToObject(pmk, pbc, NULL, riid, ppv);
3676 IMoniker_Release(pmk);
3680 IBindCtx_Release(pbc);
3685 /***********************************************************************
3686 * CoRegisterChannelHook [OLE32.@]
3688 * Registers a process-wide hook that is called during ORPC calls.
3691 * guidExtension [I] GUID of the channel hook to register.
3692 * pChannelHook [I] Channel hook object to register.
3696 * Failure: HRESULT code.
3698 HRESULT WINAPI CoRegisterChannelHook(REFGUID guidExtension, IChannelHook *pChannelHook)
3700 TRACE("(%s, %p)\n", debugstr_guid(guidExtension), pChannelHook);
3702 return RPC_RegisterChannelHook(guidExtension, pChannelHook);
3705 typedef struct Context
3707 const IComThreadingInfoVtbl *lpVtbl;
3712 static HRESULT WINAPI Context_QueryInterface(IComThreadingInfo *iface, REFIID riid, LPVOID *ppv)
3716 if (IsEqualIID(riid, &IID_IComThreadingInfo) ||
3717 IsEqualIID(riid, &IID_IUnknown))
3720 IUnknown_AddRef(iface);
3724 FIXME("interface not implemented %s\n", debugstr_guid(riid));
3725 return E_NOINTERFACE;
3728 static ULONG WINAPI Context_AddRef(IComThreadingInfo *iface)
3730 Context *This = (Context *)iface;
3731 return InterlockedIncrement(&This->refs);
3734 static ULONG WINAPI Context_Release(IComThreadingInfo *iface)
3736 Context *This = (Context *)iface;
3737 ULONG refs = InterlockedDecrement(&This->refs);
3739 HeapFree(GetProcessHeap(), 0, This);
3743 static HRESULT WINAPI Context_GetCurrentApartmentType(IComThreadingInfo *iface, APTTYPE *apttype)
3745 Context *This = (Context *)iface;
3747 TRACE("(%p)\n", apttype);
3749 *apttype = This->apttype;
3753 static HRESULT WINAPI Context_GetCurrentThreadType(IComThreadingInfo *iface, THDTYPE *thdtype)
3755 Context *This = (Context *)iface;
3757 TRACE("(%p)\n", thdtype);
3759 switch (This->apttype)
3762 case APTTYPE_MAINSTA:
3763 *thdtype = THDTYPE_PROCESSMESSAGES;
3766 *thdtype = THDTYPE_BLOCKMESSAGES;
3772 static HRESULT WINAPI Context_GetCurrentLogicalThreadId(IComThreadingInfo *iface, GUID *logical_thread_id)
3774 FIXME("(%p): stub\n", logical_thread_id);
3778 static HRESULT WINAPI Context_SetCurrentLogicalThreadId(IComThreadingInfo *iface, REFGUID logical_thread_id)
3780 FIXME("(%s): stub\n", debugstr_guid(logical_thread_id));
3784 static const IComThreadingInfoVtbl Context_Threading_Vtbl =
3786 Context_QueryInterface,
3789 Context_GetCurrentApartmentType,
3790 Context_GetCurrentThreadType,
3791 Context_GetCurrentLogicalThreadId,
3792 Context_SetCurrentLogicalThreadId
3795 /***********************************************************************
3796 * CoGetObjectContext [OLE32.@]
3798 * Retrieves an object associated with the current context (i.e. apartment).
3801 * riid [I] ID of the interface of the object to retrieve.
3802 * ppv [O] Address where object will be stored on return.
3806 * Failure: HRESULT code.
3808 HRESULT WINAPI CoGetObjectContext(REFIID riid, void **ppv)
3810 APARTMENT *apt = COM_CurrentApt();
3814 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
3819 ERR("apartment not initialised\n");
3820 return CO_E_NOTINITIALIZED;
3823 context = HeapAlloc(GetProcessHeap(), 0, sizeof(*context));
3825 return E_OUTOFMEMORY;
3827 context->lpVtbl = &Context_Threading_Vtbl;
3829 if (apt->multi_threaded)
3830 context->apttype = APTTYPE_MTA;
3832 context->apttype = APTTYPE_MAINSTA;
3834 context->apttype = APTTYPE_STA;
3836 hr = IUnknown_QueryInterface((IUnknown *)&context->lpVtbl, riid, ppv);
3837 IUnknown_Release((IUnknown *)&context->lpVtbl);
3843 /***********************************************************************
3844 * CoGetContextToken [OLE32.@]
3846 HRESULT WINAPI CoGetContextToken( ULONG_PTR *token )
3849 if(!(calls++)) FIXME( "stub\n" );
3850 if (token) *token = 0;
3855 /***********************************************************************
3858 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID fImpLoad)
3860 TRACE("%p 0x%x %p\n", hinstDLL, fdwReason, fImpLoad);
3863 case DLL_PROCESS_ATTACH:
3864 OLE32_hInstance = hinstDLL;
3865 COMPOBJ_InitProcess();
3866 if (TRACE_ON(ole)) CoRegisterMallocSpy((LPVOID)-1);
3869 case DLL_PROCESS_DETACH:
3870 if (TRACE_ON(ole)) CoRevokeMallocSpy();
3871 OLEDD_UnInitialize();
3872 COMPOBJ_UninitProcess();
3873 RPC_UnregisterAllChannelHooks();
3874 COMPOBJ_DllList_Free();
3875 OLE32_hInstance = 0;
3878 case DLL_THREAD_DETACH:
3885 /* NOTE: DllRegisterServer and DllUnregisterServer are in regsvr.c */