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
37 * - Make all ole interface marshaling use NDR to be wire compatible with
50 #define NONAMELESSUNION
51 #define NONAMELESSSTRUCT
62 #include "compobj_private.h"
64 #include "wine/unicode.h"
65 #include "wine/debug.h"
67 WINE_DEFAULT_DEBUG_CHANNEL(ole);
69 HINSTANCE OLE32_hInstance = 0; /* FIXME: make static ... */
71 #define ARRAYSIZE(array) (sizeof(array)/sizeof((array)[0]))
73 /****************************************************************************
74 * This section defines variables internal to the COM module.
76 * TODO: Most of these things will have to be made thread-safe.
79 static HRESULT COM_GetRegisteredClassObject(REFCLSID rclsid, DWORD dwClsContext, LPUNKNOWN* ppUnk);
80 static void COM_RevokeAllClasses(void);
81 static HRESULT get_inproc_class_object(HKEY hkeydll, REFCLSID rclsid, REFIID riid, void **ppv);
83 static APARTMENT *MTA; /* protected by csApartment */
84 static APARTMENT *MainApartment; /* the first STA apartment */
85 static struct list apts = LIST_INIT( apts ); /* protected by csApartment */
87 static CRITICAL_SECTION csApartment;
88 static CRITICAL_SECTION_DEBUG critsect_debug =
91 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
92 0, 0, { (DWORD_PTR)(__FILE__ ": csApartment") }
94 static CRITICAL_SECTION csApartment = { &critsect_debug, -1, 0, 0, 0, 0 };
96 struct registered_psclsid
104 * This lock count counts the number of times CoInitialize is called. It is
105 * decreased every time CoUninitialize is called. When it hits 0, the COM
106 * libraries are freed
108 static LONG s_COMLockCount = 0;
109 /* Reference count used by CoAddRefServerProcess/CoReleaseServerProcess */
110 static LONG s_COMServerProcessReferences = 0;
113 * This linked list contains the list of registered class objects. These
114 * are mostly used to register the factories for out-of-proc servers of OLE
117 * TODO: Make this data structure aware of inter-process communication. This
118 * means that parts of this will be exported to the Wine Server.
120 typedef struct tagRegisteredClass
123 CLSID classIdentifier;
124 LPUNKNOWN classObject;
128 LPSTREAM pMarshaledData; /* FIXME: only really need to store OXID and IPID */
129 void *RpcRegistration;
132 static struct list RegisteredClassList = LIST_INIT(RegisteredClassList);
134 static CRITICAL_SECTION csRegisteredClassList;
135 static CRITICAL_SECTION_DEBUG class_cs_debug =
137 0, 0, &csRegisteredClassList,
138 { &class_cs_debug.ProcessLocksList, &class_cs_debug.ProcessLocksList },
139 0, 0, { (DWORD_PTR)(__FILE__ ": csRegisteredClassList") }
141 static CRITICAL_SECTION csRegisteredClassList = { &class_cs_debug, -1, 0, 0, 0, 0 };
143 /*****************************************************************************
144 * This section contains OpenDllList definitions
146 * The OpenDllList contains only handles of dll loaded by CoGetClassObject or
147 * other functions that do LoadLibrary _without_ giving back a HMODULE.
148 * Without this list these handles would never be freed.
150 * FIXME: a DLL that says OK when asked for unloading is unloaded in the
151 * next unload-call but not before 600 sec.
154 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid, REFIID iid, LPVOID *ppv);
155 typedef HRESULT (WINAPI *DllCanUnloadNowFunc)(void);
157 typedef struct tagOpenDll
162 DllGetClassObjectFunc DllGetClassObject;
163 DllCanUnloadNowFunc DllCanUnloadNow;
167 static struct list openDllList = LIST_INIT(openDllList);
169 static CRITICAL_SECTION csOpenDllList;
170 static CRITICAL_SECTION_DEBUG dll_cs_debug =
172 0, 0, &csOpenDllList,
173 { &dll_cs_debug.ProcessLocksList, &dll_cs_debug.ProcessLocksList },
174 0, 0, { (DWORD_PTR)(__FILE__ ": csOpenDllList") }
176 static CRITICAL_SECTION csOpenDllList = { &dll_cs_debug, -1, 0, 0, 0, 0 };
178 struct apartment_loaded_dll
184 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',' ',
185 '0','x','#','#','#','#','#','#','#','#',' ',0};
186 static LRESULT CALLBACK apartment_wndproc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
187 static HRESULT apartment_getclassobject(struct apartment *apt, LPCWSTR dllpath,
188 REFCLSID rclsid, REFIID riid, void **ppv);
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);
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 (ie 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 * COM_AptWndProc by the user's code from the apartment in which the interface
209 memset(&wclass, 0, sizeof(wclass));
210 wclass.lpfnWndProc = apartment_wndproc;
211 wclass.hInstance = OLE32_hInstance;
212 wclass.lpszClassName = wszAptWinClass;
213 RegisterClassW(&wclass);
216 static void COMPOBJ_UninitProcess( void )
218 UnregisterClassW(wszAptWinClass, OLE32_hInstance);
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 HeapFree(GetProcessHeap(), 0, info);
230 NtCurrentTeb()->ReservedForOle = NULL;
234 /******************************************************************************
238 /* allocates memory and fills in the necessary fields for a new apartment
239 * object. must be called inside apartment cs */
240 static APARTMENT *apartment_construct(DWORD model)
244 TRACE("creating new apartment, model=%d\n", model);
246 apt = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*apt));
247 apt->tid = GetCurrentThreadId();
249 list_init(&apt->proxies);
250 list_init(&apt->stubmgrs);
251 list_init(&apt->psclsids);
252 list_init(&apt->loaded_dlls);
255 apt->remunk_exported = FALSE;
257 InitializeCriticalSection(&apt->cs);
258 DEBUG_SET_CRITSEC_NAME(&apt->cs, "apartment");
260 apt->multi_threaded = !(model & COINIT_APARTMENTTHREADED);
262 if (apt->multi_threaded)
264 /* FIXME: should be randomly generated by in an RPC call to rpcss */
265 apt->oxid = ((OXID)GetCurrentProcessId() << 32) | 0xcafe;
269 /* FIXME: should be randomly generated by in an RPC call to rpcss */
270 apt->oxid = ((OXID)GetCurrentProcessId() << 32) | GetCurrentThreadId();
273 TRACE("Created apartment on OXID %s\n", wine_dbgstr_longlong(apt->oxid));
275 list_add_head(&apts, &apt->entry);
280 /* gets and existing apartment if one exists or otherwise creates an apartment
281 * structure which stores OLE apartment-local information and stores a pointer
282 * to it in the thread-local storage */
283 static APARTMENT *apartment_get_or_create(DWORD model)
285 APARTMENT *apt = COM_CurrentApt();
289 if (model & COINIT_APARTMENTTHREADED)
291 EnterCriticalSection(&csApartment);
293 apt = apartment_construct(model);
298 TRACE("Created main-threaded apartment with OXID %s\n", wine_dbgstr_longlong(apt->oxid));
301 LeaveCriticalSection(&csApartment);
305 EnterCriticalSection(&csApartment);
307 /* The multi-threaded apartment (MTA) contains zero or more threads interacting
308 * with free threaded (ie thread safe) COM objects. There is only ever one MTA
312 TRACE("entering the multithreaded apartment %s\n", wine_dbgstr_longlong(MTA->oxid));
313 apartment_addref(MTA);
316 MTA = apartment_construct(model);
320 LeaveCriticalSection(&csApartment);
322 COM_CurrentInfo()->apt = apt;
328 static inline BOOL apartment_is_model(APARTMENT *apt, DWORD model)
330 return (apt->multi_threaded == !(model & COINIT_APARTMENTTHREADED));
333 DWORD apartment_addref(struct apartment *apt)
335 DWORD refs = InterlockedIncrement(&apt->refs);
336 TRACE("%s: before = %d\n", wine_dbgstr_longlong(apt->oxid), refs - 1);
340 DWORD apartment_release(struct apartment *apt)
344 EnterCriticalSection(&csApartment);
346 ret = InterlockedDecrement(&apt->refs);
347 TRACE("%s: after = %d\n", wine_dbgstr_longlong(apt->oxid), ret);
348 /* destruction stuff that needs to happen under csApartment CS */
351 if (apt == MTA) MTA = NULL;
352 else if (apt == MainApartment) MainApartment = NULL;
353 list_remove(&apt->entry);
356 LeaveCriticalSection(&csApartment);
360 struct list *cursor, *cursor2;
362 TRACE("destroying apartment %p, oxid %s\n", apt, wine_dbgstr_longlong(apt->oxid));
364 /* no locking is needed for this apartment, because no other thread
365 * can access it at this point */
367 apartment_disconnectproxies(apt);
369 if (apt->win) DestroyWindow(apt->win);
371 LIST_FOR_EACH_SAFE(cursor, cursor2, &apt->stubmgrs)
373 struct stub_manager *stubmgr = LIST_ENTRY(cursor, struct stub_manager, entry);
374 /* release the implicit reference given by the fact that the
375 * stub has external references (it must do since it is in the
376 * stub manager list in the apartment and all non-apartment users
377 * must have a ref on the apartment and so it cannot be destroyed).
379 stub_manager_int_release(stubmgr);
382 LIST_FOR_EACH_SAFE(cursor, cursor2, &apt->psclsids)
384 struct registered_psclsid *registered_psclsid =
385 LIST_ENTRY(cursor, struct registered_psclsid, entry);
387 list_remove(®istered_psclsid->entry);
388 HeapFree(GetProcessHeap(), 0, registered_psclsid);
391 /* if this assert fires, then another thread took a reference to a
392 * stub manager without taking a reference to the containing
393 * apartment, which it must do. */
394 assert(list_empty(&apt->stubmgrs));
396 if (apt->filter) IUnknown_Release(apt->filter);
398 while ((cursor = list_head(&apt->loaded_dlls)))
400 struct apartment_loaded_dll *apartment_loaded_dll = LIST_ENTRY(cursor, struct apartment_loaded_dll, entry);
401 COMPOBJ_DllList_ReleaseRef(apartment_loaded_dll->dll);
403 HeapFree(GetProcessHeap(), 0, apartment_loaded_dll);
406 DEBUG_CLEAR_CRITSEC_NAME(&apt->cs);
407 DeleteCriticalSection(&apt->cs);
409 HeapFree(GetProcessHeap(), 0, apt);
415 /* The given OXID must be local to this process:
417 * The ref parameter is here mostly to ensure people remember that
418 * they get one, you should normally take a ref for thread safety.
420 APARTMENT *apartment_findfromoxid(OXID oxid, BOOL ref)
422 APARTMENT *result = NULL;
425 EnterCriticalSection(&csApartment);
426 LIST_FOR_EACH( cursor, &apts )
428 struct apartment *apt = LIST_ENTRY( cursor, struct apartment, entry );
429 if (apt->oxid == oxid)
432 if (ref) apartment_addref(result);
436 LeaveCriticalSection(&csApartment);
441 /* gets the apartment which has a given creator thread ID. The caller must
442 * release the reference from the apartment as soon as the apartment pointer
443 * is no longer required. */
444 APARTMENT *apartment_findfromtid(DWORD tid)
446 APARTMENT *result = NULL;
449 EnterCriticalSection(&csApartment);
450 LIST_FOR_EACH( cursor, &apts )
452 struct apartment *apt = LIST_ENTRY( cursor, struct apartment, entry );
456 apartment_addref(result);
460 LeaveCriticalSection(&csApartment);
465 /* gets an apartment which has a given type. The caller must
466 * release the reference from the apartment as soon as the apartment pointer
467 * is no longer required. */
468 static APARTMENT *apartment_findfromtype(BOOL multi_threaded, BOOL main_apartment)
470 APARTMENT *result = NULL;
471 struct apartment *apt;
473 EnterCriticalSection(&csApartment);
475 if (!multi_threaded && main_apartment)
477 result = MainApartment;
478 if (result) apartment_addref(result);
479 LeaveCriticalSection(&csApartment);
483 LIST_FOR_EACH_ENTRY( apt, &apts, struct apartment, entry )
485 if (apt->multi_threaded == multi_threaded)
488 apartment_addref(result);
492 LeaveCriticalSection(&csApartment);
497 struct host_object_params
500 CLSID clsid; /* clsid of object to marshal */
501 IID iid; /* interface to marshal */
502 IStream *stream; /* stream that the object will be marshaled into */
505 static HRESULT apartment_hostobject(struct apartment *apt,
506 const struct host_object_params *params)
510 static const LARGE_INTEGER llZero;
511 WCHAR dllpath[MAX_PATH+1];
515 if (COM_RegReadPath(params->hkeydll, NULL, NULL, dllpath, ARRAYSIZE(dllpath)) != ERROR_SUCCESS)
517 /* failure: CLSID is not found in registry */
518 WARN("class %s not registered inproc\n", debugstr_guid(¶ms->clsid));
519 return REGDB_E_CLASSNOTREG;
522 hr = apartment_getclassobject(apt, dllpath, ¶ms->clsid, ¶ms->iid, (void **)&object);
526 hr = CoMarshalInterface(params->stream, ¶ms->iid, object, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
528 IUnknown_Release(object);
529 IStream_Seek(params->stream, llZero, STREAM_SEEK_SET, NULL);
534 static LRESULT CALLBACK apartment_wndproc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
539 RPC_ExecuteCall((struct dispatch_params *)lParam);
542 return apartment_hostobject(COM_CurrentApt(), (const struct host_object_params *)lParam);
544 return DefWindowProcW(hWnd, msg, wParam, lParam);
548 HRESULT apartment_createwindowifneeded(struct apartment *apt)
550 if (apt->multi_threaded)
555 HWND hwnd = CreateWindowW(wszAptWinClass, NULL, 0,
557 0, 0, OLE32_hInstance, NULL);
560 ERR("CreateWindow failed with error %d\n", GetLastError());
561 return HRESULT_FROM_WIN32(GetLastError());
563 if (InterlockedCompareExchangePointer((PVOID *)&apt->win, hwnd, NULL))
564 /* someone beat us to it */
571 HWND apartment_getwindow(struct apartment *apt)
573 assert(!apt->multi_threaded);
577 void apartment_joinmta(void)
579 apartment_addref(MTA);
580 COM_CurrentInfo()->apt = MTA;
583 static HRESULT apartment_getclassobject(struct apartment *apt, LPCWSTR dllpath,
584 REFCLSID rclsid, REFIID riid, void **ppv)
588 struct apartment_loaded_dll *apartment_loaded_dll;
590 EnterCriticalSection(&apt->cs);
592 LIST_FOR_EACH_ENTRY(apartment_loaded_dll, &apt->loaded_dlls, struct apartment_loaded_dll, entry)
593 if (!strcmpiW(dllpath, apartment_loaded_dll->dll->library_name))
595 TRACE("found %s already loaded\n", debugstr_w(dllpath));
602 apartment_loaded_dll = HeapAlloc(GetProcessHeap(), 0, sizeof(*apartment_loaded_dll));
603 if (!apartment_loaded_dll)
607 hr = COMPOBJ_DllList_Add( dllpath, &apartment_loaded_dll->dll );
609 HeapFree(GetProcessHeap(), 0, apartment_loaded_dll);
613 TRACE("added new loaded dll %s\n", debugstr_w(dllpath));
614 list_add_tail(&apt->loaded_dlls, &apartment_loaded_dll->entry);
618 LeaveCriticalSection(&apt->cs);
622 TRACE("calling DllGetClassObject %p\n", apartment_loaded_dll->dll->DllGetClassObject);
623 /* OK: get the ClassObject */
624 hr = apartment_loaded_dll->dll->DllGetClassObject(rclsid, riid, ppv);
627 ERR("DllGetClassObject returned error 0x%08x\n", hr);
633 static void apartment_freeunusedlibraries(struct apartment *apt)
635 struct apartment_loaded_dll *entry, *next;
636 EnterCriticalSection(&apt->cs);
637 LIST_FOR_EACH_ENTRY_SAFE(entry, next, &apt->loaded_dlls, struct apartment_loaded_dll, entry)
639 if (entry->dll->DllCanUnloadNow && (entry->dll->DllCanUnloadNow() == S_OK))
641 list_remove(&entry->entry);
642 COMPOBJ_DllList_ReleaseRef(entry->dll);
643 HeapFree(GetProcessHeap(), 0, entry);
646 LeaveCriticalSection(&apt->cs);
649 /*****************************************************************************
650 * This section contains OpenDllList implementation
653 /* caller must ensure that library_name is not already in the open dll list */
654 static HRESULT COMPOBJ_DllList_Add(LPCWSTR library_name, OpenDll **ret)
660 DllCanUnloadNowFunc DllCanUnloadNow;
661 DllGetClassObjectFunc DllGetClassObject;
665 *ret = COMPOBJ_DllList_Get(library_name);
666 if (*ret) return S_OK;
668 /* do this outside the csOpenDllList to avoid creating a lock dependency on
670 hLibrary = LoadLibraryExW(library_name, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
673 ERR("couldn't load in-process dll %s\n", debugstr_w(library_name));
674 /* failure: DLL could not be loaded */
675 return E_ACCESSDENIED; /* FIXME: or should this be CO_E_DLLNOTFOUND? */
678 DllCanUnloadNow = GetProcAddress(hLibrary, "DllCanUnloadNow");
679 /* Note: failing to find DllCanUnloadNow is not a failure */
680 DllGetClassObject = GetProcAddress(hLibrary, "DllGetClassObject");
681 if (!DllGetClassObject)
683 /* failure: the dll did not export DllGetClassObject */
684 ERR("couldn't find function DllGetClassObject in %s\n", debugstr_w(library_name));
685 FreeLibrary(hLibrary);
686 return CO_E_DLLNOTFOUND;
689 EnterCriticalSection( &csOpenDllList );
691 *ret = COMPOBJ_DllList_Get(library_name);
694 /* another caller to this function already added the dll while we
695 * weren't in the critical section */
696 FreeLibrary(hLibrary);
700 len = strlenW(library_name);
701 entry = HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
703 entry->library_name = HeapAlloc(GetProcessHeap(), 0, (len + 1)*sizeof(WCHAR));
704 if (entry && entry->library_name)
706 memcpy(entry->library_name, library_name, (len + 1)*sizeof(WCHAR));
707 entry->library = hLibrary;
709 entry->DllCanUnloadNow = DllCanUnloadNow;
710 entry->DllGetClassObject = DllGetClassObject;
711 list_add_tail(&openDllList, &entry->entry);
716 FreeLibrary(hLibrary);
721 LeaveCriticalSection( &csOpenDllList );
726 static OpenDll *COMPOBJ_DllList_Get(LPCWSTR library_name)
730 EnterCriticalSection(&csOpenDllList);
731 LIST_FOR_EACH_ENTRY(ptr, &openDllList, OpenDll, entry)
733 if (!strcmpiW(library_name, ptr->library_name) &&
734 (InterlockedIncrement(&ptr->refs) != 1) /* entry is being destroy if == 1 */)
740 LeaveCriticalSection(&csOpenDllList);
744 static void COMPOBJ_DllList_ReleaseRef(OpenDll *entry)
746 if (!InterlockedDecrement(&entry->refs))
748 EnterCriticalSection(&csOpenDllList);
749 list_remove(&entry->entry);
750 LeaveCriticalSection(&csOpenDllList);
752 TRACE("freeing %p\n", entry->library);
753 FreeLibrary(entry->library);
755 HeapFree(GetProcessHeap(), 0, entry->library_name);
756 HeapFree(GetProcessHeap(), 0, entry);
760 /******************************************************************************
761 * CoBuildVersion [OLE32.@]
762 * CoBuildVersion [COMPOBJ.1]
764 * Gets the build version of the DLL.
769 * Current build version, hiword is majornumber, loword is minornumber
771 DWORD WINAPI CoBuildVersion(void)
773 TRACE("Returning version %d, build %d.\n", rmm, rup);
774 return (rmm<<16)+rup;
777 /******************************************************************************
778 * CoInitialize [OLE32.@]
780 * Initializes the COM libraries by calling CoInitializeEx with
781 * COINIT_APARTMENTTHREADED, ie it enters a STA thread.
784 * lpReserved [I] Pointer to IMalloc interface (obsolete, should be NULL).
787 * Success: S_OK if not already initialized, S_FALSE otherwise.
788 * Failure: HRESULT code.
793 HRESULT WINAPI CoInitialize(LPVOID lpReserved)
796 * Just delegate to the newer method.
798 return CoInitializeEx(lpReserved, COINIT_APARTMENTTHREADED);
801 /******************************************************************************
802 * CoInitializeEx [OLE32.@]
804 * Initializes the COM libraries.
807 * lpReserved [I] Pointer to IMalloc interface (obsolete, should be NULL).
808 * dwCoInit [I] One or more flags from the COINIT enumeration. See notes.
811 * S_OK if successful,
812 * S_FALSE if this function was called already.
813 * RPC_E_CHANGED_MODE if a previous call to CoInitializeEx specified another
818 * The behavior used to set the IMalloc used for memory management is
820 * The dwCoInit parameter must specify one of the following apartment
822 *| COINIT_APARTMENTTHREADED - A single-threaded apartment (STA).
823 *| COINIT_MULTITHREADED - A multi-threaded apartment (MTA).
824 * The parameter may also specify zero or more of the following flags:
825 *| COINIT_DISABLE_OLE1DDE - Don't use DDE for OLE1 support.
826 *| COINIT_SPEED_OVER_MEMORY - Trade memory for speed.
831 HRESULT WINAPI CoInitializeEx(LPVOID lpReserved, DWORD dwCoInit)
836 TRACE("(%p, %x)\n", lpReserved, (int)dwCoInit);
838 if (lpReserved!=NULL)
840 ERR("(%p, %x) - Bad parameter passed-in %p, must be an old Windows Application\n", lpReserved, (int)dwCoInit, lpReserved);
844 * Check the lock count. If this is the first time going through the initialize
845 * process, we have to initialize the libraries.
847 * And crank-up that lock count.
849 if (InterlockedExchangeAdd(&s_COMLockCount,1)==0)
852 * Initialize the various COM libraries and data structures.
854 TRACE("() - Initializing the COM libraries\n");
856 /* we may need to defer this until after apartment initialisation */
857 RunningObjectTableImpl_Initialize();
860 if (!(apt = COM_CurrentInfo()->apt))
862 apt = apartment_get_or_create(dwCoInit);
863 if (!apt) return E_OUTOFMEMORY;
865 else if (!apartment_is_model(apt, dwCoInit))
867 /* Changing the threading model after it's been set is illegal. If this warning is triggered by Wine
868 code then we are probably using the wrong threading model to implement that API. */
869 ERR("Attempt to change threading model of this apartment from %s to %s\n",
870 apt->multi_threaded ? "multi-threaded" : "apartment threaded",
871 dwCoInit & COINIT_APARTMENTTHREADED ? "apartment threaded" : "multi-threaded");
872 return RPC_E_CHANGED_MODE;
877 COM_CurrentInfo()->inits++;
882 /* On COM finalization for a STA thread, the message queue is flushed to ensure no
883 pending RPCs are ignored. Non-COM messages are discarded at this point.
885 static void COM_FlushMessageQueue(void)
888 APARTMENT *apt = COM_CurrentApt();
890 if (!apt || !apt->win) return;
892 TRACE("Flushing STA message queue\n");
894 while (PeekMessageA(&message, NULL, 0, 0, PM_REMOVE))
896 if (message.hwnd != apt->win)
898 WARN("discarding message 0x%x for window %p\n", message.message, message.hwnd);
902 TranslateMessage(&message);
903 DispatchMessageA(&message);
907 /***********************************************************************
908 * CoUninitialize [OLE32.@]
910 * This method will decrement the refcount on the current apartment, freeing
911 * the resources associated with it if it is the last thread in the apartment.
912 * If the last apartment is freed, the function will additionally release
913 * any COM resources associated with the process.
923 void WINAPI CoUninitialize(void)
925 struct oletls * info = COM_CurrentInfo();
930 /* will only happen on OOM */
936 ERR("Mismatched CoUninitialize\n");
942 apartment_release(info->apt);
947 * Decrease the reference count.
948 * If we are back to 0 locks on the COM library, make sure we free
949 * all the associated data structures.
951 lCOMRefCnt = InterlockedExchangeAdd(&s_COMLockCount,-1);
954 TRACE("() - Releasing the COM libraries\n");
956 RunningObjectTableImpl_UnInitialize();
958 /* Release the references to the registered class objects */
959 COM_RevokeAllClasses();
961 /* This will free the loaded COM Dlls */
962 CoFreeAllLibraries();
964 /* This ensures we deal with any pending RPCs */
965 COM_FlushMessageQueue();
967 else if (lCOMRefCnt<1) {
968 ERR( "CoUninitialize() - not CoInitialized.\n" );
969 InterlockedExchangeAdd(&s_COMLockCount,1); /* restore the lock count. */
973 /******************************************************************************
974 * CoDisconnectObject [OLE32.@]
976 * Disconnects all connections to this object from remote processes. Dispatches
977 * pending RPCs while blocking new RPCs from occurring, and then calls
978 * IMarshal::DisconnectObject on the given object.
980 * Typically called when the object server is forced to shut down, for instance by
984 * lpUnk [I] The object whose stub should be disconnected.
985 * reserved [I] Reserved. Should be set to 0.
989 * Failure: HRESULT code.
992 * CoMarshalInterface, CoReleaseMarshalData, CoLockObjectExternal
994 HRESULT WINAPI CoDisconnectObject( LPUNKNOWN lpUnk, DWORD reserved )
1000 TRACE("(%p, 0x%08x)\n", lpUnk, reserved);
1002 hr = IUnknown_QueryInterface(lpUnk, &IID_IMarshal, (void **)&marshal);
1005 hr = IMarshal_DisconnectObject(marshal, reserved);
1006 IMarshal_Release(marshal);
1010 apt = COM_CurrentApt();
1012 return CO_E_NOTINITIALIZED;
1014 apartment_disconnectobject(apt, lpUnk);
1016 /* Note: native is pretty broken here because it just silently
1017 * fails, without returning an appropriate error code if the object was
1018 * not found, making apps think that the object was disconnected, when
1019 * it actually wasn't */
1024 /******************************************************************************
1025 * CoCreateGuid [OLE32.@]
1027 * Simply forwards to UuidCreate in RPCRT4.
1030 * pguid [O] Points to the GUID to initialize.
1034 * Failure: HRESULT code.
1039 HRESULT WINAPI CoCreateGuid(GUID *pguid)
1041 return UuidCreate(pguid);
1044 /******************************************************************************
1045 * CLSIDFromString [OLE32.@]
1046 * IIDFromString [OLE32.@]
1048 * Converts a unique identifier from its string representation into
1052 * idstr [I] The string representation of the GUID.
1053 * id [O] GUID converted from the string.
1057 * CO_E_CLASSSTRING if idstr is not a valid CLSID
1062 static HRESULT WINAPI __CLSIDFromString(LPCWSTR s, CLSID *id)
1068 memset( id, 0, sizeof (CLSID) );
1072 /* validate the CLSID string */
1073 if (strlenW(s) != 38)
1074 return CO_E_CLASSSTRING;
1076 if ((s[0]!='{') || (s[9]!='-') || (s[14]!='-') || (s[19]!='-') || (s[24]!='-') || (s[37]!='}'))
1077 return CO_E_CLASSSTRING;
1079 for (i=1; i<37; i++) {
1080 if ((i == 9)||(i == 14)||(i == 19)||(i == 24)) continue;
1081 if (!(((s[i] >= '0') && (s[i] <= '9')) ||
1082 ((s[i] >= 'a') && (s[i] <= 'f')) ||
1083 ((s[i] >= 'A') && (s[i] <= 'F'))))
1084 return CO_E_CLASSSTRING;
1087 TRACE("%s -> %p\n", debugstr_w(s), id);
1089 /* quick lookup table */
1090 memset(table, 0, 256);
1092 for (i = 0; i < 10; i++) {
1095 for (i = 0; i < 6; i++) {
1096 table['A' + i] = i+10;
1097 table['a' + i] = i+10;
1100 /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
1102 id->Data1 = (table[s[1]] << 28 | table[s[2]] << 24 | table[s[3]] << 20 | table[s[4]] << 16 |
1103 table[s[5]] << 12 | table[s[6]] << 8 | table[s[7]] << 4 | table[s[8]]);
1104 id->Data2 = table[s[10]] << 12 | table[s[11]] << 8 | table[s[12]] << 4 | table[s[13]];
1105 id->Data3 = table[s[15]] << 12 | table[s[16]] << 8 | table[s[17]] << 4 | table[s[18]];
1107 /* these are just sequential bytes */
1108 id->Data4[0] = table[s[20]] << 4 | table[s[21]];
1109 id->Data4[1] = table[s[22]] << 4 | table[s[23]];
1110 id->Data4[2] = table[s[25]] << 4 | table[s[26]];
1111 id->Data4[3] = table[s[27]] << 4 | table[s[28]];
1112 id->Data4[4] = table[s[29]] << 4 | table[s[30]];
1113 id->Data4[5] = table[s[31]] << 4 | table[s[32]];
1114 id->Data4[6] = table[s[33]] << 4 | table[s[34]];
1115 id->Data4[7] = table[s[35]] << 4 | table[s[36]];
1120 /*****************************************************************************/
1122 HRESULT WINAPI CLSIDFromString(LPOLESTR idstr, CLSID *id )
1127 return E_INVALIDARG;
1129 ret = __CLSIDFromString(idstr, id);
1130 if(ret != S_OK) { /* It appears a ProgID is also valid */
1131 ret = CLSIDFromProgID(idstr, id);
1136 /* Converts a GUID into the respective string representation. */
1137 HRESULT WINE_StringFromCLSID(
1138 const CLSID *id, /* [in] GUID to be converted */
1139 LPSTR idstr /* [out] pointer to buffer to contain converted guid */
1141 static const char hex[] = "0123456789ABCDEF";
1146 { ERR("called with id=Null\n");
1151 sprintf(idstr, "{%08X-%04X-%04X-%02X%02X-",
1152 id->Data1, id->Data2, id->Data3,
1153 id->Data4[0], id->Data4[1]);
1157 for (i = 2; i < 8; i++) {
1158 *s++ = hex[id->Data4[i]>>4];
1159 *s++ = hex[id->Data4[i] & 0xf];
1165 TRACE("%p->%s\n", id, idstr);
1171 /******************************************************************************
1172 * StringFromCLSID [OLE32.@]
1173 * StringFromIID [OLE32.@]
1175 * Converts a GUID into the respective string representation.
1176 * The target string is allocated using the OLE IMalloc.
1179 * id [I] the GUID to be converted.
1180 * idstr [O] A pointer to a to-be-allocated pointer pointing to the resulting string.
1187 * StringFromGUID2, CLSIDFromString
1189 HRESULT WINAPI StringFromCLSID(REFCLSID id, LPOLESTR *idstr)
1195 if ((ret = CoGetMalloc(0,&mllc)))
1198 ret=WINE_StringFromCLSID(id,buf);
1200 DWORD len = MultiByteToWideChar( CP_ACP, 0, buf, -1, NULL, 0 );
1201 *idstr = IMalloc_Alloc( mllc, len * sizeof(WCHAR) );
1202 MultiByteToWideChar( CP_ACP, 0, buf, -1, *idstr, len );
1207 /******************************************************************************
1208 * StringFromGUID2 [OLE32.@]
1209 * StringFromGUID2 [COMPOBJ.76]
1211 * Modified version of StringFromCLSID that allows you to specify max
1215 * id [I] GUID to convert to string.
1216 * str [O] Buffer where the result will be stored.
1217 * cmax [I] Size of the buffer in characters.
1220 * Success: The length of the resulting string in characters.
1223 INT WINAPI StringFromGUID2(REFGUID id, LPOLESTR str, INT cmax)
1227 if (WINE_StringFromCLSID(id,xguid))
1229 return MultiByteToWideChar( CP_ACP, 0, xguid, -1, str, cmax );
1232 /* open HKCR\\CLSID\\{string form of clsid}\\{keyname} key */
1233 HRESULT COM_OpenKeyForCLSID(REFCLSID clsid, LPCWSTR keyname, REGSAM access, HKEY *subkey)
1235 static const WCHAR wszCLSIDSlash[] = {'C','L','S','I','D','\\',0};
1236 WCHAR path[CHARS_IN_GUID + ARRAYSIZE(wszCLSIDSlash) - 1];
1240 strcpyW(path, wszCLSIDSlash);
1241 StringFromGUID2(clsid, path + strlenW(wszCLSIDSlash), CHARS_IN_GUID);
1242 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, path, 0, keyname ? KEY_READ : access, &key);
1243 if (res == ERROR_FILE_NOT_FOUND)
1244 return REGDB_E_CLASSNOTREG;
1245 else if (res != ERROR_SUCCESS)
1246 return REGDB_E_READREGDB;
1254 res = RegOpenKeyExW(key, keyname, 0, access, subkey);
1256 if (res == ERROR_FILE_NOT_FOUND)
1257 return REGDB_E_KEYMISSING;
1258 else if (res != ERROR_SUCCESS)
1259 return REGDB_E_READREGDB;
1264 /* open HKCR\\AppId\\{string form of appid clsid} key */
1265 HRESULT COM_OpenKeyForAppIdFromCLSID(REFCLSID clsid, REGSAM access, HKEY *subkey)
1267 static const WCHAR szAppId[] = { 'A','p','p','I','d',0 };
1268 static const WCHAR szAppIdKey[] = { 'A','p','p','I','d','\\',0 };
1270 WCHAR buf[CHARS_IN_GUID];
1271 WCHAR keyname[ARRAYSIZE(szAppIdKey) + CHARS_IN_GUID];
1277 /* read the AppID value under the class's key */
1278 hr = COM_OpenKeyForCLSID(clsid, NULL, KEY_READ, &hkey);
1283 res = RegQueryValueExW(hkey, szAppId, NULL, &type, (LPBYTE)buf, &size);
1285 if (res == ERROR_FILE_NOT_FOUND)
1286 return REGDB_E_KEYMISSING;
1287 else if (res != ERROR_SUCCESS || type!=REG_SZ)
1288 return REGDB_E_READREGDB;
1290 strcpyW(keyname, szAppIdKey);
1291 strcatW(keyname, buf);
1292 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, access, subkey);
1293 if (res == ERROR_FILE_NOT_FOUND)
1294 return REGDB_E_KEYMISSING;
1295 else if (res != ERROR_SUCCESS)
1296 return REGDB_E_READREGDB;
1301 /******************************************************************************
1302 * ProgIDFromCLSID [OLE32.@]
1304 * Converts a class id into the respective program ID.
1307 * clsid [I] Class ID, as found in registry.
1308 * ppszProgID [O] Associated ProgID.
1313 * REGDB_E_CLASSNOTREG if the given clsid has no associated ProgID
1315 HRESULT WINAPI ProgIDFromCLSID(REFCLSID clsid, LPOLESTR *ppszProgID)
1317 static const WCHAR wszProgID[] = {'P','r','o','g','I','D',0};
1324 ERR("ppszProgId isn't optional\n");
1325 return E_INVALIDARG;
1329 ret = COM_OpenKeyForCLSID(clsid, wszProgID, KEY_READ, &hkey);
1333 if (RegQueryValueW(hkey, NULL, NULL, &progidlen))
1334 ret = REGDB_E_CLASSNOTREG;
1338 *ppszProgID = CoTaskMemAlloc(progidlen * sizeof(WCHAR));
1341 if (RegQueryValueW(hkey, NULL, *ppszProgID, &progidlen))
1342 ret = REGDB_E_CLASSNOTREG;
1345 ret = E_OUTOFMEMORY;
1352 /******************************************************************************
1353 * CLSIDFromProgID [OLE32.@]
1355 * Converts a program id into the respective GUID.
1358 * progid [I] Unicode program ID, as found in registry.
1359 * clsid [O] Associated CLSID.
1363 * Failure: CO_E_CLASSSTRING - the given ProgID cannot be found.
1365 HRESULT WINAPI CLSIDFromProgID(LPCOLESTR progid, LPCLSID clsid)
1367 static const WCHAR clsidW[] = { '\\','C','L','S','I','D',0 };
1368 WCHAR buf2[CHARS_IN_GUID];
1369 LONG buf2len = sizeof(buf2);
1373 if (!progid || !clsid)
1375 ERR("neither progid (%p) nor clsid (%p) are optional\n", progid, clsid);
1376 return E_INVALIDARG;
1379 /* initialise clsid in case of failure */
1380 memset(clsid, 0, sizeof(*clsid));
1382 buf = HeapAlloc( GetProcessHeap(),0,(strlenW(progid)+8) * sizeof(WCHAR) );
1383 strcpyW( buf, progid );
1384 strcatW( buf, clsidW );
1385 if (RegOpenKeyW(HKEY_CLASSES_ROOT,buf,&xhkey))
1387 HeapFree(GetProcessHeap(),0,buf);
1388 WARN("couldn't open key for ProgID %s\n", debugstr_w(progid));
1389 return CO_E_CLASSSTRING;
1391 HeapFree(GetProcessHeap(),0,buf);
1393 if (RegQueryValueW(xhkey,NULL,buf2,&buf2len))
1396 WARN("couldn't query clsid value for ProgID %s\n", debugstr_w(progid));
1397 return CO_E_CLASSSTRING;
1400 return CLSIDFromString(buf2,clsid);
1404 /*****************************************************************************
1405 * CoGetPSClsid [OLE32.@]
1407 * Retrieves the CLSID of the proxy/stub factory that implements
1408 * IPSFactoryBuffer for the specified interface.
1411 * riid [I] Interface whose proxy/stub CLSID is to be returned.
1412 * pclsid [O] Where to store returned proxy/stub CLSID.
1417 * REGDB_E_IIDNOTREG if no PSFactoryBuffer is associated with the IID, or it could not be parsed
1421 * The standard marshaller activates the object with the CLSID
1422 * returned and uses the CreateProxy and CreateStub methods on its
1423 * IPSFactoryBuffer interface to construct the proxies and stubs for a
1426 * CoGetPSClsid determines this CLSID by searching the
1427 * HKEY_CLASSES_ROOT\Interface\{string form of riid}\ProxyStubClsid32
1428 * in the registry and any interface id registered by
1429 * CoRegisterPSClsid within the current process.
1433 * Native returns S_OK for interfaces with a key in HKCR\Interface, but
1434 * without a ProxyStubClsid32 key and leaves garbage in pclsid. This should be
1435 * considered a bug in native unless an application depends on this (unlikely).
1438 * CoRegisterPSClsid.
1440 HRESULT WINAPI CoGetPSClsid(REFIID riid, CLSID *pclsid)
1442 static const WCHAR wszInterface[] = {'I','n','t','e','r','f','a','c','e','\\',0};
1443 static const WCHAR wszPSC[] = {'\\','P','r','o','x','y','S','t','u','b','C','l','s','i','d','3','2',0};
1444 WCHAR path[ARRAYSIZE(wszInterface) - 1 + CHARS_IN_GUID - 1 + ARRAYSIZE(wszPSC)];
1445 WCHAR value[CHARS_IN_GUID];
1448 APARTMENT *apt = COM_CurrentApt();
1449 struct registered_psclsid *registered_psclsid;
1451 TRACE("() riid=%s, pclsid=%p\n", debugstr_guid(riid), pclsid);
1455 ERR("apartment not initialised\n");
1456 return CO_E_NOTINITIALIZED;
1461 ERR("pclsid isn't optional\n");
1462 return E_INVALIDARG;
1465 EnterCriticalSection(&apt->cs);
1467 LIST_FOR_EACH_ENTRY(registered_psclsid, &apt->psclsids, struct registered_psclsid, entry)
1468 if (IsEqualIID(®istered_psclsid->iid, riid))
1470 *pclsid = registered_psclsid->clsid;
1471 LeaveCriticalSection(&apt->cs);
1475 LeaveCriticalSection(&apt->cs);
1477 /* Interface\\{string form of riid}\\ProxyStubClsid32 */
1478 strcpyW(path, wszInterface);
1479 StringFromGUID2(riid, path + ARRAYSIZE(wszInterface) - 1, CHARS_IN_GUID);
1480 strcpyW(path + ARRAYSIZE(wszInterface) - 1 + CHARS_IN_GUID - 1, wszPSC);
1482 /* Open the key.. */
1483 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, path, 0, KEY_READ, &hkey))
1485 WARN("No PSFactoryBuffer object is registered for IID %s\n", debugstr_guid(riid));
1486 return REGDB_E_IIDNOTREG;
1489 /* ... Once we have the key, query the registry to get the
1490 value of CLSID as a string, and convert it into a
1491 proper CLSID structure to be passed back to the app */
1492 len = sizeof(value);
1493 if (ERROR_SUCCESS != RegQueryValueW(hkey, NULL, value, &len))
1496 return REGDB_E_IIDNOTREG;
1500 /* We have the CLSid we want back from the registry as a string, so
1501 lets convert it into a CLSID structure */
1502 if (CLSIDFromString(value, pclsid) != NOERROR)
1503 return REGDB_E_IIDNOTREG;
1505 TRACE ("() Returning CLSID=%s\n", debugstr_guid(pclsid));
1509 /*****************************************************************************
1510 * CoRegisterPSClsid [OLE32.@]
1512 * Register a proxy/stub CLSID for the given interface in the current process
1516 * riid [I] Interface whose proxy/stub CLSID is to be registered.
1517 * rclsid [I] CLSID of the proxy/stub.
1521 * Failure: E_OUTOFMEMORY
1525 * This function does not add anything to the registry and the effects are
1526 * limited to the lifetime of the current process.
1531 HRESULT WINAPI CoRegisterPSClsid(REFIID riid, REFCLSID rclsid)
1533 APARTMENT *apt = COM_CurrentApt();
1534 struct registered_psclsid *registered_psclsid;
1536 TRACE("(%s, %s)\n", debugstr_guid(riid), debugstr_guid(rclsid));
1540 ERR("apartment not initialised\n");
1541 return CO_E_NOTINITIALIZED;
1544 EnterCriticalSection(&apt->cs);
1546 LIST_FOR_EACH_ENTRY(registered_psclsid, &apt->psclsids, struct registered_psclsid, entry)
1547 if (IsEqualIID(®istered_psclsid->iid, riid))
1549 registered_psclsid->clsid = *rclsid;
1550 LeaveCriticalSection(&apt->cs);
1554 registered_psclsid = HeapAlloc(GetProcessHeap(), 0, sizeof(struct registered_psclsid));
1555 if (!registered_psclsid)
1557 LeaveCriticalSection(&apt->cs);
1558 return E_OUTOFMEMORY;
1561 registered_psclsid->iid = *riid;
1562 registered_psclsid->clsid = *rclsid;
1563 list_add_head(&apt->psclsids, ®istered_psclsid->entry);
1565 LeaveCriticalSection(&apt->cs);
1572 * COM_GetRegisteredClassObject
1574 * This internal method is used to scan the registered class list to
1575 * find a class object.
1578 * rclsid Class ID of the class to find.
1579 * dwClsContext Class context to match.
1580 * ppv [out] returns a pointer to the class object. Complying
1581 * to normal COM usage, this method will increase the
1582 * reference count on this object.
1584 static HRESULT COM_GetRegisteredClassObject(
1589 HRESULT hr = S_FALSE;
1590 RegisteredClass *curClass;
1597 EnterCriticalSection( &csRegisteredClassList );
1599 LIST_FOR_EACH_ENTRY(curClass, &RegisteredClassList, RegisteredClass, entry)
1602 * Check if we have a match on the class ID and context.
1604 if ((dwClsContext & curClass->runContext) &&
1605 IsEqualGUID(&(curClass->classIdentifier), rclsid))
1608 * We have a match, return the pointer to the class object.
1610 *ppUnk = curClass->classObject;
1612 IUnknown_AddRef(curClass->classObject);
1619 LeaveCriticalSection( &csRegisteredClassList );
1624 /******************************************************************************
1625 * CoRegisterClassObject [OLE32.@]
1627 * Registers the class object for a given class ID. Servers housed in EXE
1628 * files use this method instead of exporting DllGetClassObject to allow
1629 * other code to connect to their objects.
1632 * rclsid [I] CLSID of the object to register.
1633 * pUnk [I] IUnknown of the object.
1634 * dwClsContext [I] CLSCTX flags indicating the context in which to run the executable.
1635 * flags [I] REGCLS flags indicating how connections are made.
1636 * lpdwRegister [I] A unique cookie that can be passed to CoRevokeClassObject.
1640 * E_INVALIDARG if lpdwRegister or pUnk are NULL,
1641 * CO_E_OBJISREG if the object is already registered. We should not return this.
1644 * CoRevokeClassObject, CoGetClassObject
1647 * MSDN claims that multiple interface registrations are legal, but we
1648 * can't do that with our current implementation.
1650 HRESULT WINAPI CoRegisterClassObject(
1655 LPDWORD lpdwRegister)
1657 RegisteredClass* newClass;
1658 LPUNKNOWN foundObject;
1661 TRACE("(%s,%p,0x%08x,0x%08x,%p)\n",
1662 debugstr_guid(rclsid),pUnk,dwClsContext,flags,lpdwRegister);
1664 if ( (lpdwRegister==0) || (pUnk==0) )
1665 return E_INVALIDARG;
1667 if (!COM_CurrentApt())
1669 ERR("COM was not initialized\n");
1670 return CO_E_NOTINITIALIZED;
1675 /* REGCLS_MULTIPLEUSE implies registering as inproc server. This is what
1676 * differentiates the flag from REGCLS_MULTI_SEPARATE. */
1677 if (flags & REGCLS_MULTIPLEUSE)
1678 dwClsContext |= CLSCTX_INPROC_SERVER;
1681 * First, check if the class is already registered.
1682 * If it is, this should cause an error.
1684 hr = COM_GetRegisteredClassObject(rclsid, dwClsContext, &foundObject);
1686 if (flags & REGCLS_MULTIPLEUSE) {
1687 if (dwClsContext & CLSCTX_LOCAL_SERVER)
1688 hr = CoLockObjectExternal(foundObject, TRUE, FALSE);
1689 IUnknown_Release(foundObject);
1692 IUnknown_Release(foundObject);
1693 ERR("object already registered for class %s\n", debugstr_guid(rclsid));
1694 return CO_E_OBJISREG;
1697 newClass = HeapAlloc(GetProcessHeap(), 0, sizeof(RegisteredClass));
1698 if ( newClass == NULL )
1699 return E_OUTOFMEMORY;
1701 newClass->classIdentifier = *rclsid;
1702 newClass->runContext = dwClsContext;
1703 newClass->connectFlags = flags;
1704 newClass->pMarshaledData = NULL;
1705 newClass->RpcRegistration = NULL;
1708 * Use the address of the chain node as the cookie since we are sure it's
1709 * unique. FIXME: not on 64-bit platforms.
1711 newClass->dwCookie = (DWORD)newClass;
1714 * Since we're making a copy of the object pointer, we have to increase its
1717 newClass->classObject = pUnk;
1718 IUnknown_AddRef(newClass->classObject);
1720 EnterCriticalSection( &csRegisteredClassList );
1721 list_add_tail(&RegisteredClassList, &newClass->entry);
1722 LeaveCriticalSection( &csRegisteredClassList );
1724 *lpdwRegister = newClass->dwCookie;
1726 if (dwClsContext & CLSCTX_LOCAL_SERVER) {
1727 hr = CreateStreamOnHGlobal(0, TRUE, &newClass->pMarshaledData);
1729 FIXME("Failed to create stream on hglobal, %x\n", hr);
1732 hr = CoMarshalInterface(newClass->pMarshaledData, &IID_IClassFactory,
1733 newClass->classObject, MSHCTX_LOCAL, NULL,
1734 MSHLFLAGS_TABLESTRONG);
1736 FIXME("CoMarshalInterface failed, %x!\n",hr);
1740 hr = RPC_StartLocalServer(&newClass->classIdentifier,
1741 newClass->pMarshaledData,
1742 flags & (REGCLS_MULTIPLEUSE|REGCLS_MULTI_SEPARATE),
1743 &newClass->RpcRegistration);
1748 /***********************************************************************
1749 * CoRevokeClassObject [OLE32.@]
1751 * Removes a class object from the class registry.
1754 * dwRegister [I] Cookie returned from CoRegisterClassObject().
1758 * Failure: HRESULT code.
1761 * CoRegisterClassObject
1763 HRESULT WINAPI CoRevokeClassObject(
1766 HRESULT hr = E_INVALIDARG;
1767 RegisteredClass *curClass;
1769 TRACE("(%08x)\n",dwRegister);
1771 EnterCriticalSection( &csRegisteredClassList );
1773 LIST_FOR_EACH_ENTRY(curClass, &RegisteredClassList, RegisteredClass, entry)
1776 * Check if we have a match on the cookie.
1778 if (curClass->dwCookie == dwRegister)
1780 list_remove(&curClass->entry);
1782 if (curClass->runContext & CLSCTX_LOCAL_SERVER)
1783 RPC_StopLocalServer(curClass->RpcRegistration);
1786 * Release the reference to the class object.
1788 IUnknown_Release(curClass->classObject);
1790 if (curClass->pMarshaledData)
1793 memset(&zero, 0, sizeof(zero));
1794 IStream_Seek(curClass->pMarshaledData, zero, STREAM_SEEK_SET, NULL);
1795 CoReleaseMarshalData(curClass->pMarshaledData);
1799 * Free the memory used by the chain node.
1801 HeapFree(GetProcessHeap(), 0, curClass);
1808 LeaveCriticalSection( &csRegisteredClassList );
1813 /***********************************************************************
1814 * COM_RegReadPath [internal]
1816 * Reads a registry value and expands it when necessary
1818 static DWORD COM_RegReadPath(HKEY hkeyroot, const WCHAR *keyname, const WCHAR *valuename, WCHAR * dst, DWORD dstlen)
1823 WCHAR src[MAX_PATH];
1824 DWORD dwLength = dstlen * sizeof(WCHAR);
1826 if((ret = RegOpenKeyExW(hkeyroot, keyname, 0, KEY_READ, &key)) == ERROR_SUCCESS) {
1827 if( (ret = RegQueryValueExW(key, NULL, NULL, &keytype, (LPBYTE)src, &dwLength)) == ERROR_SUCCESS ) {
1828 if (keytype == REG_EXPAND_SZ) {
1829 if (dstlen <= ExpandEnvironmentStringsW(src, dst, dstlen)) ret = ERROR_MORE_DATA;
1831 lstrcpynW(dst, src, dstlen);
1839 static void get_threading_model(HKEY key, LPWSTR value, DWORD len)
1841 static const WCHAR wszThreadingModel[] = {'T','h','r','e','a','d','i','n','g','M','o','d','e','l',0};
1844 DWORD dwLength = len * sizeof(WCHAR);
1846 ret = RegQueryValueExW(key, wszThreadingModel, NULL, &keytype, (LPBYTE)value, &dwLength);
1847 if ((ret != ERROR_SUCCESS) || (keytype != REG_SZ))
1851 static HRESULT get_inproc_class_object(HKEY hkeydll, REFCLSID rclsid, REFIID riid, void **ppv)
1853 static const WCHAR wszApartment[] = {'A','p','a','r','t','m','e','n','t',0};
1854 static const WCHAR wszFree[] = {'F','r','e','e',0};
1855 static const WCHAR wszBoth[] = {'B','o','t','h',0};
1856 WCHAR dllpath[MAX_PATH+1];
1857 WCHAR threading_model[10 /* strlenW(L"apartment")+1 */];
1859 APARTMENT *apt = COM_CurrentApt();
1861 get_threading_model(hkeydll, threading_model, ARRAYSIZE(threading_model));
1863 if (!strcmpiW(threading_model, wszApartment))
1865 if (apt->multi_threaded)
1867 /* try to find an STA */
1868 APARTMENT *host_apt = apartment_findfromtype(FALSE, FALSE);
1870 FIXME("create a host apartment for apartment-threaded object %s\n", debugstr_guid(rclsid));
1873 struct host_object_params params;
1874 HWND hwnd = apartment_getwindow(host_apt);
1876 params.hkeydll = hkeydll;
1877 params.clsid = *rclsid;
1879 hr = CreateStreamOnHGlobal(NULL, TRUE, ¶ms.stream);
1882 hr = SendMessageW(hwnd, DM_HOSTOBJECT, 0, (LPARAM)¶ms);
1884 hr = CoUnmarshalInterface(params.stream, riid, ppv);
1885 IStream_Release(params.stream);
1891 else if (!strcmpiW(threading_model, wszFree))
1893 if (!apt->multi_threaded)
1895 FIXME("should create object %s in multi-threaded apartment\n",
1896 debugstr_guid(rclsid));
1899 /* everything except "Apartment", "Free" and "Both" */
1900 else if (strcmpiW(threading_model, wszBoth))
1902 /* everything else is main-threaded */
1903 if (threading_model[0])
1904 FIXME("unrecognised threading model %s for object %s, should be main-threaded?\n",
1905 debugstr_w(threading_model), debugstr_guid(rclsid));
1907 if (apt->multi_threaded || !apt->main)
1909 /* try to find an STA */
1910 APARTMENT *host_apt = apartment_findfromtype(FALSE, TRUE);
1912 FIXME("create a host apartment for main-threaded object %s\n", debugstr_guid(rclsid));
1915 struct host_object_params params;
1916 HWND hwnd = apartment_getwindow(host_apt);
1918 params.hkeydll = hkeydll;
1919 params.clsid = *rclsid;
1921 hr = CreateStreamOnHGlobal(NULL, TRUE, ¶ms.stream);
1924 hr = SendMessageW(hwnd, DM_HOSTOBJECT, 0, (LPARAM)¶ms);
1926 hr = CoUnmarshalInterface(params.stream, riid, ppv);
1927 IStream_Release(params.stream);
1933 if (COM_RegReadPath(hkeydll, NULL, NULL, dllpath, ARRAYSIZE(dllpath)) != ERROR_SUCCESS)
1935 /* failure: CLSID is not found in registry */
1936 WARN("class %s not registered inproc\n", debugstr_guid(rclsid));
1937 return REGDB_E_CLASSNOTREG;
1940 return apartment_getclassobject(apt, dllpath, rclsid, riid, ppv);
1943 /***********************************************************************
1944 * CoGetClassObject [OLE32.@]
1946 * Creates an object of the specified class.
1949 * rclsid [I] Class ID to create an instance of.
1950 * dwClsContext [I] Flags to restrict the location of the created instance.
1951 * pServerInfo [I] Optional. Details for connecting to a remote server.
1952 * iid [I] The ID of the interface of the instance to return.
1953 * ppv [O] On returns, contains a pointer to the specified interface of the object.
1957 * Failure: HRESULT code.
1960 * The dwClsContext parameter can be one or more of the following:
1961 *| CLSCTX_INPROC_SERVER - Use an in-process server, such as from a DLL.
1962 *| CLSCTX_INPROC_HANDLER - Use an in-process object which handles certain functions for an object running in another process.
1963 *| CLSCTX_LOCAL_SERVER - Connect to an object running in another process.
1964 *| CLSCTX_REMOTE_SERVER - Connect to an object running on another machine.
1967 * CoCreateInstance()
1969 HRESULT WINAPI CoGetClassObject(
1970 REFCLSID rclsid, DWORD dwClsContext, COSERVERINFO *pServerInfo,
1971 REFIID iid, LPVOID *ppv)
1973 LPUNKNOWN regClassObject;
1974 HRESULT hres = E_UNEXPECTED;
1976 TRACE("\n\tCLSID:\t%s,\n\tIID:\t%s\n", debugstr_guid(rclsid), debugstr_guid(iid));
1979 return E_INVALIDARG;
1983 if (!COM_CurrentApt())
1985 ERR("apartment not initialised\n");
1986 return CO_E_NOTINITIALIZED;
1990 FIXME("\tpServerInfo: name=%s\n",debugstr_w(pServerInfo->pwszName));
1991 FIXME("\t\tpAuthInfo=%p\n",pServerInfo->pAuthInfo);
1995 * First, try and see if we can't match the class ID with one of the
1996 * registered classes.
1998 if (S_OK == COM_GetRegisteredClassObject(rclsid, dwClsContext, ®ClassObject))
2000 /* Get the required interface from the retrieved pointer. */
2001 hres = IUnknown_QueryInterface(regClassObject, iid, ppv);
2004 * Since QI got another reference on the pointer, we want to release the
2005 * one we already have. If QI was unsuccessful, this will release the object. This
2006 * is good since we are not returning it in the "out" parameter.
2008 IUnknown_Release(regClassObject);
2013 /* First try in-process server */
2014 if (CLSCTX_INPROC_SERVER & dwClsContext)
2016 static const WCHAR wszInprocServer32[] = {'I','n','p','r','o','c','S','e','r','v','e','r','3','2',0};
2019 if (IsEqualCLSID(rclsid, &CLSID_InProcFreeMarshaler))
2020 return FTMarshalCF_Create(iid, ppv);
2022 hres = COM_OpenKeyForCLSID(rclsid, wszInprocServer32, KEY_READ, &hkey);
2025 if (hres == REGDB_E_CLASSNOTREG)
2026 ERR("class %s not registered\n", debugstr_guid(rclsid));
2028 WARN("class %s not registered as in-proc server\n", debugstr_guid(rclsid));
2031 if (SUCCEEDED(hres))
2033 hres = get_inproc_class_object(hkey, rclsid, iid, ppv);
2037 /* return if we got a class, otherwise fall through to one of the
2039 if (SUCCEEDED(hres))
2043 /* Next try in-process handler */
2044 if (CLSCTX_INPROC_HANDLER & dwClsContext)
2046 static const WCHAR wszInprocHandler32[] = {'I','n','p','r','o','c','H','a','n','d','l','e','r','3','2',0};
2049 hres = COM_OpenKeyForCLSID(rclsid, wszInprocHandler32, KEY_READ, &hkey);
2052 if (hres == REGDB_E_CLASSNOTREG)
2053 ERR("class %s not registered\n", debugstr_guid(rclsid));
2055 WARN("class %s not registered in-proc handler\n", debugstr_guid(rclsid));
2058 if (SUCCEEDED(hres))
2060 hres = get_inproc_class_object(hkey, rclsid, iid, ppv);
2064 /* return if we got a class, otherwise fall through to one of the
2066 if (SUCCEEDED(hres))
2070 /* Next try out of process */
2071 if (CLSCTX_LOCAL_SERVER & dwClsContext)
2073 hres = RPC_GetLocalClassObject(rclsid,iid,ppv);
2074 if (SUCCEEDED(hres))
2078 /* Finally try remote: this requires networked DCOM (a lot of work) */
2079 if (CLSCTX_REMOTE_SERVER & dwClsContext)
2081 FIXME ("CLSCTX_REMOTE_SERVER not supported\n");
2082 hres = E_NOINTERFACE;
2086 ERR("no class object %s could be created for context 0x%x\n",
2087 debugstr_guid(rclsid), dwClsContext);
2091 /***********************************************************************
2092 * CoResumeClassObjects (OLE32.@)
2094 * Resumes all class objects registered with REGCLS_SUSPENDED.
2098 * Failure: HRESULT code.
2100 HRESULT WINAPI CoResumeClassObjects(void)
2106 /***********************************************************************
2107 * CoCreateInstance [OLE32.@]
2109 * Creates an instance of the specified class.
2112 * rclsid [I] Class ID to create an instance of.
2113 * pUnkOuter [I] Optional outer unknown to allow aggregation with another object.
2114 * dwClsContext [I] Flags to restrict the location of the created instance.
2115 * iid [I] The ID of the interface of the instance to return.
2116 * ppv [O] On returns, contains a pointer to the specified interface of the instance.
2120 * Failure: HRESULT code.
2123 * The dwClsContext parameter can be one or more of the following:
2124 *| CLSCTX_INPROC_SERVER - Use an in-process server, such as from a DLL.
2125 *| CLSCTX_INPROC_HANDLER - Use an in-process object which handles certain functions for an object running in another process.
2126 *| CLSCTX_LOCAL_SERVER - Connect to an object running in another process.
2127 *| CLSCTX_REMOTE_SERVER - Connect to an object running on another machine.
2129 * Aggregation is the concept of deferring the IUnknown of an object to another
2130 * object. This allows a separate object to behave as though it was part of
2131 * the object and to allow this the pUnkOuter parameter can be set. Note that
2132 * not all objects support having an outer of unknown.
2135 * CoGetClassObject()
2137 HRESULT WINAPI CoCreateInstance(
2139 LPUNKNOWN pUnkOuter,
2145 LPCLASSFACTORY lpclf = 0;
2147 TRACE("(rclsid=%s, pUnkOuter=%p, dwClsContext=%08x, riid=%s, ppv=%p)\n", debugstr_guid(rclsid),
2148 pUnkOuter, dwClsContext, debugstr_guid(iid), ppv);
2157 * Initialize the "out" parameter
2161 if (!COM_CurrentApt())
2163 ERR("apartment not initialised\n");
2164 return CO_E_NOTINITIALIZED;
2168 * The Standard Global Interface Table (GIT) object is a process-wide singleton.
2169 * Rather than create a class factory, we can just check for it here
2171 if (IsEqualIID(rclsid, &CLSID_StdGlobalInterfaceTable)) {
2172 if (StdGlobalInterfaceTableInstance == NULL)
2173 StdGlobalInterfaceTableInstance = StdGlobalInterfaceTable_Construct();
2174 hres = IGlobalInterfaceTable_QueryInterface( (IGlobalInterfaceTable*) StdGlobalInterfaceTableInstance, iid, ppv);
2175 if (hres) return hres;
2177 TRACE("Retrieved GIT (%p)\n", *ppv);
2182 * Get a class factory to construct the object we want.
2184 hres = CoGetClassObject(rclsid,
2194 * Create the object and don't forget to release the factory
2196 hres = IClassFactory_CreateInstance(lpclf, pUnkOuter, iid, ppv);
2197 IClassFactory_Release(lpclf);
2199 FIXME("no instance created for interface %s of class %s, hres is 0x%08x\n",
2200 debugstr_guid(iid), debugstr_guid(rclsid),hres);
2205 /***********************************************************************
2206 * CoCreateInstanceEx [OLE32.@]
2208 HRESULT WINAPI CoCreateInstanceEx(
2210 LPUNKNOWN pUnkOuter,
2212 COSERVERINFO* pServerInfo,
2216 IUnknown* pUnk = NULL;
2219 ULONG successCount = 0;
2224 if ( (cmq==0) || (pResults==NULL))
2225 return E_INVALIDARG;
2227 if (pServerInfo!=NULL)
2228 FIXME("() non-NULL pServerInfo not supported!\n");
2231 * Initialize all the "out" parameters.
2233 for (index = 0; index < cmq; index++)
2235 pResults[index].pItf = NULL;
2236 pResults[index].hr = E_NOINTERFACE;
2240 * Get the object and get its IUnknown pointer.
2242 hr = CoCreateInstance(rclsid,
2252 * Then, query for all the interfaces requested.
2254 for (index = 0; index < cmq; index++)
2256 pResults[index].hr = IUnknown_QueryInterface(pUnk,
2257 pResults[index].pIID,
2258 (VOID**)&(pResults[index].pItf));
2260 if (pResults[index].hr == S_OK)
2265 * Release our temporary unknown pointer.
2267 IUnknown_Release(pUnk);
2269 if (successCount == 0)
2270 return E_NOINTERFACE;
2272 if (successCount!=cmq)
2273 return CO_S_NOTALLINTERFACES;
2278 /***********************************************************************
2279 * CoLoadLibrary (OLE32.@)
2284 * lpszLibName [I] Path to library.
2285 * bAutoFree [I] Whether the library should automatically be freed.
2288 * Success: Handle to loaded library.
2292 * CoFreeLibrary, CoFreeAllLibraries, CoFreeUnusedLibraries
2294 HINSTANCE WINAPI CoLoadLibrary(LPOLESTR lpszLibName, BOOL bAutoFree)
2296 TRACE("(%s, %d)\n", debugstr_w(lpszLibName), bAutoFree);
2298 return LoadLibraryExW(lpszLibName, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
2301 /***********************************************************************
2302 * CoFreeLibrary [OLE32.@]
2304 * Unloads a library from memory.
2307 * hLibrary [I] Handle to library to unload.
2313 * CoLoadLibrary, CoFreeAllLibraries, CoFreeUnusedLibraries
2315 void WINAPI CoFreeLibrary(HINSTANCE hLibrary)
2317 FreeLibrary(hLibrary);
2321 /***********************************************************************
2322 * CoFreeAllLibraries [OLE32.@]
2324 * Function for backwards compatibility only. Does nothing.
2330 * CoLoadLibrary, CoFreeLibrary, CoFreeUnusedLibraries
2332 void WINAPI CoFreeAllLibraries(void)
2338 /***********************************************************************
2339 * CoFreeUnusedLibraries [OLE32.@]
2340 * CoFreeUnusedLibraries [COMPOBJ.17]
2342 * Frees any unused libraries. Unused are identified as those that return
2343 * S_OK from their DllCanUnloadNow function.
2349 * CoLoadLibrary, CoFreeAllLibraries, CoFreeLibrary
2351 void WINAPI CoFreeUnusedLibraries(void)
2353 struct apartment *apt = COM_CurrentApt();
2356 ERR("apartment not initialised\n");
2360 apartment_freeunusedlibraries(apt);
2363 /***********************************************************************
2364 * CoFileTimeNow [OLE32.@]
2365 * CoFileTimeNow [COMPOBJ.82]
2367 * Retrieves the current time in FILETIME format.
2370 * lpFileTime [O] The current time.
2375 HRESULT WINAPI CoFileTimeNow( FILETIME *lpFileTime )
2377 GetSystemTimeAsFileTime( lpFileTime );
2381 static void COM_RevokeAllClasses(void)
2383 EnterCriticalSection( &csRegisteredClassList );
2385 while (list_head(&RegisteredClassList))
2387 RegisteredClass *curClass = LIST_ENTRY(list_head(&RegisteredClassList),
2388 RegisteredClass, entry);
2389 CoRevokeClassObject(curClass->dwCookie);
2392 LeaveCriticalSection( &csRegisteredClassList );
2395 /******************************************************************************
2396 * CoLockObjectExternal [OLE32.@]
2398 * Increments or decrements the external reference count of a stub object.
2401 * pUnk [I] Stub object.
2402 * fLock [I] If TRUE then increments the external ref-count,
2403 * otherwise decrements.
2404 * fLastUnlockReleases [I] If TRUE then the last unlock has the effect of
2405 * calling CoDisconnectObject.
2409 * Failure: HRESULT code.
2412 * If fLock is TRUE and an object is passed in that doesn't have a stub
2413 * manager then a new stub manager is created for the object.
2415 HRESULT WINAPI CoLockObjectExternal(
2418 BOOL fLastUnlockReleases)
2420 struct stub_manager *stubmgr;
2421 struct apartment *apt;
2423 TRACE("pUnk=%p, fLock=%s, fLastUnlockReleases=%s\n",
2424 pUnk, fLock ? "TRUE" : "FALSE", fLastUnlockReleases ? "TRUE" : "FALSE");
2426 apt = COM_CurrentApt();
2427 if (!apt) return CO_E_NOTINITIALIZED;
2429 stubmgr = get_stub_manager_from_object(apt, pUnk);
2434 stub_manager_ext_addref(stubmgr, 1);
2436 stub_manager_ext_release(stubmgr, 1, fLastUnlockReleases);
2438 stub_manager_int_release(stubmgr);
2444 stubmgr = new_stub_manager(apt, pUnk);
2448 stub_manager_ext_addref(stubmgr, 1);
2449 stub_manager_int_release(stubmgr);
2456 WARN("stub object not found %p\n", pUnk);
2457 /* Note: native is pretty broken here because it just silently
2458 * fails, without returning an appropriate error code, making apps
2459 * think that the object was disconnected, when it actually wasn't */
2464 /***********************************************************************
2465 * CoInitializeWOW (OLE32.@)
2467 * WOW equivalent of CoInitialize?
2476 HRESULT WINAPI CoInitializeWOW(DWORD x,DWORD y)
2478 FIXME("(0x%08x,0x%08x),stub!\n",x,y);
2482 /***********************************************************************
2483 * CoGetState [OLE32.@]
2485 * Retrieves the thread state object previously stored by CoSetState().
2488 * ppv [I] Address where pointer to object will be stored.
2492 * Failure: E_OUTOFMEMORY.
2495 * Crashes on all invalid ppv addresses, including NULL.
2496 * If the function returns a non-NULL object then the caller must release its
2497 * reference on the object when the object is no longer required.
2502 HRESULT WINAPI CoGetState(IUnknown ** ppv)
2504 struct oletls *info = COM_CurrentInfo();
2505 if (!info) return E_OUTOFMEMORY;
2511 IUnknown_AddRef(info->state);
2513 TRACE("apt->state=%p\n", info->state);
2519 /***********************************************************************
2520 * CoSetState [OLE32.@]
2522 * Sets the thread state object.
2525 * pv [I] Pointer to state object to be stored.
2528 * The system keeps a reference on the object while the object stored.
2532 * Failure: E_OUTOFMEMORY.
2534 HRESULT WINAPI CoSetState(IUnknown * pv)
2536 struct oletls *info = COM_CurrentInfo();
2537 if (!info) return E_OUTOFMEMORY;
2539 if (pv) IUnknown_AddRef(pv);
2543 TRACE("-- release %p now\n", info->state);
2544 IUnknown_Release(info->state);
2553 /******************************************************************************
2554 * CoTreatAsClass [OLE32.@]
2556 * Sets the TreatAs value of a class.
2559 * clsidOld [I] Class to set TreatAs value on.
2560 * clsidNew [I] The class the clsidOld should be treated as.
2564 * Failure: HRESULT code.
2569 HRESULT WINAPI CoTreatAsClass(REFCLSID clsidOld, REFCLSID clsidNew)
2571 static const WCHAR wszAutoTreatAs[] = {'A','u','t','o','T','r','e','a','t','A','s',0};
2572 static const WCHAR wszTreatAs[] = {'T','r','e','a','t','A','s',0};
2574 WCHAR szClsidNew[CHARS_IN_GUID];
2576 WCHAR auto_treat_as[CHARS_IN_GUID];
2577 LONG auto_treat_as_size = sizeof(auto_treat_as);
2580 res = COM_OpenKeyForCLSID(clsidOld, NULL, KEY_READ | KEY_WRITE, &hkey);
2583 if (!memcmp( clsidOld, clsidNew, sizeof(*clsidOld) ))
2585 if (!RegQueryValueW(hkey, wszAutoTreatAs, auto_treat_as, &auto_treat_as_size) &&
2586 !CLSIDFromString(auto_treat_as, &id))
2588 if (RegSetValueW(hkey, wszTreatAs, REG_SZ, auto_treat_as, sizeof(auto_treat_as)))
2590 res = REGDB_E_WRITEREGDB;
2596 RegDeleteKeyW(hkey, wszTreatAs);
2600 else if (!StringFromGUID2(clsidNew, szClsidNew, ARRAYSIZE(szClsidNew)) &&
2601 !RegSetValueW(hkey, wszTreatAs, REG_SZ, szClsidNew, sizeof(szClsidNew)))
2603 res = REGDB_E_WRITEREGDB;
2608 if (hkey) RegCloseKey(hkey);
2612 /******************************************************************************
2613 * CoGetTreatAsClass [OLE32.@]
2615 * Gets the TreatAs value of a class.
2618 * clsidOld [I] Class to get the TreatAs value of.
2619 * clsidNew [I] The class the clsidOld should be treated as.
2623 * Failure: HRESULT code.
2628 HRESULT WINAPI CoGetTreatAsClass(REFCLSID clsidOld, LPCLSID clsidNew)
2630 static const WCHAR wszTreatAs[] = {'T','r','e','a','t','A','s',0};
2632 WCHAR szClsidNew[CHARS_IN_GUID];
2634 LONG len = sizeof(szClsidNew);
2636 FIXME("(%s,%p)\n", debugstr_guid(clsidOld), clsidNew);
2637 memcpy(clsidNew,clsidOld,sizeof(CLSID)); /* copy over old value */
2639 res = COM_OpenKeyForCLSID(clsidOld, wszTreatAs, KEY_READ, &hkey);
2642 if (RegQueryValueW(hkey, NULL, szClsidNew, &len))
2647 res = CLSIDFromString(szClsidNew,clsidNew);
2649 ERR("Failed CLSIDFromStringA(%s), hres 0x%08x\n", debugstr_w(szClsidNew), res);
2651 if (hkey) RegCloseKey(hkey);
2655 /******************************************************************************
2656 * CoGetCurrentProcess [OLE32.@]
2657 * CoGetCurrentProcess [COMPOBJ.34]
2659 * Gets the current process ID.
2662 * The current process ID.
2665 * Is DWORD really the correct return type for this function?
2667 DWORD WINAPI CoGetCurrentProcess(void)
2669 return GetCurrentProcessId();
2672 /******************************************************************************
2673 * CoRegisterMessageFilter [OLE32.@]
2675 * Registers a message filter.
2678 * lpMessageFilter [I] Pointer to interface.
2679 * lplpMessageFilter [O] Indirect pointer to prior instance if non-NULL.
2683 * Failure: HRESULT code.
2686 * Both lpMessageFilter and lplpMessageFilter are optional. Passing in a NULL
2687 * lpMessageFilter removes the message filter.
2689 * If lplpMessageFilter is not NULL the previous message filter will be
2690 * returned in the memory pointer to this parameter and the caller is
2691 * responsible for releasing the object.
2693 * The current thread be in an apartment otherwise the function will crash.
2695 HRESULT WINAPI CoRegisterMessageFilter(
2696 LPMESSAGEFILTER lpMessageFilter,
2697 LPMESSAGEFILTER *lplpMessageFilter)
2699 struct apartment *apt;
2700 IMessageFilter *lpOldMessageFilter;
2702 TRACE("(%p, %p)\n", lpMessageFilter, lplpMessageFilter);
2704 apt = COM_CurrentApt();
2706 /* can't set a message filter in a multi-threaded apartment */
2707 if (!apt || apt->multi_threaded)
2709 WARN("can't set message filter in MTA or uninitialized apt\n");
2710 return CO_E_NOT_SUPPORTED;
2713 if (lpMessageFilter)
2714 IMessageFilter_AddRef(lpMessageFilter);
2716 EnterCriticalSection(&apt->cs);
2718 lpOldMessageFilter = apt->filter;
2719 apt->filter = lpMessageFilter;
2721 LeaveCriticalSection(&apt->cs);
2723 if (lplpMessageFilter)
2724 *lplpMessageFilter = lpOldMessageFilter;
2725 else if (lpOldMessageFilter)
2726 IMessageFilter_Release(lpOldMessageFilter);
2731 /***********************************************************************
2732 * CoIsOle1Class [OLE32.@]
2734 * Determines whether the specified class an OLE v1 class.
2737 * clsid [I] Class to test.
2740 * TRUE if the class is an OLE v1 class, or FALSE otherwise.
2742 BOOL WINAPI CoIsOle1Class(REFCLSID clsid)
2744 FIXME("%s\n", debugstr_guid(clsid));
2748 /***********************************************************************
2749 * IsEqualGUID [OLE32.@]
2751 * Compares two Unique Identifiers.
2754 * rguid1 [I] The first GUID to compare.
2755 * rguid2 [I] The other GUID to compare.
2761 BOOL WINAPI IsEqualGUID(
2765 return !memcmp(rguid1,rguid2,sizeof(GUID));
2768 /***********************************************************************
2769 * CoInitializeSecurity [OLE32.@]
2771 HRESULT WINAPI CoInitializeSecurity(PSECURITY_DESCRIPTOR pSecDesc, LONG cAuthSvc,
2772 SOLE_AUTHENTICATION_SERVICE* asAuthSvc,
2773 void* pReserved1, DWORD dwAuthnLevel,
2774 DWORD dwImpLevel, void* pReserved2,
2775 DWORD dwCapabilities, void* pReserved3)
2777 FIXME("(%p,%d,%p,%p,%d,%d,%p,%d,%p) - stub!\n", pSecDesc, cAuthSvc,
2778 asAuthSvc, pReserved1, dwAuthnLevel, dwImpLevel, pReserved2,
2779 dwCapabilities, pReserved3);
2783 /***********************************************************************
2784 * CoSuspendClassObjects [OLE32.@]
2786 * Suspends all registered class objects to prevent further requests coming in
2787 * for those objects.
2791 * Failure: HRESULT code.
2793 HRESULT WINAPI CoSuspendClassObjects(void)
2799 /***********************************************************************
2800 * CoAddRefServerProcess [OLE32.@]
2802 * Helper function for incrementing the reference count of a local-server
2806 * New reference count.
2809 * CoReleaseServerProcess().
2811 ULONG WINAPI CoAddRefServerProcess(void)
2817 EnterCriticalSection(&csRegisteredClassList);
2818 refs = ++s_COMServerProcessReferences;
2819 LeaveCriticalSection(&csRegisteredClassList);
2821 TRACE("refs before: %d\n", refs - 1);
2826 /***********************************************************************
2827 * CoReleaseServerProcess [OLE32.@]
2829 * Helper function for decrementing the reference count of a local-server
2833 * New reference count.
2836 * When reference count reaches 0, this function suspends all registered
2837 * classes so no new connections are accepted.
2840 * CoAddRefServerProcess(), CoSuspendClassObjects().
2842 ULONG WINAPI CoReleaseServerProcess(void)
2848 EnterCriticalSection(&csRegisteredClassList);
2850 refs = --s_COMServerProcessReferences;
2851 /* FIXME: if (!refs) COM_SuspendClassObjects(); */
2853 LeaveCriticalSection(&csRegisteredClassList);
2855 TRACE("refs after: %d\n", refs);
2860 /***********************************************************************
2861 * CoIsHandlerConnected [OLE32.@]
2863 * Determines whether a proxy is connected to a remote stub.
2866 * pUnk [I] Pointer to object that may or may not be connected.
2869 * TRUE if pUnk is not a proxy or if pUnk is connected to a remote stub, or
2872 BOOL WINAPI CoIsHandlerConnected(IUnknown *pUnk)
2874 FIXME("%p\n", pUnk);
2879 /***********************************************************************
2880 * CoAllowSetForegroundWindow [OLE32.@]
2883 HRESULT WINAPI CoAllowSetForegroundWindow(IUnknown *pUnk, void *pvReserved)
2885 FIXME("(%p, %p): stub\n", pUnk, pvReserved);
2889 /***********************************************************************
2890 * CoQueryProxyBlanket [OLE32.@]
2892 * Retrieves the security settings being used by a proxy.
2895 * pProxy [I] Pointer to the proxy object.
2896 * pAuthnSvc [O] The type of authentication service.
2897 * pAuthzSvc [O] The type of authorization service.
2898 * ppServerPrincName [O] Optional. The server prinicple name.
2899 * pAuthnLevel [O] The authentication level.
2900 * pImpLevel [O] The impersonation level.
2901 * ppAuthInfo [O] Information specific to the authorization/authentication service.
2902 * pCapabilities [O] Flags affecting the security behaviour.
2906 * Failure: HRESULT code.
2909 * CoCopyProxy, CoSetProxyBlanket.
2911 HRESULT WINAPI CoQueryProxyBlanket(IUnknown *pProxy, DWORD *pAuthnSvc,
2912 DWORD *pAuthzSvc, OLECHAR **ppServerPrincName, DWORD *pAuthnLevel,
2913 DWORD *pImpLevel, void **ppAuthInfo, DWORD *pCapabilities)
2915 IClientSecurity *pCliSec;
2918 TRACE("%p\n", pProxy);
2920 hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (void **)&pCliSec);
2923 hr = IClientSecurity_QueryBlanket(pCliSec, pProxy, pAuthnSvc,
2924 pAuthzSvc, ppServerPrincName,
2925 pAuthnLevel, pImpLevel, ppAuthInfo,
2927 IClientSecurity_Release(pCliSec);
2930 if (FAILED(hr)) ERR("-- failed with 0x%08x\n", hr);
2934 /***********************************************************************
2935 * CoSetProxyBlanket [OLE32.@]
2937 * Sets the security settings for a proxy.
2940 * pProxy [I] Pointer to the proxy object.
2941 * AuthnSvc [I] The type of authentication service.
2942 * AuthzSvc [I] The type of authorization service.
2943 * pServerPrincName [I] The server prinicple name.
2944 * AuthnLevel [I] The authentication level.
2945 * ImpLevel [I] The impersonation level.
2946 * pAuthInfo [I] Information specific to the authorization/authentication service.
2947 * Capabilities [I] Flags affecting the security behaviour.
2951 * Failure: HRESULT code.
2954 * CoQueryProxyBlanket, CoCopyProxy.
2956 HRESULT WINAPI CoSetProxyBlanket(IUnknown *pProxy, DWORD AuthnSvc,
2957 DWORD AuthzSvc, OLECHAR *pServerPrincName, DWORD AuthnLevel,
2958 DWORD ImpLevel, void *pAuthInfo, DWORD Capabilities)
2960 IClientSecurity *pCliSec;
2963 TRACE("%p\n", pProxy);
2965 hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (void **)&pCliSec);
2968 hr = IClientSecurity_SetBlanket(pCliSec, pProxy, AuthnSvc,
2969 AuthzSvc, pServerPrincName,
2970 AuthnLevel, ImpLevel, pAuthInfo,
2972 IClientSecurity_Release(pCliSec);
2975 if (FAILED(hr)) ERR("-- failed with 0x%08x\n", hr);
2979 /***********************************************************************
2980 * CoCopyProxy [OLE32.@]
2985 * pProxy [I] Pointer to the proxy object.
2986 * ppCopy [O] Copy of the proxy.
2990 * Failure: HRESULT code.
2993 * CoQueryProxyBlanket, CoSetProxyBlanket.
2995 HRESULT WINAPI CoCopyProxy(IUnknown *pProxy, IUnknown **ppCopy)
2997 IClientSecurity *pCliSec;
3000 TRACE("%p\n", pProxy);
3002 hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (void **)&pCliSec);
3005 hr = IClientSecurity_CopyProxy(pCliSec, pProxy, ppCopy);
3006 IClientSecurity_Release(pCliSec);
3009 if (FAILED(hr)) ERR("-- failed with 0x%08x\n", hr);
3014 /***********************************************************************
3015 * CoGetCallContext [OLE32.@]
3017 * Gets the context of the currently executing server call in the current
3021 * riid [I] Context interface to return.
3022 * ppv [O] Pointer to memory that will receive the context on return.
3026 * Failure: HRESULT code.
3028 HRESULT WINAPI CoGetCallContext(REFIID riid, void **ppv)
3030 FIXME("(%s, %p): stub\n", debugstr_guid(riid), ppv);
3033 return E_NOINTERFACE;
3036 /***********************************************************************
3037 * CoQueryClientBlanket [OLE32.@]
3039 * Retrieves the authentication information about the client of the currently
3040 * executing server call in the current thread.
3043 * pAuthnSvc [O] Optional. The type of authentication service.
3044 * pAuthzSvc [O] Optional. The type of authorization service.
3045 * pServerPrincName [O] Optional. The server prinicple name.
3046 * pAuthnLevel [O] Optional. The authentication level.
3047 * pImpLevel [O] Optional. The impersonation level.
3048 * pPrivs [O] Optional. Information about the privileges of the client.
3049 * pCapabilities [IO] Optional. Flags affecting the security behaviour.
3053 * Failure: HRESULT code.
3056 * CoImpersonateClient, CoRevertToSelf, CoGetCallContext.
3058 HRESULT WINAPI CoQueryClientBlanket(
3061 OLECHAR **pServerPrincName,
3064 RPC_AUTHZ_HANDLE *pPrivs,
3065 DWORD *pCapabilities)
3067 IServerSecurity *pSrvSec;
3070 TRACE("(%p, %p, %p, %p, %p, %p, %p)\n",
3071 pAuthnSvc, pAuthzSvc, pServerPrincName, pAuthnLevel, pImpLevel,
3072 pPrivs, pCapabilities);
3074 hr = CoGetCallContext(&IID_IServerSecurity, (void **)&pSrvSec);
3077 hr = IServerSecurity_QueryBlanket(
3078 pSrvSec, pAuthnSvc, pAuthzSvc, pServerPrincName, pAuthnLevel,
3079 pImpLevel, pPrivs, pCapabilities);
3080 IServerSecurity_Release(pSrvSec);
3086 /***********************************************************************
3087 * CoImpersonateClient [OLE32.@]
3089 * Impersonates the client of the currently executing server call in the
3097 * Failure: HRESULT code.
3100 * If this function fails then the current thread will not be impersonating
3101 * the client and all actions will take place on behalf of the server.
3102 * Therefore, it is important to check the return value from this function.
3105 * CoRevertToSelf, CoQueryClientBlanket, CoGetCallContext.
3107 HRESULT WINAPI CoImpersonateClient(void)
3109 IServerSecurity *pSrvSec;
3114 hr = CoGetCallContext(&IID_IServerSecurity, (void **)&pSrvSec);
3117 hr = IServerSecurity_ImpersonateClient(pSrvSec);
3118 IServerSecurity_Release(pSrvSec);
3124 /***********************************************************************
3125 * CoRevertToSelf [OLE32.@]
3127 * Ends the impersonation of the client of the currently executing server
3128 * call in the current thread.
3135 * Failure: HRESULT code.
3138 * CoImpersonateClient, CoQueryClientBlanket, CoGetCallContext.
3140 HRESULT WINAPI CoRevertToSelf(void)
3142 IServerSecurity *pSrvSec;
3147 hr = CoGetCallContext(&IID_IServerSecurity, (void **)&pSrvSec);
3150 hr = IServerSecurity_RevertToSelf(pSrvSec);
3151 IServerSecurity_Release(pSrvSec);
3157 static BOOL COM_PeekMessage(struct apartment *apt, MSG *msg)
3159 /* first try to retrieve messages for incoming COM calls to the apartment window */
3160 return PeekMessageW(msg, apt->win, WM_USER, WM_APP - 1, PM_REMOVE|PM_NOYIELD) ||
3161 /* next retrieve other messages necessary for the app to remain responsive */
3162 PeekMessageW(msg, NULL, 0, 0, PM_QS_PAINT|PM_QS_POSTMESSAGE|PM_REMOVE|PM_NOYIELD);
3165 /***********************************************************************
3166 * CoWaitForMultipleHandles [OLE32.@]
3168 * Waits for one or more handles to become signaled.
3171 * dwFlags [I] Flags. See notes.
3172 * dwTimeout [I] Timeout in milliseconds.
3173 * cHandles [I] Number of handles pointed to by pHandles.
3174 * pHandles [I] Handles to wait for.
3175 * lpdwindex [O] Index of handle that was signaled.
3179 * Failure: RPC_S_CALLPENDING on timeout.
3183 * The dwFlags parameter can be zero or more of the following:
3184 *| COWAIT_WAITALL - Wait for all of the handles to become signaled.
3185 *| COWAIT_ALERTABLE - Allows a queued APC to run during the wait.
3188 * MsgWaitForMultipleObjects, WaitForMultipleObjects.
3190 HRESULT WINAPI CoWaitForMultipleHandles(DWORD dwFlags, DWORD dwTimeout,
3191 ULONG cHandles, LPHANDLE pHandles, LPDWORD lpdwindex)
3194 DWORD start_time = GetTickCount();
3195 APARTMENT *apt = COM_CurrentApt();
3196 BOOL message_loop = apt && !apt->multi_threaded;
3198 TRACE("(0x%08x, 0x%08x, %d, %p, %p)\n", dwFlags, dwTimeout, cHandles,
3199 pHandles, lpdwindex);
3203 DWORD now = GetTickCount();
3206 if ((dwTimeout != INFINITE) && (start_time + dwTimeout >= now))
3208 hr = RPC_S_CALLPENDING;
3214 DWORD wait_flags = (dwFlags & COWAIT_WAITALL) ? MWMO_WAITALL : 0 |
3215 (dwFlags & COWAIT_ALERTABLE ) ? MWMO_ALERTABLE : 0;
3217 TRACE("waiting for rpc completion or window message\n");
3219 res = MsgWaitForMultipleObjectsEx(cHandles, pHandles,
3220 (dwTimeout == INFINITE) ? INFINITE : start_time + dwTimeout - now,
3221 QS_ALLINPUT, wait_flags);
3223 if (res == WAIT_OBJECT_0 + cHandles) /* messages available */
3227 /* call message filter */
3229 if (COM_CurrentApt()->filter)
3231 PENDINGTYPE pendingtype =
3232 COM_CurrentInfo()->pending_call_count_server ?
3233 PENDINGTYPE_NESTED : PENDINGTYPE_TOPLEVEL;
3234 DWORD be_handled = IMessageFilter_MessagePending(
3235 COM_CurrentApt()->filter, 0 /* FIXME */,
3236 now - start_time, pendingtype);
3237 TRACE("IMessageFilter_MessagePending returned %d\n", be_handled);
3240 case PENDINGMSG_CANCELCALL:
3241 WARN("call canceled\n");
3242 hr = RPC_E_CALL_CANCELED;
3244 case PENDINGMSG_WAITNOPROCESS:
3245 case PENDINGMSG_WAITDEFPROCESS:
3247 /* FIXME: MSDN is very vague about the difference
3248 * between WAITNOPROCESS and WAITDEFPROCESS - there
3249 * appears to be none, so it is possibly a left-over
3250 * from the 16-bit world. */
3255 /* note: using "if" here instead of "while" might seem less
3256 * efficient, but only if we are optimising for quick delivery
3257 * of pending messages, rather than quick completion of the
3259 if (COM_PeekMessage(apt, &msg))
3261 TRACE("received message whilst waiting for RPC: 0x%04x\n", msg.message);
3262 TranslateMessage(&msg);
3263 DispatchMessageW(&msg);
3264 if (msg.message == WM_QUIT)
3266 TRACE("resending WM_QUIT to outer message loop\n");
3267 PostQuitMessage(msg.wParam);
3268 /* no longer need to process messages */
3269 message_loop = FALSE;
3277 TRACE("waiting for rpc completion\n");
3279 res = WaitForMultipleObjectsEx(cHandles, pHandles,
3280 (dwFlags & COWAIT_WAITALL) ? TRUE : FALSE,
3281 (dwTimeout == INFINITE) ? INFINITE : start_time + dwTimeout - now,
3282 (dwFlags & COWAIT_ALERTABLE) ? TRUE : FALSE);
3285 if ((res >= WAIT_OBJECT_0) && (res < WAIT_OBJECT_0 + cHandles))
3287 /* handle signaled, store index */
3288 *lpdwindex = (res - WAIT_OBJECT_0);
3291 else if (res == WAIT_TIMEOUT)
3293 hr = RPC_S_CALLPENDING;
3298 ERR("Unexpected wait termination: %d, %d\n", res, GetLastError());
3303 TRACE("-- 0x%08x\n", hr);
3308 /***********************************************************************
3309 * CoGetObject [OLE32.@]
3311 * Gets the object named by coverting the name to a moniker and binding to it.
3314 * pszName [I] String representing the object.
3315 * pBindOptions [I] Parameters affecting the binding to the named object.
3316 * riid [I] Interface to bind to on the objecct.
3317 * ppv [O] On output, the interface riid of the object represented
3322 * Failure: HRESULT code.
3325 * MkParseDisplayName.
3327 HRESULT WINAPI CoGetObject(LPCWSTR pszName, BIND_OPTS *pBindOptions,
3328 REFIID riid, void **ppv)
3335 hr = CreateBindCtx(0, &pbc);
3339 hr = IBindCtx_SetBindOptions(pbc, pBindOptions);
3346 hr = MkParseDisplayName(pbc, pszName, &chEaten, &pmk);
3349 hr = IMoniker_BindToObject(pmk, pbc, NULL, riid, ppv);
3350 IMoniker_Release(pmk);
3354 IBindCtx_Release(pbc);
3359 /***********************************************************************
3360 * CoRegisterChannelHook [OLE32.@]
3362 * Registers a process-wide hook that is called during ORPC calls.
3365 * guidExtension [I] GUID of the channel hook to register.
3366 * pChannelHook [I] Channel hook object to register.
3370 * Failure: HRESULT code.
3372 HRESULT WINAPI CoRegisterChannelHook(REFGUID guidExtension, IChannelHook *pChannelHook)
3374 TRACE("(%s, %p)\n", debugstr_guid(guidExtension), pChannelHook);
3376 return RPC_RegisterChannelHook(guidExtension, pChannelHook);
3379 /***********************************************************************
3382 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID fImpLoad)
3384 TRACE("%p 0x%x %p\n", hinstDLL, fdwReason, fImpLoad);
3387 case DLL_PROCESS_ATTACH:
3388 OLE32_hInstance = hinstDLL;
3389 COMPOBJ_InitProcess();
3390 if (TRACE_ON(ole)) CoRegisterMallocSpy((LPVOID)-1);
3393 case DLL_PROCESS_DETACH:
3394 if (TRACE_ON(ole)) CoRevokeMallocSpy();
3395 OLEDD_UnInitialize();
3396 COMPOBJ_UninitProcess();
3397 RPC_UnregisterAllChannelHooks();
3398 OLE32_hInstance = 0;
3401 case DLL_THREAD_DETACH:
3408 /* NOTE: DllRegisterServer and DllUnregisterServer are in regsvr.c */