Moved mciavi32 to the top-level dlls directory.
[wine] / dlls / ole32 / compobj.c
1 /*
2  *      COMPOBJ library
3  *
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  *
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.
15  *
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.
20  *
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
24  *
25  * Note
26  * 1. COINIT_MULTITHREADED is 0; it is the lack of COINIT_APARTMENTTHREADED
27  *    Therefore do not test against COINIT_MULTITHREADED
28  *
29  * TODO list:           (items bunched together depend on each other)
30  *
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
35  *
36  *   - Pump the message loop during RPC calls.
37  *   - Call IMessageFilter functions.
38  *
39  *   - Make all ole interface marshaling use NDR to be wire compatible with
40  *     native DCOM
41  *   - Use & interpret ORPCTHIS & ORPCTHAT.
42  *
43  */
44
45 #include "config.h"
46
47 #include <stdlib.h>
48 #include <stdarg.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <assert.h>
52
53 #define COBJMACROS
54 #define NONAMELESSUNION
55 #define NONAMELESSSTRUCT
56
57 #include "windef.h"
58 #include "winbase.h"
59 #include "winuser.h"
60 #include "objbase.h"
61 #include "ole2.h"
62 #include "ole2ver.h"
63 #include "rpc.h"
64 #include "winerror.h"
65 #include "winreg.h"
66 #include "wownt32.h"
67 #include "wine/unicode.h"
68 #include "objbase.h"
69 #include "compobj_private.h"
70
71 #include "wine/debug.h"
72
73 WINE_DEFAULT_DEBUG_CHANNEL(ole);
74
75 typedef LPCSTR LPCOLESTR16;
76
77 HINSTANCE OLE32_hInstance = 0; /* FIXME: make static ... */
78
79 #define ARRAYSIZE(array) (sizeof(array)/sizeof((array)[0]))
80
81 /****************************************************************************
82  * This section defines variables internal to the COM module.
83  *
84  * TODO: Most of these things will have to be made thread-safe.
85  */
86
87 static HRESULT COM_GetRegisteredClassObject(REFCLSID rclsid, DWORD dwClsContext, LPUNKNOWN*  ppUnk);
88 static void COM_RevokeAllClasses(void);
89
90 const CLSID CLSID_StdGlobalInterfaceTable = { 0x00000323, 0, 0, {0xc0, 0, 0, 0, 0, 0, 0, 0x46} };
91
92 APARTMENT *MTA; /* protected by csApartment */
93 static struct list apts = LIST_INIT( apts ); /* protected by csApartment */
94
95 static CRITICAL_SECTION csApartment;
96 static CRITICAL_SECTION_DEBUG critsect_debug =
97 {
98     0, 0, &csApartment,
99     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
100       0, 0, { 0, (DWORD)(__FILE__ ": csApartment") }
101 };
102 static CRITICAL_SECTION csApartment = { &critsect_debug, -1, 0, 0, 0, 0 };
103
104 /*
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
108  */
109 static LONG s_COMLockCount = 0;
110
111 /*
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
114  * objects.
115  *
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.
118  */
119 typedef struct tagRegisteredClass
120 {
121   CLSID     classIdentifier;
122   LPUNKNOWN classObject;
123   DWORD     runContext;
124   DWORD     connectFlags;
125   DWORD     dwCookie;
126   LPSTREAM  pMarshaledData; /* FIXME: only really need to store OXID and IPID */
127   struct tagRegisteredClass* nextClass;
128 } RegisteredClass;
129
130 static RegisteredClass* firstRegisteredClass = NULL;
131
132 static CRITICAL_SECTION csRegisteredClassList;
133 static CRITICAL_SECTION_DEBUG class_cs_debug =
134 {
135     0, 0, &csRegisteredClassList,
136     { &class_cs_debug.ProcessLocksList, &class_cs_debug.ProcessLocksList },
137       0, 0, { 0, (DWORD)(__FILE__ ": csRegisteredClassList") }
138 };
139 static CRITICAL_SECTION csRegisteredClassList = { &class_cs_debug, -1, 0, 0, 0, 0 };
140
141 /*****************************************************************************
142  * This section contains OpenDllList definitions
143  *
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.
147  *
148  * FIXME: a DLL that says OK when asked for unloading is unloaded in the
149  * next unload-call but not before 600 sec.
150  */
151
152 typedef struct tagOpenDll {
153   HINSTANCE hLibrary;
154   struct tagOpenDll *next;
155 } OpenDll;
156
157 static OpenDll *openDllList = NULL; /* linked list of open dlls */
158
159 static CRITICAL_SECTION csOpenDllList;
160 static CRITICAL_SECTION_DEBUG dll_cs_debug =
161 {
162     0, 0, &csOpenDllList,
163     { &dll_cs_debug.ProcessLocksList, &dll_cs_debug.ProcessLocksList },
164       0, 0, { 0, (DWORD)(__FILE__ ": csOpenDllList") }
165 };
166 static CRITICAL_SECTION csOpenDllList = { &dll_cs_debug, -1, 0, 0, 0, 0 };
167
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);
171
172 static void COMPOBJ_DLLList_Add(HANDLE hLibrary);
173 static void COMPOBJ_DllList_FreeUnused(int Timeout);
174
175 static void COMPOBJ_InitProcess( void )
176 {
177     WNDCLASSW wclass;
178
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
186      * was unmarshalled.
187      */
188     memset(&wclass, 0, sizeof(wclass));
189     wclass.lpfnWndProc = apartment_wndproc;
190     wclass.hInstance = OLE32_hInstance;
191     wclass.lpszClassName = wszAptWinClass;
192     RegisterClassW(&wclass);
193 }
194
195 static void COMPOBJ_UninitProcess( void )
196 {
197     UnregisterClassW(wszAptWinClass, OLE32_hInstance);
198 }
199
200 static void COM_TlsDestroy(void)
201 {
202     struct oletls *info = NtCurrentTeb()->ReservedForOle;
203     if (info)
204     {
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;
210     }
211 }
212
213 /******************************************************************************
214  * Manage apartments.
215  */
216
217 /* allocates memory and fills in the necessary fields for a new apartment
218  * object */
219 static APARTMENT *apartment_construct(DWORD model)
220 {
221     APARTMENT *apt;
222
223     TRACE("creating new apartment, model=%ld\n", model);
224
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);
230
231     list_init(&apt->proxies);
232     list_init(&apt->stubmgrs);
233     apt->ipidc = 0;
234     apt->refs = 1;
235     apt->remunk_exported = FALSE;
236     apt->oidc = 1;
237     InitializeCriticalSection(&apt->cs);
238     DEBUG_SET_CRITSEC_NAME(&apt->cs, "apartment");
239
240     apt->model = model;
241
242     if (model & COINIT_APARTMENTTHREADED)
243     {
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,
247                                  0, 0, 0, 0,
248                                  0, 0, OLE32_hInstance, NULL);
249     }
250     else
251     {
252         /* FIXME: should be randomly generated by in an RPC call to rpcss */
253         apt->oxid = ((OXID)GetCurrentProcessId() << 32) | 0xcafe;
254     }
255
256     TRACE("Created apartment on OXID %s\n", wine_dbgstr_longlong(apt->oxid));
257
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);
263
264     return apt;
265 }
266
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)
271 {
272     APARTMENT *apt = COM_CurrentApt();
273
274     if (!apt)
275     {
276         if (model & COINIT_APARTMENTTHREADED)
277         {
278             apt = apartment_construct(model);
279             COM_CurrentInfo()->apt = apt;
280         }
281         else
282         {
283             EnterCriticalSection(&csApartment);
284
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
287              * in a process */
288             if (MTA)
289             {
290                 TRACE("entering the multithreaded apartment %s\n", wine_dbgstr_longlong(MTA->oxid));
291                 apartment_addref(MTA);
292             }
293             else
294                 MTA = apartment_construct(model);
295
296             apt = MTA;
297             COM_CurrentInfo()->apt = apt;
298
299             LeaveCriticalSection(&csApartment);
300         }
301     }
302
303     return apt;
304 }
305
306 DWORD apartment_addref(struct apartment *apt)
307 {
308     DWORD refs = InterlockedIncrement(&apt->refs);
309     TRACE("%s: before = %ld\n", wine_dbgstr_longlong(apt->oxid), refs - 1);
310     return refs;
311 }
312
313 DWORD apartment_release(struct apartment *apt)
314 {
315     DWORD ret;
316
317     EnterCriticalSection(&csApartment);
318
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 */
322     if (ret == 0)
323     {
324         if (apt == MTA) MTA = NULL;
325         list_remove(&apt->entry);
326     }
327
328     LeaveCriticalSection(&csApartment);
329
330     if (ret == 0)
331     {
332         struct list *cursor, *cursor2;
333
334         TRACE("destroying apartment %p, oxid %s\n", apt, wine_dbgstr_longlong(apt->oxid));
335
336         /* no locking is needed for this apartment, because no other thread
337          * can access it at this point */
338
339         apartment_disconnectproxies(apt);
340
341         if (apt->win) DestroyWindow(apt->win);
342
343         LIST_FOR_EACH_SAFE(cursor, cursor2, &apt->stubmgrs)
344         {
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).
350              */
351             stub_manager_int_release(stubmgr);
352         }
353
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));
358
359         if (apt->filter) IUnknown_Release(apt->filter);
360
361         DEBUG_CLEAR_CRITSEC_NAME(&apt->cs);
362         DeleteCriticalSection(&apt->cs);
363         CloseHandle(apt->thread);
364
365         HeapFree(GetProcessHeap(), 0, apt);
366     }
367
368     return ret;
369 }
370
371 /* The given OXID must be local to this process: 
372  *
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.
375  */
376 APARTMENT *apartment_findfromoxid(OXID oxid, BOOL ref)
377 {
378     APARTMENT *result = NULL;
379     struct list *cursor;
380
381     EnterCriticalSection(&csApartment);
382     LIST_FOR_EACH( cursor, &apts )
383     {
384         struct apartment *apt = LIST_ENTRY( cursor, struct apartment, entry );
385         if (apt->oxid == oxid)
386         {
387             result = apt;
388             if (ref) apartment_addref(result);
389             break;
390         }
391     }
392     LeaveCriticalSection(&csApartment);
393
394     return result;
395 }
396
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)
401 {
402     APARTMENT *result = NULL;
403     struct list *cursor;
404
405     EnterCriticalSection(&csApartment);
406     LIST_FOR_EACH( cursor, &apts )
407     {
408         struct apartment *apt = LIST_ENTRY( cursor, struct apartment, entry );
409         if (apt->tid == tid)
410         {
411             result = apt;
412             apartment_addref(result);
413             break;
414         }
415     }
416     LeaveCriticalSection(&csApartment);
417
418     return result;
419 }
420
421 static LRESULT CALLBACK apartment_wndproc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
422 {
423     switch (msg)
424     {
425     case DM_EXECUTERPC:
426         return RPC_ExecuteCall((struct dispatch_params *)lParam);
427     default:
428         return DefWindowProcW(hWnd, msg, wParam, lParam);
429     }
430 }
431
432 /*****************************************************************************
433  * This section contains OpenDllList implemantation
434  */
435
436 static void COMPOBJ_DLLList_Add(HANDLE hLibrary)
437 {
438     OpenDll *ptr;
439     OpenDll *tmp;
440
441     TRACE("\n");
442
443     EnterCriticalSection( &csOpenDllList );
444
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;
450     } else {
451         /* search for this dll */
452         int found = FALSE;
453         for (ptr = openDllList; ptr->next != NULL; ptr=ptr->next) {
454             if (ptr->hLibrary == hLibrary) {
455                 found = TRUE;
456                 break;
457             }
458         }
459         if (!found) {
460             /* dll not found, add it */
461             tmp = openDllList;
462             openDllList = HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
463             openDllList->hLibrary = hLibrary;
464             openDllList->next = tmp;
465         }
466     }
467
468     LeaveCriticalSection( &csOpenDllList );
469 }
470
471 static void COMPOBJ_DllList_FreeUnused(int Timeout)
472 {
473     OpenDll *curr, *next, *prev = NULL;
474     typedef HRESULT (WINAPI *DllCanUnloadNowFunc)(void);
475     DllCanUnloadNowFunc DllCanUnloadNow;
476
477     TRACE("\n");
478
479     EnterCriticalSection( &csOpenDllList );
480
481     for (curr = openDllList; curr != NULL; ) {
482         DllCanUnloadNow = (DllCanUnloadNowFunc) GetProcAddress(curr->hLibrary, "DllCanUnloadNow");
483
484         if ( (DllCanUnloadNow != NULL) && (DllCanUnloadNow() == S_OK) ) {
485             next = curr->next;
486
487             TRACE("freeing %p\n", curr->hLibrary);
488             FreeLibrary(curr->hLibrary);
489
490             HeapFree(GetProcessHeap(), 0, curr);
491             if (curr == openDllList) {
492                 openDllList = next;
493             } else {
494               prev->next = next;
495             }
496
497             curr = next;
498         } else {
499             prev = curr;
500             curr = curr->next;
501         }
502     }
503
504     LeaveCriticalSection( &csOpenDllList );
505 }
506
507 /******************************************************************************
508  *           CoBuildVersion [OLE32.@]
509  *           CoBuildVersion [COMPOBJ.1]
510  *
511  * Gets the build version of the DLL.
512  *
513  * PARAMS
514  *
515  * RETURNS
516  *      Current build version, hiword is majornumber, loword is minornumber
517  */
518 DWORD WINAPI CoBuildVersion(void)
519 {
520     TRACE("Returning version %d, build %d.\n", rmm, rup);
521     return (rmm<<16)+rup;
522 }
523
524 /******************************************************************************
525  *              CoInitialize    [OLE32.@]
526  *
527  * Initializes the COM libraries by calling CoInitializeEx with
528  * COINIT_APARTMENTTHREADED, ie it enters a STA thread.
529  *
530  * PARAMS
531  *  lpReserved [I] Pointer to IMalloc interface (obsolete, should be NULL).
532  *
533  * RETURNS
534  *  Success: S_OK if not already initialized, S_FALSE otherwise.
535  *  Failure: HRESULT code.
536  *
537  * SEE ALSO
538  *   CoInitializeEx
539  */
540 HRESULT WINAPI CoInitialize(LPVOID lpReserved)
541 {
542   /*
543    * Just delegate to the newer method.
544    */
545   return CoInitializeEx(lpReserved, COINIT_APARTMENTTHREADED);
546 }
547
548 /******************************************************************************
549  *              CoInitializeEx  [OLE32.@]
550  *
551  * Initializes the COM libraries.
552  *
553  * PARAMS
554  *  lpReserved [I] Pointer to IMalloc interface (obsolete, should be NULL).
555  *  dwCoInit   [I] One or more flags from the COINIT enumeration. See notes.
556  *
557  * RETURNS
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
561  *                     threading model.
562  *
563  * NOTES
564  *
565  * The behavior used to set the IMalloc used for memory management is
566  * obsolete.
567  * The dwCoInit parameter must specify of of the following apartment
568  * threading models:
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.
574  *
575  * SEE ALSO
576  *   CoUninitialize
577  */
578 HRESULT WINAPI CoInitializeEx(LPVOID lpReserved, DWORD dwCoInit)
579 {
580   HRESULT hr = S_OK;
581   APARTMENT *apt;
582
583   TRACE("(%p, %x)\n", lpReserved, (int)dwCoInit);
584
585   if (lpReserved!=NULL)
586   {
587     ERR("(%p, %x) - Bad parameter passed-in %p, must be an old Windows Application\n", lpReserved, (int)dwCoInit, lpReserved);
588   }
589
590   /*
591    * Check the lock count. If this is the first time going through the initialize
592    * process, we have to initialize the libraries.
593    *
594    * And crank-up that lock count.
595    */
596   if (InterlockedExchangeAdd(&s_COMLockCount,1)==0)
597   {
598     /*
599      * Initialize the various COM libraries and data structures.
600      */
601     TRACE("() - Initializing the COM libraries\n");
602
603     /* we may need to defer this until after apartment initialisation */
604     RunningObjectTableImpl_Initialize();
605   }
606
607   if (!(apt = COM_CurrentInfo()->apt))
608   {
609     apt = apartment_get_or_create(dwCoInit);
610     if (!apt) return E_OUTOFMEMORY;
611   }
612   else if (dwCoInit != apt->model)
613   {
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;
618   }
619   else
620     hr = S_FALSE;
621
622   COM_CurrentInfo()->inits++;
623
624   return hr;
625 }
626
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.
629  */
630 static void COM_FlushMessageQueue(void)
631 {
632     MSG message;
633     APARTMENT *apt = COM_CurrentApt();
634
635     if (!apt || !apt->win) return;
636
637     TRACE("Flushing STA message queue\n");
638
639     while (PeekMessageA(&message, NULL, 0, 0, PM_REMOVE))
640     {
641         if (message.hwnd != apt->win)
642         {
643             WARN("discarding message 0x%x for window %p\n", message.message, message.hwnd);
644             continue;
645         }
646
647         TranslateMessage(&message);
648         DispatchMessageA(&message);
649     }
650 }
651
652 /***********************************************************************
653  *           CoUninitialize   [OLE32.@]
654  *
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.
659  *
660  * PARAMS
661  *
662  * RETURNS
663  *  Nothing.
664  *
665  * SEE ALSO
666  *   CoInitializeEx
667  */
668 void WINAPI CoUninitialize(void)
669 {
670   struct oletls * info = COM_CurrentInfo();
671   LONG lCOMRefCnt;
672
673   TRACE("()\n");
674
675   /* will only happen on OOM */
676   if (!info) return;
677
678   /* sanity check */
679   if (!info->inits)
680   {
681     ERR("Mismatched CoUninitialize\n");
682     return;
683   }
684
685   if (!--info->inits)
686   {
687     apartment_release(info->apt);
688     info->apt = NULL;
689   }
690
691   /*
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.
695    */
696   lCOMRefCnt = InterlockedExchangeAdd(&s_COMLockCount,-1);
697   if (lCOMRefCnt==1)
698   {
699     TRACE("() - Releasing the COM libraries\n");
700
701     RunningObjectTableImpl_UnInitialize();
702
703     /* Release the references to the registered class objects */
704     COM_RevokeAllClasses();
705
706     /* This will free the loaded COM Dlls  */
707     CoFreeAllLibraries();
708
709     /* This ensures we deal with any pending RPCs */
710     COM_FlushMessageQueue();
711   }
712   else if (lCOMRefCnt<1) {
713     ERR( "CoUninitialize() - not CoInitialized.\n" );
714     InterlockedExchangeAdd(&s_COMLockCount,1); /* restore the lock count. */
715   }
716 }
717
718 /******************************************************************************
719  *              CoDisconnectObject      [OLE32.@]
720  *              CoDisconnectObject      [COMPOBJ.15]
721  *
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.
725  *
726  * Typically called when the object server is forced to shut down, for instance by
727  * the user.
728  *
729  * PARAMS
730  *  lpUnk    [I] The object whose stub should be disconnected.
731  *  reserved [I] Reserved. Should be set to 0.
732  *
733  * RETURNS
734  *  Success: S_OK.
735  *  Failure: HRESULT code.
736  *
737  * SEE ALSO
738  *  CoMarshalInterface, CoReleaseMarshalData, CoLockObjectExternal
739  */
740 HRESULT WINAPI CoDisconnectObject( LPUNKNOWN lpUnk, DWORD reserved )
741 {
742     HRESULT hr;
743     IMarshal *marshal;
744     APARTMENT *apt;
745
746     TRACE("(%p, 0x%08lx)\n", lpUnk, reserved);
747
748     hr = IUnknown_QueryInterface(lpUnk, &IID_IMarshal, (void **)&marshal);
749     if (hr == S_OK)
750     {
751         hr = IMarshal_DisconnectObject(marshal, reserved);
752         IMarshal_Release(marshal);
753         return hr;
754     }
755
756     apt = COM_CurrentApt();
757     if (!apt)
758         return CO_E_NOTINITIALIZED;
759
760     apartment_disconnectobject(apt, lpUnk);
761
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 */
766
767     return S_OK;
768 }
769
770 /******************************************************************************
771  *              CoCreateGuid [OLE32.@]
772  *
773  * Simply forwards to UuidCreate in RPCRT4.
774  *
775  * PARAMS
776  *  pguid [O] Points to the GUID to initialize.
777  *
778  * RETURNS
779  *  Success: S_OK.
780  *  Failure: HRESULT code.
781  *
782  * SEE ALSO
783  *   UuidCreate
784  */
785 HRESULT WINAPI CoCreateGuid(GUID *pguid)
786 {
787     return UuidCreate(pguid);
788 }
789
790 /******************************************************************************
791  *              CLSIDFromString [OLE32.@]
792  *              IIDFromString   [OLE32.@]
793  *
794  * Converts a unique identifier from its string representation into
795  * the GUID struct.
796  *
797  * PARAMS
798  *  idstr [I] The string representation of the GUID.
799  *  id    [O] GUID converted from the string.
800  *
801  * RETURNS
802  *   S_OK on success
803  *   CO_E_CLASSSTRING if idstr is not a valid CLSID
804  *
805  * BUGS
806  *
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.
810  *
811  * SEE ALSO
812  *  StringFromCLSID
813  */
814 HRESULT WINAPI __CLSIDFromStringA(LPCSTR idstr, CLSID *id)
815 {
816   const BYTE *s;
817   int   i;
818   BYTE table[256];
819
820   if (!idstr) {
821     memset( id, 0, sizeof (CLSID) );
822     return S_OK;
823   }
824
825   /* validate the CLSID string */
826   if (strlen(idstr) != 38)
827     return CO_E_CLASSSTRING;
828
829   s = (const BYTE *) idstr;
830   if ((s[0]!='{') || (s[9]!='-') || (s[14]!='-') || (s[19]!='-') || (s[24]!='-') || (s[37]!='}'))
831     return CO_E_CLASSSTRING;
832
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;
839   }
840
841   TRACE("%s -> %p\n", s, id);
842
843   /* quick lookup table */
844   memset(table, 0, 256);
845
846   for (i = 0; i < 10; i++) {
847     table['0' + i] = i;
848   }
849   for (i = 0; i < 6; i++) {
850     table['A' + i] = i+10;
851     table['a' + i] = i+10;
852   }
853
854   /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
855
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]];
860
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]];
870
871   return S_OK;
872 }
873
874 /*****************************************************************************/
875
876 HRESULT WINAPI CLSIDFromString(LPOLESTR idstr, CLSID *id )
877 {
878     char xid[40];
879     HRESULT ret;
880
881     if (!WideCharToMultiByte( CP_ACP, 0, idstr, -1, xid, sizeof(xid), NULL, NULL ))
882         return CO_E_CLASSSTRING;
883
884
885     ret = __CLSIDFromStringA(xid,id);
886     if(ret != S_OK) { /* It appears a ProgID is also valid */
887         ret = CLSIDFromProgID(idstr, id);
888     }
889     return ret;
890 }
891
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 */
896 ) {
897   static const char *hex = "0123456789ABCDEF";
898   char *s;
899   int   i;
900
901   if (!id)
902         { ERR("called with id=Null\n");
903           *idstr = 0x00;
904           return E_FAIL;
905         }
906
907   sprintf(idstr, "{%08lX-%04X-%04X-%02X%02X-",
908           id->Data1, id->Data2, id->Data3,
909           id->Data4[0], id->Data4[1]);
910   s = &idstr[25];
911
912   /* 6 hex bytes */
913   for (i = 2; i < 8; i++) {
914     *s++ = hex[id->Data4[i]>>4];
915     *s++ = hex[id->Data4[i] & 0xf];
916   }
917
918   *s++ = '}';
919   *s++ = '\0';
920
921   TRACE("%p->%s\n", id, idstr);
922
923   return S_OK;
924 }
925
926
927 /******************************************************************************
928  *              StringFromCLSID [OLE32.@]
929  *              StringFromIID   [OLE32.@]
930  *
931  * Converts a GUID into the respective string representation.
932  * The target string is allocated using the OLE IMalloc.
933  *
934  * PARAMS
935  *  id    [I] the GUID to be converted.
936  *  idstr [O] A pointer to a to-be-allocated pointer pointing to the resulting string.
937  *
938  * RETURNS
939  *   S_OK
940  *   E_FAIL
941  *
942  * SEE ALSO
943  *  StringFromGUID2, CLSIDFromString
944  */
945 HRESULT WINAPI StringFromCLSID(REFCLSID id, LPOLESTR *idstr)
946 {
947         char            buf[80];
948         HRESULT       ret;
949         LPMALLOC        mllc;
950
951         if ((ret = CoGetMalloc(0,&mllc)))
952                 return ret;
953
954         ret=WINE_StringFromCLSID(id,buf);
955         if (!ret) {
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 );
959         }
960         return ret;
961 }
962
963 /******************************************************************************
964  *              StringFromGUID2 [OLE32.@]
965  *              StringFromGUID2 [COMPOBJ.76]
966  *
967  * Modified version of StringFromCLSID that allows you to specify max
968  * buffer size.
969  *
970  * PARAMS
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.
974  *
975  * RETURNS
976  *      Success: The length of the resulting string in characters.
977  *  Failure: 0.
978  */
979 INT WINAPI StringFromGUID2(REFGUID id, LPOLESTR str, INT cmax)
980 {
981   char          xguid[80];
982
983   if (WINE_StringFromCLSID(id,xguid))
984         return 0;
985   return MultiByteToWideChar( CP_ACP, 0, xguid, -1, str, cmax );
986 }
987
988 /* open HKCR\\CLSID\\{string form of clsid} key */
989 DWORD COM_OpenKeyForCLSID(REFCLSID clsid, REGSAM access, HKEY *key)
990 {
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);
996 }
997
998 /******************************************************************************
999  *               ProgIDFromCLSID [OLE32.@]
1000  *
1001  * Converts a class id into the respective program ID.
1002  *
1003  * PARAMS
1004  *  clsid        [I] Class ID, as found in registry.
1005  *  lplpszProgID [O] Associated ProgID.
1006  *
1007  * RETURNS
1008  *   S_OK
1009  *   E_OUTOFMEMORY
1010  *   REGDB_E_CLASSNOTREG if the given clsid has no associated ProgID
1011  */
1012 HRESULT WINAPI ProgIDFromCLSID(REFCLSID clsid, LPOLESTR *lplpszProgID)
1013 {
1014   static const WCHAR wszProgID[] = {'P','r','o','g','I','D',0};
1015   HKEY     hkey = NULL;
1016   HKEY     hkey_clsid;
1017   HRESULT  ret = S_OK;
1018
1019   if (ERROR_SUCCESS != COM_OpenKeyForCLSID(clsid, KEY_READ, &hkey_clsid))
1020     ret = REGDB_E_CLASSNOTREG;
1021
1022   if (ret == S_OK)
1023   {
1024     if (RegOpenKeyExW(hkey_clsid, wszProgID, 0, KEY_READ, &hkey))
1025       ret = REGDB_E_CLASSNOTREG;
1026     RegCloseKey(hkey_clsid);
1027   }
1028
1029   if (ret == S_OK)
1030   {
1031     LONG progidlen = 0;
1032
1033     if (RegQueryValueW(hkey, NULL, NULL, &progidlen))
1034       ret = REGDB_E_CLASSNOTREG;
1035
1036     if (ret == S_OK)
1037     {
1038       *lplpszProgID = CoTaskMemAlloc(progidlen * sizeof(WCHAR));
1039       if (*lplpszProgID)
1040       {
1041         if (RegQueryValueW(hkey, NULL, *lplpszProgID, &progidlen))
1042           ret = REGDB_E_CLASSNOTREG;
1043       }
1044       else
1045         ret = E_OUTOFMEMORY;
1046     }
1047   }
1048
1049   RegCloseKey(hkey);
1050   return ret;
1051 }
1052
1053 /******************************************************************************
1054  *              CLSIDFromProgID [COMPOBJ.61]
1055  *
1056  * Converts a program ID into the respective GUID.
1057  *
1058  * PARAMS
1059  *  progid       [I] program id as found in registry
1060  *  riid         [O] associated CLSID
1061  *
1062  * RETURNS
1063  *      Success: S_OK
1064  *  Failure: CO_E_CLASSSTRING - the given ProgID cannot be found.
1065  */
1066 HRESULT WINAPI CLSIDFromProgID16(LPCOLESTR16 progid, LPCLSID riid)
1067 {
1068         char    *buf,buf2[80];
1069         LONG    buf2len;
1070         HRESULT err;
1071         HKEY    xhkey;
1072
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;
1078         }
1079         HeapFree(GetProcessHeap(),0,buf);
1080         buf2len = sizeof(buf2);
1081         if ((err=RegQueryValueA(xhkey,NULL,buf2,&buf2len))) {
1082                 RegCloseKey(xhkey);
1083                 return CO_E_CLASSSTRING;
1084         }
1085         RegCloseKey(xhkey);
1086         return __CLSIDFromStringA(buf2,riid);
1087 }
1088
1089 /******************************************************************************
1090  *              CLSIDFromProgID [OLE32.@]
1091  *
1092  * Converts a program id into the respective GUID.
1093  *
1094  * PARAMS
1095  *  progid [I] Unicode program ID, as found in registry.
1096  *  riid   [O] Associated CLSID.
1097  *
1098  * RETURNS
1099  *      Success: S_OK
1100  *  Failure: CO_E_CLASSSTRING - the given ProgID cannot be found.
1101  */
1102 HRESULT WINAPI CLSIDFromProgID(LPCOLESTR progid, LPCLSID riid)
1103 {
1104     static const WCHAR clsidW[] = { '\\','C','L','S','I','D',0 };
1105     WCHAR buf2[CHARS_IN_GUID];
1106     LONG buf2len = sizeof(buf2);
1107     HKEY xhkey;
1108
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))
1113     {
1114         HeapFree(GetProcessHeap(),0,buf);
1115         return CO_E_CLASSSTRING;
1116     }
1117     HeapFree(GetProcessHeap(),0,buf);
1118
1119     if (RegQueryValueW(xhkey,NULL,buf2,&buf2len))
1120     {
1121         RegCloseKey(xhkey);
1122         return CO_E_CLASSSTRING;
1123     }
1124     RegCloseKey(xhkey);
1125     return CLSIDFromString(buf2,riid);
1126 }
1127
1128
1129
1130 /*****************************************************************************
1131  *             CoGetPSClsid [OLE32.@]
1132  *
1133  * Retrieves the CLSID of the proxy/stub factory that implements
1134  * IPSFactoryBuffer for the specified interface.
1135  *
1136  * PARAMS
1137  *  riid   [I] Interface whose proxy/stub CLSID is to be returned.
1138  *  pclsid [O] Where to store returned proxy/stub CLSID.
1139  * 
1140  * RETURNS
1141  *   S_OK
1142  *   E_OUTOFMEMORY
1143  *   REGDB_E_IIDNOTREG if no PSFactoryBuffer is associated with the IID, or it could not be parsed
1144  *
1145  * NOTES
1146  *
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
1150  * given object.
1151  *
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.
1156  *
1157  * BUGS
1158  *
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).
1164  */
1165 HRESULT WINAPI CoGetPSClsid(REFIID riid, CLSID *pclsid)
1166 {
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];
1171     LONG len;
1172     HKEY hkey;
1173
1174     TRACE("() riid=%s, pclsid=%p\n", debugstr_guid(riid), pclsid);
1175
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);
1180
1181     /* Open the key.. */
1182     if (RegOpenKeyExW(HKEY_CLASSES_ROOT, path, 0, KEY_READ, &hkey))
1183     {
1184         WARN("No PSFactoryBuffer object is registered for IID %s\n", debugstr_guid(riid));
1185         return REGDB_E_IIDNOTREG;
1186     }
1187
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))
1193     {
1194         RegCloseKey(hkey);
1195         return REGDB_E_IIDNOTREG;
1196     }
1197     RegCloseKey(hkey);
1198
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;
1203
1204     TRACE ("() Returning CLSID=%s\n", debugstr_guid(pclsid));
1205     return S_OK;
1206 }
1207
1208
1209
1210 /***********************************************************************
1211  *              WriteClassStm (OLE32.@)
1212  *
1213  * Writes a CLSID to a stream.
1214  *
1215  * PARAMS
1216  *  pStm   [I] Stream to write to.
1217  *  rclsid [I] CLSID to write.
1218  *
1219  * RETURNS
1220  *  Success: S_OK.
1221  *  Failure: HRESULT code.
1222  */
1223 HRESULT WINAPI WriteClassStm(IStream *pStm,REFCLSID rclsid)
1224 {
1225     TRACE("(%p,%p)\n",pStm,rclsid);
1226
1227     if (rclsid==NULL)
1228         return E_INVALIDARG;
1229
1230     return IStream_Write(pStm,rclsid,sizeof(CLSID),NULL);
1231 }
1232
1233 /***********************************************************************
1234  *              ReadClassStm (OLE32.@)
1235  *
1236  * Reads a CLSID from a stream.
1237  *
1238  * PARAMS
1239  *  pStm   [I] Stream to read from.
1240  *  rclsid [O] CLSID to read.
1241  *
1242  * RETURNS
1243  *  Success: S_OK.
1244  *  Failure: HRESULT code.
1245  */
1246 HRESULT WINAPI ReadClassStm(IStream *pStm,CLSID *pclsid)
1247 {
1248     ULONG nbByte;
1249     HRESULT res;
1250
1251     TRACE("(%p,%p)\n",pStm,pclsid);
1252
1253     if (pclsid==NULL)
1254         return E_INVALIDARG;
1255
1256     res = IStream_Read(pStm,(void*)pclsid,sizeof(CLSID),&nbByte);
1257
1258     if (FAILED(res))
1259         return res;
1260
1261     if (nbByte != sizeof(CLSID))
1262         return S_FALSE;
1263     else
1264         return S_OK;
1265 }
1266
1267
1268 /***
1269  * COM_GetRegisteredClassObject
1270  *
1271  * This internal method is used to scan the registered class list to
1272  * find a class object.
1273  *
1274  * Params:
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.
1280  */
1281 static HRESULT COM_GetRegisteredClassObject(
1282         REFCLSID    rclsid,
1283         DWORD       dwClsContext,
1284         LPUNKNOWN*  ppUnk)
1285 {
1286   HRESULT hr = S_FALSE;
1287   RegisteredClass* curClass;
1288
1289   EnterCriticalSection( &csRegisteredClassList );
1290
1291   /*
1292    * Sanity check
1293    */
1294   assert(ppUnk!=0);
1295
1296   /*
1297    * Iterate through the whole list and try to match the class ID.
1298    */
1299   curClass = firstRegisteredClass;
1300
1301   while (curClass != 0)
1302   {
1303     /*
1304      * Check if we have a match on the class ID.
1305      */
1306     if (IsEqualGUID(&(curClass->classIdentifier), rclsid))
1307     {
1308       /*
1309        * Since we don't do out-of process or DCOM just right away, let's ignore the
1310        * class context.
1311        */
1312
1313       /*
1314        * We have a match, return the pointer to the class object.
1315        */
1316       *ppUnk = curClass->classObject;
1317
1318       IUnknown_AddRef(curClass->classObject);
1319
1320       hr = S_OK;
1321       goto end;
1322     }
1323
1324     /*
1325      * Step to the next class in the list.
1326      */
1327     curClass = curClass->nextClass;
1328   }
1329
1330 end:
1331   LeaveCriticalSection( &csRegisteredClassList );
1332   /*
1333    * If we get to here, we haven't found our class.
1334    */
1335   return hr;
1336 }
1337
1338 /******************************************************************************
1339  *              CoRegisterClassObject   [OLE32.@]
1340  *
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.
1344  *
1345  * PARAMS
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.
1351  *
1352  * RETURNS
1353  *   S_OK on success,
1354  *   E_INVALIDARG if lpdwRegister or pUnk are NULL,
1355  *   CO_E_OBJISREG if the object is already registered. We should not return this.
1356  *
1357  * SEE ALSO
1358  *   CoRevokeClassObject, CoGetClassObject
1359  *
1360  * BUGS
1361  *  MSDN claims that multiple interface registrations are legal, but we
1362  *  can't do that with our current implementation.
1363  */
1364 HRESULT WINAPI CoRegisterClassObject(
1365     REFCLSID rclsid,
1366     LPUNKNOWN pUnk,
1367     DWORD dwClsContext,
1368     DWORD flags,
1369     LPDWORD lpdwRegister)
1370 {
1371   RegisteredClass* newClass;
1372   LPUNKNOWN        foundObject;
1373   HRESULT          hr;
1374
1375   TRACE("(%s,%p,0x%08lx,0x%08lx,%p)\n",
1376         debugstr_guid(rclsid),pUnk,dwClsContext,flags,lpdwRegister);
1377
1378   if ( (lpdwRegister==0) || (pUnk==0) )
1379     return E_INVALIDARG;
1380
1381   if (!COM_CurrentApt())
1382   {
1383       ERR("COM was not initialized\n");
1384       return CO_E_NOTINITIALIZED;
1385   }
1386
1387   *lpdwRegister = 0;
1388
1389   /*
1390    * First, check if the class is already registered.
1391    * If it is, this should cause an error.
1392    */
1393   hr = COM_GetRegisteredClassObject(rclsid, dwClsContext, &foundObject);
1394   if (hr == S_OK) {
1395     IUnknown_Release(foundObject);
1396     return CO_E_OBJISREG;
1397   }
1398
1399   newClass = HeapAlloc(GetProcessHeap(), 0, sizeof(RegisteredClass));
1400   if ( newClass == NULL )
1401     return E_OUTOFMEMORY;
1402
1403   EnterCriticalSection( &csRegisteredClassList );
1404
1405   newClass->classIdentifier = *rclsid;
1406   newClass->runContext      = dwClsContext;
1407   newClass->connectFlags    = flags;
1408   newClass->pMarshaledData  = NULL;
1409
1410   /*
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.
1413    */
1414   newClass->dwCookie        = (DWORD)newClass;
1415   newClass->nextClass       = firstRegisteredClass;
1416
1417   /*
1418    * Since we're making a copy of the object pointer, we have to increase its
1419    * reference count.
1420    */
1421   newClass->classObject     = pUnk;
1422   IUnknown_AddRef(newClass->classObject);
1423
1424   firstRegisteredClass = newClass;
1425   LeaveCriticalSection( &csRegisteredClassList );
1426
1427   *lpdwRegister = newClass->dwCookie;
1428
1429   if (dwClsContext & CLSCTX_LOCAL_SERVER) {
1430       IClassFactory *classfac;
1431
1432       hr = IUnknown_QueryInterface(newClass->classObject, &IID_IClassFactory,
1433                                    (LPVOID*)&classfac);
1434       if (hr) return hr;
1435
1436       hr = CreateStreamOnHGlobal(0, TRUE, &newClass->pMarshaledData);
1437       if (hr) {
1438           FIXME("Failed to create stream on hglobal, %lx\n", hr);
1439           IUnknown_Release(classfac);
1440           return hr;
1441       }
1442       hr = CoMarshalInterface(newClass->pMarshaledData, &IID_IClassFactory,
1443                               (LPVOID)classfac, MSHCTX_LOCAL, NULL,
1444                               MSHLFLAGS_TABLESTRONG);
1445       if (hr) {
1446           FIXME("CoMarshalInterface failed, %lx!\n",hr);
1447           IUnknown_Release(classfac);
1448           return hr;
1449       }
1450
1451       IUnknown_Release(classfac);
1452
1453       RPC_StartLocalServer(&newClass->classIdentifier, newClass->pMarshaledData);
1454   }
1455   return S_OK;
1456 }
1457
1458 /***********************************************************************
1459  *           CoRevokeClassObject [OLE32.@]
1460  *
1461  * Removes a class object from the class registry.
1462  *
1463  * PARAMS
1464  *  dwRegister [I] Cookie returned from CoRegisterClassObject().
1465  *
1466  * RETURNS
1467  *  Success: S_OK.
1468  *  Failure: HRESULT code.
1469  *
1470  * SEE ALSO
1471  *  CoRegisterClassObject
1472  */
1473 HRESULT WINAPI CoRevokeClassObject(
1474         DWORD dwRegister)
1475 {
1476   HRESULT hr = E_INVALIDARG;
1477   RegisteredClass** prevClassLink;
1478   RegisteredClass*  curClass;
1479
1480   TRACE("(%08lx)\n",dwRegister);
1481
1482   EnterCriticalSection( &csRegisteredClassList );
1483
1484   /*
1485    * Iterate through the whole list and try to match the cookie.
1486    */
1487   curClass      = firstRegisteredClass;
1488   prevClassLink = &firstRegisteredClass;
1489
1490   while (curClass != 0)
1491   {
1492     /*
1493      * Check if we have a match on the cookie.
1494      */
1495     if (curClass->dwCookie == dwRegister)
1496     {
1497       /*
1498        * Remove the class from the chain.
1499        */
1500       *prevClassLink = curClass->nextClass;
1501
1502       /*
1503        * Release the reference to the class object.
1504        */
1505       IUnknown_Release(curClass->classObject);
1506
1507       if (curClass->pMarshaledData)
1508       {
1509         LARGE_INTEGER zero;
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);
1514       }
1515
1516       /*
1517        * Free the memory used by the chain node.
1518        */
1519       HeapFree(GetProcessHeap(), 0, curClass);
1520
1521       hr = S_OK;
1522       goto end;
1523     }
1524
1525     /*
1526      * Step to the next class in the list.
1527      */
1528     prevClassLink = &(curClass->nextClass);
1529     curClass      = curClass->nextClass;
1530   }
1531
1532 end:
1533   LeaveCriticalSection( &csRegisteredClassList );
1534   /*
1535    * If we get to here, we haven't found our class.
1536    */
1537   return hr;
1538 }
1539
1540 /***********************************************************************
1541  *      COM_RegReadPath [internal]
1542  *
1543  *      Reads a registry value and expands it when necessary
1544  */
1545 HRESULT COM_RegReadPath(HKEY hkeyroot, const WCHAR *keyname, const WCHAR *valuename, WCHAR * dst, DWORD dstlen)
1546 {
1547         HRESULT hres;
1548         HKEY key;
1549         DWORD keytype;
1550         WCHAR src[MAX_PATH];
1551         DWORD dwLength = dstlen * sizeof(WCHAR);
1552
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;
1557             } else {
1558               lstrcpynW(dst, src, dstlen);
1559             }
1560           }
1561           RegCloseKey (key);
1562         }
1563         return hres;
1564 }
1565
1566 /***********************************************************************
1567  *           CoGetClassObject [OLE32.@]
1568  *
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)
1573  */
1574 HRESULT WINAPI CoGetClassObject(
1575     REFCLSID rclsid, DWORD dwClsContext, COSERVERINFO *pServerInfo,
1576     REFIID iid, LPVOID *ppv)
1577 {
1578     LPUNKNOWN   regClassObject;
1579     HRESULT     hres = E_UNEXPECTED;
1580
1581     TRACE("\n\tCLSID:\t%s,\n\tIID:\t%s\n", debugstr_guid(rclsid), debugstr_guid(iid));
1582
1583     if (pServerInfo) {
1584         FIXME("\tpServerInfo: name=%s\n",debugstr_w(pServerInfo->pwszName));
1585         FIXME("\t\tpAuthInfo=%p\n",pServerInfo->pAuthInfo);
1586     }
1587
1588     /*
1589      * First, try and see if we can't match the class ID with one of the
1590      * registered classes.
1591      */
1592     if (S_OK == COM_GetRegisteredClassObject(rclsid, dwClsContext, &regClassObject))
1593     {
1594       /* Get the required interface from the retrieved pointer. */
1595       hres = IUnknown_QueryInterface(regClassObject, iid, ppv);
1596
1597       /*
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.
1601        */
1602       IUnknown_Release(regClassObject);
1603
1604       return hres;
1605     }
1606
1607     /* first try: in-process */
1608     if ((CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER) & dwClsContext)
1609     {
1610         static const WCHAR wszInprocServer32[] = {'I','n','p','r','o','c','S','e','r','v','e','r','3','2',0};
1611         HINSTANCE hLibrary;
1612         typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid, REFIID iid, LPVOID *ppv);
1613         DllGetClassObjectFunc DllGetClassObject;
1614         WCHAR dllpath[MAX_PATH+1];
1615         HKEY hkey;
1616
1617         if (ERROR_SUCCESS != COM_OpenKeyForCLSID(rclsid, KEY_READ, &hkey))
1618         {
1619             ERR("class %s not registered\n", debugstr_guid(rclsid));
1620             hres = REGDB_E_CLASSNOTREG;
1621         }
1622
1623         if (COM_RegReadPath(hkey, wszInprocServer32, NULL, dllpath, ARRAYSIZE(dllpath)) != ERROR_SUCCESS)
1624         {
1625             /* failure: CLSID is not found in registry */
1626             WARN("class %s not registered inproc\n", debugstr_guid(rclsid));
1627             hres = REGDB_E_CLASSNOTREG;
1628         }
1629         else
1630         {
1631             if ((hLibrary = LoadLibraryExW(dllpath, 0, LOAD_WITH_ALTERED_SEARCH_PATH)) == 0)
1632             {
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? */
1636             }
1637             else if (!(DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress(hLibrary, "DllGetClassObject")))
1638             {
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;
1643             }
1644             else
1645             {
1646                 /* OK: get the ClassObject */
1647                 COMPOBJ_DLLList_Add( hLibrary );
1648                 return DllGetClassObject(rclsid, iid, ppv);
1649             }
1650         }
1651     }
1652
1653     /* Next try out of process */
1654     if (CLSCTX_LOCAL_SERVER & dwClsContext)
1655     {
1656         return RPC_GetLocalClassObject(rclsid,iid,ppv);
1657     }
1658
1659     /* Finally try remote: this requires networked DCOM (a lot of work) */
1660     if (CLSCTX_REMOTE_SERVER & dwClsContext)
1661     {
1662         FIXME ("CLSCTX_REMOTE_SERVER not supported\n");
1663         hres = E_NOINTERFACE;
1664     }
1665
1666     return hres;
1667 }
1668
1669 /***********************************************************************
1670  *           CoGetClassObject [COMPOBJ.7]
1671  *
1672  */
1673 HRESULT WINAPI CoGetClassObject16(
1674     REFCLSID rclsid, DWORD dwClsContext, COSERVERINFO *pServerInfo,
1675     REFIID iid, LPVOID *ppv)
1676 {
1677     FIXME(", stub!\n\tCLSID:\t%s,\n\tIID:\t%s\n", debugstr_guid(rclsid), debugstr_guid(iid));
1678
1679     if (pServerInfo) {
1680         FIXME("\tpServerInfo: name=%s\n",debugstr_w(pServerInfo->pwszName));
1681         FIXME("\t\tpAuthInfo=%p\n",pServerInfo->pAuthInfo);
1682     }
1683     return E_NOTIMPL;
1684 }
1685 /***********************************************************************
1686  *        CoResumeClassObjects (OLE32.@)
1687  *
1688  * Resumes all class objects registered with REGCLS_SUSPENDED.
1689  *
1690  * RETURNS
1691  *  Success: S_OK.
1692  *  Failure: HRESULT code.
1693  */
1694 HRESULT WINAPI CoResumeClassObjects(void)
1695 {
1696        FIXME("stub\n");
1697         return S_OK;
1698 }
1699
1700 /***********************************************************************
1701  *        GetClassFile (OLE32.@)
1702  *
1703  * This function supplies the CLSID associated with the given filename.
1704  */
1705 HRESULT WINAPI GetClassFile(LPCOLESTR filePathName,CLSID *pclsid)
1706 {
1707     IStorage *pstg=0;
1708     HRESULT res;
1709     int nbElm, length, i;
1710     LONG sizeProgId;
1711     LPOLESTR *pathDec=0,absFile=0,progId=0;
1712     LPWSTR extension;
1713     static const WCHAR bkslashW[] = {'\\',0};
1714     static const WCHAR dotW[] = {'.',0};
1715
1716     TRACE("%s, %p\n", debugstr_w(filePathName), pclsid);
1717
1718     /* if the file contain a storage object the return the CLSID written by IStorage_SetClass method*/
1719     if((StgIsStorageFile(filePathName))==S_OK){
1720
1721         res=StgOpenStorage(filePathName,NULL,STGM_READ | STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
1722
1723         if (SUCCEEDED(res))
1724             res=ReadClassStg(pstg,pclsid);
1725
1726         IStorage_Release(pstg);
1727
1728         return res;
1729     }
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
1732        this case
1733
1734      for(i=0;i<nFileTypes;i++)
1735
1736         for(i=0;j<nPatternsForType;j++){
1737
1738             PATTERN pat;
1739             HANDLE  hFile;
1740
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){
1746
1747                 *pclsid=ReadCLSIDFromRegistry(i);
1748                 return S_OK;
1749             }
1750         }
1751      */
1752
1753     /* if the above strategies fail then search for the extension key in the registry */
1754
1755     /* get the last element (absolute file) in the path name */
1756     nbElm=FileMonikerImpl_DecomposePath(filePathName,&pathDec);
1757     absFile=pathDec[nbElm-1];
1758
1759     /* failed if the path represente a directory and not an absolute file name*/
1760     if (!lstrcmpW(absFile, bkslashW))
1761         return MK_E_INVALIDEXTENSION;
1762
1763     /* get the extension of the file */
1764     extension = NULL;
1765     length=lstrlenW(absFile);
1766     for(i = length-1; (i >= 0) && *(extension = &absFile[i]) != '.'; i--)
1767         /* nothing */;
1768
1769     if (!extension || !lstrcmpW(extension, dotW))
1770         return MK_E_INVALIDEXTENSION;
1771
1772     res=RegQueryValueW(HKEY_CLASSES_ROOT, extension, NULL, &sizeProgId);
1773
1774     /* get the progId associated to the extension */
1775     progId = CoTaskMemAlloc(sizeProgId);
1776     res = RegQueryValueW(HKEY_CLASSES_ROOT, extension, progId, &sizeProgId);
1777
1778     if (res==ERROR_SUCCESS)
1779         /* return the clsid associated to the progId */
1780         res= CLSIDFromProgID(progId,pclsid);
1781
1782     for(i=0; pathDec[i]!=NULL;i++)
1783         CoTaskMemFree(pathDec[i]);
1784     CoTaskMemFree(pathDec);
1785
1786     CoTaskMemFree(progId);
1787
1788     if (res==ERROR_SUCCESS)
1789         return res;
1790
1791     return MK_E_INVALIDEXTENSION;
1792 }
1793
1794 /***********************************************************************
1795  *           CoCreateInstance [OLE32.@]
1796  */
1797 HRESULT WINAPI CoCreateInstance(
1798         REFCLSID rclsid,
1799         LPUNKNOWN pUnkOuter,
1800         DWORD dwClsContext,
1801         REFIID iid,
1802         LPVOID *ppv)
1803 {
1804   HRESULT hres;
1805   LPCLASSFACTORY lpclf = 0;
1806
1807   if (!COM_CurrentApt()) return CO_E_NOTINITIALIZED;
1808
1809   /*
1810    * Sanity check
1811    */
1812   if (ppv==0)
1813     return E_POINTER;
1814
1815   /*
1816    * Initialize the "out" parameter
1817    */
1818   *ppv = 0;
1819
1820   /*
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
1823    */
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;
1829
1830     TRACE("Retrieved GIT (%p)\n", *ppv);
1831     return S_OK;
1832   }
1833
1834   /*
1835    * Get a class factory to construct the object we want.
1836    */
1837   hres = CoGetClassObject(rclsid,
1838                           dwClsContext,
1839                           NULL,
1840                           &IID_IClassFactory,
1841                           (LPVOID)&lpclf);
1842
1843   if (FAILED(hres)) {
1844     FIXME("no classfactory created for CLSID %s, hres is 0x%08lx\n",
1845           debugstr_guid(rclsid),hres);
1846     return hres;
1847   }
1848
1849   /*
1850    * Create the object and don't forget to release the factory
1851    */
1852         hres = IClassFactory_CreateInstance(lpclf, pUnkOuter, iid, ppv);
1853         IClassFactory_Release(lpclf);
1854         if(FAILED(hres))
1855           FIXME("no instance created for interface %s of class %s, hres is 0x%08lx\n",
1856                 debugstr_guid(iid), debugstr_guid(rclsid),hres);
1857
1858         return hres;
1859 }
1860
1861 /***********************************************************************
1862  *           CoCreateInstance [COMPOBJ.13]
1863  */
1864 HRESULT WINAPI CoCreateInstance16(
1865         REFCLSID rclsid,
1866         LPUNKNOWN pUnkOuter,
1867         DWORD dwClsContext,
1868         REFIID iid,
1869         LPVOID *ppv)
1870 {
1871   FIXME("(%s, %p, %lx, %s, %p), stub!\n", 
1872         debugstr_guid(rclsid), pUnkOuter, dwClsContext, debugstr_guid(iid),
1873         ppv
1874   );
1875   return E_NOTIMPL;
1876 }
1877
1878 /***********************************************************************
1879  *           CoCreateInstanceEx [OLE32.@]
1880  */
1881 HRESULT WINAPI CoCreateInstanceEx(
1882   REFCLSID      rclsid,
1883   LPUNKNOWN     pUnkOuter,
1884   DWORD         dwClsContext,
1885   COSERVERINFO* pServerInfo,
1886   ULONG         cmq,
1887   MULTI_QI*     pResults)
1888 {
1889   IUnknown* pUnk = NULL;
1890   HRESULT   hr;
1891   ULONG     index;
1892   ULONG     successCount = 0;
1893
1894   /*
1895    * Sanity check
1896    */
1897   if ( (cmq==0) || (pResults==NULL))
1898     return E_INVALIDARG;
1899
1900   if (pServerInfo!=NULL)
1901     FIXME("() non-NULL pServerInfo not supported!\n");
1902
1903   /*
1904    * Initialize all the "out" parameters.
1905    */
1906   for (index = 0; index < cmq; index++)
1907   {
1908     pResults[index].pItf = NULL;
1909     pResults[index].hr   = E_NOINTERFACE;
1910   }
1911
1912   /*
1913    * Get the object and get its IUnknown pointer.
1914    */
1915   hr = CoCreateInstance(rclsid,
1916                         pUnkOuter,
1917                         dwClsContext,
1918                         &IID_IUnknown,
1919                         (VOID**)&pUnk);
1920
1921   if (hr)
1922     return hr;
1923
1924   /*
1925    * Then, query for all the interfaces requested.
1926    */
1927   for (index = 0; index < cmq; index++)
1928   {
1929     pResults[index].hr = IUnknown_QueryInterface(pUnk,
1930                                                  pResults[index].pIID,
1931                                                  (VOID**)&(pResults[index].pItf));
1932
1933     if (pResults[index].hr == S_OK)
1934       successCount++;
1935   }
1936
1937   /*
1938    * Release our temporary unknown pointer.
1939    */
1940   IUnknown_Release(pUnk);
1941
1942   if (successCount == 0)
1943     return E_NOINTERFACE;
1944
1945   if (successCount!=cmq)
1946     return CO_S_NOTALLINTERFACES;
1947
1948   return S_OK;
1949 }
1950
1951 /***********************************************************************
1952  *           CoLoadLibrary (OLE32.@)
1953  *
1954  * Loads a library.
1955  *
1956  * PARAMS
1957  *  lpszLibName [I] Path to library.
1958  *  bAutoFree   [I] Whether the library should automatically be freed.
1959  *
1960  * RETURNS
1961  *  Success: Handle to loaded library.
1962  *  Failure: NULL.
1963  *
1964  * SEE ALSO
1965  *  CoFreeLibrary, CoFreeAllLibraries, CoFreeUnusedLibraries
1966  */
1967 HINSTANCE WINAPI CoLoadLibrary(LPOLESTR lpszLibName, BOOL bAutoFree)
1968 {
1969     TRACE("(%s, %d)\n", debugstr_w(lpszLibName), bAutoFree);
1970
1971     return LoadLibraryExW(lpszLibName, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
1972 }
1973
1974 /***********************************************************************
1975  *           CoFreeLibrary [OLE32.@]
1976  *
1977  * Unloads a library from memory.
1978  *
1979  * PARAMS
1980  *  hLibrary [I] Handle to library to unload.
1981  *
1982  * RETURNS
1983  *  Nothing
1984  *
1985  * SEE ALSO
1986  *  CoLoadLibrary, CoFreeAllLibraries, CoFreeUnusedLibraries
1987  */
1988 void WINAPI CoFreeLibrary(HINSTANCE hLibrary)
1989 {
1990     FreeLibrary(hLibrary);
1991 }
1992
1993
1994 /***********************************************************************
1995  *           CoFreeAllLibraries [OLE32.@]
1996  *
1997  * Function for backwards compatibility only. Does nothing.
1998  *
1999  * RETURNS
2000  *  Nothing.
2001  *
2002  * SEE ALSO
2003  *  CoLoadLibrary, CoFreeLibrary, CoFreeUnusedLibraries
2004  */
2005 void WINAPI CoFreeAllLibraries(void)
2006 {
2007     /* NOP */
2008 }
2009
2010
2011 /***********************************************************************
2012  *           CoFreeUnusedLibraries [OLE32.@]
2013  *           CoFreeUnusedLibraries [COMPOBJ.17]
2014  *
2015  * Frees any unused libraries. Unused are identified as those that return
2016  * S_OK from their DllCanUnloadNow function.
2017  *
2018  * RETURNS
2019  *  Nothing.
2020  *
2021  * SEE ALSO
2022  *  CoLoadLibrary, CoFreeAllLibraries, CoFreeLibrary
2023  */
2024 void WINAPI CoFreeUnusedLibraries(void)
2025 {
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);
2029 }
2030
2031 /***********************************************************************
2032  *           CoFileTimeNow [OLE32.@]
2033  *           CoFileTimeNow [COMPOBJ.82]
2034  *
2035  * Retrieves the current time in FILETIME format.
2036  *
2037  * PARAMS
2038  *  lpFileTime [O] The current time.
2039  *
2040  * RETURNS
2041  *      S_OK.
2042  */
2043 HRESULT WINAPI CoFileTimeNow( FILETIME *lpFileTime )
2044 {
2045     GetSystemTimeAsFileTime( lpFileTime );
2046     return S_OK;
2047 }
2048
2049 static void COM_RevokeAllClasses()
2050 {
2051   EnterCriticalSection( &csRegisteredClassList );
2052
2053   while (firstRegisteredClass!=0)
2054   {
2055     CoRevokeClassObject(firstRegisteredClass->dwCookie);
2056   }
2057
2058   LeaveCriticalSection( &csRegisteredClassList );
2059 }
2060
2061 /******************************************************************************
2062  *              CoLockObjectExternal    [OLE32.@]
2063  *
2064  * Increments or decrements the external reference count of a stub object.
2065  *
2066  * PARAMS
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.
2072  *
2073  * RETURNS
2074  *  Success: S_OK.
2075  *  Failure: HRESULT code.
2076  */
2077 HRESULT WINAPI CoLockObjectExternal(
2078     LPUNKNOWN pUnk,
2079     BOOL fLock,
2080     BOOL fLastUnlockReleases)
2081 {
2082     struct stub_manager *stubmgr;
2083     struct apartment *apt;
2084
2085     TRACE("pUnk=%p, fLock=%s, fLastUnlockReleases=%s\n",
2086           pUnk, fLock ? "TRUE" : "FALSE", fLastUnlockReleases ? "TRUE" : "FALSE");
2087
2088     apt = COM_CurrentApt();
2089     if (!apt) return CO_E_NOTINITIALIZED;
2090
2091     stubmgr = get_stub_manager_from_object(apt, pUnk);
2092     
2093     if (stubmgr)
2094     {
2095         if (fLock)
2096             stub_manager_ext_addref(stubmgr, 1);
2097         else
2098             stub_manager_ext_release(stubmgr, 1);
2099         
2100         stub_manager_int_release(stubmgr);
2101
2102         return S_OK;
2103     }
2104     else
2105     {
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 */
2110         return S_OK;
2111     }
2112 }
2113
2114 /***********************************************************************
2115  *           CoInitializeWOW (OLE32.@)
2116  *
2117  * WOW equivalent of CoInitialize?
2118  *
2119  * PARAMS
2120  *  x [I] Unknown.
2121  *  y [I] Unknown.
2122  *
2123  * RETURNS
2124  *  Unknown.
2125  */
2126 HRESULT WINAPI CoInitializeWOW(DWORD x,DWORD y)
2127 {
2128     FIXME("(0x%08lx,0x%08lx),stub!\n",x,y);
2129     return 0;
2130 }
2131
2132 /***********************************************************************
2133  *           CoGetState [OLE32.@]
2134  *
2135  * Retrieves the thread state object previously stored by CoSetState().
2136  *
2137  * PARAMS
2138  *  ppv [I] Address where pointer to object will be stored.
2139  *
2140  * RETURNS
2141  *  Success: S_OK.
2142  *  Failure: E_OUTOFMEMORY.
2143  *
2144  * NOTES
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.
2148  *
2149  * SEE ALSO
2150  *  CoSetState().
2151  */
2152 HRESULT WINAPI CoGetState(IUnknown ** ppv)
2153 {
2154     struct oletls *info = COM_CurrentInfo();
2155     if (!info) return E_OUTOFMEMORY;
2156
2157     *ppv = NULL;
2158
2159     if (info->state)
2160     {
2161         IUnknown_AddRef(info->state);
2162         *ppv = info->state;
2163         TRACE("apt->state=%p\n", info->state);
2164     }
2165
2166     return S_OK;
2167 }
2168
2169 /***********************************************************************
2170  *           CoSetState [OLE32.@]
2171  *
2172  * Sets the thread state object.
2173  *
2174  * PARAMS
2175  *  pv [I] Pointer to state object to be stored.
2176  *
2177  * NOTES
2178  *  The system keeps a reference on the object while the object stored.
2179  *
2180  * RETURNS
2181  *  Success: S_OK.
2182  *  Failure: E_OUTOFMEMORY.
2183  */
2184 HRESULT WINAPI CoSetState(IUnknown * pv)
2185 {
2186     struct oletls *info = COM_CurrentInfo();
2187     if (!info) return E_OUTOFMEMORY;
2188
2189     if (pv) IUnknown_AddRef(pv);
2190
2191     if (info->state)
2192     {
2193         TRACE("-- release %p now\n", info->state);
2194         IUnknown_Release(info->state);
2195     }
2196
2197     info->state = pv;
2198
2199     return S_OK;
2200 }
2201
2202
2203 /******************************************************************************
2204  *              OleGetAutoConvert        [OLE32.@]
2205  */
2206 HRESULT WINAPI OleGetAutoConvert(REFCLSID clsidOld, LPCLSID pClsidNew)
2207 {
2208     static const WCHAR wszAutoConvertTo[] = {'A','u','t','o','C','o','n','v','e','r','t','T','o',0};
2209     HKEY hkey = NULL;
2210     WCHAR buf[CHARS_IN_GUID];
2211     LONG len;
2212     HRESULT res = S_OK;
2213
2214     if (ERROR_SUCCESS != COM_OpenKeyForCLSID(clsidOld, KEY_READ, &hkey))
2215     {
2216         res = REGDB_E_CLASSNOTREG;
2217         goto done;
2218     }
2219
2220     len = sizeof(buf);
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))
2224     {
2225         res = REGDB_E_KEYMISSING;
2226         goto done;
2227     }
2228     res = CLSIDFromString(buf, pClsidNew);
2229 done:
2230     if (hkey) RegCloseKey(hkey);
2231     return res;
2232 }
2233
2234 /******************************************************************************
2235  *              CoTreatAsClass        [OLE32.@]
2236  *
2237  * Sets the TreatAs value of a class.
2238  *
2239  * PARAMS
2240  *  clsidOld [I] Class to set TreatAs value on.
2241  *  clsidNew [I] The class the clsidOld should be treated as.
2242  *
2243  * RETURNS
2244  *  Success: S_OK.
2245  *  Failure: HRESULT code.
2246  *
2247  * SEE ALSO
2248  *  CoGetTreatAsClass
2249  */
2250 HRESULT WINAPI CoTreatAsClass(REFCLSID clsidOld, REFCLSID clsidNew)
2251 {
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};
2254     HKEY hkey = NULL;
2255     WCHAR szClsidNew[CHARS_IN_GUID];
2256     HRESULT res = S_OK;
2257     WCHAR auto_treat_as[CHARS_IN_GUID];
2258     LONG auto_treat_as_size = sizeof(auto_treat_as);
2259     CLSID id;
2260
2261     if (ERROR_SUCCESS != COM_OpenKeyForCLSID(clsidOld, KEY_READ | KEY_WRITE, &hkey))
2262     {
2263         res = REGDB_E_CLASSNOTREG;
2264         goto done;
2265     }
2266     if (!memcmp( clsidOld, clsidNew, sizeof(*clsidOld) ))
2267     {
2268        if (!RegQueryValueW(hkey, wszAutoTreatAs, auto_treat_as, &auto_treat_as_size) &&
2269            !CLSIDFromString(auto_treat_as, &id))
2270        {
2271            if (RegSetValueW(hkey, wszTreatAs, REG_SZ, auto_treat_as, sizeof(auto_treat_as)))
2272            {
2273                res = REGDB_E_WRITEREGDB;
2274                goto done;
2275            }
2276        }
2277        else
2278        {
2279            RegDeleteKeyW(hkey, wszTreatAs);
2280            goto done;
2281        }
2282     }
2283     else if (!StringFromGUID2(clsidNew, szClsidNew, ARRAYSIZE(szClsidNew)) &&
2284              !RegSetValueW(hkey, wszTreatAs, REG_SZ, szClsidNew, sizeof(szClsidNew)))
2285     {
2286         res = REGDB_E_WRITEREGDB;
2287         goto done;
2288     }
2289
2290 done:
2291     if (hkey) RegCloseKey(hkey);
2292     return res;
2293 }
2294
2295 /******************************************************************************
2296  *              CoGetTreatAsClass        [OLE32.@]
2297  *
2298  * Gets the TreatAs value of a class.
2299  *
2300  * PARAMS
2301  *  clsidOld [I] Class to get the TreatAs value of.
2302  *  clsidNew [I] The class the clsidOld should be treated as.
2303  *
2304  * RETURNS
2305  *  Success: S_OK.
2306  *  Failure: HRESULT code.
2307  *
2308  * SEE ALSO
2309  *  CoSetTreatAsClass
2310  */
2311 HRESULT WINAPI CoGetTreatAsClass(REFCLSID clsidOld, LPCLSID clsidNew)
2312 {
2313     static const WCHAR wszTreatAs[] = {'T','r','e','a','t','A','s',0};
2314     HKEY hkey = NULL;
2315     WCHAR szClsidNew[CHARS_IN_GUID];
2316     HRESULT res = S_OK;
2317     LONG len = sizeof(szClsidNew);
2318
2319     FIXME("(%s,%p)\n", debugstr_guid(clsidOld), clsidNew);
2320     memcpy(clsidNew,clsidOld,sizeof(CLSID)); /* copy over old value */
2321
2322     if (COM_OpenKeyForCLSID(clsidOld, KEY_READ, &hkey))
2323     {
2324         res = REGDB_E_CLASSNOTREG;
2325         goto done;
2326     }
2327     if (RegQueryValueW(hkey, wszTreatAs, szClsidNew, &len))
2328     {
2329         res = S_FALSE;
2330         goto done;
2331     }
2332     res = CLSIDFromString(szClsidNew,clsidNew);
2333     if (FAILED(res))
2334         ERR("Failed CLSIDFromStringA(%s), hres 0x%08lx\n", debugstr_w(szClsidNew), res);
2335 done:
2336     if (hkey) RegCloseKey(hkey);
2337     return res;
2338 }
2339
2340 /******************************************************************************
2341  *              CoGetCurrentProcess     [OLE32.@]
2342  *              CoGetCurrentProcess     [COMPOBJ.34]
2343  *
2344  * Gets the current process ID.
2345  *
2346  * RETURNS
2347  *  The current process ID.
2348  *
2349  * NOTES
2350  *   Is DWORD really the correct return type for this function?
2351  */
2352 DWORD WINAPI CoGetCurrentProcess(void)
2353 {
2354         return GetCurrentProcessId();
2355 }
2356
2357 /******************************************************************************
2358  *              CoRegisterMessageFilter [OLE32.@]
2359  *
2360  * Registers a message filter.
2361  *
2362  * PARAMS
2363  *  lpMessageFilter [I] Pointer to interface.
2364  *  lplpMessageFilter [O] Indirect pointer to prior instance if non-NULL.
2365  *
2366  * RETURNS
2367  *  Success: S_OK.
2368  *  Failure: HRESULT code.
2369  */
2370 HRESULT WINAPI CoRegisterMessageFilter(
2371     LPMESSAGEFILTER lpMessageFilter,
2372     LPMESSAGEFILTER *lplpMessageFilter)
2373 {
2374     FIXME("stub\n");
2375     if (lplpMessageFilter) {
2376         *lplpMessageFilter = NULL;
2377     }
2378     return S_OK;
2379 }
2380
2381 /***********************************************************************
2382  *           CoIsOle1Class [OLE32.@]
2383  *
2384  * Determines whether the specified class an OLE v1 class.
2385  *
2386  * PARAMS
2387  *  clsid [I] Class to test.
2388  *
2389  * RETURNS
2390  *  TRUE if the class is an OLE v1 class, or FALSE otherwise.
2391  */
2392 BOOL WINAPI CoIsOle1Class(REFCLSID clsid)
2393 {
2394   FIXME("%s\n", debugstr_guid(clsid));
2395   return FALSE;
2396 }
2397
2398 /***********************************************************************
2399  *           IsEqualGUID [OLE32.@]
2400  *
2401  * Compares two Unique Identifiers.
2402  *
2403  * PARAMS
2404  *  rguid1 [I] The first GUID to compare.
2405  *  rguid2 [I] The other GUID to compare.
2406  *
2407  * RETURNS
2408  *      TRUE if equal
2409  */
2410 #undef IsEqualGUID
2411 BOOL WINAPI IsEqualGUID(
2412      REFGUID rguid1,
2413      REFGUID rguid2)
2414 {
2415     return !memcmp(rguid1,rguid2,sizeof(GUID));
2416 }
2417
2418 /***********************************************************************
2419  *           CoInitializeSecurity [OLE32.@]
2420  */
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)
2426 {
2427   FIXME("(%p,%ld,%p,%p,%ld,%ld,%p,%ld,%p) - stub!\n", pSecDesc, cAuthSvc,
2428         asAuthSvc, pReserved1, dwAuthnLevel, dwImpLevel, pReserved2,
2429         dwCapabilities, pReserved3);
2430   return S_OK;
2431 }
2432
2433 /***********************************************************************
2434  *           CoSuspendClassObjects [OLE32.@]
2435  *
2436  * Suspends all registered class objects to prevent further requests coming in
2437  * for those objects.
2438  *
2439  * RETURNS
2440  *  Success: S_OK.
2441  *  Failure: HRESULT code.
2442  */
2443 HRESULT WINAPI CoSuspendClassObjects(void)
2444 {
2445     FIXME("\n");
2446     return S_OK;
2447 }
2448
2449 /***********************************************************************
2450  *           CoAddRefServerProcess [OLE32.@]
2451  *
2452  * Helper function for incrementing the reference count of a local-server
2453  * process.
2454  *
2455  * RETURNS
2456  *  New reference count.
2457  */
2458 ULONG WINAPI CoAddRefServerProcess(void)
2459 {
2460     FIXME("\n");
2461     return 2;
2462 }
2463
2464 /***********************************************************************
2465  *           CoReleaseServerProcess [OLE32.@]
2466  *
2467  * Helper function for decrementing the reference count of a local-server
2468  * process.
2469  *
2470  * RETURNS
2471  *  New reference count.
2472  */
2473 ULONG WINAPI CoReleaseServerProcess(void)
2474 {
2475     FIXME("\n");
2476     return 1;
2477 }
2478
2479 /***********************************************************************
2480  *           CoIsHandlerConnected [OLE32.@]
2481  *
2482  * Determines whether a proxy is connected to a remote stub.
2483  *
2484  * PARAMS
2485  *  pUnk [I] Pointer to object that may or may not be connected.
2486  *
2487  * RETURNS
2488  *  TRUE if pUnk is not a proxy or if pUnk is connected to a remote stub, or
2489  *  FALSE otherwise.
2490  */
2491 BOOL WINAPI CoIsHandlerConnected(IUnknown *pUnk)
2492 {
2493     FIXME("%p\n", pUnk);
2494
2495     return TRUE;
2496 }
2497  
2498 /***********************************************************************
2499  *           CoQueryProxyBlanket [OLE32.@]
2500  *
2501  * Retrieves the security settings being used by a proxy.
2502  *
2503  * PARAMS
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.
2512  *
2513  * RETURNS
2514  *  Success: S_OK.
2515  *  Failure: HRESULT code.
2516  *
2517  * SEE ALSO
2518  *  CoCopyProxy, CoSetProxyBlanket.
2519  */
2520 HRESULT WINAPI CoQueryProxyBlanket(IUnknown *pProxy, DWORD *pAuthnSvc,
2521     DWORD *pAuthzSvc, OLECHAR **ppServerPrincName, DWORD *pAuthnLevel,
2522     DWORD *pImpLevel, void **ppAuthInfo, DWORD *pCapabilities)
2523 {
2524     IClientSecurity *pCliSec;
2525     HRESULT hr;
2526
2527     TRACE("%p\n", pProxy);
2528
2529     hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (void **)&pCliSec);
2530     if (SUCCEEDED(hr))
2531     {
2532         hr = IClientSecurity_QueryBlanket(pCliSec, pProxy, pAuthnSvc,
2533                                           pAuthzSvc, ppServerPrincName,
2534                                           pAuthnLevel, pImpLevel, ppAuthInfo,
2535                                           pCapabilities);
2536         IClientSecurity_Release(pCliSec);
2537     }
2538
2539     if (FAILED(hr)) ERR("-- failed with 0x%08lx\n", hr);
2540     return hr;
2541 }
2542
2543 /***********************************************************************
2544  *           CoSetProxyBlanket [OLE32.@]
2545  *
2546  * Sets the security settings for a proxy.
2547  *
2548  * PARAMS
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.
2557  *
2558  * RETURNS
2559  *  Success: S_OK.
2560  *  Failure: HRESULT code.
2561  *
2562  * SEE ALSO
2563  *  CoQueryProxyBlanket, CoCopyProxy.
2564  */
2565 HRESULT WINAPI CoSetProxyBlanket(IUnknown *pProxy, DWORD AuthnSvc,
2566     DWORD AuthzSvc, OLECHAR *pServerPrincName, DWORD AuthnLevel,
2567     DWORD ImpLevel, void *pAuthInfo, DWORD Capabilities)
2568 {
2569     IClientSecurity *pCliSec;
2570     HRESULT hr;
2571
2572     TRACE("%p\n", pProxy);
2573
2574     hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (void **)&pCliSec);
2575     if (SUCCEEDED(hr))
2576     {
2577         hr = IClientSecurity_SetBlanket(pCliSec, pProxy, AuthnSvc,
2578                                         AuthzSvc, pServerPrincName,
2579                                         AuthnLevel, ImpLevel, pAuthInfo,
2580                                         Capabilities);
2581         IClientSecurity_Release(pCliSec);
2582     }
2583
2584     if (FAILED(hr)) ERR("-- failed with 0x%08lx\n", hr);
2585     return hr;
2586 }
2587
2588 /***********************************************************************
2589  *           CoCopyProxy [OLE32.@]
2590  *
2591  * Copies a proxy.
2592  *
2593  * PARAMS
2594  *  pProxy [I] Pointer to the proxy object.
2595  *  ppCopy [O] Copy of the proxy.
2596  *
2597  * RETURNS
2598  *  Success: S_OK.
2599  *  Failure: HRESULT code.
2600  *
2601  * SEE ALSO
2602  *  CoQueryProxyBlanket, CoSetProxyBlanket.
2603  */
2604 HRESULT WINAPI CoCopyProxy(IUnknown *pProxy, IUnknown **ppCopy)
2605 {
2606     IClientSecurity *pCliSec;
2607     HRESULT hr;
2608
2609     TRACE("%p\n", pProxy);
2610
2611     hr = IUnknown_QueryInterface(pProxy, &IID_IClientSecurity, (void **)&pCliSec);
2612     if (SUCCEEDED(hr))
2613     {
2614         hr = IClientSecurity_CopyProxy(pCliSec, pProxy, ppCopy);
2615         IClientSecurity_Release(pCliSec);
2616     }
2617
2618     if (FAILED(hr)) ERR("-- failed with 0x%08lx\n", hr);
2619     return hr;
2620 }
2621
2622
2623 /***********************************************************************
2624  *           CoWaitForMultipleHandles [OLE32.@]
2625  *
2626  * Waits for one or more handles to become signaled.
2627  *
2628  * PARAMS
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.
2634  *
2635  * RETURNS
2636  *  Success: S_OK.
2637  *  Failure: RPC_S_CALLPENDING on timeout.
2638  *
2639  * NOTES
2640  *
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.
2644  *
2645  * SEE ALSO
2646  *  MsgWaitForMultipleObjects, WaitForMultipleObjects.
2647  */
2648 HRESULT WINAPI CoWaitForMultipleHandles(DWORD dwFlags, DWORD dwTimeout,
2649     ULONG cHandles, const HANDLE* pHandles, LPDWORD lpdwindex)
2650 {
2651     HRESULT hr = S_OK;
2652     DWORD wait_flags = (dwFlags & COWAIT_WAITALL) ? MWMO_WAITALL : 0 |
2653                        (dwFlags & COWAIT_ALERTABLE ) ? MWMO_ALERTABLE : 0;
2654     DWORD start_time = GetTickCount();
2655
2656     TRACE("(0x%08lx, 0x%08lx, %ld, %p, %p)\n", dwFlags, dwTimeout, cHandles,
2657         pHandles, lpdwindex);
2658
2659     while (TRUE)
2660     {
2661         DWORD now = GetTickCount();
2662         DWORD res;
2663         
2664         if ((dwTimeout != INFINITE) && (start_time + dwTimeout >= now))
2665         {
2666             hr = RPC_S_CALLPENDING;
2667             break;
2668         }
2669
2670         TRACE("waiting for rpc completion or window message\n");
2671
2672         res = MsgWaitForMultipleObjectsEx(cHandles, pHandles,
2673             (dwTimeout == INFINITE) ? INFINITE : start_time + dwTimeout - now,
2674             QS_ALLINPUT, wait_flags);
2675
2676         if (res == WAIT_OBJECT_0 + cHandles)  /* messages available */
2677         {
2678             MSG msg;
2679             while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
2680             {
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);
2685             }
2686         }
2687         else if ((res >= WAIT_OBJECT_0) && (res < WAIT_OBJECT_0 + cHandles))
2688         {
2689             /* handle signaled, store index */
2690             *lpdwindex = (res - WAIT_OBJECT_0);
2691             break;
2692         }
2693         else if (res == WAIT_TIMEOUT)
2694         {
2695             hr = RPC_S_CALLPENDING;
2696             break;
2697         }
2698         else
2699         {
2700             ERR("Unexpected wait termination: %ld, %ld\n", res, GetLastError());
2701             hr = E_UNEXPECTED;
2702             break;
2703         }
2704     }
2705     TRACE("-- 0x%08lx\n", hr);
2706     return hr;
2707 }
2708
2709 /***********************************************************************
2710  *              DllMain (OLE32.@)
2711  */
2712 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID fImpLoad)
2713 {
2714     TRACE("%p 0x%lx %p\n", hinstDLL, fdwReason, fImpLoad);
2715
2716     switch(fdwReason) {
2717     case DLL_PROCESS_ATTACH:
2718         OLE32_hInstance = hinstDLL;
2719         COMPOBJ_InitProcess();
2720         if (TRACE_ON(ole)) CoRegisterMallocSpy((LPVOID)-1);
2721         break;
2722
2723     case DLL_PROCESS_DETACH:
2724         if (TRACE_ON(ole)) CoRevokeMallocSpy();
2725         COMPOBJ_UninitProcess();
2726         OLE32_hInstance = 0;
2727         break;
2728
2729     case DLL_THREAD_DETACH:
2730         COM_TlsDestroy();
2731         break;
2732     }
2733     return TRUE;
2734 }
2735
2736 /* NOTE: DllRegisterServer and DllUnregisterServer are in regsvr.c */