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
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * 1. COINIT_MULTITHREADED is 0; it is the lack of COINIT_APARTMENTTHREADED
27 * Therefore do not test against COINIT_MULTITHREADED
29 * TODO list: (items bunched together depend on each other)
31 * - Implement the service control manager (in rpcss) to keep track
32 * of registered class objects: ISCM::ServerRegisterClsid et al
33 * - Implement the OXID resolver so we don't need magic endpoint names for
34 * clients and servers to meet up
36 * - Pump the message loop during RPC calls.
37 * - Call IMessageFilter functions.
39 * - Make all ole interface marshaling use NDR to be wire compatible with
41 * - Use & interpret ORPCTHIS & ORPCTHAT.
54 #define NONAMELESSUNION
55 #define NONAMELESSSTRUCT
67 #include "wine/unicode.h"
69 #include "compobj_private.h"
71 #include "wine/debug.h"
73 WINE_DEFAULT_DEBUG_CHANNEL(ole);
75 typedef LPCSTR LPCOLESTR16;
77 HINSTANCE OLE32_hInstance = 0; /* FIXME: make static ... */
79 #define ARRAYSIZE(array) (sizeof(array)/sizeof((array)[0]))
81 /****************************************************************************
82 * This section defines variables internal to the COM module.
84 * TODO: Most of these things will have to be made thread-safe.
87 static HRESULT COM_GetRegisteredClassObject(REFCLSID rclsid, DWORD dwClsContext, LPUNKNOWN* ppUnk);
88 static void COM_RevokeAllClasses(void);
90 const CLSID CLSID_StdGlobalInterfaceTable = { 0x00000323, 0, 0, {0xc0, 0, 0, 0, 0, 0, 0, 0x46} };
92 APARTMENT *MTA; /* protected by csApartment */
93 static struct list apts = LIST_INIT( apts ); /* protected by csApartment */
95 static CRITICAL_SECTION csApartment;
96 static CRITICAL_SECTION_DEBUG critsect_debug =
99 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
100 0, 0, { 0, (DWORD)(__FILE__ ": csApartment") }
102 static CRITICAL_SECTION csApartment = { &critsect_debug, -1, 0, 0, 0, 0 };
105 * This lock count counts the number of times CoInitialize is called. It is
106 * decreased every time CoUninitialize is called. When it hits 0, the COM
107 * libraries are freed
109 static LONG s_COMLockCount = 0;
112 * This linked list contains the list of registered class objects. These
113 * are mostly used to register the factories for out-of-proc servers of OLE
116 * TODO: Make this data structure aware of inter-process communication. This
117 * means that parts of this will be exported to the Wine Server.
119 typedef struct tagRegisteredClass
121 CLSID classIdentifier;
122 LPUNKNOWN classObject;
126 LPSTREAM pMarshaledData; /* FIXME: only really need to store OXID and IPID */
127 struct tagRegisteredClass* nextClass;
130 static RegisteredClass* firstRegisteredClass = NULL;
132 static CRITICAL_SECTION csRegisteredClassList;
133 static CRITICAL_SECTION_DEBUG class_cs_debug =
135 0, 0, &csRegisteredClassList,
136 { &class_cs_debug.ProcessLocksList, &class_cs_debug.ProcessLocksList },
137 0, 0, { 0, (DWORD)(__FILE__ ": csRegisteredClassList") }
139 static CRITICAL_SECTION csRegisteredClassList = { &class_cs_debug, -1, 0, 0, 0, 0 };
141 /*****************************************************************************
142 * This section contains OpenDllList definitions
144 * The OpenDllList contains only handles of dll loaded by CoGetClassObject or
145 * other functions that do LoadLibrary _without_ giving back a HMODULE.
146 * Without this list these handles would never be freed.
148 * FIXME: a DLL that says OK when asked for unloading is unloaded in the
149 * next unload-call but not before 600 sec.
152 typedef struct tagOpenDll {
154 struct tagOpenDll *next;
157 static OpenDll *openDllList = NULL; /* linked list of open dlls */
159 static CRITICAL_SECTION csOpenDllList;
160 static CRITICAL_SECTION_DEBUG dll_cs_debug =
162 0, 0, &csOpenDllList,
163 { &dll_cs_debug.ProcessLocksList, &dll_cs_debug.ProcessLocksList },
164 0, 0, { 0, (DWORD)(__FILE__ ": csOpenDllList") }
166 static CRITICAL_SECTION csOpenDllList = { &dll_cs_debug, -1, 0, 0, 0, 0 };
168 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',' ',
169 '0','x','#','#','#','#','#','#','#','#',' ',0};
170 static LRESULT CALLBACK apartment_wndproc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
172 static void COMPOBJ_DLLList_Add(HANDLE hLibrary);
173 static void COMPOBJ_DllList_FreeUnused(int Timeout);
175 static void COMPOBJ_InitProcess( void )
179 /* Dispatching to the correct thread in an apartment is done through
180 * window messages rather than RPC transports. When an interface is
181 * marshalled into another apartment in the same process, a window of the
182 * following class is created. The *caller* of CoMarshalInterface (ie the
183 * application) is responsible for pumping the message loop in that thread.
184 * The WM_USER messages which point to the RPCs are then dispatched to
185 * COM_AptWndProc by the user's code from the apartment in which the interface
188 memset(&wclass, 0, sizeof(wclass));
189 wclass.lpfnWndProc = apartment_wndproc;
190 wclass.hInstance = OLE32_hInstance;
191 wclass.lpszClassName = wszAptWinClass;
192 RegisterClassW(&wclass);
195 static void COMPOBJ_UninitProcess( void )
197 UnregisterClassW(wszAptWinClass, OLE32_hInstance);
200 static void COM_TlsDestroy(void)
202 struct oletls *info = NtCurrentTeb()->ReservedForOle;
205 if (info->apt) apartment_release(info->apt);
206 if (info->errorinfo) IErrorInfo_Release(info->errorinfo);
207 if (info->state) IUnknown_Release(info->state);
208 HeapFree(GetProcessHeap(), 0, info);
209 NtCurrentTeb()->ReservedForOle = NULL;
213 /******************************************************************************
217 /* allocates memory and fills in the necessary fields for a new apartment
219 static APARTMENT *apartment_construct(DWORD model)
223 TRACE("creating new apartment, model=%ld\n", model);
225 apt = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*apt));
226 apt->tid = GetCurrentThreadId();
227 DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
228 GetCurrentProcess(), &apt->thread,
229 THREAD_ALL_ACCESS, FALSE, 0);
231 list_init(&apt->proxies);
232 list_init(&apt->stubmgrs);
235 apt->remunk_exported = FALSE;
237 InitializeCriticalSection(&apt->cs);
238 DEBUG_SET_CRITSEC_NAME(&apt->cs, "apartment");
242 if (model & COINIT_APARTMENTTHREADED)
244 /* FIXME: should be randomly generated by in an RPC call to rpcss */
245 apt->oxid = ((OXID)GetCurrentProcessId() << 32) | GetCurrentThreadId();
246 apt->win = CreateWindowW(wszAptWinClass, NULL, 0,
248 0, 0, OLE32_hInstance, NULL);
252 /* FIXME: should be randomly generated by in an RPC call to rpcss */
253 apt->oxid = ((OXID)GetCurrentProcessId() << 32) | 0xcafe;
256 TRACE("Created apartment on OXID %s\n", wine_dbgstr_longlong(apt->oxid));
258 /* the locking here is not currently needed for the MTA case, but it
259 * doesn't hurt and makes the code simpler */
260 EnterCriticalSection(&csApartment);
261 list_add_head(&apts, &apt->entry);
262 LeaveCriticalSection(&csApartment);
267 /* gets and existing apartment if one exists or otherwise creates an apartment
268 * structure which stores OLE apartment-local information and stores a pointer
269 * to it in the thread-local storage */
270 static APARTMENT *apartment_get_or_create(DWORD model)
272 APARTMENT *apt = COM_CurrentApt();
276 if (model & COINIT_APARTMENTTHREADED)
278 apt = apartment_construct(model);
279 COM_CurrentInfo()->apt = apt;
283 EnterCriticalSection(&csApartment);
285 /* The multi-threaded apartment (MTA) contains zero or more threads interacting
286 * with free threaded (ie thread safe) COM objects. There is only ever one MTA
290 TRACE("entering the multithreaded apartment %s\n", wine_dbgstr_longlong(MTA->oxid));
291 apartment_addref(MTA);
294 MTA = apartment_construct(model);
297 COM_CurrentInfo()->apt = apt;
299 LeaveCriticalSection(&csApartment);
306 DWORD apartment_addref(struct apartment *apt)
308 DWORD refs = InterlockedIncrement(&apt->refs);
309 TRACE("%s: before = %ld\n", wine_dbgstr_longlong(apt->oxid), refs - 1);
313 DWORD apartment_release(struct apartment *apt)
317 EnterCriticalSection(&csApartment);
319 ret = InterlockedDecrement(&apt->refs);
320 TRACE("%s: after = %ld\n", wine_dbgstr_longlong(apt->oxid), ret);
321 /* destruction stuff that needs to happen under csApartment CS */
324 if (apt == MTA) MTA = NULL;
325 list_remove(&apt->entry);
328 LeaveCriticalSection(&csApartment);
332 struct list *cursor, *cursor2;
334 TRACE("destroying apartment %p, oxid %s\n", apt, wine_dbgstr_longlong(apt->oxid));
336 /* no locking is needed for this apartment, because no other thread
337 * can access it at this point */
339 apartment_disconnectproxies(apt);
341 if (apt->win) DestroyWindow(apt->win);
343 LIST_FOR_EACH_SAFE(cursor, cursor2, &apt->stubmgrs)
345 struct stub_manager *stubmgr = LIST_ENTRY(cursor, struct stub_manager, entry);
346 /* release the implicit reference given by the fact that the
347 * stub has external references (it must do since it is in the
348 * stub manager list in the apartment and all non-apartment users
349 * must have a ref on the apartment and so it cannot be destroyed).
351 stub_manager_int_release(stubmgr);
354 /* if this assert fires, then another thread took a reference to a
355 * stub manager without taking a reference to the containing
356 * apartment, which it must do. */
357 assert(list_empty(&apt->stubmgrs));
359 if (apt->filter) IUnknown_Release(apt->filter);
361 DEBUG_CLEAR_CRITSEC_NAME(&apt->cs);
362 DeleteCriticalSection(&apt->cs);
363 CloseHandle(apt->thread);
365 HeapFree(GetProcessHeap(), 0, apt);
371 /* The given OXID must be local to this process:
373 * The ref parameter is here mostly to ensure people remember that
374 * they get one, you should normally take a ref for thread safety.
376 APARTMENT *apartment_findfromoxid(OXID oxid, BOOL ref)
378 APARTMENT *result = NULL;
381 EnterCriticalSection(&csApartment);
382 LIST_FOR_EACH( cursor, &apts )
384 struct apartment *apt = LIST_ENTRY( cursor, struct apartment, entry );
385 if (apt->oxid == oxid)
388 if (ref) apartment_addref(result);
392 LeaveCriticalSection(&csApartment);
397 /* gets the apartment which has a given creator thread ID. The caller must
398 * release the reference from the apartment as soon as the apartment pointer
399 * is no longer required. */
400 APARTMENT *apartment_findfromtid(DWORD tid)
402 APARTMENT *result = NULL;
405 EnterCriticalSection(&csApartment);
406 LIST_FOR_EACH( cursor, &apts )
408 struct apartment *apt = LIST_ENTRY( cursor, struct apartment, entry );
412 apartment_addref(result);
416 LeaveCriticalSection(&csApartment);
421 static LRESULT CALLBACK apartment_wndproc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
426 return RPC_ExecuteCall((struct dispatch_params *)lParam);
428 return DefWindowProcW(hWnd, msg, wParam, lParam);
432 /*****************************************************************************
433 * This section contains OpenDllList implemantation
436 static void COMPOBJ_DLLList_Add(HANDLE hLibrary)
443 EnterCriticalSection( &csOpenDllList );
445 if (openDllList == NULL) {
446 /* empty list -- add first node */
447 openDllList = HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
448 openDllList->hLibrary=hLibrary;
449 openDllList->next = NULL;
451 /* search for this dll */
453 for (ptr = openDllList; ptr->next != NULL; ptr=ptr->next) {
454 if (ptr->hLibrary == hLibrary) {
460 /* dll not found, add it */
462 openDllList = HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
463 openDllList->hLibrary = hLibrary;
464 openDllList->next = tmp;
468 LeaveCriticalSection( &csOpenDllList );
471 static void COMPOBJ_DllList_FreeUnused(int Timeout)
473 OpenDll *curr, *next, *prev = NULL;
474 typedef HRESULT (WINAPI *DllCanUnloadNowFunc)(void);
475 DllCanUnloadNowFunc DllCanUnloadNow;
479 EnterCriticalSection( &csOpenDllList );
481 for (curr = openDllList; curr != NULL; ) {
482 DllCanUnloadNow = (DllCanUnloadNowFunc) GetProcAddress(curr->hLibrary, "DllCanUnloadNow");
484 if ( (DllCanUnloadNow != NULL) && (DllCanUnloadNow() == S_OK) ) {
487 TRACE("freeing %p\n", curr->hLibrary);
488 FreeLibrary(curr->hLibrary);
490 HeapFree(GetProcessHeap(), 0, curr);
491 if (curr == openDllList) {
504 LeaveCriticalSection( &csOpenDllList );
507 /******************************************************************************
508 * CoBuildVersion [OLE32.@]
509 * CoBuildVersion [COMPOBJ.1]
511 * Gets the build version of the DLL.
516 * Current build version, hiword is majornumber, loword is minornumber
518 DWORD WINAPI CoBuildVersion(void)
520 TRACE("Returning version %d, build %d.\n", rmm, rup);
521 return (rmm<<16)+rup;
524 /******************************************************************************
525 * CoInitialize [OLE32.@]
527 * Initializes the COM libraries by calling CoInitializeEx with
528 * COINIT_APARTMENTTHREADED, ie it enters a STA thread.
531 * lpReserved [I] Pointer to IMalloc interface (obsolete, should be NULL).
534 * Success: S_OK if not already initialized, S_FALSE otherwise.
535 * Failure: HRESULT code.
540 HRESULT WINAPI CoInitialize(LPVOID lpReserved)
543 * Just delegate to the newer method.
545 return CoInitializeEx(lpReserved, COINIT_APARTMENTTHREADED);
548 /******************************************************************************
549 * CoInitializeEx [OLE32.@]
551 * Initializes the COM libraries.
554 * lpReserved [I] Pointer to IMalloc interface (obsolete, should be NULL).
555 * dwCoInit [I] One or more flags from the COINIT enumeration. See notes.
558 * S_OK if successful,
559 * S_FALSE if this function was called already.
560 * RPC_E_CHANGED_MODE if a previous call to CoInitializeEx specified another
565 * The behavior used to set the IMalloc used for memory management is
567 * The dwCoInit parameter must specify of of the following apartment
569 *| COINIT_APARTMENTTHREADED - A single-threaded apartment (STA).
570 *| COINIT_MULTITHREADED - A multi-threaded apartment (MTA).
571 * The parameter may also specify zero or more of the following flags:
572 *| COINIT_DISABLE_OLE1DDE - Don't use DDE for OLE1 support.
573 *| COINIT_SPEED_OVER_MEMORY - Trade memory for speed.
578 HRESULT WINAPI CoInitializeEx(LPVOID lpReserved, DWORD dwCoInit)
583 TRACE("(%p, %x)\n", lpReserved, (int)dwCoInit);
585 if (lpReserved!=NULL)
587 ERR("(%p, %x) - Bad parameter passed-in %p, must be an old Windows Application\n", lpReserved, (int)dwCoInit, lpReserved);
591 * Check the lock count. If this is the first time going through the initialize
592 * process, we have to initialize the libraries.
594 * And crank-up that lock count.
596 if (InterlockedExchangeAdd(&s_COMLockCount,1)==0)
599 * Initialize the various COM libraries and data structures.
601 TRACE("() - Initializing the COM libraries\n");
603 /* we may need to defer this until after apartment initialisation */
604 RunningObjectTableImpl_Initialize();
607 if (!(apt = COM_CurrentInfo()->apt))
609 apt = apartment_get_or_create(dwCoInit);
610 if (!apt) return E_OUTOFMEMORY;
612 else if (dwCoInit != apt->model)
614 /* Changing the threading model after it's been set is illegal. If this warning is triggered by Wine
615 code then we are probably using the wrong threading model to implement that API. */
616 ERR("Attempt to change threading model of this apartment from 0x%lx to 0x%lx\n", apt->model, dwCoInit);
617 return RPC_E_CHANGED_MODE;
622 COM_CurrentInfo()->inits++;
627 /* On COM finalization for a STA thread, the message queue is flushed to ensure no
628 pending RPCs are ignored. Non-COM messages are discarded at this point.
630 static void COM_FlushMessageQueue(void)
633 APARTMENT *apt = COM_CurrentApt();
635 if (!apt || !apt->win) return;
637 TRACE("Flushing STA message queue\n");
639 while (PeekMessageA(&message, NULL, 0, 0, PM_REMOVE))
641 if (message.hwnd != apt->win)
643 WARN("discarding message 0x%x for window %p\n", message.message, message.hwnd);
647 TranslateMessage(&message);
648 DispatchMessageA(&message);
652 /***********************************************************************
653 * CoUninitialize [OLE32.@]
655 * This method will decrement the refcount on the current apartment, freeing
656 * the resources associated with it if it is the last thread in the apartment.
657 * If the last apartment is freed, the function will additionally release
658 * any COM resources associated with the process.
668 void WINAPI CoUninitialize(void)
670 struct oletls * info = COM_CurrentInfo();
675 /* will only happen on OOM */
681 ERR("Mismatched CoUninitialize\n");
687 apartment_release(info->apt);
692 * Decrease the reference count.
693 * If we are back to 0 locks on the COM library, make sure we free
694 * all the associated data structures.
696 lCOMRefCnt = InterlockedExchangeAdd(&s_COMLockCount,-1);
699 TRACE("() - Releasing the COM libraries\n");
701 RunningObjectTableImpl_UnInitialize();
703 /* Release the references to the registered class objects */
704 COM_RevokeAllClasses();
706 /* This will free the loaded COM Dlls */
707 CoFreeAllLibraries();
709 /* This ensures we deal with any pending RPCs */
710 COM_FlushMessageQueue();
712 else if (lCOMRefCnt<1) {
713 ERR( "CoUninitialize() - not CoInitialized.\n" );
714 InterlockedExchangeAdd(&s_COMLockCount,1); /* restore the lock count. */
718 /******************************************************************************
719 * CoDisconnectObject [OLE32.@]
720 * CoDisconnectObject [COMPOBJ.15]
722 * Disconnects all connections to this object from remote processes. Dispatches
723 * pending RPCs while blocking new RPCs from occurring, and then calls
724 * IMarshal::DisconnectObject on the given object.
726 * Typically called when the object server is forced to shut down, for instance by
730 * lpUnk [I] The object whose stub should be disconnected.
731 * reserved [I] Reserved. Should be set to 0.
735 * Failure: HRESULT code.
738 * CoMarshalInterface, CoReleaseMarshalData, CoLockObjectExternal
740 HRESULT WINAPI CoDisconnectObject( LPUNKNOWN lpUnk, DWORD reserved )
746 TRACE("(%p, 0x%08lx)\n", lpUnk, reserved);
748 hr = IUnknown_QueryInterface(lpUnk, &IID_IMarshal, (void **)&marshal);
751 hr = IMarshal_DisconnectObject(marshal, reserved);
752 IMarshal_Release(marshal);
756 apt = COM_CurrentApt();
758 return CO_E_NOTINITIALIZED;
760 apartment_disconnectobject(apt, lpUnk);
762 /* Note: native is pretty broken here because it just silently
763 * fails, without returning an appropriate error code if the object was
764 * not found, making apps think that the object was disconnected, when
765 * it actually wasn't */
770 /******************************************************************************
771 * CoCreateGuid [OLE32.@]
773 * Simply forwards to UuidCreate in RPCRT4.
776 * pguid [O] Points to the GUID to initialize.
780 * Failure: HRESULT code.
785 HRESULT WINAPI CoCreateGuid(GUID *pguid)
787 return UuidCreate(pguid);
790 /******************************************************************************
791 * CLSIDFromString [OLE32.@]
792 * IIDFromString [OLE32.@]
794 * Converts a unique identifier from its string representation into
798 * idstr [I] The string representation of the GUID.
799 * id [O] GUID converted from the string.
803 * CO_E_CLASSSTRING if idstr is not a valid CLSID
807 * In Windows, if idstr is not a valid CLSID string then it gets
808 * treated as a ProgID. Wine currently doesn't do this. If idstr is
809 * NULL it's treated as an all-zero GUID.
814 HRESULT WINAPI __CLSIDFromStringA(LPCSTR idstr, CLSID *id)
821 memset( id, 0, sizeof (CLSID) );
825 /* validate the CLSID string */
826 if (strlen(idstr) != 38)
827 return CO_E_CLASSSTRING;
829 s = (const BYTE *) idstr;
830 if ((s[0]!='{') || (s[9]!='-') || (s[14]!='-') || (s[19]!='-') || (s[24]!='-') || (s[37]!='}'))
831 return CO_E_CLASSSTRING;
833 for (i=1; i<37; i++) {
834 if ((i == 9)||(i == 14)||(i == 19)||(i == 24)) continue;
835 if (!(((s[i] >= '0') && (s[i] <= '9')) ||
836 ((s[i] >= 'a') && (s[i] <= 'f')) ||
837 ((s[i] >= 'A') && (s[i] <= 'F'))))
838 return CO_E_CLASSSTRING;
841 TRACE("%s -> %p\n", s, id);
843 /* quick lookup table */
844 memset(table, 0, 256);
846 for (i = 0; i < 10; i++) {
849 for (i = 0; i < 6; i++) {
850 table['A' + i] = i+10;
851 table['a' + i] = i+10;
854 /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
856 id->Data1 = (table[s[1]] << 28 | table[s[2]] << 24 | table[s[3]] << 20 | table[s[4]] << 16 |
857 table[s[5]] << 12 | table[s[6]] << 8 | table[s[7]] << 4 | table[s[8]]);
858 id->Data2 = table[s[10]] << 12 | table[s[11]] << 8 | table[s[12]] << 4 | table[s[13]];
859 id->Data3 = table[s[15]] << 12 | table[s[16]] << 8 | table[s[17]] << 4 | table[s[18]];
861 /* these are just sequential bytes */
862 id->Data4[0] = table[s[20]] << 4 | table[s[21]];
863 id->Data4[1] = table[s[22]] << 4 | table[s[23]];
864 id->Data4[2] = table[s[25]] << 4 | table[s[26]];
865 id->Data4[3] = table[s[27]] << 4 | table[s[28]];
866 id->Data4[4] = table[s[29]] << 4 | table[s[30]];
867 id->Data4[5] = table[s[31]] << 4 | table[s[32]];
868 id->Data4[6] = table[s[33]] << 4 | table[s[34]];
869 id->Data4[7] = table[s[35]] << 4 | table[s[36]];
874 /*****************************************************************************/
876 HRESULT WINAPI CLSIDFromString(LPOLESTR idstr, CLSID *id )
881 if (!WideCharToMultiByte( CP_ACP, 0, idstr, -1, xid, sizeof(xid), NULL, NULL ))
882 return CO_E_CLASSSTRING;
885 ret = __CLSIDFromStringA(xid,id);
886 if(ret != S_OK) { /* It appears a ProgID is also valid */
887 ret = CLSIDFromProgID(idstr, id);
892 /* Converts a GUID into the respective string representation. */
893 HRESULT WINE_StringFromCLSID(
894 const CLSID *id, /* [in] GUID to be converted */
895 LPSTR idstr /* [out] pointer to buffer to contain converted guid */
897 static const char *hex = "0123456789ABCDEF";
902 { ERR("called with id=Null\n");
907 sprintf(idstr, "{%08lX-%04X-%04X-%02X%02X-",
908 id->Data1, id->Data2, id->Data3,
909 id->Data4[0], id->Data4[1]);
913 for (i = 2; i < 8; i++) {
914 *s++ = hex[id->Data4[i]>>4];
915 *s++ = hex[id->Data4[i] & 0xf];
921 TRACE("%p->%s\n", id, idstr);
927 /******************************************************************************
928 * StringFromCLSID [OLE32.@]
929 * StringFromIID [OLE32.@]
931 * Converts a GUID into the respective string representation.
932 * The target string is allocated using the OLE IMalloc.
935 * id [I] the GUID to be converted.
936 * idstr [O] A pointer to a to-be-allocated pointer pointing to the resulting string.
943 * StringFromGUID2, CLSIDFromString
945 HRESULT WINAPI StringFromCLSID(REFCLSID id, LPOLESTR *idstr)
951 if ((ret = CoGetMalloc(0,&mllc)))
954 ret=WINE_StringFromCLSID(id,buf);
956 DWORD len = MultiByteToWideChar( CP_ACP, 0, buf, -1, NULL, 0 );
957 *idstr = IMalloc_Alloc( mllc, len * sizeof(WCHAR) );
958 MultiByteToWideChar( CP_ACP, 0, buf, -1, *idstr, len );
963 /******************************************************************************
964 * StringFromGUID2 [OLE32.@]
965 * StringFromGUID2 [COMPOBJ.76]
967 * Modified version of StringFromCLSID that allows you to specify max
971 * id [I] GUID to convert to string.
972 * str [O] Buffer where the result will be stored.
973 * cmax [I] Size of the buffer in characters.
976 * Success: The length of the resulting string in characters.
979 INT WINAPI StringFromGUID2(REFGUID id, LPOLESTR str, INT cmax)
983 if (WINE_StringFromCLSID(id,xguid))
985 return MultiByteToWideChar( CP_ACP, 0, xguid, -1, str, cmax );
988 /* open HKCR\\CLSID\\{string form of clsid} key */
989 DWORD COM_OpenKeyForCLSID(REFCLSID clsid, REGSAM access, HKEY *key)
991 static const WCHAR wszCLSIDSlash[] = {'C','L','S','I','D','\\',0};
992 WCHAR path[CHARS_IN_GUID + ARRAYSIZE(wszCLSIDSlash) - 1];
993 strcpyW(path, wszCLSIDSlash);
994 StringFromGUID2(clsid, path + strlenW(wszCLSIDSlash), CHARS_IN_GUID);
995 return RegOpenKeyExW(HKEY_CLASSES_ROOT, path, 0, access, key);
998 /******************************************************************************
999 * ProgIDFromCLSID [OLE32.@]
1001 * Converts a class id into the respective program ID.
1004 * clsid [I] Class ID, as found in registry.
1005 * lplpszProgID [O] Associated ProgID.
1010 * REGDB_E_CLASSNOTREG if the given clsid has no associated ProgID
1012 HRESULT WINAPI ProgIDFromCLSID(REFCLSID clsid, LPOLESTR *lplpszProgID)
1014 static const WCHAR wszProgID[] = {'P','r','o','g','I','D',0};
1019 if (ERROR_SUCCESS != COM_OpenKeyForCLSID(clsid, KEY_READ, &hkey_clsid))
1020 ret = REGDB_E_CLASSNOTREG;
1024 if (RegOpenKeyExW(hkey_clsid, wszProgID, 0, KEY_READ, &hkey))
1025 ret = REGDB_E_CLASSNOTREG;
1026 RegCloseKey(hkey_clsid);
1033 if (RegQueryValueW(hkey, NULL, NULL, &progidlen))
1034 ret = REGDB_E_CLASSNOTREG;
1038 *lplpszProgID = CoTaskMemAlloc(progidlen * sizeof(WCHAR));
1041 if (RegQueryValueW(hkey, NULL, *lplpszProgID, &progidlen))
1042 ret = REGDB_E_CLASSNOTREG;
1045 ret = E_OUTOFMEMORY;
1053 /******************************************************************************
1054 * CLSIDFromProgID [COMPOBJ.61]
1056 * Converts a program ID into the respective GUID.
1059 * progid [I] program id as found in registry
1060 * riid [O] associated CLSID
1064 * Failure: CO_E_CLASSSTRING - the given ProgID cannot be found.
1066 HRESULT WINAPI CLSIDFromProgID16(LPCOLESTR16 progid, LPCLSID riid)
1073 buf = HeapAlloc(GetProcessHeap(),0,strlen(progid)+8);
1074 sprintf(buf,"%s\\CLSID",progid);
1075 if ((err=RegOpenKeyA(HKEY_CLASSES_ROOT,buf,&xhkey))) {
1076 HeapFree(GetProcessHeap(),0,buf);
1077 return CO_E_CLASSSTRING;
1079 HeapFree(GetProcessHeap(),0,buf);
1080 buf2len = sizeof(buf2);
1081 if ((err=RegQueryValueA(xhkey,NULL,buf2,&buf2len))) {
1083 return CO_E_CLASSSTRING;
1086 return __CLSIDFromStringA(buf2,riid);
1089 /******************************************************************************
1090 * CLSIDFromProgID [OLE32.@]
1092 * Converts a program id into the respective GUID.
1095 * progid [I] Unicode program ID, as found in registry.
1096 * riid [O] Associated CLSID.
1100 * Failure: CO_E_CLASSSTRING - the given ProgID cannot be found.
1102 HRESULT WINAPI CLSIDFromProgID(LPCOLESTR progid, LPCLSID riid)
1104 static const WCHAR clsidW[] = { '\\','C','L','S','I','D',0 };
1105 WCHAR buf2[CHARS_IN_GUID];
1106 LONG buf2len = sizeof(buf2);
1109 WCHAR *buf = HeapAlloc( GetProcessHeap(),0,(strlenW(progid)+8) * sizeof(WCHAR) );
1110 strcpyW( buf, progid );
1111 strcatW( buf, clsidW );
1112 if (RegOpenKeyW(HKEY_CLASSES_ROOT,buf,&xhkey))
1114 HeapFree(GetProcessHeap(),0,buf);
1115 return CO_E_CLASSSTRING;
1117 HeapFree(GetProcessHeap(),0,buf);
1119 if (RegQueryValueW(xhkey,NULL,buf2,&buf2len))
1122 return CO_E_CLASSSTRING;
1125 return CLSIDFromString(buf2,riid);
1130 /*****************************************************************************
1131 * CoGetPSClsid [OLE32.@]
1133 * Retrieves the CLSID of the proxy/stub factory that implements
1134 * IPSFactoryBuffer for the specified interface.
1137 * riid [I] Interface whose proxy/stub CLSID is to be returned.
1138 * pclsid [O] Where to store returned proxy/stub CLSID.
1143 * REGDB_E_IIDNOTREG if no PSFactoryBuffer is associated with the IID, or it could not be parsed
1147 * The standard marshaller activates the object with the CLSID
1148 * returned and uses the CreateProxy and CreateStub methods on its
1149 * IPSFactoryBuffer interface to construct the proxies and stubs for a
1152 * CoGetPSClsid determines this CLSID by searching the
1153 * HKEY_CLASSES_ROOT\Interface\{string form of riid}\ProxyStubClsid32
1154 * in the registry and any interface id registered by
1155 * CoRegisterPSClsid within the current process.
1159 * We only search the registry, not ids registered with
1160 * CoRegisterPSClsid.
1161 * Also, native returns S_OK for interfaces with a key in HKCR\Interface, but
1162 * without a ProxyStubClsid32 key and leaves garbage in pclsid. This should be
1163 * considered a bug in native unless an application depends on this (unlikely).
1165 HRESULT WINAPI CoGetPSClsid(REFIID riid, CLSID *pclsid)
1167 static const WCHAR wszInterface[] = {'I','n','t','e','r','f','a','c','e','\\',0};
1168 static const WCHAR wszPSC[] = {'\\','P','r','o','x','y','S','t','u','b','C','l','s','i','d','3','2',0};
1169 WCHAR path[ARRAYSIZE(wszInterface) - 1 + CHARS_IN_GUID - 1 + ARRAYSIZE(wszPSC)];
1170 WCHAR value[CHARS_IN_GUID];
1174 TRACE("() riid=%s, pclsid=%p\n", debugstr_guid(riid), pclsid);
1176 /* Interface\\{string form of riid}\\ProxyStubClsid32 */
1177 strcpyW(path, wszInterface);
1178 StringFromGUID2(riid, path + ARRAYSIZE(wszInterface) - 1, CHARS_IN_GUID);
1179 strcpyW(path + ARRAYSIZE(wszInterface) - 1 + CHARS_IN_GUID - 1, wszPSC);
1181 /* Open the key.. */
1182 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, path, 0, KEY_READ, &hkey))
1184 WARN("No PSFactoryBuffer object is registered for IID %s\n", debugstr_guid(riid));
1185 return REGDB_E_IIDNOTREG;
1188 /* ... Once we have the key, query the registry to get the
1189 value of CLSID as a string, and convert it into a
1190 proper CLSID structure to be passed back to the app */
1191 len = sizeof(value);
1192 if (ERROR_SUCCESS != RegQueryValueW(hkey, NULL, value, &len))
1195 return REGDB_E_IIDNOTREG;
1199 /* We have the CLSid we want back from the registry as a string, so
1200 lets convert it into a CLSID structure */
1201 if (CLSIDFromString(value, pclsid) != NOERROR)
1202 return REGDB_E_IIDNOTREG;
1204 TRACE ("() Returning CLSID=%s\n", debugstr_guid(pclsid));
1210 /***********************************************************************
1211 * WriteClassStm (OLE32.@)
1213 * Writes a CLSID to a stream.
1216 * pStm [I] Stream to write to.
1217 * rclsid [I] CLSID to write.
1221 * Failure: HRESULT code.
1223 HRESULT WINAPI WriteClassStm(IStream *pStm,REFCLSID rclsid)
1225 TRACE("(%p,%p)\n",pStm,rclsid);
1228 return E_INVALIDARG;
1230 return IStream_Write(pStm,rclsid,sizeof(CLSID),NULL);
1233 /***********************************************************************
1234 * ReadClassStm (OLE32.@)
1236 * Reads a CLSID from a stream.
1239 * pStm [I] Stream to read from.
1240 * rclsid [O] CLSID to read.
1244 * Failure: HRESULT code.
1246 HRESULT WINAPI ReadClassStm(IStream *pStm,CLSID *pclsid)
1251 TRACE("(%p,%p)\n",pStm,pclsid);
1254 return E_INVALIDARG;
1256 res = IStream_Read(pStm,(void*)pclsid,sizeof(CLSID),&nbByte);
1261 if (nbByte != sizeof(CLSID))
1269 * COM_GetRegisteredClassObject
1271 * This internal method is used to scan the registered class list to
1272 * find a class object.
1275 * rclsid Class ID of the class to find.
1276 * dwClsContext Class context to match.
1277 * ppv [out] returns a pointer to the class object. Complying
1278 * to normal COM usage, this method will increase the
1279 * reference count on this object.
1281 static HRESULT COM_GetRegisteredClassObject(
1286 HRESULT hr = S_FALSE;
1287 RegisteredClass* curClass;
1289 EnterCriticalSection( &csRegisteredClassList );
1297 * Iterate through the whole list and try to match the class ID.
1299 curClass = firstRegisteredClass;
1301 while (curClass != 0)
1304 * Check if we have a match on the class ID.
1306 if (IsEqualGUID(&(curClass->classIdentifier), rclsid))
1309 * Since we don't do out-of process or DCOM just right away, let's ignore the
1314 * We have a match, return the pointer to the class object.
1316 *ppUnk = curClass->classObject;
1318 IUnknown_AddRef(curClass->classObject);
1325 * Step to the next class in the list.
1327 curClass = curClass->nextClass;
1331 LeaveCriticalSection( &csRegisteredClassList );
1333 * If we get to here, we haven't found our class.
1338 /******************************************************************************
1339 * CoRegisterClassObject [OLE32.@]
1341 * Registers the class object for a given class ID. Servers housed in EXE
1342 * files use this method instead of exporting DllGetClassObject to allow
1343 * other code to connect to their objects.
1346 * rclsid [I] CLSID of the object to register.
1347 * pUnk [I] IUnknown of the object.
1348 * dwClsContext [I] CLSCTX flags indicating the context in which to run the executable.
1349 * flags [I] REGCLS flags indicating how connections are made.
1350 * lpdwRegister [I] A unique cookie that can be passed to CoRevokeClassObject.
1354 * E_INVALIDARG if lpdwRegister or pUnk are NULL,
1355 * CO_E_OBJISREG if the object is already registered. We should not return this.
1358 * CoRevokeClassObject, CoGetClassObject
1361 * MSDN claims that multiple interface registrations are legal, but we
1362 * can't do that with our current implementation.
1364 HRESULT WINAPI CoRegisterClassObject(
1369 LPDWORD lpdwRegister)
1371 RegisteredClass* newClass;
1372 LPUNKNOWN foundObject;
1375 TRACE("(%s,%p,0x%08lx,0x%08lx,%p)\n",
1376 debugstr_guid(rclsid),pUnk,dwClsContext,flags,lpdwRegister);
1378 if ( (lpdwRegister==0) || (pUnk==0) )
1379 return E_INVALIDARG;
1381 if (!COM_CurrentApt())
1383 ERR("COM was not initialized\n");
1384 return CO_E_NOTINITIALIZED;
1390 * First, check if the class is already registered.
1391 * If it is, this should cause an error.
1393 hr = COM_GetRegisteredClassObject(rclsid, dwClsContext, &foundObject);
1395 IUnknown_Release(foundObject);
1396 return CO_E_OBJISREG;
1399 newClass = HeapAlloc(GetProcessHeap(), 0, sizeof(RegisteredClass));
1400 if ( newClass == NULL )
1401 return E_OUTOFMEMORY;
1403 EnterCriticalSection( &csRegisteredClassList );
1405 newClass->classIdentifier = *rclsid;
1406 newClass->runContext = dwClsContext;
1407 newClass->connectFlags = flags;
1408 newClass->pMarshaledData = NULL;
1411 * Use the address of the chain node as the cookie since we are sure it's
1412 * unique. FIXME: not on 64-bit platforms.
1414 newClass->dwCookie = (DWORD)newClass;
1415 newClass->nextClass = firstRegisteredClass;
1418 * Since we're making a copy of the object pointer, we have to increase its
1421 newClass->classObject = pUnk;
1422 IUnknown_AddRef(newClass->classObject);
1424 firstRegisteredClass = newClass;
1425 LeaveCriticalSection( &csRegisteredClassList );
1427 *lpdwRegister = newClass->dwCookie;
1429 if (dwClsContext & CLSCTX_LOCAL_SERVER) {
1430 IClassFactory *classfac;
1432 hr = IUnknown_QueryInterface(newClass->classObject, &IID_IClassFactory,
1433 (LPVOID*)&classfac);
1436 hr = CreateStreamOnHGlobal(0, TRUE, &newClass->pMarshaledData);
1438 FIXME("Failed to create stream on hglobal, %lx\n", hr);
1439 IUnknown_Release(classfac);
1442 hr = CoMarshalInterface(newClass->pMarshaledData, &IID_IClassFactory,
1443 (LPVOID)classfac, MSHCTX_LOCAL, NULL,
1444 MSHLFLAGS_TABLESTRONG);
1446 FIXME("CoMarshalInterface failed, %lx!\n",hr);
1447 IUnknown_Release(classfac);
1451 IUnknown_Release(classfac);
1453 RPC_StartLocalServer(&newClass->classIdentifier, newClass->pMarshaledData);
1458 /***********************************************************************
1459 * CoRevokeClassObject [OLE32.@]
1461 * Removes a class object from the class registry.
1464 * dwRegister [I] Cookie returned from CoRegisterClassObject().
1468 * Failure: HRESULT code.
1471 * CoRegisterClassObject
1473 HRESULT WINAPI CoRevokeClassObject(
1476 HRESULT hr = E_INVALIDARG;
1477 RegisteredClass** prevClassLink;
1478 RegisteredClass* curClass;
1480 TRACE("(%08lx)\n",dwRegister);
1482 EnterCriticalSection( &csRegisteredClassList );
1485 * Iterate through the whole list and try to match the cookie.
1487 curClass = firstRegisteredClass;
1488 prevClassLink = &firstRegisteredClass;
1490 while (curClass != 0)
1493 * Check if we have a match on the cookie.
1495 if (curClass->dwCookie == dwRegister)
1498 * Remove the class from the chain.
1500 *prevClassLink = curClass->nextClass;
1503 * Release the reference to the class object.
1505 IUnknown_Release(curClass->classObject);
1507 if (curClass->pMarshaledData)
1510 memset(&zero, 0, sizeof(zero));
1511 /* FIXME: stop local server thread */
1512 IStream_Seek(curClass->pMarshaledData, zero, SEEK_SET, NULL);
1513 CoReleaseMarshalData(curClass->pMarshaledData);
1517 * Free the memory used by the chain node.
1519 HeapFree(GetProcessHeap(), 0, curClass);
1526 * Step to the next class in the list.
1528 prevClassLink = &(curClass->nextClass);
1529 curClass = curClass->nextClass;
1533 LeaveCriticalSection( &csRegisteredClassList );
1535 * If we get to here, we haven't found our class.
1540 /***********************************************************************
1541 * COM_RegReadPath [internal]
1543 * Reads a registry value and expands it when necessary
1545 HRESULT COM_RegReadPath(HKEY hkeyroot, const WCHAR *keyname, const WCHAR *valuename, WCHAR * dst, DWORD dstlen)
1550 WCHAR src[MAX_PATH];
1551 DWORD dwLength = dstlen * sizeof(WCHAR);
1553 if((hres = RegOpenKeyExW(hkeyroot, keyname, 0, KEY_READ, &key)) == ERROR_SUCCESS) {
1554 if( (hres = RegQueryValueExW(key, NULL, NULL, &keytype, (LPBYTE)src, &dwLength)) == ERROR_SUCCESS ) {
1555 if (keytype == REG_EXPAND_SZ) {
1556 if (dstlen <= ExpandEnvironmentStringsW(src, dst, dstlen)) hres = ERROR_MORE_DATA;
1558 lstrcpynW(dst, src, dstlen);
1566 /***********************************************************************
1567 * CoGetClassObject [OLE32.@]
1569 * FIXME. If request allows of several options and there is a failure
1570 * with one (other than not being registered) do we try the
1571 * others or return failure? (E.g. inprocess is registered but
1572 * the DLL is not found but the server version works)
1574 HRESULT WINAPI CoGetClassObject(
1575 REFCLSID rclsid, DWORD dwClsContext, COSERVERINFO *pServerInfo,
1576 REFIID iid, LPVOID *ppv)
1578 LPUNKNOWN regClassObject;
1579 HRESULT hres = E_UNEXPECTED;
1581 TRACE("\n\tCLSID:\t%s,\n\tIID:\t%s\n", debugstr_guid(rclsid), debugstr_guid(iid));
1584 FIXME("\tpServerInfo: name=%s\n",debugstr_w(pServerInfo->pwszName));
1585 FIXME("\t\tpAuthInfo=%p\n",pServerInfo->pAuthInfo);
1589 * First, try and see if we can't match the class ID with one of the
1590 * registered classes.
1592 if (S_OK == COM_GetRegisteredClassObject(rclsid, dwClsContext, ®ClassObject))
1594 /* Get the required interface from the retrieved pointer. */
1595 hres = IUnknown_QueryInterface(regClassObject, iid, ppv);
1598 * Since QI got another reference on the pointer, we want to release the
1599 * one we already have. If QI was unsuccessful, this will release the object. This
1600 * is good since we are not returning it in the "out" parameter.
1602 IUnknown_Release(regClassObject);
1607 /* first try: in-process */
1608 if ((CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER) & dwClsContext)
1610 static const WCHAR wszInprocServer32[] = {'I','n','p','r','o','c','S','e','r','v','e','r','3','2',0};
1612 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid, REFIID iid, LPVOID *ppv);
1613 DllGetClassObjectFunc DllGetClassObject;
1614 WCHAR dllpath[MAX_PATH+1];
1617 if (ERROR_SUCCESS != COM_OpenKeyForCLSID(rclsid, KEY_READ, &hkey))
1619 ERR("class %s not registered\n", debugstr_guid(rclsid));
1620 hres = REGDB_E_CLASSNOTREG;
1623 if (COM_RegReadPath(hkey, wszInprocServer32, NULL, dllpath, ARRAYSIZE(dllpath)) != ERROR_SUCCESS)
1625 /* failure: CLSID is not found in registry */
1626 WARN("class %s not registered inproc\n", debugstr_guid(rclsid));
1627 hres = REGDB_E_CLASSNOTREG;
1631 if ((hLibrary = LoadLibraryExW(dllpath, 0, LOAD_WITH_ALTERED_SEARCH_PATH)) == 0)
1633 /* failure: DLL could not be loaded */
1634 ERR("couldn't load InprocServer32 dll %s\n", debugstr_w(dllpath));
1635 hres = E_ACCESSDENIED; /* FIXME: or should this be CO_E_DLLNOTFOUND? */
1637 else if (!(DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress(hLibrary, "DllGetClassObject")))
1639 /* failure: the dll did not export DllGetClassObject */
1640 ERR("couldn't find function DllGetClassObject in %s\n", debugstr_w(dllpath));
1641 FreeLibrary( hLibrary );
1642 hres = CO_E_DLLNOTFOUND;
1646 /* OK: get the ClassObject */
1647 COMPOBJ_DLLList_Add( hLibrary );
1648 return DllGetClassObject(rclsid, iid, ppv);
1653 /* Next try out of process */
1654 if (CLSCTX_LOCAL_SERVER & dwClsContext)
1656 return RPC_GetLocalClassObject(rclsid,iid,ppv);
1659 /* Finally try remote: this requires networked DCOM (a lot of work) */
1660 if (CLSCTX_REMOTE_SERVER & dwClsContext)
1662 FIXME ("CLSCTX_REMOTE_SERVER not supported\n");
1663 hres = E_NOINTERFACE;
1669 /***********************************************************************
1670 * CoGetClassObject [COMPOBJ.7]
1673 HRESULT WINAPI CoGetClassObject16(
1674 REFCLSID rclsid, DWORD dwClsContext, COSERVERINFO *pServerInfo,
1675 REFIID iid, LPVOID *ppv)
1677 FIXME(", stub!\n\tCLSID:\t%s,\n\tIID:\t%s\n", debugstr_guid(rclsid), debugstr_guid(iid));
1680 FIXME("\tpServerInfo: name=%s\n",debugstr_w(pServerInfo->pwszName));
1681 FIXME("\t\tpAuthInfo=%p\n",pServerInfo->pAuthInfo);
1685 /***********************************************************************
1686 * CoResumeClassObjects (OLE32.@)
1688 * Resumes all class objects registered with REGCLS_SUSPENDED.
1692 * Failure: HRESULT code.
1694 HRESULT WINAPI CoResumeClassObjects(void)
1700 /***********************************************************************
1701 * GetClassFile (OLE32.@)
1703 * This function supplies the CLSID associated with the given filename.
1705 HRESULT WINAPI GetClassFile(LPCOLESTR filePathName,CLSID *pclsid)
1709 int nbElm, length, i;
1711 LPOLESTR *pathDec=0,absFile=0,progId=0;
1713 static const WCHAR bkslashW[] = {'\\',0};
1714 static const WCHAR dotW[] = {'.',0};
1716 TRACE("%s, %p\n", debugstr_w(filePathName), pclsid);
1718 /* if the file contain a storage object the return the CLSID written by IStorage_SetClass method*/
1719 if((StgIsStorageFile(filePathName))==S_OK){
1721 res=StgOpenStorage(filePathName,NULL,STGM_READ | STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
1724 res=ReadClassStg(pstg,pclsid);
1726 IStorage_Release(pstg);
1730 /* if the file is not a storage object then attemps to match various bits in the file against a
1731 pattern in the registry. this case is not frequently used ! so I present only the psodocode for
1734 for(i=0;i<nFileTypes;i++)
1736 for(i=0;j<nPatternsForType;j++){
1741 pat=ReadPatternFromRegistry(i,j);
1742 hFile=CreateFileW(filePathName,,,,,,hFile);
1743 SetFilePosition(hFile,pat.offset);
1744 ReadFile(hFile,buf,pat.size,&r,NULL);
1745 if (memcmp(buf&pat.mask,pat.pattern.pat.size)==0){
1747 *pclsid=ReadCLSIDFromRegistry(i);
1753 /* if the above strategies fail then search for the extension key in the registry */
1755 /* get the last element (absolute file) in the path name */
1756 nbElm=FileMonikerImpl_DecomposePath(filePathName,&pathDec);
1757 absFile=pathDec[nbElm-1];
1759 /* failed if the path represente a directory and not an absolute file name*/
1760 if (!lstrcmpW(absFile, bkslashW))
1761 return MK_E_INVALIDEXTENSION;
1763 /* get the extension of the file */
1765 length=lstrlenW(absFile);
1766 for(i = length-1; (i >= 0) && *(extension = &absFile[i]) != '.'; i--)
1769 if (!extension || !lstrcmpW(extension, dotW))
1770 return MK_E_INVALIDEXTENSION;
1772 res=RegQueryValueW(HKEY_CLASSES_ROOT, extension, NULL, &sizeProgId);
1774 /* get the progId associated to the extension */
1775 progId = CoTaskMemAlloc(sizeProgId);
1776 res = RegQueryValueW(HKEY_CLASSES_ROOT, extension, progId, &sizeProgId);
1778 if (res==ERROR_SUCCESS)
1779 /* return the clsid associated to the progId */
1780 res= CLSIDFromProgID(progId,pclsid);
1782 for(i=0; pathDec[i]!=NULL;i++)
1783 CoTaskMemFree(pathDec[i]);
1784 CoTaskMemFree(pathDec);
1786 CoTaskMemFree(progId);
1788 if (res==ERROR_SUCCESS)
1791 return MK_E_INVALIDEXTENSION;
1794 /***********************************************************************
1795 * CoCreateInstance [OLE32.@]
1797 HRESULT WINAPI CoCreateInstance(
1799 LPUNKNOWN pUnkOuter,
1805 LPCLASSFACTORY lpclf = 0;
1807 if (!COM_CurrentApt()) return CO_E_NOTINITIALIZED;
1816 * Initialize the "out" parameter
1821 * The Standard Global Interface Table (GIT) object is a process-wide singleton.
1822 * Rather than create a class factory, we can just check for it here
1824 if (IsEqualIID(rclsid, &CLSID_StdGlobalInterfaceTable)) {
1825 if (StdGlobalInterfaceTableInstance == NULL)
1826 StdGlobalInterfaceTableInstance = StdGlobalInterfaceTable_Construct();
1827 hres = IGlobalInterfaceTable_QueryInterface( (IGlobalInterfaceTable*) StdGlobalInterfaceTableInstance, iid, ppv);
1828 if (hres) return hres;
1830 TRACE("Retrieved GIT (%p)\n", *ppv);
1835 * Get a class factory to construct the object we want.
1837 hres = CoGetClassObject(rclsid,
1844 FIXME("no classfactory created for CLSID %s, hres is 0x%08lx\n",
1845 debugstr_guid(rclsid),hres);
1850 * Create the object and don't forget to release the factory
1852 hres = IClassFactory_CreateInstance(lpclf, pUnkOuter, iid, ppv);
1853 IClassFactory_Release(lpclf);
1855 FIXME("no instance created for interface %s of class %s, hres is 0x%08lx\n",
1856 debugstr_guid(iid), debugstr_guid(rclsid),hres);
1861 /***********************************************************************
1862 * CoCreateInstance [COMPOBJ.13]
1864 HRESULT WINAPI CoCreateInstance16(
1866 LPUNKNOWN pUnkOuter,
1871 FIXME("(%s, %p, %lx, %s, %p), stub!\n",
1872 debugstr_guid(rclsid), pUnkOuter, dwClsContext, debugstr_guid(iid),
1878 /***********************************************************************
1879 * CoCreateInstanceEx [OLE32.@]
1881 HRESULT WINAPI CoCreateInstanceEx(
1883 LPUNKNOWN pUnkOuter,
1885 COSERVERINFO* pServerInfo,
1889 IUnknown* pUnk = NULL;
1892 ULONG successCount = 0;
1897 if ( (cmq==0) || (pResults==NULL))
1898 return E_INVALIDARG;
1900 if (pServerInfo!=NULL)
1901 FIXME("() non-NULL pServerInfo not supported!\n");
1904 * Initialize all the "out" parameters.
1906 for (index = 0; index < cmq; index++)
1908 pResults[index].pItf = NULL;
1909 pResults[index].hr = E_NOINTERFACE;
1913 * Get the object and get its IUnknown pointer.
1915 hr = CoCreateInstance(rclsid,
1925 * Then, query for all the interfaces requested.
1927 for (index = 0; index < cmq; index++)
1929 pResults[index].hr = IUnknown_QueryInterface(pUnk,
1930 pResults[index].pIID,
1931 (VOID**)&(pResults[index].pItf));
1933 if (pResults[index].hr == S_OK)
1938 * Release our temporary unknown pointer.
1940 IUnknown_Release(pUnk);
1942 if (successCount == 0)
1943 return E_NOINTERFACE;
1945 if (successCount!=cmq)
1946 return CO_S_NOTALLINTERFACES;
1951 /***********************************************************************
1952 * CoLoadLibrary (OLE32.@)
1957 * lpszLibName [I] Path to library.
1958 * bAutoFree [I] Whether the library should automatically be freed.
1961 * Success: Handle to loaded library.
1965 * CoFreeLibrary, CoFreeAllLibraries, CoFreeUnusedLibraries
1967 HINSTANCE WINAPI CoLoadLibrary(LPOLESTR lpszLibName, BOOL bAutoFree)
1969 TRACE("(%s, %d)\n", debugstr_w(lpszLibName), bAutoFree);
1971 return LoadLibraryExW(lpszLibName, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
1974 /***********************************************************************
1975 * CoFreeLibrary [OLE32.@]
1977 * Unloads a library from memory.
1980 * hLibrary [I] Handle to library to unload.
1986 * CoLoadLibrary, CoFreeAllLibraries, CoFreeUnusedLibraries
1988 void WINAPI CoFreeLibrary(HINSTANCE hLibrary)
1990 FreeLibrary(hLibrary);
1994 /***********************************************************************
1995 * CoFreeAllLibraries [OLE32.@]
1997 * Function for backwards compatibility only. Does nothing.
2003 * CoLoadLibrary, CoFreeLibrary, CoFreeUnusedLibraries
2005 void WINAPI CoFreeAllLibraries(void)
2011 /***********************************************************************
2012 * CoFreeUnusedLibraries [OLE32.@]
2013 * CoFreeUnusedLibraries [COMPOBJ.17]
2015 * Frees any unused libraries. Unused are identified as those that return
2016 * S_OK from their DllCanUnloadNow function.
2022 * CoLoadLibrary, CoFreeAllLibraries, CoFreeLibrary
2024 void WINAPI CoFreeUnusedLibraries(void)
2026 /* FIXME: Calls to CoFreeUnusedLibraries from any thread always route
2027 * through the main apartment's thread to call DllCanUnloadNow */
2028 COMPOBJ_DllList_FreeUnused(0);
2031 /***********************************************************************
2032 * CoFileTimeNow [OLE32.@]
2033 * CoFileTimeNow [COMPOBJ.82]
2035 * Retrieves the current time in FILETIME format.
2038 * lpFileTime [O] The current time.
2043 HRESULT WINAPI CoFileTimeNow( FILETIME *lpFileTime )
2045 GetSystemTimeAsFileTime( lpFileTime );
2049 static void COM_RevokeAllClasses()
2051 EnterCriticalSection( &csRegisteredClassList );
2053 while (firstRegisteredClass!=0)
2055 CoRevokeClassObject(firstRegisteredClass->dwCookie);
2058 LeaveCriticalSection( &csRegisteredClassList );
2061 /******************************************************************************
2062 * CoLockObjectExternal [OLE32.@]
2064 * Increments or decrements the external reference count of a stub object.
2067 * pUnk [I] Stub object.
2068 * fLock [I] If TRUE then increments the external ref-count,
2069 * otherwise decrements.
2070 * fLastUnlockReleases [I] If TRUE then the last unlock has the effect of
2071 * calling CoDisconnectObject.
2075 * Failure: HRESULT code.
2077 HRESULT WINAPI CoLockObjectExternal(
2080 BOOL fLastUnlockReleases)
2082 struct stub_manager *stubmgr;
2083 struct apartment *apt;
2085 TRACE("pUnk=%p, fLock=%s, fLastUnlockReleases=%s\n",
2086 pUnk, fLock ? "TRUE" : "FALSE", fLastUnlockReleases ? "TRUE" : "FALSE");
2088 apt = COM_CurrentApt();
2089 if (!apt) return CO_E_NOTINITIALIZED;
2091 stubmgr = get_stub_manager_from_object(apt, pUnk);
2096 stub_manager_ext_addref(stubmgr, 1);
2098 stub_manager_ext_release(stubmgr, 1);
2100 stub_manager_int_release(stubmgr);
2106 WARN("stub object not found %p\n", pUnk);
2107 /* Note: native is pretty broken here because it just silently
2108 * fails, without returning an appropriate error code, making apps
2109 * think that the object was disconnected, when it actually wasn't */
2114 /***********************************************************************
2115 * CoInitializeWOW (OLE32.@)
2117 * WOW equivalent of CoInitialize?
2126 HRESULT WINAPI CoInitializeWOW(DWORD x,DWORD y)
2128 FIXME("(0x%08lx,0x%08lx),stub!\n",x,y);
2132 /***********************************************************************
2133 * CoGetState [OLE32.@]
2135 * Retrieves the thread state object previously stored by CoSetState().
2138 * ppv [I] Address where pointer to object will be stored.
2142 * Failure: E_OUTOFMEMORY.
2145 * Crashes on all invalid ppv addresses, including NULL.
2146 * If the function returns a non-NULL object then the caller must release its
2147 * reference on the object when the object is no longer required.
2152 HRESULT WINAPI CoGetState(IUnknown ** ppv)
2154 struct oletls *info = COM_CurrentInfo();
2155 if (!info) return E_OUTOFMEMORY;
2161 IUnknown_AddRef(info->state);
2163 TRACE("apt->state=%p\n", info->state);
2169 /***********************************************************************
2170 * CoSetState [OLE32.@]
2172 * Sets the thread state object.
2175 * pv [I] Pointer to state object to be stored.
2178 * The system keeps a reference on the object while the object stored.
2182 * Failure: E_OUTOFMEMORY.
2184 HRESULT WINAPI CoSetState(IUnknown * pv)
2186 struct oletls *info = COM_CurrentInfo();
2187 if (!info) return E_OUTOFMEMORY;
2189 if (pv) IUnknown_AddRef(pv);
2193 TRACE("-- release %p now\n", info->state);
2194 IUnknown_Release(info->state);
2203 /******************************************************************************
2204 * OleGetAutoConvert [OLE32.@]
2206 HRESULT WINAPI OleGetAutoConvert(REFCLSID clsidOld, LPCLSID pClsidNew)
2208 static const WCHAR wszAutoConvertTo[] = {'A','u','t','o','C','o','n','v','e','r','t','T','o',0};
2210 WCHAR buf[CHARS_IN_GUID];
2214 if (ERROR_SUCCESS != COM_OpenKeyForCLSID(clsidOld, KEY_READ, &hkey))
2216 res = REGDB_E_CLASSNOTREG;
2221 /* we can just query for the default value of AutoConvertTo key like that,
2222 without opening the AutoConvertTo key and querying for NULL (default) */
2223 if (RegQueryValueW(hkey, wszAutoConvertTo, buf, &len))
2225 res = REGDB_E_KEYMISSING;
2228 res = CLSIDFromString(buf, pClsidNew);
2230 if (hkey) RegCloseKey(hkey);
2234 /******************************************************************************
2235 * CoTreatAsClass [OLE32.@]
2237 * Sets the TreatAs value of a class.
2240 * clsidOld [I] Class to set TreatAs value on.
2241 * clsidNew [I] The class the clsidOld should be treated as.
2245 * Failure: HRESULT code.
2250 HRESULT WINAPI CoTreatAsClass(REFCLSID clsidOld, REFCLSID clsidNew)
2252 static const WCHAR wszAutoTreatAs[] = {'A','u','t','o','T','r','e','a','t','A','s',0};
2253 static const WCHAR wszTreatAs[] = {'T','r','e','a','t','A','s',0};
2255 WCHAR szClsidNew[CHARS_IN_GUID];
2257 WCHAR auto_treat_as[CHARS_IN_GUID];
2258 LONG auto_treat_as_size = sizeof(auto_treat_as);
2261 if (ERROR_SUCCESS != COM_OpenKeyForCLSID(clsidOld, KEY_READ | KEY_WRITE, &hkey))
2263 res = REGDB_E_CLASSNOTREG;
2266 if (!memcmp( clsidOld, clsidNew, sizeof(*clsidOld) ))
2268 if (!RegQueryValueW(hkey, wszAutoTreatAs, auto_treat_as, &auto_treat_as_size) &&
2269 !CLSIDFromString(auto_treat_as, &id))
2271 if (RegSetValueW(hkey, wszTreatAs, REG_SZ, auto_treat_as, sizeof(auto_treat_as)))
2273 res = REGDB_E_WRITEREGDB;
2279 RegDeleteKeyW(hkey, wszTreatAs);
2283 else if (!StringFromGUID2(clsidNew, szClsidNew, ARRAYSIZE(szClsidNew)) &&
2284 !RegSetValueW(hkey, wszTreatAs, REG_SZ, szClsidNew, sizeof(szClsidNew)))
2286 res = REGDB_E_WRITEREGDB;
2291 if (hkey) RegCloseKey(hkey);
2295 /******************************************************************************
2296 * CoGetTreatAsClass [OLE32.@]
2298 * Gets the TreatAs value of a class.
2301 * clsidOld [I] Class to get the TreatAs value of.
2302 * clsidNew [I] The class the clsidOld should be treated as.
2306 * Failure: HRESULT code.
2311 HRESULT WINAPI CoGetTreatAsClass(REFCLSID clsidOld, LPCLSID clsidNew)
2313 static const WCHAR wszTreatAs[] = {'T','r','e','a','t','A','s',0};
2315 WCHAR szClsidNew[CHARS_IN_GUID];
2317 LONG len = sizeof(szClsidNew);
2319 FIXME("(%s,%p)\n", debugstr_guid(clsidOld), clsidNew);
2320 memcpy(clsidNew,clsidOld,sizeof(CLSID)); /* copy over old value */
2322 if (COM_OpenKeyForCLSID(clsidOld, KEY_READ, &hkey))
2324 res = REGDB_E_CLASSNOTREG;
2327 if (RegQueryValueW(hkey, wszTreatAs, szClsidNew, &len))
2332 res = CLSIDFromString(szClsidNew,clsidNew);
2334 ERR("Failed CLSIDFromStringA(%s), hres 0x%08lx\n", debugstr_w(szClsidNew), res);
2336 if (hkey) RegCloseKey(hkey);
2340 /******************************************************************************
2341 * CoGetCurrentProcess [OLE32.@]
2342 * CoGetCurrentProcess [COMPOBJ.34]
2344 * Gets the current process ID.
2347 * The current process ID.
2350 * Is DWORD really the correct return type for this function?
2352 DWORD WINAPI CoGetCurrentProcess(void)
2354 return GetCurrentProcessId();
2357 /******************************************************************************
2358 * CoRegisterMessageFilter [OLE32.@]
2360 * Registers a message filter.
2363 * lpMessageFilter [I] Pointer to interface.
2364 * lplpMessageFilter [O] Indirect pointer to prior instance if non-NULL.
2368 * Failure: HRESULT code.
2370 HRESULT WINAPI CoRegisterMessageFilter(
2371 LPMESSAGEFILTER lpMessageFilter,
2372 LPMESSAGEFILTER *lplpMessageFilter)
2375 if (lplpMessageFilter) {
2376 *lplpMessageFilter = NULL;
2381 /***********************************************************************
2382 * CoIsOle1Class [OLE32.@]
2384 * Determines whether the specified class an OLE v1 class.
2387 * clsid [I] Class to test.
2390 * TRUE if the class is an OLE v1 class, or FALSE otherwise.
2392 BOOL WINAPI CoIsOle1Class(REFCLSID clsid)
2394 FIXME("%s\n", debugstr_guid(clsid));
2398 /***********************************************************************
2399 * IsEqualGUID [OLE32.@]
2401 * Compares two Unique Identifiers.
2404 * rguid1 [I] The first GUID to compare.
2405 * rguid2 [I] The other GUID to compare.
2411 BOOL WINAPI IsEqualGUID(
2415 return !memcmp(rguid1,rguid2,sizeof(GUID));
2418 /***********************************************************************
2419 * CoInitializeSecurity [OLE32.@]
2421 HRESULT WINAPI CoInitializeSecurity(PSECURITY_DESCRIPTOR pSecDesc, LONG cAuthSvc,
2422 SOLE_AUTHENTICATION_SERVICE* asAuthSvc,
2423 void* pReserved1, DWORD dwAuthnLevel,
2424 DWORD dwImpLevel, void* pReserved2,
2425 DWORD dwCapabilities, void* pReserved3)
2427 FIXME("(%p,%ld,%p,%p,%ld,%ld,%p,%ld,%p) - stub!\n", pSecDesc, cAuthSvc,
2428 asAuthSvc, pReserved1, dwAuthnLevel, dwImpLevel, pReserved2,
2429 dwCapabilities, pReserved3);
2433 /***********************************************************************
2434 * CoSuspendClassObjects [OLE32.@]
2436 * Suspends all registered class objects to prevent further requests coming in
2437 * for those objects.
2441 * Failure: HRESULT code.
2443 HRESULT WINAPI CoSuspendClassObjects(void)
2449 /***********************************************************************
2450 * CoAddRefServerProcess [OLE32.@]
2452 * Helper function for incrementing the reference count of a local-server
2456 * New reference count.
2458 ULONG WINAPI CoAddRefServerProcess(void)
2464 /***********************************************************************
2465 * CoReleaseServerProcess [OLE32.@]
2467 * Helper function for decrementing the reference count of a local-server
2471 * New reference count.
2473 ULONG WINAPI CoReleaseServerProcess(void)
2479 /***********************************************************************
2480 * CoIsHandlerConnected [OLE32.@]
2482 * Determines whether a proxy is connected to a remote stub.
2485 * pUnk [I] Pointer to object that may or may not be connected.
2488 * TRUE if pUnk is not a proxy or if pUnk is connected to a remote stub, or
2491 BOOL WINAPI CoIsHandlerConnected(IUnknown *pUnk)
2493 FIXME("%p\n", pUnk);
2498 /***********************************************************************
2499 * CoQueryProxyBlanket [OLE32.@]
2501 * Retrieves the security settings being used by a proxy.
2504 * pProxy [I] Pointer to the proxy object.
2505 * pAuthnSvc [O] The type of authentication service.
2506 * pAuthzSvc [O] The type of authorization service.
2507 * ppServerPrincName [O] Optional. The server prinicple name.
2508 * pAuthnLevel [O] The authentication level.
2509 * pImpLevel [O] The impersonation level.
2510 * ppAuthInfo [O] Information specific to the authorization/authentication service.
2511 * pCapabilities [O] Flags affecting the security behaviour.
2515 * Failure: HRESULT code.
2518 * CoCopyProxy, CoSetProxyBlanket.
2520 HRESULT WINAPI CoQueryProxyBlanket(IUnknown *pProxy, DWORD *pAuthnSvc,
2521 DWORD *pAuthzSvc, OLECHAR **ppServerPrincName, DWORD *pAuthnLevel,
2522 DWORD *pImpLevel, void **ppAuthInfo, DWORD *pCapabilities)
2524 IClientSecurity *pCliSec;
2527 TRACE("%p\n", pProxy);
2529 hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (void **)&pCliSec);
2532 hr = IClientSecurity_QueryBlanket(pCliSec, pProxy, pAuthnSvc,
2533 pAuthzSvc, ppServerPrincName,
2534 pAuthnLevel, pImpLevel, ppAuthInfo,
2536 IClientSecurity_Release(pCliSec);
2539 if (FAILED(hr)) ERR("-- failed with 0x%08lx\n", hr);
2543 /***********************************************************************
2544 * CoSetProxyBlanket [OLE32.@]
2546 * Sets the security settings for a proxy.
2549 * pProxy [I] Pointer to the proxy object.
2550 * AuthnSvc [I] The type of authentication service.
2551 * AuthzSvc [I] The type of authorization service.
2552 * pServerPrincName [I] The server prinicple name.
2553 * AuthnLevel [I] The authentication level.
2554 * ImpLevel [I] The impersonation level.
2555 * pAuthInfo [I] Information specific to the authorization/authentication service.
2556 * Capabilities [I] Flags affecting the security behaviour.
2560 * Failure: HRESULT code.
2563 * CoQueryProxyBlanket, CoCopyProxy.
2565 HRESULT WINAPI CoSetProxyBlanket(IUnknown *pProxy, DWORD AuthnSvc,
2566 DWORD AuthzSvc, OLECHAR *pServerPrincName, DWORD AuthnLevel,
2567 DWORD ImpLevel, void *pAuthInfo, DWORD Capabilities)
2569 IClientSecurity *pCliSec;
2572 TRACE("%p\n", pProxy);
2574 hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (void **)&pCliSec);
2577 hr = IClientSecurity_SetBlanket(pCliSec, pProxy, AuthnSvc,
2578 AuthzSvc, pServerPrincName,
2579 AuthnLevel, ImpLevel, pAuthInfo,
2581 IClientSecurity_Release(pCliSec);
2584 if (FAILED(hr)) ERR("-- failed with 0x%08lx\n", hr);
2588 /***********************************************************************
2589 * CoCopyProxy [OLE32.@]
2594 * pProxy [I] Pointer to the proxy object.
2595 * ppCopy [O] Copy of the proxy.
2599 * Failure: HRESULT code.
2602 * CoQueryProxyBlanket, CoSetProxyBlanket.
2604 HRESULT WINAPI CoCopyProxy(IUnknown *pProxy, IUnknown **ppCopy)
2606 IClientSecurity *pCliSec;
2609 TRACE("%p\n", pProxy);
2611 hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (void **)&pCliSec);
2614 hr = IClientSecurity_CopyProxy(pCliSec, pProxy, ppCopy);
2615 IClientSecurity_Release(pCliSec);
2618 if (FAILED(hr)) ERR("-- failed with 0x%08lx\n", hr);
2623 /***********************************************************************
2624 * CoWaitForMultipleHandles [OLE32.@]
2626 * Waits for one or more handles to become signaled.
2629 * dwFlags [I] Flags. See notes.
2630 * dwTimeout [I] Timeout in milliseconds.
2631 * cHandles [I] Number of handles pointed to by pHandles.
2632 * pHandles [I] Handles to wait for.
2633 * lpdwindex [O] Index of handle that was signaled.
2637 * Failure: RPC_S_CALLPENDING on timeout.
2641 * The dwFlags parameter can be zero or more of the following:
2642 *| COWAIT_WAITALL - Wait for all of the handles to become signaled.
2643 *| COWAIT_ALERTABLE - Allows a queued APC to run during the wait.
2646 * MsgWaitForMultipleObjects, WaitForMultipleObjects.
2648 HRESULT WINAPI CoWaitForMultipleHandles(DWORD dwFlags, DWORD dwTimeout,
2649 ULONG cHandles, const HANDLE* pHandles, LPDWORD lpdwindex)
2652 DWORD wait_flags = (dwFlags & COWAIT_WAITALL) ? MWMO_WAITALL : 0 |
2653 (dwFlags & COWAIT_ALERTABLE ) ? MWMO_ALERTABLE : 0;
2654 DWORD start_time = GetTickCount();
2656 TRACE("(0x%08lx, 0x%08lx, %ld, %p, %p)\n", dwFlags, dwTimeout, cHandles,
2657 pHandles, lpdwindex);
2661 DWORD now = GetTickCount();
2664 if ((dwTimeout != INFINITE) && (start_time + dwTimeout >= now))
2666 hr = RPC_S_CALLPENDING;
2670 TRACE("waiting for rpc completion or window message\n");
2672 res = MsgWaitForMultipleObjectsEx(cHandles, pHandles,
2673 (dwTimeout == INFINITE) ? INFINITE : start_time + dwTimeout - now,
2674 QS_ALLINPUT, wait_flags);
2676 if (res == WAIT_OBJECT_0 + cHandles) /* messages available */
2679 while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
2681 /* FIXME: filter the messages here */
2682 TRACE("received message whilst waiting for RPC: 0x%04x\n", msg.message);
2683 TranslateMessage(&msg);
2684 DispatchMessageW(&msg);
2687 else if ((res >= WAIT_OBJECT_0) && (res < WAIT_OBJECT_0 + cHandles))
2689 /* handle signaled, store index */
2690 *lpdwindex = (res - WAIT_OBJECT_0);
2693 else if (res == WAIT_TIMEOUT)
2695 hr = RPC_S_CALLPENDING;
2700 ERR("Unexpected wait termination: %ld, %ld\n", res, GetLastError());
2705 TRACE("-- 0x%08lx\n", hr);
2709 /***********************************************************************
2712 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID fImpLoad)
2714 TRACE("%p 0x%lx %p\n", hinstDLL, fdwReason, fImpLoad);
2717 case DLL_PROCESS_ATTACH:
2718 OLE32_hInstance = hinstDLL;
2719 COMPOBJ_InitProcess();
2720 if (TRACE_ON(ole)) CoRegisterMallocSpy((LPVOID)-1);
2723 case DLL_PROCESS_DETACH:
2724 if (TRACE_ON(ole)) CoRevokeMallocSpy();
2725 COMPOBJ_UninitProcess();
2726 OLE32_hInstance = 0;
2729 case DLL_THREAD_DETACH:
2736 /* NOTE: DllRegisterServer and DllUnregisterServer are in regsvr.c */