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
60 #include "compobj_private.h"
62 #include "wine/unicode.h"
63 #include "wine/debug.h"
65 WINE_DEFAULT_DEBUG_CHANNEL(ole);
67 #define ARRAYSIZE(array) (sizeof(array)/sizeof((array)[0]))
69 /****************************************************************************
70 * This section defines variables internal to the COM module.
73 static HRESULT COM_GetRegisteredClassObject(const struct apartment *apt, REFCLSID rclsid,
74 DWORD dwClsContext, LPUNKNOWN* ppUnk);
75 static void COM_RevokeAllClasses(const struct apartment *apt);
76 static HRESULT get_inproc_class_object(APARTMENT *apt, HKEY hkeydll, REFCLSID rclsid, REFIID riid, BOOL hostifnecessary, void **ppv);
78 static APARTMENT *MTA; /* protected by csApartment */
79 static APARTMENT *MainApartment; /* the first STA apartment */
80 static struct list apts = LIST_INIT( apts ); /* protected by csApartment */
82 static CRITICAL_SECTION csApartment;
83 static CRITICAL_SECTION_DEBUG critsect_debug =
86 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
87 0, 0, { (DWORD_PTR)(__FILE__ ": csApartment") }
89 static CRITICAL_SECTION csApartment = { &critsect_debug, -1, 0, 0, 0, 0 };
91 struct registered_psclsid
99 * This lock count counts the number of times CoInitialize is called. It is
100 * decreased every time CoUninitialize is called. When it hits 0, the COM
101 * libraries are freed
103 static LONG s_COMLockCount = 0;
104 /* Reference count used by CoAddRefServerProcess/CoReleaseServerProcess */
105 static LONG s_COMServerProcessReferences = 0;
108 * This linked list contains the list of registered class objects. These
109 * are mostly used to register the factories for out-of-proc servers of OLE
112 * TODO: Make this data structure aware of inter-process communication. This
113 * means that parts of this will be exported to rpcss.
115 typedef struct tagRegisteredClass
118 CLSID classIdentifier;
120 LPUNKNOWN classObject;
124 LPSTREAM pMarshaledData; /* FIXME: only really need to store OXID and IPID */
125 void *RpcRegistration;
128 static struct list RegisteredClassList = LIST_INIT(RegisteredClassList);
130 static CRITICAL_SECTION csRegisteredClassList;
131 static CRITICAL_SECTION_DEBUG class_cs_debug =
133 0, 0, &csRegisteredClassList,
134 { &class_cs_debug.ProcessLocksList, &class_cs_debug.ProcessLocksList },
135 0, 0, { (DWORD_PTR)(__FILE__ ": csRegisteredClassList") }
137 static CRITICAL_SECTION csRegisteredClassList = { &class_cs_debug, -1, 0, 0, 0, 0 };
139 /*****************************************************************************
140 * This section contains OpenDllList definitions
142 * The OpenDllList contains only handles of dll loaded by CoGetClassObject or
143 * other functions that do LoadLibrary _without_ giving back a HMODULE.
144 * Without this list these handles would never be freed.
146 * FIXME: a DLL that says OK when asked for unloading is unloaded in the
147 * next unload-call but not before 600 sec.
150 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid, REFIID iid, LPVOID *ppv);
151 typedef HRESULT (WINAPI *DllCanUnloadNowFunc)(void);
153 typedef struct tagOpenDll
158 DllGetClassObjectFunc DllGetClassObject;
159 DllCanUnloadNowFunc DllCanUnloadNow;
163 static struct list openDllList = LIST_INIT(openDllList);
165 static CRITICAL_SECTION csOpenDllList;
166 static CRITICAL_SECTION_DEBUG dll_cs_debug =
168 0, 0, &csOpenDllList,
169 { &dll_cs_debug.ProcessLocksList, &dll_cs_debug.ProcessLocksList },
170 0, 0, { (DWORD_PTR)(__FILE__ ": csOpenDllList") }
172 static CRITICAL_SECTION csOpenDllList = { &dll_cs_debug, -1, 0, 0, 0, 0 };
174 struct apartment_loaded_dll
182 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',' ',
183 '0','x','#','#','#','#','#','#','#','#',' ',0};
184 static LRESULT CALLBACK apartment_wndproc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
185 static HRESULT apartment_getclassobject(struct apartment *apt, LPCWSTR dllpath,
186 BOOL apartment_threaded,
187 REFCLSID rclsid, REFIID riid, void **ppv);
188 static void apartment_freeunusedlibraries(struct apartment *apt, DWORD delay);
190 static HRESULT COMPOBJ_DllList_Add(LPCWSTR library_name, OpenDll **ret);
191 static OpenDll *COMPOBJ_DllList_Get(LPCWSTR library_name);
192 static void COMPOBJ_DllList_ReleaseRef(OpenDll *entry, BOOL free_entry);
194 static DWORD COM_RegReadPath(HKEY hkeyroot, const WCHAR *keyname, const WCHAR *valuename, WCHAR * dst, DWORD dstlen);
196 static void COMPOBJ_InitProcess( void )
200 /* Dispatching to the correct thread in an apartment is done through
201 * window messages rather than RPC transports. When an interface is
202 * marshalled into another apartment in the same process, a window of the
203 * following class is created. The *caller* of CoMarshalInterface (i.e., the
204 * application) is responsible for pumping the message loop in that thread.
205 * The WM_USER messages which point to the RPCs are then dispatched to
206 * apartment_wndproc by the user's code from the apartment in which the
207 * interface was unmarshalled.
209 memset(&wclass, 0, sizeof(wclass));
210 wclass.lpfnWndProc = apartment_wndproc;
211 wclass.hInstance = hProxyDll;
212 wclass.lpszClassName = wszAptWinClass;
213 RegisterClassW(&wclass);
216 static void COMPOBJ_UninitProcess( void )
218 UnregisterClassW(wszAptWinClass, hProxyDll);
221 static void COM_TlsDestroy(void)
223 struct oletls *info = NtCurrentTeb()->ReservedForOle;
226 if (info->apt) apartment_release(info->apt);
227 if (info->errorinfo) IErrorInfo_Release(info->errorinfo);
228 if (info->state) IUnknown_Release(info->state);
229 if (info->spy) IUnknown_Release(info->spy);
230 HeapFree(GetProcessHeap(), 0, info);
231 NtCurrentTeb()->ReservedForOle = NULL;
235 /******************************************************************************
239 /* allocates memory and fills in the necessary fields for a new apartment
240 * object. must be called inside apartment cs */
241 static APARTMENT *apartment_construct(DWORD model)
245 TRACE("creating new apartment, model=%d\n", model);
247 apt = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*apt));
248 apt->tid = GetCurrentThreadId();
250 list_init(&apt->proxies);
251 list_init(&apt->stubmgrs);
252 list_init(&apt->psclsids);
253 list_init(&apt->loaded_dlls);
256 apt->remunk_exported = FALSE;
258 InitializeCriticalSection(&apt->cs);
259 DEBUG_SET_CRITSEC_NAME(&apt->cs, "apartment");
261 apt->multi_threaded = !(model & COINIT_APARTMENTTHREADED);
263 if (apt->multi_threaded)
265 /* FIXME: should be randomly generated by in an RPC call to rpcss */
266 apt->oxid = ((OXID)GetCurrentProcessId() << 32) | 0xcafe;
270 /* FIXME: should be randomly generated by in an RPC call to rpcss */
271 apt->oxid = ((OXID)GetCurrentProcessId() << 32) | GetCurrentThreadId();
274 TRACE("Created apartment on OXID %s\n", wine_dbgstr_longlong(apt->oxid));
276 list_add_head(&apts, &apt->entry);
281 /* gets and existing apartment if one exists or otherwise creates an apartment
282 * structure which stores OLE apartment-local information and stores a pointer
283 * to it in the thread-local storage */
284 static APARTMENT *apartment_get_or_create(DWORD model)
286 APARTMENT *apt = COM_CurrentApt();
290 if (model & COINIT_APARTMENTTHREADED)
292 EnterCriticalSection(&csApartment);
294 apt = apartment_construct(model);
299 TRACE("Created main-threaded apartment with OXID %s\n", wine_dbgstr_longlong(apt->oxid));
302 LeaveCriticalSection(&csApartment);
305 apartment_createwindowifneeded(apt);
309 EnterCriticalSection(&csApartment);
311 /* The multi-threaded apartment (MTA) contains zero or more threads interacting
312 * with free threaded (ie thread safe) COM objects. There is only ever one MTA
316 TRACE("entering the multithreaded apartment %s\n", wine_dbgstr_longlong(MTA->oxid));
317 apartment_addref(MTA);
320 MTA = apartment_construct(model);
324 LeaveCriticalSection(&csApartment);
326 COM_CurrentInfo()->apt = apt;
332 static inline BOOL apartment_is_model(const APARTMENT *apt, DWORD model)
334 return (apt->multi_threaded == !(model & COINIT_APARTMENTTHREADED));
337 DWORD apartment_addref(struct apartment *apt)
339 DWORD refs = InterlockedIncrement(&apt->refs);
340 TRACE("%s: before = %d\n", wine_dbgstr_longlong(apt->oxid), refs - 1);
344 DWORD apartment_release(struct apartment *apt)
348 EnterCriticalSection(&csApartment);
350 ret = InterlockedDecrement(&apt->refs);
351 TRACE("%s: after = %d\n", wine_dbgstr_longlong(apt->oxid), ret);
352 /* destruction stuff that needs to happen under csApartment CS */
355 if (apt == MTA) MTA = NULL;
356 else if (apt == MainApartment) MainApartment = NULL;
357 list_remove(&apt->entry);
360 LeaveCriticalSection(&csApartment);
364 struct list *cursor, *cursor2;
366 TRACE("destroying apartment %p, oxid %s\n", apt, wine_dbgstr_longlong(apt->oxid));
368 /* Release the references to the registered class objects */
369 COM_RevokeAllClasses(apt);
371 /* no locking is needed for this apartment, because no other thread
372 * can access it at this point */
374 apartment_disconnectproxies(apt);
376 if (apt->win) DestroyWindow(apt->win);
377 if (apt->host_apt_tid) PostThreadMessageW(apt->host_apt_tid, WM_QUIT, 0, 0);
379 LIST_FOR_EACH_SAFE(cursor, cursor2, &apt->stubmgrs)
381 struct stub_manager *stubmgr = LIST_ENTRY(cursor, struct stub_manager, entry);
382 /* release the implicit reference given by the fact that the
383 * stub has external references (it must do since it is in the
384 * stub manager list in the apartment and all non-apartment users
385 * must have a ref on the apartment and so it cannot be destroyed).
387 stub_manager_int_release(stubmgr);
390 LIST_FOR_EACH_SAFE(cursor, cursor2, &apt->psclsids)
392 struct registered_psclsid *registered_psclsid =
393 LIST_ENTRY(cursor, struct registered_psclsid, entry);
395 list_remove(®istered_psclsid->entry);
396 HeapFree(GetProcessHeap(), 0, registered_psclsid);
399 /* if this assert fires, then another thread took a reference to a
400 * stub manager without taking a reference to the containing
401 * apartment, which it must do. */
402 assert(list_empty(&apt->stubmgrs));
404 if (apt->filter) IUnknown_Release(apt->filter);
406 /* free as many unused libraries as possible... */
407 apartment_freeunusedlibraries(apt, 0);
409 /* ... and free the memory for the apartment loaded dll entry and
410 * release the dll list reference without freeing the library for the
412 while ((cursor = list_head(&apt->loaded_dlls)))
414 struct apartment_loaded_dll *apartment_loaded_dll = LIST_ENTRY(cursor, struct apartment_loaded_dll, entry);
415 COMPOBJ_DllList_ReleaseRef(apartment_loaded_dll->dll, FALSE);
417 HeapFree(GetProcessHeap(), 0, apartment_loaded_dll);
420 DEBUG_CLEAR_CRITSEC_NAME(&apt->cs);
421 DeleteCriticalSection(&apt->cs);
423 HeapFree(GetProcessHeap(), 0, apt);
429 /* The given OXID must be local to this process:
431 * The ref parameter is here mostly to ensure people remember that
432 * they get one, you should normally take a ref for thread safety.
434 APARTMENT *apartment_findfromoxid(OXID oxid, BOOL ref)
436 APARTMENT *result = NULL;
439 EnterCriticalSection(&csApartment);
440 LIST_FOR_EACH( cursor, &apts )
442 struct apartment *apt = LIST_ENTRY( cursor, struct apartment, entry );
443 if (apt->oxid == oxid)
446 if (ref) apartment_addref(result);
450 LeaveCriticalSection(&csApartment);
455 /* gets the apartment which has a given creator thread ID. The caller must
456 * release the reference from the apartment as soon as the apartment pointer
457 * is no longer required. */
458 APARTMENT *apartment_findfromtid(DWORD tid)
460 APARTMENT *result = NULL;
463 EnterCriticalSection(&csApartment);
464 LIST_FOR_EACH( cursor, &apts )
466 struct apartment *apt = LIST_ENTRY( cursor, struct apartment, entry );
470 apartment_addref(result);
474 LeaveCriticalSection(&csApartment);
479 /* gets the main apartment if it exists. The caller must
480 * release the reference from the apartment as soon as the apartment pointer
481 * is no longer required. */
482 static APARTMENT *apartment_findmain(void)
486 EnterCriticalSection(&csApartment);
488 result = MainApartment;
489 if (result) apartment_addref(result);
491 LeaveCriticalSection(&csApartment);
496 /* gets the multi-threaded apartment if it exists. The caller must
497 * release the reference from the apartment as soon as the apartment pointer
498 * is no longer required. */
499 static APARTMENT *apartment_find_multi_threaded(void)
501 APARTMENT *result = NULL;
504 EnterCriticalSection(&csApartment);
506 LIST_FOR_EACH( cursor, &apts )
508 struct apartment *apt = LIST_ENTRY( cursor, struct apartment, entry );
509 if (apt->multi_threaded)
512 apartment_addref(result);
517 LeaveCriticalSection(&csApartment);
521 struct host_object_params
524 CLSID clsid; /* clsid of object to marshal */
525 IID iid; /* interface to marshal */
526 HANDLE event; /* event signalling when ready for multi-threaded case */
527 HRESULT hr; /* result for multi-threaded case */
528 IStream *stream; /* stream that the object will be marshaled into */
529 BOOL apartment_threaded; /* is the component purely apartment-threaded? */
532 static HRESULT apartment_hostobject(struct apartment *apt,
533 const struct host_object_params *params)
537 static const LARGE_INTEGER llZero;
538 WCHAR dllpath[MAX_PATH+1];
540 TRACE("clsid %s, iid %s\n", debugstr_guid(¶ms->clsid), debugstr_guid(¶ms->iid));
542 if (COM_RegReadPath(params->hkeydll, NULL, NULL, dllpath, ARRAYSIZE(dllpath)) != ERROR_SUCCESS)
544 /* failure: CLSID is not found in registry */
545 WARN("class %s not registered inproc\n", debugstr_guid(¶ms->clsid));
546 return REGDB_E_CLASSNOTREG;
549 hr = apartment_getclassobject(apt, dllpath, params->apartment_threaded,
550 ¶ms->clsid, ¶ms->iid, (void **)&object);
554 hr = CoMarshalInterface(params->stream, ¶ms->iid, object, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
556 IUnknown_Release(object);
557 IStream_Seek(params->stream, llZero, STREAM_SEEK_SET, NULL);
562 static LRESULT CALLBACK apartment_wndproc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
567 RPC_ExecuteCall((struct dispatch_params *)lParam);
570 return apartment_hostobject(COM_CurrentApt(), (const struct host_object_params *)lParam);
572 return DefWindowProcW(hWnd, msg, wParam, lParam);
576 struct host_thread_params
578 COINIT threading_model;
583 /* thread for hosting an object to allow an object to appear to be created in
584 * an apartment with an incompatible threading model */
585 static DWORD CALLBACK apartment_hostobject_thread(LPVOID p)
587 struct host_thread_params *params = p;
590 struct apartment *apt;
594 hr = CoInitializeEx(NULL, params->threading_model);
595 if (FAILED(hr)) return hr;
597 apt = COM_CurrentApt();
598 if (params->threading_model == COINIT_APARTMENTTHREADED)
600 apartment_createwindowifneeded(apt);
601 params->apartment_hwnd = apartment_getwindow(apt);
604 params->apartment_hwnd = NULL;
606 /* force the message queue to be created before signaling parent thread */
607 PeekMessageW(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
609 SetEvent(params->ready_event);
610 params = NULL; /* can't touch params after here as it may be invalid */
612 while (GetMessageW(&msg, NULL, 0, 0))
614 if (!msg.hwnd && (msg.message == DM_HOSTOBJECT))
616 struct host_object_params *obj_params = (struct host_object_params *)msg.lParam;
617 obj_params->hr = apartment_hostobject(apt, obj_params);
618 SetEvent(obj_params->event);
622 TranslateMessage(&msg);
623 DispatchMessageW(&msg);
634 /* finds or creates a host apartment, creates the object inside it and returns
635 * a proxy to it so that the object can be used in the apartment of the
636 * caller of this function */
637 static HRESULT apartment_hostobject_in_hostapt(
638 struct apartment *apt, BOOL multi_threaded, BOOL main_apartment,
639 HKEY hkeydll, REFCLSID rclsid, REFIID riid, void **ppv)
641 struct host_object_params params;
642 HWND apartment_hwnd = NULL;
643 DWORD apartment_tid = 0;
646 if (!multi_threaded && main_apartment)
648 APARTMENT *host_apt = apartment_findmain();
651 apartment_hwnd = apartment_getwindow(host_apt);
652 apartment_release(host_apt);
658 EnterCriticalSection(&apt->cs);
660 if (!apt->host_apt_tid)
662 struct host_thread_params thread_params;
666 thread_params.threading_model = multi_threaded ? COINIT_MULTITHREADED : COINIT_APARTMENTTHREADED;
667 handles[0] = thread_params.ready_event = CreateEventW(NULL, FALSE, FALSE, NULL);
668 thread_params.apartment_hwnd = NULL;
669 handles[1] = CreateThread(NULL, 0, apartment_hostobject_thread, &thread_params, 0, &apt->host_apt_tid);
672 CloseHandle(handles[0]);
673 LeaveCriticalSection(&apt->cs);
674 return E_OUTOFMEMORY;
676 wait_value = WaitForMultipleObjects(2, handles, FALSE, INFINITE);
677 CloseHandle(handles[0]);
678 CloseHandle(handles[1]);
679 if (wait_value == WAIT_OBJECT_0)
680 apt->host_apt_hwnd = thread_params.apartment_hwnd;
683 LeaveCriticalSection(&apt->cs);
684 return E_OUTOFMEMORY;
688 if (multi_threaded || !main_apartment)
690 apartment_hwnd = apt->host_apt_hwnd;
691 apartment_tid = apt->host_apt_tid;
694 LeaveCriticalSection(&apt->cs);
697 /* another thread may have become the main apartment in the time it took
698 * us to create the thread for the host apartment */
699 if (!apartment_hwnd && !multi_threaded && main_apartment)
701 APARTMENT *host_apt = apartment_findmain();
704 apartment_hwnd = apartment_getwindow(host_apt);
705 apartment_release(host_apt);
709 params.hkeydll = hkeydll;
710 params.clsid = *rclsid;
712 hr = CreateStreamOnHGlobal(NULL, TRUE, ¶ms.stream);
715 params.apartment_threaded = !multi_threaded;
719 params.event = CreateEventW(NULL, FALSE, FALSE, NULL);
720 if (!PostThreadMessageW(apartment_tid, DM_HOSTOBJECT, 0, (LPARAM)¶ms))
724 WaitForSingleObject(params.event, INFINITE);
727 CloseHandle(params.event);
733 ERR("host apartment didn't create window\n");
737 hr = SendMessageW(apartment_hwnd, DM_HOSTOBJECT, 0, (LPARAM)¶ms);
740 hr = CoUnmarshalInterface(params.stream, riid, ppv);
741 IStream_Release(params.stream);
745 /* create a window for the apartment or return the current one if one has
746 * already been created */
747 HRESULT apartment_createwindowifneeded(struct apartment *apt)
749 if (apt->multi_threaded)
754 HWND hwnd = CreateWindowW(wszAptWinClass, NULL, 0,
756 HWND_MESSAGE, 0, hProxyDll, NULL);
759 ERR("CreateWindow failed with error %d\n", GetLastError());
760 return HRESULT_FROM_WIN32(GetLastError());
762 if (InterlockedCompareExchangePointer((PVOID *)&apt->win, hwnd, NULL))
763 /* someone beat us to it */
770 /* retrieves the window for the main- or apartment-threaded apartment */
771 HWND apartment_getwindow(const struct apartment *apt)
773 assert(!apt->multi_threaded);
777 void apartment_joinmta(void)
779 apartment_addref(MTA);
780 COM_CurrentInfo()->apt = MTA;
783 /* gets the specified class object by loading the appropriate DLL, if
784 * necessary and calls the DllGetClassObject function for the DLL */
785 static HRESULT apartment_getclassobject(struct apartment *apt, LPCWSTR dllpath,
786 BOOL apartment_threaded,
787 REFCLSID rclsid, REFIID riid, void **ppv)
789 static const WCHAR wszOle32[] = {'o','l','e','3','2','.','d','l','l',0};
792 struct apartment_loaded_dll *apartment_loaded_dll;
794 if (!strcmpiW(dllpath, wszOle32))
796 /* we don't need to control the lifetime of this dll, so use the local
797 * implementation of DllGetClassObject directly */
798 TRACE("calling ole32!DllGetClassObject\n");
799 hr = DllGetClassObject(rclsid, riid, ppv);
802 ERR("DllGetClassObject returned error 0x%08x\n", hr);
807 EnterCriticalSection(&apt->cs);
809 LIST_FOR_EACH_ENTRY(apartment_loaded_dll, &apt->loaded_dlls, struct apartment_loaded_dll, entry)
810 if (!strcmpiW(dllpath, apartment_loaded_dll->dll->library_name))
812 TRACE("found %s already loaded\n", debugstr_w(dllpath));
819 apartment_loaded_dll = HeapAlloc(GetProcessHeap(), 0, sizeof(*apartment_loaded_dll));
820 if (!apartment_loaded_dll)
824 apartment_loaded_dll->unload_time = 0;
825 apartment_loaded_dll->multi_threaded = FALSE;
826 hr = COMPOBJ_DllList_Add( dllpath, &apartment_loaded_dll->dll );
828 HeapFree(GetProcessHeap(), 0, apartment_loaded_dll);
832 TRACE("added new loaded dll %s\n", debugstr_w(dllpath));
833 list_add_tail(&apt->loaded_dlls, &apartment_loaded_dll->entry);
837 LeaveCriticalSection(&apt->cs);
841 /* one component being multi-threaded overrides any number of
842 * apartment-threaded components */
843 if (!apartment_threaded)
844 apartment_loaded_dll->multi_threaded = TRUE;
846 TRACE("calling DllGetClassObject %p\n", apartment_loaded_dll->dll->DllGetClassObject);
847 /* OK: get the ClassObject */
848 hr = apartment_loaded_dll->dll->DllGetClassObject(rclsid, riid, ppv);
851 ERR("DllGetClassObject returned error 0x%08x\n", hr);
857 /* frees unused libraries loaded by apartment_getclassobject by calling the
858 * DLL's DllCanUnloadNow entry point */
859 static void apartment_freeunusedlibraries(struct apartment *apt, DWORD delay)
861 struct apartment_loaded_dll *entry, *next;
862 EnterCriticalSection(&apt->cs);
863 LIST_FOR_EACH_ENTRY_SAFE(entry, next, &apt->loaded_dlls, struct apartment_loaded_dll, entry)
865 if (entry->dll->DllCanUnloadNow && (entry->dll->DllCanUnloadNow() == S_OK))
867 DWORD real_delay = delay;
869 if (real_delay == INFINITE)
871 /* DLLs that return multi-threaded objects aren't unloaded
872 * straight away to cope for programs that have races between
873 * last object destruction and threads in the DLLs that haven't
874 * finished, despite DllCanUnloadNow returning S_OK */
875 if (entry->multi_threaded)
876 real_delay = 10 * 60 * 1000; /* 10 minutes */
881 if (!real_delay || (entry->unload_time && (entry->unload_time < GetTickCount())))
883 list_remove(&entry->entry);
884 COMPOBJ_DllList_ReleaseRef(entry->dll, TRUE);
885 HeapFree(GetProcessHeap(), 0, entry);
888 entry->unload_time = GetTickCount() + real_delay;
890 else if (entry->unload_time)
891 entry->unload_time = 0;
893 LeaveCriticalSection(&apt->cs);
896 /*****************************************************************************
897 * This section contains OpenDllList implementation
900 /* caller must ensure that library_name is not already in the open dll list */
901 static HRESULT COMPOBJ_DllList_Add(LPCWSTR library_name, OpenDll **ret)
907 DllCanUnloadNowFunc DllCanUnloadNow;
908 DllGetClassObjectFunc DllGetClassObject;
912 *ret = COMPOBJ_DllList_Get(library_name);
913 if (*ret) return S_OK;
915 /* do this outside the csOpenDllList to avoid creating a lock dependency on
917 hLibrary = LoadLibraryExW(library_name, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
920 ERR("couldn't load in-process dll %s\n", debugstr_w(library_name));
921 /* failure: DLL could not be loaded */
922 return E_ACCESSDENIED; /* FIXME: or should this be CO_E_DLLNOTFOUND? */
925 DllCanUnloadNow = (void *)GetProcAddress(hLibrary, "DllCanUnloadNow");
926 /* Note: failing to find DllCanUnloadNow is not a failure */
927 DllGetClassObject = (void *)GetProcAddress(hLibrary, "DllGetClassObject");
928 if (!DllGetClassObject)
930 /* failure: the dll did not export DllGetClassObject */
931 ERR("couldn't find function DllGetClassObject in %s\n", debugstr_w(library_name));
932 FreeLibrary(hLibrary);
933 return CO_E_DLLNOTFOUND;
936 EnterCriticalSection( &csOpenDllList );
938 *ret = COMPOBJ_DllList_Get(library_name);
941 /* another caller to this function already added the dll while we
942 * weren't in the critical section */
943 FreeLibrary(hLibrary);
947 len = strlenW(library_name);
948 entry = HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
950 entry->library_name = HeapAlloc(GetProcessHeap(), 0, (len + 1)*sizeof(WCHAR));
951 if (entry && entry->library_name)
953 memcpy(entry->library_name, library_name, (len + 1)*sizeof(WCHAR));
954 entry->library = hLibrary;
956 entry->DllCanUnloadNow = DllCanUnloadNow;
957 entry->DllGetClassObject = DllGetClassObject;
958 list_add_tail(&openDllList, &entry->entry);
962 HeapFree(GetProcessHeap(), 0, entry);
964 FreeLibrary(hLibrary);
969 LeaveCriticalSection( &csOpenDllList );
974 static OpenDll *COMPOBJ_DllList_Get(LPCWSTR library_name)
978 EnterCriticalSection(&csOpenDllList);
979 LIST_FOR_EACH_ENTRY(ptr, &openDllList, OpenDll, entry)
981 if (!strcmpiW(library_name, ptr->library_name) &&
982 (InterlockedIncrement(&ptr->refs) != 1) /* entry is being destroy if == 1 */)
988 LeaveCriticalSection(&csOpenDllList);
992 /* pass FALSE for free_entry to release a reference without destroying the
993 * entry if it reaches zero or TRUE otherwise */
994 static void COMPOBJ_DllList_ReleaseRef(OpenDll *entry, BOOL free_entry)
996 if (!InterlockedDecrement(&entry->refs) && free_entry)
998 EnterCriticalSection(&csOpenDllList);
999 list_remove(&entry->entry);
1000 LeaveCriticalSection(&csOpenDllList);
1002 TRACE("freeing %p\n", entry->library);
1003 FreeLibrary(entry->library);
1005 HeapFree(GetProcessHeap(), 0, entry->library_name);
1006 HeapFree(GetProcessHeap(), 0, entry);
1010 /* frees memory associated with active dll list */
1011 static void COMPOBJ_DllList_Free(void)
1013 OpenDll *entry, *cursor2;
1014 EnterCriticalSection(&csOpenDllList);
1015 LIST_FOR_EACH_ENTRY_SAFE(entry, cursor2, &openDllList, OpenDll, entry)
1017 list_remove(&entry->entry);
1019 HeapFree(GetProcessHeap(), 0, entry->library_name);
1020 HeapFree(GetProcessHeap(), 0, entry);
1022 LeaveCriticalSection(&csOpenDllList);
1025 /******************************************************************************
1026 * CoBuildVersion [OLE32.@]
1028 * Gets the build version of the DLL.
1033 * Current build version, hiword is majornumber, loword is minornumber
1035 DWORD WINAPI CoBuildVersion(void)
1037 TRACE("Returning version %d, build %d.\n", rmm, rup);
1038 return (rmm<<16)+rup;
1041 /******************************************************************************
1042 * CoRegisterInitializeSpy [OLE32.@]
1044 * Add a Spy that watches CoInitializeEx calls
1047 * spy [I] Pointer to IUnknown interface that will be QueryInterface'd.
1048 * cookie [II] cookie receiver
1051 * Success: S_OK if not already initialized, S_FALSE otherwise.
1052 * Failure: HRESULT code.
1057 HRESULT WINAPI CoRegisterInitializeSpy(IInitializeSpy *spy, ULARGE_INTEGER *cookie)
1059 struct oletls *info = COM_CurrentInfo();
1062 TRACE("(%p, %p)\n", spy, cookie);
1064 if (!spy || !cookie || !info)
1067 WARN("Could not allocate tls\n");
1068 return E_INVALIDARG;
1073 FIXME("Already registered?\n");
1074 return E_UNEXPECTED;
1077 hr = IUnknown_QueryInterface(spy, &IID_IInitializeSpy, (void **) &info->spy);
1080 cookie->QuadPart = (DWORD_PTR)spy;
1086 /******************************************************************************
1087 * CoRevokeInitializeSpy [OLE32.@]
1089 * Remove a spy that previously watched CoInitializeEx calls
1092 * cookie [I] The cookie obtained from a previous CoRegisterInitializeSpy call
1095 * Success: S_OK if a spy is removed
1096 * Failure: E_INVALIDARG
1101 HRESULT WINAPI CoRevokeInitializeSpy(ULARGE_INTEGER cookie)
1103 struct oletls *info = COM_CurrentInfo();
1104 TRACE("(%s)\n", wine_dbgstr_longlong(cookie.QuadPart));
1106 if (!info || !info->spy || cookie.QuadPart != (DWORD_PTR)info->spy)
1107 return E_INVALIDARG;
1109 IUnknown_Release(info->spy);
1115 /******************************************************************************
1116 * CoInitialize [OLE32.@]
1118 * Initializes the COM libraries by calling CoInitializeEx with
1119 * COINIT_APARTMENTTHREADED, ie it enters a STA thread.
1122 * lpReserved [I] Pointer to IMalloc interface (obsolete, should be NULL).
1125 * Success: S_OK if not already initialized, S_FALSE otherwise.
1126 * Failure: HRESULT code.
1131 HRESULT WINAPI CoInitialize(LPVOID lpReserved)
1134 * Just delegate to the newer method.
1136 return CoInitializeEx(lpReserved, COINIT_APARTMENTTHREADED);
1139 /******************************************************************************
1140 * CoInitializeEx [OLE32.@]
1142 * Initializes the COM libraries.
1145 * lpReserved [I] Pointer to IMalloc interface (obsolete, should be NULL).
1146 * dwCoInit [I] One or more flags from the COINIT enumeration. See notes.
1149 * S_OK if successful,
1150 * S_FALSE if this function was called already.
1151 * RPC_E_CHANGED_MODE if a previous call to CoInitializeEx specified another
1156 * The behavior used to set the IMalloc used for memory management is
1158 * The dwCoInit parameter must specify one of the following apartment
1160 *| COINIT_APARTMENTTHREADED - A single-threaded apartment (STA).
1161 *| COINIT_MULTITHREADED - A multi-threaded apartment (MTA).
1162 * The parameter may also specify zero or more of the following flags:
1163 *| COINIT_DISABLE_OLE1DDE - Don't use DDE for OLE1 support.
1164 *| COINIT_SPEED_OVER_MEMORY - Trade memory for speed.
1169 HRESULT WINAPI CoInitializeEx(LPVOID lpReserved, DWORD dwCoInit)
1171 struct oletls *info = COM_CurrentInfo();
1175 TRACE("(%p, %x)\n", lpReserved, (int)dwCoInit);
1177 if (lpReserved!=NULL)
1179 ERR("(%p, %x) - Bad parameter passed-in %p, must be an old Windows Application\n", lpReserved, (int)dwCoInit, lpReserved);
1183 * Check the lock count. If this is the first time going through the initialize
1184 * process, we have to initialize the libraries.
1186 * And crank-up that lock count.
1188 if (InterlockedExchangeAdd(&s_COMLockCount,1)==0)
1191 * Initialize the various COM libraries and data structures.
1193 TRACE("() - Initializing the COM libraries\n");
1195 /* we may need to defer this until after apartment initialisation */
1196 RunningObjectTableImpl_Initialize();
1200 IInitializeSpy_PreInitialize(info->spy, dwCoInit, info->inits);
1202 if (!(apt = info->apt))
1204 apt = apartment_get_or_create(dwCoInit);
1205 if (!apt) return E_OUTOFMEMORY;
1207 else if (!apartment_is_model(apt, dwCoInit))
1209 /* Changing the threading model after it's been set is illegal. If this warning is triggered by Wine
1210 code then we are probably using the wrong threading model to implement that API. */
1211 ERR("Attempt to change threading model of this apartment from %s to %s\n",
1212 apt->multi_threaded ? "multi-threaded" : "apartment threaded",
1213 dwCoInit & COINIT_APARTMENTTHREADED ? "apartment threaded" : "multi-threaded");
1214 return RPC_E_CHANGED_MODE;
1222 IInitializeSpy_PostInitialize(info->spy, hr, dwCoInit, info->inits);
1227 /***********************************************************************
1228 * CoUninitialize [OLE32.@]
1230 * This method will decrement the refcount on the current apartment, freeing
1231 * the resources associated with it if it is the last thread in the apartment.
1232 * If the last apartment is freed, the function will additionally release
1233 * any COM resources associated with the process.
1243 void WINAPI CoUninitialize(void)
1245 struct oletls * info = COM_CurrentInfo();
1250 /* will only happen on OOM */
1254 IInitializeSpy_PreUninitialize(info->spy, info->inits);
1259 ERR("Mismatched CoUninitialize\n");
1262 IInitializeSpy_PostUninitialize(info->spy, info->inits);
1268 apartment_release(info->apt);
1273 * Decrease the reference count.
1274 * If we are back to 0 locks on the COM library, make sure we free
1275 * all the associated data structures.
1277 lCOMRefCnt = InterlockedExchangeAdd(&s_COMLockCount,-1);
1280 TRACE("() - Releasing the COM libraries\n");
1282 RunningObjectTableImpl_UnInitialize();
1284 else if (lCOMRefCnt<1) {
1285 ERR( "CoUninitialize() - not CoInitialized.\n" );
1286 InterlockedExchangeAdd(&s_COMLockCount,1); /* restore the lock count. */
1289 IInitializeSpy_PostUninitialize(info->spy, info->inits);
1292 /******************************************************************************
1293 * CoDisconnectObject [OLE32.@]
1295 * Disconnects all connections to this object from remote processes. Dispatches
1296 * pending RPCs while blocking new RPCs from occurring, and then calls
1297 * IMarshal::DisconnectObject on the given object.
1299 * Typically called when the object server is forced to shut down, for instance by
1303 * lpUnk [I] The object whose stub should be disconnected.
1304 * reserved [I] Reserved. Should be set to 0.
1308 * Failure: HRESULT code.
1311 * CoMarshalInterface, CoReleaseMarshalData, CoLockObjectExternal
1313 HRESULT WINAPI CoDisconnectObject( LPUNKNOWN lpUnk, DWORD reserved )
1319 TRACE("(%p, 0x%08x)\n", lpUnk, reserved);
1321 hr = IUnknown_QueryInterface(lpUnk, &IID_IMarshal, (void **)&marshal);
1324 hr = IMarshal_DisconnectObject(marshal, reserved);
1325 IMarshal_Release(marshal);
1329 apt = COM_CurrentApt();
1331 return CO_E_NOTINITIALIZED;
1333 apartment_disconnectobject(apt, lpUnk);
1335 /* Note: native is pretty broken here because it just silently
1336 * fails, without returning an appropriate error code if the object was
1337 * not found, making apps think that the object was disconnected, when
1338 * it actually wasn't */
1343 /******************************************************************************
1344 * CoCreateGuid [OLE32.@]
1346 * Simply forwards to UuidCreate in RPCRT4.
1349 * pguid [O] Points to the GUID to initialize.
1353 * Failure: HRESULT code.
1358 HRESULT WINAPI CoCreateGuid(GUID *pguid)
1360 DWORD status = UuidCreate(pguid);
1361 if (status == RPC_S_OK || status == RPC_S_UUID_LOCAL_ONLY) return S_OK;
1362 return HRESULT_FROM_WIN32( status );
1365 /******************************************************************************
1366 * CLSIDFromString [OLE32.@]
1367 * IIDFromString [OLE32.@]
1369 * Converts a unique identifier from its string representation into
1373 * idstr [I] The string representation of the GUID.
1374 * id [O] GUID converted from the string.
1378 * CO_E_CLASSSTRING if idstr is not a valid CLSID
1383 static HRESULT __CLSIDFromString(LPCWSTR s, CLSID *id)
1389 memset( id, 0, sizeof (CLSID) );
1393 /* validate the CLSID string */
1394 if (strlenW(s) != 38)
1395 return CO_E_CLASSSTRING;
1397 if ((s[0]!='{') || (s[9]!='-') || (s[14]!='-') || (s[19]!='-') || (s[24]!='-') || (s[37]!='}'))
1398 return CO_E_CLASSSTRING;
1400 for (i=1; i<37; i++) {
1401 if ((i == 9)||(i == 14)||(i == 19)||(i == 24)) continue;
1402 if (!(((s[i] >= '0') && (s[i] <= '9')) ||
1403 ((s[i] >= 'a') && (s[i] <= 'f')) ||
1404 ((s[i] >= 'A') && (s[i] <= 'F'))))
1405 return CO_E_CLASSSTRING;
1408 TRACE("%s -> %p\n", debugstr_w(s), id);
1410 /* quick lookup table */
1411 memset(table, 0, 256);
1413 for (i = 0; i < 10; i++) {
1416 for (i = 0; i < 6; i++) {
1417 table['A' + i] = i+10;
1418 table['a' + i] = i+10;
1421 /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
1423 id->Data1 = (table[s[1]] << 28 | table[s[2]] << 24 | table[s[3]] << 20 | table[s[4]] << 16 |
1424 table[s[5]] << 12 | table[s[6]] << 8 | table[s[7]] << 4 | table[s[8]]);
1425 id->Data2 = table[s[10]] << 12 | table[s[11]] << 8 | table[s[12]] << 4 | table[s[13]];
1426 id->Data3 = table[s[15]] << 12 | table[s[16]] << 8 | table[s[17]] << 4 | table[s[18]];
1428 /* these are just sequential bytes */
1429 id->Data4[0] = table[s[20]] << 4 | table[s[21]];
1430 id->Data4[1] = table[s[22]] << 4 | table[s[23]];
1431 id->Data4[2] = table[s[25]] << 4 | table[s[26]];
1432 id->Data4[3] = table[s[27]] << 4 | table[s[28]];
1433 id->Data4[4] = table[s[29]] << 4 | table[s[30]];
1434 id->Data4[5] = table[s[31]] << 4 | table[s[32]];
1435 id->Data4[6] = table[s[33]] << 4 | table[s[34]];
1436 id->Data4[7] = table[s[35]] << 4 | table[s[36]];
1441 /*****************************************************************************/
1443 HRESULT WINAPI CLSIDFromString(LPOLESTR idstr, CLSID *id )
1448 return E_INVALIDARG;
1450 ret = __CLSIDFromString(idstr, id);
1451 if(ret != S_OK) { /* It appears a ProgID is also valid */
1452 ret = CLSIDFromProgID(idstr, id);
1458 /******************************************************************************
1459 * StringFromCLSID [OLE32.@]
1460 * StringFromIID [OLE32.@]
1462 * Converts a GUID into the respective string representation.
1463 * The target string is allocated using the OLE IMalloc.
1466 * id [I] the GUID to be converted.
1467 * idstr [O] A pointer to a to-be-allocated pointer pointing to the resulting string.
1474 * StringFromGUID2, CLSIDFromString
1476 HRESULT WINAPI StringFromCLSID(REFCLSID id, LPOLESTR *idstr)
1481 if ((ret = CoGetMalloc(0,&mllc))) return ret;
1482 if (!(*idstr = IMalloc_Alloc( mllc, CHARS_IN_GUID * sizeof(WCHAR) ))) return E_OUTOFMEMORY;
1483 StringFromGUID2( id, *idstr, CHARS_IN_GUID );
1487 /******************************************************************************
1488 * StringFromGUID2 [OLE32.@]
1490 * Modified version of StringFromCLSID that allows you to specify max
1494 * id [I] GUID to convert to string.
1495 * str [O] Buffer where the result will be stored.
1496 * cmax [I] Size of the buffer in characters.
1499 * Success: The length of the resulting string in characters.
1502 INT WINAPI StringFromGUID2(REFGUID id, LPOLESTR str, INT cmax)
1504 static const WCHAR formatW[] = { '{','%','0','8','X','-','%','0','4','X','-',
1505 '%','0','4','X','-','%','0','2','X','%','0','2','X','-',
1506 '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
1507 '%','0','2','X','%','0','2','X','}',0 };
1508 if (cmax < CHARS_IN_GUID) return 0;
1509 sprintfW( str, formatW, id->Data1, id->Data2, id->Data3,
1510 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
1511 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
1512 return CHARS_IN_GUID;
1515 /* open HKCR\\CLSID\\{string form of clsid}\\{keyname} key */
1516 HRESULT COM_OpenKeyForCLSID(REFCLSID clsid, LPCWSTR keyname, REGSAM access, HKEY *subkey)
1518 static const WCHAR wszCLSIDSlash[] = {'C','L','S','I','D','\\',0};
1519 WCHAR path[CHARS_IN_GUID + ARRAYSIZE(wszCLSIDSlash) - 1];
1523 strcpyW(path, wszCLSIDSlash);
1524 StringFromGUID2(clsid, path + strlenW(wszCLSIDSlash), CHARS_IN_GUID);
1525 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, path, 0, keyname ? KEY_READ : access, &key);
1526 if (res == ERROR_FILE_NOT_FOUND)
1527 return REGDB_E_CLASSNOTREG;
1528 else if (res != ERROR_SUCCESS)
1529 return REGDB_E_READREGDB;
1537 res = RegOpenKeyExW(key, keyname, 0, access, subkey);
1539 if (res == ERROR_FILE_NOT_FOUND)
1540 return REGDB_E_KEYMISSING;
1541 else if (res != ERROR_SUCCESS)
1542 return REGDB_E_READREGDB;
1547 /* open HKCR\\AppId\\{string form of appid clsid} key */
1548 HRESULT COM_OpenKeyForAppIdFromCLSID(REFCLSID clsid, REGSAM access, HKEY *subkey)
1550 static const WCHAR szAppId[] = { 'A','p','p','I','d',0 };
1551 static const WCHAR szAppIdKey[] = { 'A','p','p','I','d','\\',0 };
1553 WCHAR buf[CHARS_IN_GUID];
1554 WCHAR keyname[ARRAYSIZE(szAppIdKey) + CHARS_IN_GUID];
1560 /* read the AppID value under the class's key */
1561 hr = COM_OpenKeyForCLSID(clsid, NULL, KEY_READ, &hkey);
1566 res = RegQueryValueExW(hkey, szAppId, NULL, &type, (LPBYTE)buf, &size);
1568 if (res == ERROR_FILE_NOT_FOUND)
1569 return REGDB_E_KEYMISSING;
1570 else if (res != ERROR_SUCCESS || type!=REG_SZ)
1571 return REGDB_E_READREGDB;
1573 strcpyW(keyname, szAppIdKey);
1574 strcatW(keyname, buf);
1575 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, access, subkey);
1576 if (res == ERROR_FILE_NOT_FOUND)
1577 return REGDB_E_KEYMISSING;
1578 else if (res != ERROR_SUCCESS)
1579 return REGDB_E_READREGDB;
1584 /******************************************************************************
1585 * ProgIDFromCLSID [OLE32.@]
1587 * Converts a class id into the respective program ID.
1590 * clsid [I] Class ID, as found in registry.
1591 * ppszProgID [O] Associated ProgID.
1596 * REGDB_E_CLASSNOTREG if the given clsid has no associated ProgID
1598 HRESULT WINAPI ProgIDFromCLSID(REFCLSID clsid, LPOLESTR *ppszProgID)
1600 static const WCHAR wszProgID[] = {'P','r','o','g','I','D',0};
1607 ERR("ppszProgId isn't optional\n");
1608 return E_INVALIDARG;
1612 ret = COM_OpenKeyForCLSID(clsid, wszProgID, KEY_READ, &hkey);
1616 if (RegQueryValueW(hkey, NULL, NULL, &progidlen))
1617 ret = REGDB_E_CLASSNOTREG;
1621 *ppszProgID = CoTaskMemAlloc(progidlen * sizeof(WCHAR));
1624 if (RegQueryValueW(hkey, NULL, *ppszProgID, &progidlen))
1625 ret = REGDB_E_CLASSNOTREG;
1628 ret = E_OUTOFMEMORY;
1635 /******************************************************************************
1636 * CLSIDFromProgID [OLE32.@]
1638 * Converts a program id into the respective GUID.
1641 * progid [I] Unicode program ID, as found in registry.
1642 * clsid [O] Associated CLSID.
1646 * Failure: CO_E_CLASSSTRING - the given ProgID cannot be found.
1648 HRESULT WINAPI CLSIDFromProgID(LPCOLESTR progid, LPCLSID clsid)
1650 static const WCHAR clsidW[] = { '\\','C','L','S','I','D',0 };
1651 WCHAR buf2[CHARS_IN_GUID];
1652 LONG buf2len = sizeof(buf2);
1656 if (!progid || !clsid)
1658 ERR("neither progid (%p) nor clsid (%p) are optional\n", progid, clsid);
1659 return E_INVALIDARG;
1662 /* initialise clsid in case of failure */
1663 memset(clsid, 0, sizeof(*clsid));
1665 buf = HeapAlloc( GetProcessHeap(),0,(strlenW(progid)+8) * sizeof(WCHAR) );
1666 strcpyW( buf, progid );
1667 strcatW( buf, clsidW );
1668 if (RegOpenKeyW(HKEY_CLASSES_ROOT,buf,&xhkey))
1670 HeapFree(GetProcessHeap(),0,buf);
1671 WARN("couldn't open key for ProgID %s\n", debugstr_w(progid));
1672 return CO_E_CLASSSTRING;
1674 HeapFree(GetProcessHeap(),0,buf);
1676 if (RegQueryValueW(xhkey,NULL,buf2,&buf2len))
1679 WARN("couldn't query clsid value for ProgID %s\n", debugstr_w(progid));
1680 return CO_E_CLASSSTRING;
1683 return __CLSIDFromString(buf2,clsid);
1687 /*****************************************************************************
1688 * CoGetPSClsid [OLE32.@]
1690 * Retrieves the CLSID of the proxy/stub factory that implements
1691 * IPSFactoryBuffer for the specified interface.
1694 * riid [I] Interface whose proxy/stub CLSID is to be returned.
1695 * pclsid [O] Where to store returned proxy/stub CLSID.
1700 * REGDB_E_IIDNOTREG if no PSFactoryBuffer is associated with the IID, or it could not be parsed
1704 * The standard marshaller activates the object with the CLSID
1705 * returned and uses the CreateProxy and CreateStub methods on its
1706 * IPSFactoryBuffer interface to construct the proxies and stubs for a
1709 * CoGetPSClsid determines this CLSID by searching the
1710 * HKEY_CLASSES_ROOT\Interface\{string form of riid}\ProxyStubClsid32
1711 * in the registry and any interface id registered by
1712 * CoRegisterPSClsid within the current process.
1716 * Native returns S_OK for interfaces with a key in HKCR\Interface, but
1717 * without a ProxyStubClsid32 key and leaves garbage in pclsid. This should be
1718 * considered a bug in native unless an application depends on this (unlikely).
1721 * CoRegisterPSClsid.
1723 HRESULT WINAPI CoGetPSClsid(REFIID riid, CLSID *pclsid)
1725 static const WCHAR wszInterface[] = {'I','n','t','e','r','f','a','c','e','\\',0};
1726 static const WCHAR wszPSC[] = {'\\','P','r','o','x','y','S','t','u','b','C','l','s','i','d','3','2',0};
1727 WCHAR path[ARRAYSIZE(wszInterface) - 1 + CHARS_IN_GUID - 1 + ARRAYSIZE(wszPSC)];
1728 WCHAR value[CHARS_IN_GUID];
1731 APARTMENT *apt = COM_CurrentApt();
1732 struct registered_psclsid *registered_psclsid;
1734 TRACE("() riid=%s, pclsid=%p\n", debugstr_guid(riid), pclsid);
1738 ERR("apartment not initialised\n");
1739 return CO_E_NOTINITIALIZED;
1744 ERR("pclsid isn't optional\n");
1745 return E_INVALIDARG;
1748 EnterCriticalSection(&apt->cs);
1750 LIST_FOR_EACH_ENTRY(registered_psclsid, &apt->psclsids, struct registered_psclsid, entry)
1751 if (IsEqualIID(®istered_psclsid->iid, riid))
1753 *pclsid = registered_psclsid->clsid;
1754 LeaveCriticalSection(&apt->cs);
1758 LeaveCriticalSection(&apt->cs);
1760 /* Interface\\{string form of riid}\\ProxyStubClsid32 */
1761 strcpyW(path, wszInterface);
1762 StringFromGUID2(riid, path + ARRAYSIZE(wszInterface) - 1, CHARS_IN_GUID);
1763 strcpyW(path + ARRAYSIZE(wszInterface) - 1 + CHARS_IN_GUID - 1, wszPSC);
1765 /* Open the key.. */
1766 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, path, 0, KEY_READ, &hkey))
1768 WARN("No PSFactoryBuffer object is registered for IID %s\n", debugstr_guid(riid));
1769 return REGDB_E_IIDNOTREG;
1772 /* ... Once we have the key, query the registry to get the
1773 value of CLSID as a string, and convert it into a
1774 proper CLSID structure to be passed back to the app */
1775 len = sizeof(value);
1776 if (ERROR_SUCCESS != RegQueryValueW(hkey, NULL, value, &len))
1779 return REGDB_E_IIDNOTREG;
1783 /* We have the CLSID we want back from the registry as a string, so
1784 let's convert it into a CLSID structure */
1785 if (CLSIDFromString(value, pclsid) != NOERROR)
1786 return REGDB_E_IIDNOTREG;
1788 TRACE ("() Returning CLSID=%s\n", debugstr_guid(pclsid));
1792 /*****************************************************************************
1793 * CoRegisterPSClsid [OLE32.@]
1795 * Register a proxy/stub CLSID for the given interface in the current process
1799 * riid [I] Interface whose proxy/stub CLSID is to be registered.
1800 * rclsid [I] CLSID of the proxy/stub.
1804 * Failure: E_OUTOFMEMORY
1808 * This function does not add anything to the registry and the effects are
1809 * limited to the lifetime of the current process.
1814 HRESULT WINAPI CoRegisterPSClsid(REFIID riid, REFCLSID rclsid)
1816 APARTMENT *apt = COM_CurrentApt();
1817 struct registered_psclsid *registered_psclsid;
1819 TRACE("(%s, %s)\n", debugstr_guid(riid), debugstr_guid(rclsid));
1823 ERR("apartment not initialised\n");
1824 return CO_E_NOTINITIALIZED;
1827 EnterCriticalSection(&apt->cs);
1829 LIST_FOR_EACH_ENTRY(registered_psclsid, &apt->psclsids, struct registered_psclsid, entry)
1830 if (IsEqualIID(®istered_psclsid->iid, riid))
1832 registered_psclsid->clsid = *rclsid;
1833 LeaveCriticalSection(&apt->cs);
1837 registered_psclsid = HeapAlloc(GetProcessHeap(), 0, sizeof(struct registered_psclsid));
1838 if (!registered_psclsid)
1840 LeaveCriticalSection(&apt->cs);
1841 return E_OUTOFMEMORY;
1844 registered_psclsid->iid = *riid;
1845 registered_psclsid->clsid = *rclsid;
1846 list_add_head(&apt->psclsids, ®istered_psclsid->entry);
1848 LeaveCriticalSection(&apt->cs);
1855 * COM_GetRegisteredClassObject
1857 * This internal method is used to scan the registered class list to
1858 * find a class object.
1861 * rclsid Class ID of the class to find.
1862 * dwClsContext Class context to match.
1863 * ppv [out] returns a pointer to the class object. Complying
1864 * to normal COM usage, this method will increase the
1865 * reference count on this object.
1867 static HRESULT COM_GetRegisteredClassObject(const struct apartment *apt, REFCLSID rclsid,
1868 DWORD dwClsContext, LPUNKNOWN* ppUnk)
1870 HRESULT hr = S_FALSE;
1871 RegisteredClass *curClass;
1873 EnterCriticalSection( &csRegisteredClassList );
1875 LIST_FOR_EACH_ENTRY(curClass, &RegisteredClassList, RegisteredClass, entry)
1878 * Check if we have a match on the class ID and context.
1880 if ((apt->oxid == curClass->apartment_id) &&
1881 (dwClsContext & curClass->runContext) &&
1882 IsEqualGUID(&(curClass->classIdentifier), rclsid))
1885 * We have a match, return the pointer to the class object.
1887 *ppUnk = curClass->classObject;
1889 IUnknown_AddRef(curClass->classObject);
1896 LeaveCriticalSection( &csRegisteredClassList );
1901 /******************************************************************************
1902 * CoRegisterClassObject [OLE32.@]
1904 * Registers the class object for a given class ID. Servers housed in EXE
1905 * files use this method instead of exporting DllGetClassObject to allow
1906 * other code to connect to their objects.
1909 * rclsid [I] CLSID of the object to register.
1910 * pUnk [I] IUnknown of the object.
1911 * dwClsContext [I] CLSCTX flags indicating the context in which to run the executable.
1912 * flags [I] REGCLS flags indicating how connections are made.
1913 * lpdwRegister [I] A unique cookie that can be passed to CoRevokeClassObject.
1917 * E_INVALIDARG if lpdwRegister or pUnk are NULL,
1918 * CO_E_OBJISREG if the object is already registered. We should not return this.
1921 * CoRevokeClassObject, CoGetClassObject
1924 * In-process objects are only registered for the current apartment.
1925 * CoGetClassObject() and CoCreateInstance() will not return objects registered
1926 * in other apartments.
1929 * MSDN claims that multiple interface registrations are legal, but we
1930 * can't do that with our current implementation.
1932 HRESULT WINAPI CoRegisterClassObject(
1937 LPDWORD lpdwRegister)
1939 RegisteredClass* newClass;
1940 LPUNKNOWN foundObject;
1944 TRACE("(%s,%p,0x%08x,0x%08x,%p)\n",
1945 debugstr_guid(rclsid),pUnk,dwClsContext,flags,lpdwRegister);
1947 if ( (lpdwRegister==0) || (pUnk==0) )
1948 return E_INVALIDARG;
1950 apt = COM_CurrentApt();
1953 ERR("COM was not initialized\n");
1954 return CO_E_NOTINITIALIZED;
1959 /* REGCLS_MULTIPLEUSE implies registering as inproc server. This is what
1960 * differentiates the flag from REGCLS_MULTI_SEPARATE. */
1961 if (flags & REGCLS_MULTIPLEUSE)
1962 dwClsContext |= CLSCTX_INPROC_SERVER;
1965 * First, check if the class is already registered.
1966 * If it is, this should cause an error.
1968 hr = COM_GetRegisteredClassObject(apt, rclsid, dwClsContext, &foundObject);
1970 if (flags & REGCLS_MULTIPLEUSE) {
1971 if (dwClsContext & CLSCTX_LOCAL_SERVER)
1972 hr = CoLockObjectExternal(foundObject, TRUE, FALSE);
1973 IUnknown_Release(foundObject);
1976 IUnknown_Release(foundObject);
1977 ERR("object already registered for class %s\n", debugstr_guid(rclsid));
1978 return CO_E_OBJISREG;
1981 newClass = HeapAlloc(GetProcessHeap(), 0, sizeof(RegisteredClass));
1982 if ( newClass == NULL )
1983 return E_OUTOFMEMORY;
1985 newClass->classIdentifier = *rclsid;
1986 newClass->apartment_id = apt->oxid;
1987 newClass->runContext = dwClsContext;
1988 newClass->connectFlags = flags;
1989 newClass->pMarshaledData = NULL;
1990 newClass->RpcRegistration = NULL;
1993 * Use the address of the chain node as the cookie since we are sure it's
1994 * unique. FIXME: not on 64-bit platforms.
1996 newClass->dwCookie = (DWORD)newClass;
1999 * Since we're making a copy of the object pointer, we have to increase its
2002 newClass->classObject = pUnk;
2003 IUnknown_AddRef(newClass->classObject);
2005 EnterCriticalSection( &csRegisteredClassList );
2006 list_add_tail(&RegisteredClassList, &newClass->entry);
2007 LeaveCriticalSection( &csRegisteredClassList );
2009 *lpdwRegister = newClass->dwCookie;
2011 if (dwClsContext & CLSCTX_LOCAL_SERVER) {
2012 hr = CreateStreamOnHGlobal(0, TRUE, &newClass->pMarshaledData);
2014 FIXME("Failed to create stream on hglobal, %x\n", hr);
2017 hr = CoMarshalInterface(newClass->pMarshaledData, &IID_IClassFactory,
2018 newClass->classObject, MSHCTX_LOCAL, NULL,
2019 MSHLFLAGS_TABLESTRONG);
2021 FIXME("CoMarshalInterface failed, %x!\n",hr);
2025 hr = RPC_StartLocalServer(&newClass->classIdentifier,
2026 newClass->pMarshaledData,
2027 flags & (REGCLS_MULTIPLEUSE|REGCLS_MULTI_SEPARATE),
2028 &newClass->RpcRegistration);
2033 static void COM_RevokeRegisteredClassObject(RegisteredClass *curClass)
2035 list_remove(&curClass->entry);
2037 if (curClass->runContext & CLSCTX_LOCAL_SERVER)
2038 RPC_StopLocalServer(curClass->RpcRegistration);
2041 * Release the reference to the class object.
2043 IUnknown_Release(curClass->classObject);
2045 if (curClass->pMarshaledData)
2048 memset(&zero, 0, sizeof(zero));
2049 IStream_Seek(curClass->pMarshaledData, zero, STREAM_SEEK_SET, NULL);
2050 CoReleaseMarshalData(curClass->pMarshaledData);
2051 IStream_Release(curClass->pMarshaledData);
2054 HeapFree(GetProcessHeap(), 0, curClass);
2057 static void COM_RevokeAllClasses(const struct apartment *apt)
2059 RegisteredClass *curClass, *cursor;
2061 EnterCriticalSection( &csRegisteredClassList );
2063 LIST_FOR_EACH_ENTRY_SAFE(curClass, cursor, &RegisteredClassList, RegisteredClass, entry)
2065 if (curClass->apartment_id == apt->oxid)
2066 COM_RevokeRegisteredClassObject(curClass);
2069 LeaveCriticalSection( &csRegisteredClassList );
2072 /***********************************************************************
2073 * CoRevokeClassObject [OLE32.@]
2075 * Removes a class object from the class registry.
2078 * dwRegister [I] Cookie returned from CoRegisterClassObject().
2082 * Failure: HRESULT code.
2085 * Must be called from the same apartment that called CoRegisterClassObject(),
2086 * otherwise it will fail with RPC_E_WRONG_THREAD.
2089 * CoRegisterClassObject
2091 HRESULT WINAPI CoRevokeClassObject(
2094 HRESULT hr = E_INVALIDARG;
2095 RegisteredClass *curClass;
2098 TRACE("(%08x)\n",dwRegister);
2100 apt = COM_CurrentApt();
2103 ERR("COM was not initialized\n");
2104 return CO_E_NOTINITIALIZED;
2107 EnterCriticalSection( &csRegisteredClassList );
2109 LIST_FOR_EACH_ENTRY(curClass, &RegisteredClassList, RegisteredClass, entry)
2112 * Check if we have a match on the cookie.
2114 if (curClass->dwCookie == dwRegister)
2116 if (curClass->apartment_id == apt->oxid)
2118 COM_RevokeRegisteredClassObject(curClass);
2123 ERR("called from wrong apartment, should be called from %s\n",
2124 wine_dbgstr_longlong(curClass->apartment_id));
2125 hr = RPC_E_WRONG_THREAD;
2131 LeaveCriticalSection( &csRegisteredClassList );
2136 /***********************************************************************
2137 * COM_RegReadPath [internal]
2139 * Reads a registry value and expands it when necessary
2141 static DWORD COM_RegReadPath(HKEY hkeyroot, const WCHAR *keyname, const WCHAR *valuename, WCHAR * dst, DWORD dstlen)
2146 WCHAR src[MAX_PATH];
2147 DWORD dwLength = dstlen * sizeof(WCHAR);
2149 if((ret = RegOpenKeyExW(hkeyroot, keyname, 0, KEY_READ, &key)) == ERROR_SUCCESS) {
2150 if( (ret = RegQueryValueExW(key, NULL, NULL, &keytype, (LPBYTE)src, &dwLength)) == ERROR_SUCCESS ) {
2151 if (keytype == REG_EXPAND_SZ) {
2152 if (dstlen <= ExpandEnvironmentStringsW(src, dst, dstlen)) ret = ERROR_MORE_DATA;
2154 lstrcpynW(dst, src, dstlen);
2162 static void get_threading_model(HKEY key, LPWSTR value, DWORD len)
2164 static const WCHAR wszThreadingModel[] = {'T','h','r','e','a','d','i','n','g','M','o','d','e','l',0};
2167 DWORD dwLength = len * sizeof(WCHAR);
2169 ret = RegQueryValueExW(key, wszThreadingModel, NULL, &keytype, (LPBYTE)value, &dwLength);
2170 if ((ret != ERROR_SUCCESS) || (keytype != REG_SZ))
2174 static HRESULT get_inproc_class_object(APARTMENT *apt, HKEY hkeydll,
2175 REFCLSID rclsid, REFIID riid,
2176 BOOL hostifnecessary, void **ppv)
2178 WCHAR dllpath[MAX_PATH+1];
2179 BOOL apartment_threaded;
2181 if (hostifnecessary)
2183 static const WCHAR wszApartment[] = {'A','p','a','r','t','m','e','n','t',0};
2184 static const WCHAR wszFree[] = {'F','r','e','e',0};
2185 static const WCHAR wszBoth[] = {'B','o','t','h',0};
2186 WCHAR threading_model[10 /* strlenW(L"apartment")+1 */];
2188 get_threading_model(hkeydll, threading_model, ARRAYSIZE(threading_model));
2190 if (!strcmpiW(threading_model, wszApartment))
2192 apartment_threaded = TRUE;
2193 if (apt->multi_threaded)
2194 return apartment_hostobject_in_hostapt(apt, FALSE, FALSE, hkeydll, rclsid, riid, ppv);
2197 else if (!strcmpiW(threading_model, wszFree))
2199 apartment_threaded = FALSE;
2200 if (!apt->multi_threaded)
2201 return apartment_hostobject_in_hostapt(apt, TRUE, FALSE, hkeydll, rclsid, riid, ppv);
2203 /* everything except "Apartment", "Free" and "Both" */
2204 else if (strcmpiW(threading_model, wszBoth))
2206 apartment_threaded = TRUE;
2207 /* everything else is main-threaded */
2208 if (threading_model[0])
2209 FIXME("unrecognised threading model %s for object %s, should be main-threaded?\n",
2210 debugstr_w(threading_model), debugstr_guid(rclsid));
2212 if (apt->multi_threaded || !apt->main)
2213 return apartment_hostobject_in_hostapt(apt, FALSE, TRUE, hkeydll, rclsid, riid, ppv);
2216 apartment_threaded = FALSE;
2219 apartment_threaded = !apt->multi_threaded;
2221 if (COM_RegReadPath(hkeydll, NULL, NULL, dllpath, ARRAYSIZE(dllpath)) != ERROR_SUCCESS)
2223 /* failure: CLSID is not found in registry */
2224 WARN("class %s not registered inproc\n", debugstr_guid(rclsid));
2225 return REGDB_E_CLASSNOTREG;
2228 return apartment_getclassobject(apt, dllpath, apartment_threaded,
2232 /***********************************************************************
2233 * CoGetClassObject [OLE32.@]
2235 * Creates an object of the specified class.
2238 * rclsid [I] Class ID to create an instance of.
2239 * dwClsContext [I] Flags to restrict the location of the created instance.
2240 * pServerInfo [I] Optional. Details for connecting to a remote server.
2241 * iid [I] The ID of the interface of the instance to return.
2242 * ppv [O] On returns, contains a pointer to the specified interface of the object.
2246 * Failure: HRESULT code.
2249 * The dwClsContext parameter can be one or more of the following:
2250 *| CLSCTX_INPROC_SERVER - Use an in-process server, such as from a DLL.
2251 *| CLSCTX_INPROC_HANDLER - Use an in-process object which handles certain functions for an object running in another process.
2252 *| CLSCTX_LOCAL_SERVER - Connect to an object running in another process.
2253 *| CLSCTX_REMOTE_SERVER - Connect to an object running on another machine.
2256 * CoCreateInstance()
2258 HRESULT WINAPI CoGetClassObject(
2259 REFCLSID rclsid, DWORD dwClsContext, COSERVERINFO *pServerInfo,
2260 REFIID iid, LPVOID *ppv)
2262 LPUNKNOWN regClassObject;
2263 HRESULT hres = E_UNEXPECTED;
2265 BOOL release_apt = FALSE;
2267 TRACE("\n\tCLSID:\t%s,\n\tIID:\t%s\n", debugstr_guid(rclsid), debugstr_guid(iid));
2270 return E_INVALIDARG;
2274 if (!(apt = COM_CurrentApt()))
2276 if (!(apt = apartment_find_multi_threaded()))
2278 ERR("apartment not initialised\n");
2279 return CO_E_NOTINITIALIZED;
2285 FIXME("\tpServerInfo: name=%s\n",debugstr_w(pServerInfo->pwszName));
2286 FIXME("\t\tpAuthInfo=%p\n",pServerInfo->pAuthInfo);
2290 * First, try and see if we can't match the class ID with one of the
2291 * registered classes.
2293 if (S_OK == COM_GetRegisteredClassObject(apt, rclsid, dwClsContext,
2296 /* Get the required interface from the retrieved pointer. */
2297 hres = IUnknown_QueryInterface(regClassObject, iid, ppv);
2300 * Since QI got another reference on the pointer, we want to release the
2301 * one we already have. If QI was unsuccessful, this will release the object. This
2302 * is good since we are not returning it in the "out" parameter.
2304 IUnknown_Release(regClassObject);
2305 if (release_apt) apartment_release(apt);
2309 /* First try in-process server */
2310 if (CLSCTX_INPROC_SERVER & dwClsContext)
2312 static const WCHAR wszInprocServer32[] = {'I','n','p','r','o','c','S','e','r','v','e','r','3','2',0};
2315 if (IsEqualCLSID(rclsid, &CLSID_InProcFreeMarshaler))
2317 if (release_apt) apartment_release(apt);
2318 return FTMarshalCF_Create(iid, ppv);
2321 hres = COM_OpenKeyForCLSID(rclsid, wszInprocServer32, KEY_READ, &hkey);
2324 if (hres == REGDB_E_CLASSNOTREG)
2325 ERR("class %s not registered\n", debugstr_guid(rclsid));
2326 else if (hres == REGDB_E_KEYMISSING)
2328 WARN("class %s not registered as in-proc server\n", debugstr_guid(rclsid));
2329 hres = REGDB_E_CLASSNOTREG;
2333 if (SUCCEEDED(hres))
2335 hres = get_inproc_class_object(apt, hkey, rclsid, iid,
2336 !(dwClsContext & WINE_CLSCTX_DONT_HOST), ppv);
2340 /* return if we got a class, otherwise fall through to one of the
2342 if (SUCCEEDED(hres))
2344 if (release_apt) apartment_release(apt);
2349 /* Next try in-process handler */
2350 if (CLSCTX_INPROC_HANDLER & dwClsContext)
2352 static const WCHAR wszInprocHandler32[] = {'I','n','p','r','o','c','H','a','n','d','l','e','r','3','2',0};
2355 hres = COM_OpenKeyForCLSID(rclsid, wszInprocHandler32, KEY_READ, &hkey);
2358 if (hres == REGDB_E_CLASSNOTREG)
2359 ERR("class %s not registered\n", debugstr_guid(rclsid));
2360 else if (hres == REGDB_E_KEYMISSING)
2362 WARN("class %s not registered in-proc handler\n", debugstr_guid(rclsid));
2363 hres = REGDB_E_CLASSNOTREG;
2367 if (SUCCEEDED(hres))
2369 hres = get_inproc_class_object(apt, hkey, rclsid, iid,
2370 !(dwClsContext & WINE_CLSCTX_DONT_HOST), ppv);
2374 /* return if we got a class, otherwise fall through to one of the
2376 if (SUCCEEDED(hres))
2378 if (release_apt) apartment_release(apt);
2382 if (release_apt) apartment_release(apt);
2384 /* Next try out of process */
2385 if (CLSCTX_LOCAL_SERVER & dwClsContext)
2387 hres = RPC_GetLocalClassObject(rclsid,iid,ppv);
2388 if (SUCCEEDED(hres))
2392 /* Finally try remote: this requires networked DCOM (a lot of work) */
2393 if (CLSCTX_REMOTE_SERVER & dwClsContext)
2395 FIXME ("CLSCTX_REMOTE_SERVER not supported\n");
2396 hres = E_NOINTERFACE;
2400 ERR("no class object %s could be created for context 0x%x\n",
2401 debugstr_guid(rclsid), dwClsContext);
2405 /***********************************************************************
2406 * CoResumeClassObjects (OLE32.@)
2408 * Resumes all class objects registered with REGCLS_SUSPENDED.
2412 * Failure: HRESULT code.
2414 HRESULT WINAPI CoResumeClassObjects(void)
2420 /***********************************************************************
2421 * CoCreateInstance [OLE32.@]
2423 * Creates an instance of the specified class.
2426 * rclsid [I] Class ID to create an instance of.
2427 * pUnkOuter [I] Optional outer unknown to allow aggregation with another object.
2428 * dwClsContext [I] Flags to restrict the location of the created instance.
2429 * iid [I] The ID of the interface of the instance to return.
2430 * ppv [O] On returns, contains a pointer to the specified interface of the instance.
2434 * Failure: HRESULT code.
2437 * The dwClsContext parameter can be one or more of the following:
2438 *| CLSCTX_INPROC_SERVER - Use an in-process server, such as from a DLL.
2439 *| CLSCTX_INPROC_HANDLER - Use an in-process object which handles certain functions for an object running in another process.
2440 *| CLSCTX_LOCAL_SERVER - Connect to an object running in another process.
2441 *| CLSCTX_REMOTE_SERVER - Connect to an object running on another machine.
2443 * Aggregation is the concept of deferring the IUnknown of an object to another
2444 * object. This allows a separate object to behave as though it was part of
2445 * the object and to allow this the pUnkOuter parameter can be set. Note that
2446 * not all objects support having an outer of unknown.
2449 * CoGetClassObject()
2451 HRESULT WINAPI CoCreateInstance(
2453 LPUNKNOWN pUnkOuter,
2459 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 (!(apt = COM_CurrentApt()))
2478 if (!(apt = apartment_find_multi_threaded()))
2480 ERR("apartment not initialised\n");
2481 return CO_E_NOTINITIALIZED;
2483 apartment_release(apt);
2487 * The Standard Global Interface Table (GIT) object is a process-wide singleton.
2488 * Rather than create a class factory, we can just check for it here
2490 if (IsEqualIID(rclsid, &CLSID_StdGlobalInterfaceTable)) {
2491 if (StdGlobalInterfaceTableInstance == NULL)
2492 StdGlobalInterfaceTableInstance = StdGlobalInterfaceTable_Construct();
2493 hres = IGlobalInterfaceTable_QueryInterface( (IGlobalInterfaceTable*) StdGlobalInterfaceTableInstance, iid, ppv);
2494 if (hres) return hres;
2496 TRACE("Retrieved GIT (%p)\n", *ppv);
2501 * Get a class factory to construct the object we want.
2503 hres = CoGetClassObject(rclsid,
2513 * Create the object and don't forget to release the factory
2515 hres = IClassFactory_CreateInstance(lpclf, pUnkOuter, iid, ppv);
2516 IClassFactory_Release(lpclf);
2519 if (hres == CLASS_E_NOAGGREGATION && pUnkOuter)
2520 FIXME("Class %s does not support aggregation\n", debugstr_guid(rclsid));
2522 FIXME("no instance created for interface %s of class %s, hres is 0x%08x\n", debugstr_guid(iid), debugstr_guid(rclsid),hres);
2528 /***********************************************************************
2529 * CoCreateInstanceEx [OLE32.@]
2531 HRESULT WINAPI CoCreateInstanceEx(
2533 LPUNKNOWN pUnkOuter,
2535 COSERVERINFO* pServerInfo,
2539 IUnknown* pUnk = NULL;
2542 ULONG successCount = 0;
2547 if ( (cmq==0) || (pResults==NULL))
2548 return E_INVALIDARG;
2550 if (pServerInfo!=NULL)
2551 FIXME("() non-NULL pServerInfo not supported!\n");
2554 * Initialize all the "out" parameters.
2556 for (index = 0; index < cmq; index++)
2558 pResults[index].pItf = NULL;
2559 pResults[index].hr = E_NOINTERFACE;
2563 * Get the object and get its IUnknown pointer.
2565 hr = CoCreateInstance(rclsid,
2575 * Then, query for all the interfaces requested.
2577 for (index = 0; index < cmq; index++)
2579 pResults[index].hr = IUnknown_QueryInterface(pUnk,
2580 pResults[index].pIID,
2581 (VOID**)&(pResults[index].pItf));
2583 if (pResults[index].hr == S_OK)
2588 * Release our temporary unknown pointer.
2590 IUnknown_Release(pUnk);
2592 if (successCount == 0)
2593 return E_NOINTERFACE;
2595 if (successCount!=cmq)
2596 return CO_S_NOTALLINTERFACES;
2601 /***********************************************************************
2602 * CoLoadLibrary (OLE32.@)
2607 * lpszLibName [I] Path to library.
2608 * bAutoFree [I] Whether the library should automatically be freed.
2611 * Success: Handle to loaded library.
2615 * CoFreeLibrary, CoFreeAllLibraries, CoFreeUnusedLibraries
2617 HINSTANCE WINAPI CoLoadLibrary(LPOLESTR lpszLibName, BOOL bAutoFree)
2619 TRACE("(%s, %d)\n", debugstr_w(lpszLibName), bAutoFree);
2621 return LoadLibraryExW(lpszLibName, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
2624 /***********************************************************************
2625 * CoFreeLibrary [OLE32.@]
2627 * Unloads a library from memory.
2630 * hLibrary [I] Handle to library to unload.
2636 * CoLoadLibrary, CoFreeAllLibraries, CoFreeUnusedLibraries
2638 void WINAPI CoFreeLibrary(HINSTANCE hLibrary)
2640 FreeLibrary(hLibrary);
2644 /***********************************************************************
2645 * CoFreeAllLibraries [OLE32.@]
2647 * Function for backwards compatibility only. Does nothing.
2653 * CoLoadLibrary, CoFreeLibrary, CoFreeUnusedLibraries
2655 void WINAPI CoFreeAllLibraries(void)
2660 /***********************************************************************
2661 * CoFreeUnusedLibrariesEx [OLE32.@]
2663 * Frees any previously unused libraries whose delay has expired and marks
2664 * currently unused libraries for unloading. Unused are identified as those that
2665 * return S_OK from their DllCanUnloadNow function.
2668 * dwUnloadDelay [I] Unload delay in milliseconds.
2669 * dwReserved [I] Reserved. Set to 0.
2675 * CoLoadLibrary, CoFreeAllLibraries, CoFreeLibrary
2677 void WINAPI CoFreeUnusedLibrariesEx(DWORD dwUnloadDelay, DWORD dwReserved)
2679 struct apartment *apt = COM_CurrentApt();
2682 ERR("apartment not initialised\n");
2686 apartment_freeunusedlibraries(apt, dwUnloadDelay);
2689 /***********************************************************************
2690 * CoFreeUnusedLibraries [OLE32.@]
2692 * Frees any unused libraries. Unused are identified as those that return
2693 * S_OK from their DllCanUnloadNow function.
2699 * CoLoadLibrary, CoFreeAllLibraries, CoFreeLibrary
2701 void WINAPI CoFreeUnusedLibraries(void)
2703 CoFreeUnusedLibrariesEx(INFINITE, 0);
2706 /***********************************************************************
2707 * CoFileTimeNow [OLE32.@]
2709 * Retrieves the current time in FILETIME format.
2712 * lpFileTime [O] The current time.
2717 HRESULT WINAPI CoFileTimeNow( FILETIME *lpFileTime )
2719 GetSystemTimeAsFileTime( lpFileTime );
2723 /******************************************************************************
2724 * CoLockObjectExternal [OLE32.@]
2726 * Increments or decrements the external reference count of a stub object.
2729 * pUnk [I] Stub object.
2730 * fLock [I] If TRUE then increments the external ref-count,
2731 * otherwise decrements.
2732 * fLastUnlockReleases [I] If TRUE then the last unlock has the effect of
2733 * calling CoDisconnectObject.
2737 * Failure: HRESULT code.
2740 * If fLock is TRUE and an object is passed in that doesn't have a stub
2741 * manager then a new stub manager is created for the object.
2743 HRESULT WINAPI CoLockObjectExternal(
2746 BOOL fLastUnlockReleases)
2748 struct stub_manager *stubmgr;
2749 struct apartment *apt;
2751 TRACE("pUnk=%p, fLock=%s, fLastUnlockReleases=%s\n",
2752 pUnk, fLock ? "TRUE" : "FALSE", fLastUnlockReleases ? "TRUE" : "FALSE");
2754 apt = COM_CurrentApt();
2755 if (!apt) return CO_E_NOTINITIALIZED;
2757 stubmgr = get_stub_manager_from_object(apt, pUnk);
2762 stub_manager_ext_addref(stubmgr, 1, FALSE);
2764 stub_manager_ext_release(stubmgr, 1, FALSE, fLastUnlockReleases);
2766 stub_manager_int_release(stubmgr);
2772 stubmgr = new_stub_manager(apt, pUnk);
2776 stub_manager_ext_addref(stubmgr, 1, FALSE);
2777 stub_manager_int_release(stubmgr);
2784 WARN("stub object not found %p\n", pUnk);
2785 /* Note: native is pretty broken here because it just silently
2786 * fails, without returning an appropriate error code, making apps
2787 * think that the object was disconnected, when it actually wasn't */
2792 /***********************************************************************
2793 * CoInitializeWOW (OLE32.@)
2795 * WOW equivalent of CoInitialize?
2804 HRESULT WINAPI CoInitializeWOW(DWORD x,DWORD y)
2806 FIXME("(0x%08x,0x%08x),stub!\n",x,y);
2810 /***********************************************************************
2811 * CoGetState [OLE32.@]
2813 * Retrieves the thread state object previously stored by CoSetState().
2816 * ppv [I] Address where pointer to object will be stored.
2820 * Failure: E_OUTOFMEMORY.
2823 * Crashes on all invalid ppv addresses, including NULL.
2824 * If the function returns a non-NULL object then the caller must release its
2825 * reference on the object when the object is no longer required.
2830 HRESULT WINAPI CoGetState(IUnknown ** ppv)
2832 struct oletls *info = COM_CurrentInfo();
2833 if (!info) return E_OUTOFMEMORY;
2839 IUnknown_AddRef(info->state);
2841 TRACE("apt->state=%p\n", info->state);
2847 /***********************************************************************
2848 * CoSetState [OLE32.@]
2850 * Sets the thread state object.
2853 * pv [I] Pointer to state object to be stored.
2856 * The system keeps a reference on the object while the object stored.
2860 * Failure: E_OUTOFMEMORY.
2862 HRESULT WINAPI CoSetState(IUnknown * pv)
2864 struct oletls *info = COM_CurrentInfo();
2865 if (!info) return E_OUTOFMEMORY;
2867 if (pv) IUnknown_AddRef(pv);
2871 TRACE("-- release %p now\n", info->state);
2872 IUnknown_Release(info->state);
2881 /******************************************************************************
2882 * CoTreatAsClass [OLE32.@]
2884 * Sets the TreatAs value of a class.
2887 * clsidOld [I] Class to set TreatAs value on.
2888 * clsidNew [I] The class the clsidOld should be treated as.
2892 * Failure: HRESULT code.
2897 HRESULT WINAPI CoTreatAsClass(REFCLSID clsidOld, REFCLSID clsidNew)
2899 static const WCHAR wszAutoTreatAs[] = {'A','u','t','o','T','r','e','a','t','A','s',0};
2900 static const WCHAR wszTreatAs[] = {'T','r','e','a','t','A','s',0};
2902 WCHAR szClsidNew[CHARS_IN_GUID];
2904 WCHAR auto_treat_as[CHARS_IN_GUID];
2905 LONG auto_treat_as_size = sizeof(auto_treat_as);
2908 res = COM_OpenKeyForCLSID(clsidOld, NULL, KEY_READ | KEY_WRITE, &hkey);
2911 if (!memcmp( clsidOld, clsidNew, sizeof(*clsidOld) ))
2913 if (!RegQueryValueW(hkey, wszAutoTreatAs, auto_treat_as, &auto_treat_as_size) &&
2914 CLSIDFromString(auto_treat_as, &id) == S_OK)
2916 if (RegSetValueW(hkey, wszTreatAs, REG_SZ, auto_treat_as, sizeof(auto_treat_as)))
2918 res = REGDB_E_WRITEREGDB;
2924 RegDeleteKeyW(hkey, wszTreatAs);
2928 else if (!StringFromGUID2(clsidNew, szClsidNew, ARRAYSIZE(szClsidNew)) &&
2929 !RegSetValueW(hkey, wszTreatAs, REG_SZ, szClsidNew, sizeof(szClsidNew)))
2931 res = REGDB_E_WRITEREGDB;
2936 if (hkey) RegCloseKey(hkey);
2940 /******************************************************************************
2941 * CoGetTreatAsClass [OLE32.@]
2943 * Gets the TreatAs value of a class.
2946 * clsidOld [I] Class to get the TreatAs value of.
2947 * clsidNew [I] The class the clsidOld should be treated as.
2951 * Failure: HRESULT code.
2956 HRESULT WINAPI CoGetTreatAsClass(REFCLSID clsidOld, LPCLSID clsidNew)
2958 static const WCHAR wszTreatAs[] = {'T','r','e','a','t','A','s',0};
2960 WCHAR szClsidNew[CHARS_IN_GUID];
2962 LONG len = sizeof(szClsidNew);
2964 FIXME("(%s,%p)\n", debugstr_guid(clsidOld), clsidNew);
2965 *clsidNew = *clsidOld; /* copy over old value */
2967 res = COM_OpenKeyForCLSID(clsidOld, wszTreatAs, KEY_READ, &hkey);
2973 if (RegQueryValueW(hkey, NULL, szClsidNew, &len))
2978 res = CLSIDFromString(szClsidNew,clsidNew);
2980 ERR("Failed CLSIDFromStringA(%s), hres 0x%08x\n", debugstr_w(szClsidNew), res);
2982 if (hkey) RegCloseKey(hkey);
2986 /******************************************************************************
2987 * CoGetCurrentProcess [OLE32.@]
2989 * Gets the current process ID.
2992 * The current process ID.
2995 * Is DWORD really the correct return type for this function?
2997 DWORD WINAPI CoGetCurrentProcess(void)
2999 return GetCurrentProcessId();
3002 /******************************************************************************
3003 * CoRegisterMessageFilter [OLE32.@]
3005 * Registers a message filter.
3008 * lpMessageFilter [I] Pointer to interface.
3009 * lplpMessageFilter [O] Indirect pointer to prior instance if non-NULL.
3013 * Failure: HRESULT code.
3016 * Both lpMessageFilter and lplpMessageFilter are optional. Passing in a NULL
3017 * lpMessageFilter removes the message filter.
3019 * If lplpMessageFilter is not NULL the previous message filter will be
3020 * returned in the memory pointer to this parameter and the caller is
3021 * responsible for releasing the object.
3023 * The current thread be in an apartment otherwise the function will crash.
3025 HRESULT WINAPI CoRegisterMessageFilter(
3026 LPMESSAGEFILTER lpMessageFilter,
3027 LPMESSAGEFILTER *lplpMessageFilter)
3029 struct apartment *apt;
3030 IMessageFilter *lpOldMessageFilter;
3032 TRACE("(%p, %p)\n", lpMessageFilter, lplpMessageFilter);
3034 apt = COM_CurrentApt();
3036 /* can't set a message filter in a multi-threaded apartment */
3037 if (!apt || apt->multi_threaded)
3039 WARN("can't set message filter in MTA or uninitialized apt\n");
3040 return CO_E_NOT_SUPPORTED;
3043 if (lpMessageFilter)
3044 IMessageFilter_AddRef(lpMessageFilter);
3046 EnterCriticalSection(&apt->cs);
3048 lpOldMessageFilter = apt->filter;
3049 apt->filter = lpMessageFilter;
3051 LeaveCriticalSection(&apt->cs);
3053 if (lplpMessageFilter)
3054 *lplpMessageFilter = lpOldMessageFilter;
3055 else if (lpOldMessageFilter)
3056 IMessageFilter_Release(lpOldMessageFilter);
3061 /***********************************************************************
3062 * CoIsOle1Class [OLE32.@]
3064 * Determines whether the specified class an OLE v1 class.
3067 * clsid [I] Class to test.
3070 * TRUE if the class is an OLE v1 class, or FALSE otherwise.
3072 BOOL WINAPI CoIsOle1Class(REFCLSID clsid)
3074 FIXME("%s\n", debugstr_guid(clsid));
3078 /***********************************************************************
3079 * IsEqualGUID [OLE32.@]
3081 * Compares two Unique Identifiers.
3084 * rguid1 [I] The first GUID to compare.
3085 * rguid2 [I] The other GUID to compare.
3091 BOOL WINAPI IsEqualGUID(
3095 return !memcmp(rguid1,rguid2,sizeof(GUID));
3098 /***********************************************************************
3099 * CoInitializeSecurity [OLE32.@]
3101 HRESULT WINAPI CoInitializeSecurity(PSECURITY_DESCRIPTOR pSecDesc, LONG cAuthSvc,
3102 SOLE_AUTHENTICATION_SERVICE* asAuthSvc,
3103 void* pReserved1, DWORD dwAuthnLevel,
3104 DWORD dwImpLevel, void* pReserved2,
3105 DWORD dwCapabilities, void* pReserved3)
3107 FIXME("(%p,%d,%p,%p,%d,%d,%p,%d,%p) - stub!\n", pSecDesc, cAuthSvc,
3108 asAuthSvc, pReserved1, dwAuthnLevel, dwImpLevel, pReserved2,
3109 dwCapabilities, pReserved3);
3113 /***********************************************************************
3114 * CoSuspendClassObjects [OLE32.@]
3116 * Suspends all registered class objects to prevent further requests coming in
3117 * for those objects.
3121 * Failure: HRESULT code.
3123 HRESULT WINAPI CoSuspendClassObjects(void)
3129 /***********************************************************************
3130 * CoAddRefServerProcess [OLE32.@]
3132 * Helper function for incrementing the reference count of a local-server
3136 * New reference count.
3139 * CoReleaseServerProcess().
3141 ULONG WINAPI CoAddRefServerProcess(void)
3147 EnterCriticalSection(&csRegisteredClassList);
3148 refs = ++s_COMServerProcessReferences;
3149 LeaveCriticalSection(&csRegisteredClassList);
3151 TRACE("refs before: %d\n", refs - 1);
3156 /***********************************************************************
3157 * CoReleaseServerProcess [OLE32.@]
3159 * Helper function for decrementing the reference count of a local-server
3163 * New reference count.
3166 * When reference count reaches 0, this function suspends all registered
3167 * classes so no new connections are accepted.
3170 * CoAddRefServerProcess(), CoSuspendClassObjects().
3172 ULONG WINAPI CoReleaseServerProcess(void)
3178 EnterCriticalSection(&csRegisteredClassList);
3180 refs = --s_COMServerProcessReferences;
3181 /* FIXME: if (!refs) COM_SuspendClassObjects(); */
3183 LeaveCriticalSection(&csRegisteredClassList);
3185 TRACE("refs after: %d\n", refs);
3190 /***********************************************************************
3191 * CoIsHandlerConnected [OLE32.@]
3193 * Determines whether a proxy is connected to a remote stub.
3196 * pUnk [I] Pointer to object that may or may not be connected.
3199 * TRUE if pUnk is not a proxy or if pUnk is connected to a remote stub, or
3202 BOOL WINAPI CoIsHandlerConnected(IUnknown *pUnk)
3204 FIXME("%p\n", pUnk);
3209 /***********************************************************************
3210 * CoAllowSetForegroundWindow [OLE32.@]
3213 HRESULT WINAPI CoAllowSetForegroundWindow(IUnknown *pUnk, void *pvReserved)
3215 FIXME("(%p, %p): stub\n", pUnk, pvReserved);
3219 /***********************************************************************
3220 * CoQueryProxyBlanket [OLE32.@]
3222 * Retrieves the security settings being used by a proxy.
3225 * pProxy [I] Pointer to the proxy object.
3226 * pAuthnSvc [O] The type of authentication service.
3227 * pAuthzSvc [O] The type of authorization service.
3228 * ppServerPrincName [O] Optional. The server prinicple name.
3229 * pAuthnLevel [O] The authentication level.
3230 * pImpLevel [O] The impersonation level.
3231 * ppAuthInfo [O] Information specific to the authorization/authentication service.
3232 * pCapabilities [O] Flags affecting the security behaviour.
3236 * Failure: HRESULT code.
3239 * CoCopyProxy, CoSetProxyBlanket.
3241 HRESULT WINAPI CoQueryProxyBlanket(IUnknown *pProxy, DWORD *pAuthnSvc,
3242 DWORD *pAuthzSvc, OLECHAR **ppServerPrincName, DWORD *pAuthnLevel,
3243 DWORD *pImpLevel, void **ppAuthInfo, DWORD *pCapabilities)
3245 IClientSecurity *pCliSec;
3248 TRACE("%p\n", pProxy);
3250 hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (void **)&pCliSec);
3253 hr = IClientSecurity_QueryBlanket(pCliSec, pProxy, pAuthnSvc,
3254 pAuthzSvc, ppServerPrincName,
3255 pAuthnLevel, pImpLevel, ppAuthInfo,
3257 IClientSecurity_Release(pCliSec);
3260 if (FAILED(hr)) ERR("-- failed with 0x%08x\n", hr);
3264 /***********************************************************************
3265 * CoSetProxyBlanket [OLE32.@]
3267 * Sets the security settings for a proxy.
3270 * pProxy [I] Pointer to the proxy object.
3271 * AuthnSvc [I] The type of authentication service.
3272 * AuthzSvc [I] The type of authorization service.
3273 * pServerPrincName [I] The server prinicple name.
3274 * AuthnLevel [I] The authentication level.
3275 * ImpLevel [I] The impersonation level.
3276 * pAuthInfo [I] Information specific to the authorization/authentication service.
3277 * Capabilities [I] Flags affecting the security behaviour.
3281 * Failure: HRESULT code.
3284 * CoQueryProxyBlanket, CoCopyProxy.
3286 HRESULT WINAPI CoSetProxyBlanket(IUnknown *pProxy, DWORD AuthnSvc,
3287 DWORD AuthzSvc, OLECHAR *pServerPrincName, DWORD AuthnLevel,
3288 DWORD ImpLevel, void *pAuthInfo, DWORD Capabilities)
3290 IClientSecurity *pCliSec;
3293 TRACE("%p\n", pProxy);
3295 hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (void **)&pCliSec);
3298 hr = IClientSecurity_SetBlanket(pCliSec, pProxy, AuthnSvc,
3299 AuthzSvc, pServerPrincName,
3300 AuthnLevel, ImpLevel, pAuthInfo,
3302 IClientSecurity_Release(pCliSec);
3305 if (FAILED(hr)) ERR("-- failed with 0x%08x\n", hr);
3309 /***********************************************************************
3310 * CoCopyProxy [OLE32.@]
3315 * pProxy [I] Pointer to the proxy object.
3316 * ppCopy [O] Copy of the proxy.
3320 * Failure: HRESULT code.
3323 * CoQueryProxyBlanket, CoSetProxyBlanket.
3325 HRESULT WINAPI CoCopyProxy(IUnknown *pProxy, IUnknown **ppCopy)
3327 IClientSecurity *pCliSec;
3330 TRACE("%p\n", pProxy);
3332 hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (void **)&pCliSec);
3335 hr = IClientSecurity_CopyProxy(pCliSec, pProxy, ppCopy);
3336 IClientSecurity_Release(pCliSec);
3339 if (FAILED(hr)) ERR("-- failed with 0x%08x\n", hr);
3344 /***********************************************************************
3345 * CoGetCallContext [OLE32.@]
3347 * Gets the context of the currently executing server call in the current
3351 * riid [I] Context interface to return.
3352 * ppv [O] Pointer to memory that will receive the context on return.
3356 * Failure: HRESULT code.
3358 HRESULT WINAPI CoGetCallContext(REFIID riid, void **ppv)
3360 struct oletls *info = COM_CurrentInfo();
3362 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
3365 return E_OUTOFMEMORY;
3367 if (!info->call_state)
3368 return RPC_E_CALL_COMPLETE;
3370 return IUnknown_QueryInterface(info->call_state, riid, ppv);
3373 /***********************************************************************
3374 * CoSwitchCallContext [OLE32.@]
3376 * Switches the context of the currently executing server call in the current
3380 * pObject [I] Pointer to new context object
3381 * ppOldObject [O] Pointer to memory that will receive old context object pointer
3385 * Failure: HRESULT code.
3387 HRESULT WINAPI CoSwitchCallContext(IUnknown *pObject, IUnknown **ppOldObject)
3389 struct oletls *info = COM_CurrentInfo();
3391 TRACE("(%p, %p)\n", pObject, ppOldObject);
3394 return E_OUTOFMEMORY;
3396 *ppOldObject = info->call_state;
3397 info->call_state = pObject; /* CoSwitchCallContext does not addref nor release objects */
3402 /***********************************************************************
3403 * CoQueryClientBlanket [OLE32.@]
3405 * Retrieves the authentication information about the client of the currently
3406 * executing server call in the current thread.
3409 * pAuthnSvc [O] Optional. The type of authentication service.
3410 * pAuthzSvc [O] Optional. The type of authorization service.
3411 * pServerPrincName [O] Optional. The server prinicple name.
3412 * pAuthnLevel [O] Optional. The authentication level.
3413 * pImpLevel [O] Optional. The impersonation level.
3414 * pPrivs [O] Optional. Information about the privileges of the client.
3415 * pCapabilities [IO] Optional. Flags affecting the security behaviour.
3419 * Failure: HRESULT code.
3422 * CoImpersonateClient, CoRevertToSelf, CoGetCallContext.
3424 HRESULT WINAPI CoQueryClientBlanket(
3427 OLECHAR **pServerPrincName,
3430 RPC_AUTHZ_HANDLE *pPrivs,
3431 DWORD *pCapabilities)
3433 IServerSecurity *pSrvSec;
3436 TRACE("(%p, %p, %p, %p, %p, %p, %p)\n",
3437 pAuthnSvc, pAuthzSvc, pServerPrincName, pAuthnLevel, pImpLevel,
3438 pPrivs, pCapabilities);
3440 hr = CoGetCallContext(&IID_IServerSecurity, (void **)&pSrvSec);
3443 hr = IServerSecurity_QueryBlanket(
3444 pSrvSec, pAuthnSvc, pAuthzSvc, pServerPrincName, pAuthnLevel,
3445 pImpLevel, pPrivs, pCapabilities);
3446 IServerSecurity_Release(pSrvSec);
3452 /***********************************************************************
3453 * CoImpersonateClient [OLE32.@]
3455 * Impersonates the client of the currently executing server call in the
3463 * Failure: HRESULT code.
3466 * If this function fails then the current thread will not be impersonating
3467 * the client and all actions will take place on behalf of the server.
3468 * Therefore, it is important to check the return value from this function.
3471 * CoRevertToSelf, CoQueryClientBlanket, CoGetCallContext.
3473 HRESULT WINAPI CoImpersonateClient(void)
3475 IServerSecurity *pSrvSec;
3480 hr = CoGetCallContext(&IID_IServerSecurity, (void **)&pSrvSec);
3483 hr = IServerSecurity_ImpersonateClient(pSrvSec);
3484 IServerSecurity_Release(pSrvSec);
3490 /***********************************************************************
3491 * CoRevertToSelf [OLE32.@]
3493 * Ends the impersonation of the client of the currently executing server
3494 * call in the current thread.
3501 * Failure: HRESULT code.
3504 * CoImpersonateClient, CoQueryClientBlanket, CoGetCallContext.
3506 HRESULT WINAPI CoRevertToSelf(void)
3508 IServerSecurity *pSrvSec;
3513 hr = CoGetCallContext(&IID_IServerSecurity, (void **)&pSrvSec);
3516 hr = IServerSecurity_RevertToSelf(pSrvSec);
3517 IServerSecurity_Release(pSrvSec);
3523 static BOOL COM_PeekMessage(struct apartment *apt, MSG *msg)
3525 /* first try to retrieve messages for incoming COM calls to the apartment window */
3526 return PeekMessageW(msg, apt->win, WM_USER, WM_APP - 1, PM_REMOVE|PM_NOYIELD) ||
3527 /* next retrieve other messages necessary for the app to remain responsive */
3528 PeekMessageW(msg, NULL, 0, 0, PM_QS_PAINT|PM_QS_POSTMESSAGE|PM_REMOVE|PM_NOYIELD);
3531 /***********************************************************************
3532 * CoWaitForMultipleHandles [OLE32.@]
3534 * Waits for one or more handles to become signaled.
3537 * dwFlags [I] Flags. See notes.
3538 * dwTimeout [I] Timeout in milliseconds.
3539 * cHandles [I] Number of handles pointed to by pHandles.
3540 * pHandles [I] Handles to wait for.
3541 * lpdwindex [O] Index of handle that was signaled.
3545 * Failure: RPC_S_CALLPENDING on timeout.
3549 * The dwFlags parameter can be zero or more of the following:
3550 *| COWAIT_WAITALL - Wait for all of the handles to become signaled.
3551 *| COWAIT_ALERTABLE - Allows a queued APC to run during the wait.
3554 * MsgWaitForMultipleObjects, WaitForMultipleObjects.
3556 HRESULT WINAPI CoWaitForMultipleHandles(DWORD dwFlags, DWORD dwTimeout,
3557 ULONG cHandles, LPHANDLE pHandles, LPDWORD lpdwindex)
3560 DWORD start_time = GetTickCount();
3561 APARTMENT *apt = COM_CurrentApt();
3562 BOOL message_loop = apt && !apt->multi_threaded;
3564 TRACE("(0x%08x, 0x%08x, %d, %p, %p)\n", dwFlags, dwTimeout, cHandles,
3565 pHandles, lpdwindex);
3569 DWORD now = GetTickCount();
3572 if (now - start_time > dwTimeout)
3574 hr = RPC_S_CALLPENDING;
3580 DWORD wait_flags = ((dwFlags & COWAIT_WAITALL) ? MWMO_WAITALL : 0) |
3581 ((dwFlags & COWAIT_ALERTABLE ) ? MWMO_ALERTABLE : 0);
3583 TRACE("waiting for rpc completion or window message\n");
3585 res = MsgWaitForMultipleObjectsEx(cHandles, pHandles,
3586 (dwTimeout == INFINITE) ? INFINITE : start_time + dwTimeout - now,
3587 QS_ALLINPUT, wait_flags);
3589 if (res == WAIT_OBJECT_0 + cHandles) /* messages available */
3593 /* call message filter */
3595 if (COM_CurrentApt()->filter)
3597 PENDINGTYPE pendingtype =
3598 COM_CurrentInfo()->pending_call_count_server ?
3599 PENDINGTYPE_NESTED : PENDINGTYPE_TOPLEVEL;
3600 DWORD be_handled = IMessageFilter_MessagePending(
3601 COM_CurrentApt()->filter, 0 /* FIXME */,
3602 now - start_time, pendingtype);
3603 TRACE("IMessageFilter_MessagePending returned %d\n", be_handled);
3606 case PENDINGMSG_CANCELCALL:
3607 WARN("call canceled\n");
3608 hr = RPC_E_CALL_CANCELED;
3610 case PENDINGMSG_WAITNOPROCESS:
3611 case PENDINGMSG_WAITDEFPROCESS:
3613 /* FIXME: MSDN is very vague about the difference
3614 * between WAITNOPROCESS and WAITDEFPROCESS - there
3615 * appears to be none, so it is possibly a left-over
3616 * from the 16-bit world. */
3621 /* note: using "if" here instead of "while" might seem less
3622 * efficient, but only if we are optimising for quick delivery
3623 * of pending messages, rather than quick completion of the
3625 if (COM_PeekMessage(apt, &msg))
3627 TRACE("received message whilst waiting for RPC: 0x%04x\n", msg.message);
3628 TranslateMessage(&msg);
3629 DispatchMessageW(&msg);
3630 if (msg.message == WM_QUIT)
3632 TRACE("resending WM_QUIT to outer message loop\n");
3633 PostQuitMessage(msg.wParam);
3634 /* no longer need to process messages */
3635 message_loop = FALSE;
3643 TRACE("waiting for rpc completion\n");
3645 res = WaitForMultipleObjectsEx(cHandles, pHandles,
3646 (dwFlags & COWAIT_WAITALL) ? TRUE : FALSE,
3647 (dwTimeout == INFINITE) ? INFINITE : start_time + dwTimeout - now,
3648 (dwFlags & COWAIT_ALERTABLE) ? TRUE : FALSE);
3651 if (res < WAIT_OBJECT_0 + cHandles)
3653 /* handle signaled, store index */
3654 *lpdwindex = (res - WAIT_OBJECT_0);
3657 else if (res == WAIT_TIMEOUT)
3659 hr = RPC_S_CALLPENDING;
3664 ERR("Unexpected wait termination: %d, %d\n", res, GetLastError());
3669 TRACE("-- 0x%08x\n", hr);
3674 /***********************************************************************
3675 * CoGetObject [OLE32.@]
3677 * Gets the object named by converting the name to a moniker and binding to it.
3680 * pszName [I] String representing the object.
3681 * pBindOptions [I] Parameters affecting the binding to the named object.
3682 * riid [I] Interface to bind to on the objecct.
3683 * ppv [O] On output, the interface riid of the object represented
3688 * Failure: HRESULT code.
3691 * MkParseDisplayName.
3693 HRESULT WINAPI CoGetObject(LPCWSTR pszName, BIND_OPTS *pBindOptions,
3694 REFIID riid, void **ppv)
3701 hr = CreateBindCtx(0, &pbc);
3705 hr = IBindCtx_SetBindOptions(pbc, pBindOptions);
3712 hr = MkParseDisplayName(pbc, pszName, &chEaten, &pmk);
3715 hr = IMoniker_BindToObject(pmk, pbc, NULL, riid, ppv);
3716 IMoniker_Release(pmk);
3720 IBindCtx_Release(pbc);
3725 /***********************************************************************
3726 * CoRegisterChannelHook [OLE32.@]
3728 * Registers a process-wide hook that is called during ORPC calls.
3731 * guidExtension [I] GUID of the channel hook to register.
3732 * pChannelHook [I] Channel hook object to register.
3736 * Failure: HRESULT code.
3738 HRESULT WINAPI CoRegisterChannelHook(REFGUID guidExtension, IChannelHook *pChannelHook)
3740 TRACE("(%s, %p)\n", debugstr_guid(guidExtension), pChannelHook);
3742 return RPC_RegisterChannelHook(guidExtension, pChannelHook);
3745 typedef struct Context
3747 const IComThreadingInfoVtbl *lpVtbl;
3748 const IContextCallbackVtbl *lpCallbackVtbl;
3753 static inline Context *impl_from_IComThreadingInfo( IComThreadingInfo *iface )
3755 return (Context *)((char*)iface - FIELD_OFFSET(Context, lpVtbl));
3758 static inline Context *impl_from_IContextCallback( IContextCallback *iface )
3760 return (Context *)((char*)iface - FIELD_OFFSET(Context, lpCallbackVtbl));
3763 static HRESULT Context_QueryInterface(Context *iface, REFIID riid, LPVOID *ppv)
3767 if (IsEqualIID(riid, &IID_IComThreadingInfo) ||
3768 IsEqualIID(riid, &IID_IUnknown))
3770 *ppv = &iface->lpVtbl;
3771 } else if (IsEqualIID(riid, &IID_IContextCallback))
3773 *ppv = &iface->lpCallbackVtbl;
3778 IUnknown_AddRef((IUnknown*)*ppv);
3782 FIXME("interface not implemented %s\n", debugstr_guid(riid));
3783 return E_NOINTERFACE;
3786 static ULONG Context_AddRef(Context *This)
3788 return InterlockedIncrement(&This->refs);
3791 static ULONG Context_Release(Context *This)
3793 ULONG refs = InterlockedDecrement(&This->refs);
3795 HeapFree(GetProcessHeap(), 0, This);
3799 static HRESULT WINAPI Context_CTI_QueryInterface(IComThreadingInfo *iface, REFIID riid, LPVOID *ppv)
3801 Context *This = impl_from_IComThreadingInfo(iface);
3802 return Context_QueryInterface(This, riid, ppv);
3805 static ULONG WINAPI Context_CTI_AddRef(IComThreadingInfo *iface)
3807 Context *This = impl_from_IComThreadingInfo(iface);
3808 return Context_AddRef(This);
3811 static ULONG WINAPI Context_CTI_Release(IComThreadingInfo *iface)
3813 Context *This = impl_from_IComThreadingInfo(iface);
3814 return Context_Release(This);
3817 static HRESULT WINAPI Context_CTI_GetCurrentApartmentType(IComThreadingInfo *iface, APTTYPE *apttype)
3819 Context *This = impl_from_IComThreadingInfo(iface);
3821 TRACE("(%p)\n", apttype);
3823 *apttype = This->apttype;
3827 static HRESULT WINAPI Context_CTI_GetCurrentThreadType(IComThreadingInfo *iface, THDTYPE *thdtype)
3829 Context *This = impl_from_IComThreadingInfo(iface);
3831 TRACE("(%p)\n", thdtype);
3833 switch (This->apttype)
3836 case APTTYPE_MAINSTA:
3837 *thdtype = THDTYPE_PROCESSMESSAGES;
3840 *thdtype = THDTYPE_BLOCKMESSAGES;
3846 static HRESULT WINAPI Context_CTI_GetCurrentLogicalThreadId(IComThreadingInfo *iface, GUID *logical_thread_id)
3848 FIXME("(%p): stub\n", logical_thread_id);
3852 static HRESULT WINAPI Context_CTI_SetCurrentLogicalThreadId(IComThreadingInfo *iface, REFGUID logical_thread_id)
3854 FIXME("(%s): stub\n", debugstr_guid(logical_thread_id));
3858 static const IComThreadingInfoVtbl Context_Threading_Vtbl =
3860 Context_CTI_QueryInterface,
3862 Context_CTI_Release,
3863 Context_CTI_GetCurrentApartmentType,
3864 Context_CTI_GetCurrentThreadType,
3865 Context_CTI_GetCurrentLogicalThreadId,
3866 Context_CTI_SetCurrentLogicalThreadId
3869 static HRESULT WINAPI Context_CC_QueryInterface(IContextCallback *iface, REFIID riid, LPVOID *ppv)
3871 Context *This = impl_from_IContextCallback(iface);
3872 return Context_QueryInterface(This, riid, ppv);
3875 static ULONG WINAPI Context_CC_AddRef(IContextCallback *iface)
3877 Context *This = impl_from_IContextCallback(iface);
3878 return Context_AddRef(This);
3881 static ULONG WINAPI Context_CC_Release(IContextCallback *iface)
3883 Context *This = impl_from_IContextCallback(iface);
3884 return Context_Release(This);
3887 static HRESULT WINAPI Context_CC_ContextCallback(IContextCallback *iface, PFNCONTEXTCALL pCallback,
3888 ComCallData *param, REFIID riid, int method, IUnknown *punk)
3890 Context *This = impl_from_IContextCallback(iface);
3892 FIXME("(%p/%p)->(%p, %p, %s, %d, %p)\n", This, iface, pCallback, param, debugstr_guid(riid), method, punk);
3896 static const IContextCallbackVtbl Context_Callback_Vtbl =
3898 Context_CC_QueryInterface,
3901 Context_CC_ContextCallback
3905 /***********************************************************************
3906 * CoGetObjectContext [OLE32.@]
3908 * Retrieves an object associated with the current context (i.e. apartment).
3911 * riid [I] ID of the interface of the object to retrieve.
3912 * ppv [O] Address where object will be stored on return.
3916 * Failure: HRESULT code.
3918 HRESULT WINAPI CoGetObjectContext(REFIID riid, void **ppv)
3920 APARTMENT *apt = COM_CurrentApt();
3924 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
3929 ERR("apartment not initialised\n");
3930 return CO_E_NOTINITIALIZED;
3933 context = HeapAlloc(GetProcessHeap(), 0, sizeof(*context));
3935 return E_OUTOFMEMORY;
3937 context->lpVtbl = &Context_Threading_Vtbl;
3938 context->lpCallbackVtbl = &Context_Callback_Vtbl;
3940 if (apt->multi_threaded)
3941 context->apttype = APTTYPE_MTA;
3943 context->apttype = APTTYPE_MAINSTA;
3945 context->apttype = APTTYPE_STA;
3947 hr = IUnknown_QueryInterface((IUnknown *)&context->lpVtbl, riid, ppv);
3948 IUnknown_Release((IUnknown *)&context->lpVtbl);
3954 /***********************************************************************
3955 * CoGetContextToken [OLE32.@]
3957 HRESULT WINAPI CoGetContextToken( ULONG_PTR *token )
3959 struct oletls *info = COM_CurrentInfo();
3961 if(!(calls++)) FIXME( "stub\n" );
3963 return E_OUTOFMEMORY;
3964 if (token) *token = info->context_token;
3969 /***********************************************************************
3972 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID fImpLoad)
3974 TRACE("%p 0x%x %p\n", hinstDLL, fdwReason, fImpLoad);
3977 case DLL_PROCESS_ATTACH:
3978 hProxyDll = hinstDLL;
3979 COMPOBJ_InitProcess();
3980 if (TRACE_ON(ole)) CoRegisterMallocSpy((LPVOID)-1);
3983 case DLL_PROCESS_DETACH:
3984 if (TRACE_ON(ole)) CoRevokeMallocSpy();
3985 OLEDD_UnInitialize();
3986 COMPOBJ_UninitProcess();
3987 RPC_UnregisterAllChannelHooks();
3988 COMPOBJ_DllList_Free();
3991 case DLL_THREAD_DETACH:
3998 /* NOTE: DllRegisterServer and DllUnregisterServer are in regsvr.c */