Move __stdcall/__cdecl to the right place.
[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  */
9
10 #include "config.h"
11
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <assert.h>
16
17 #include "windef.h"
18 #include "objbase.h"
19 #include "ole2.h"
20 #include "ole2ver.h"
21 #include "rpc.h"
22 #include "winerror.h"
23 #include "winreg.h"
24 #include "wownt32.h"
25 #include "wtypes.h"
26 #include "wine/unicode.h"
27 #include "wine/obj_base.h"
28 #include "wine/obj_clientserver.h"
29 #include "wine/obj_misc.h"
30 #include "wine/obj_marshal.h"
31 #include "wine/obj_storage.h"
32 #include "wine/winbase16.h"
33 #include "compobj_private.h"
34 #include "ifs.h"
35
36 #include "debugtools.h"
37
38 DEFAULT_DEBUG_CHANNEL(ole);
39
40 /****************************************************************************
41  *  COM External Lock structures and methods declaration
42  *
43  *  This api provides a linked list to managed external references to
44  *  COM objects.
45  *
46  *  The public interface consists of three calls:
47  *      COM_ExternalLockAddRef
48  *      COM_ExternalLockRelease
49  *      COM_ExternalLockFreeList
50  */
51
52 #define EL_END_OF_LIST 0
53 #define EL_NOT_FOUND   0
54
55 /*
56  * Declaration of the static structure that manage the
57  * external lock to COM  objects.
58  */
59 typedef struct COM_ExternalLock     COM_ExternalLock;
60 typedef struct COM_ExternalLockList COM_ExternalLockList;
61
62 struct COM_ExternalLock
63 {
64   IUnknown         *pUnk;     /* IUnknown referenced */
65   ULONG            uRefCount; /* external lock counter to IUnknown object*/
66   COM_ExternalLock *next;     /* Pointer to next element in list */
67 };
68
69 struct COM_ExternalLockList
70 {
71   COM_ExternalLock *head;     /* head of list */
72 };
73
74 /*
75  * Declaration and initialization of the static structure that manages
76  * the external lock to COM objects.
77  */
78 static COM_ExternalLockList elList = { EL_END_OF_LIST };
79
80 /*
81  * Public Interface to the external lock list
82  */
83 static void COM_ExternalLockFreeList();
84 static void COM_ExternalLockAddRef(IUnknown *pUnk);
85 static void COM_ExternalLockRelease(IUnknown *pUnk, BOOL bRelAll);
86 void COM_ExternalLockDump(); /* testing purposes, not static to avoid warning */
87
88 /*
89  * Private methods used to managed the linked list
90  */
91 static BOOL COM_ExternalLockInsert(
92   IUnknown *pUnk);
93
94 static void COM_ExternalLockDelete(
95   COM_ExternalLock *element);
96
97 static COM_ExternalLock* COM_ExternalLockFind(
98   IUnknown *pUnk);
99
100 static COM_ExternalLock* COM_ExternalLockLocate(
101   COM_ExternalLock *element,
102   IUnknown         *pUnk);
103
104 /****************************************************************************
105  * This section defines variables internal to the COM module.
106  *
107  * TODO: Most of these things will have to be made thread-safe.
108  */
109 HINSTANCE16     COMPOBJ_hInstance = 0;
110 HINSTANCE       COMPOBJ_hInstance32 = 0;
111 static int      COMPOBJ_Attach = 0;
112
113 LPMALLOC16 currentMalloc16=NULL;
114 LPMALLOC currentMalloc32=NULL;
115
116 HTASK16 hETask = 0;
117 WORD Table_ETask[62];
118
119 /*
120  * This lock count counts the number of times CoInitialize is called. It is
121  * decreased every time CoUninitialize is called. When it hits 0, the COM
122  * libraries are freed
123  */
124 static LONG s_COMLockCount = 0;
125
126 /*
127  * This linked list contains the list of registered class objects. These
128  * are mostly used to register the factories for out-of-proc servers of OLE
129  * objects.
130  *
131  * TODO: Make this data structure aware of inter-process communication. This
132  *       means that parts of this will be exported to the Wine Server.
133  */
134 typedef struct tagRegisteredClass
135 {
136   CLSID     classIdentifier;
137   LPUNKNOWN classObject;
138   DWORD     runContext;
139   DWORD     connectFlags;
140   DWORD     dwCookie;
141   struct tagRegisteredClass* nextClass;
142 } RegisteredClass;
143
144 static CRITICAL_SECTION csRegisteredClassList;
145 static RegisteredClass* firstRegisteredClass = NULL;
146
147 /* this open DLL table belongs in a per process table, but my guess is that
148  * it shouldn't live in the kernel, so I'll put them out here in DLL
149  * space assuming that there is one OLE32 per process.
150  */
151 typedef struct tagOpenDll {
152   HINSTANCE hLibrary;
153   struct tagOpenDll *next;
154 } OpenDll;
155
156 static CRITICAL_SECTION csOpenDllList;
157 static OpenDll *openDllList = NULL; /* linked list of open dlls */
158
159 /*****************************************************************************
160  * This section contains prototypes to internal methods for this
161  * module
162  */
163 static HRESULT COM_GetRegisteredClassObject(REFCLSID    rclsid,
164                                             DWORD       dwClsContext,
165                                             LPUNKNOWN*  ppUnk);
166
167 static void COM_RevokeAllClasses();
168
169
170 /******************************************************************************
171  * Initialize/Uninitialize critical sections.
172  */
173 void COMPOBJ_InitProcess( void )
174 {
175     InitializeCriticalSection( &csRegisteredClassList );
176     InitializeCriticalSection( &csOpenDllList );
177 }
178
179 void COMPOBJ_UninitProcess( void )
180 {
181     DeleteCriticalSection( &csRegisteredClassList );
182     DeleteCriticalSection( &csOpenDllList );
183 }
184
185 /******************************************************************************
186  *           CoBuildVersion [COMPOBJ.1]
187  *           CoBuildVersion [OLE32.4]
188  *
189  * RETURNS
190  *      Current build version, hiword is majornumber, loword is minornumber
191  */
192 DWORD WINAPI CoBuildVersion(void)
193 {
194     TRACE("Returning version %d, build %d.\n", rmm, rup);
195     return (rmm<<16)+rup;
196 }
197
198 /******************************************************************************
199  *              CoInitialize    [COMPOBJ.2]
200  * Set the win16 IMalloc used for memory management
201  */
202 HRESULT WINAPI CoInitialize16(
203         LPVOID lpReserved       /* [in] pointer to win16 malloc interface */
204 ) {
205     currentMalloc16 = (LPMALLOC16)lpReserved;
206     return S_OK;
207 }
208
209 /******************************************************************************
210  *              CoInitialize    [OLE32.26]
211  *
212  * Initializes the COM libraries.
213  *
214  * See CoInitializeEx
215  */
216 HRESULT WINAPI CoInitialize(
217         LPVOID lpReserved       /* [in] pointer to win32 malloc interface
218                                    (obsolete, should be NULL) */
219 )
220 {
221   /*
222    * Just delegate to the newer method.
223    */
224   return CoInitializeEx(lpReserved, COINIT_APARTMENTTHREADED);
225 }
226
227 /******************************************************************************
228  *              CoInitializeEx  [OLE32.163]
229  *
230  * Initializes the COM libraries. The behavior used to set the win32 IMalloc
231  * used for memory management is obsolete.
232  *
233  * RETURNS
234  *  S_OK               if successful,
235  *  S_FALSE            if this function was called already.
236  *  RPC_E_CHANGED_MODE if a previous call to CoInitialize specified another
237  *                      threading model.
238  *
239  * BUGS
240  * Only the single threaded model is supported. As a result RPC_E_CHANGED_MODE
241  * is never returned.
242  *
243  * See the windows documentation for more details.
244  */
245 HRESULT WINAPI CoInitializeEx(
246         LPVOID lpReserved,      /* [in] pointer to win32 malloc interface
247                                    (obsolete, should be NULL) */
248         DWORD dwCoInit          /* [in] A value from COINIT specifies the threading model */
249 )
250 {
251   HRESULT hr;
252
253   TRACE("(%p, %x)\n", lpReserved, (int)dwCoInit);
254
255   if (lpReserved!=NULL)
256   {
257     ERR("(%p, %x) - Bad parameter passed-in %p, must be an old Windows Application\n", lpReserved, (int)dwCoInit, lpReserved);
258   }
259
260   /*
261    * Check for unsupported features.
262    */
263   if (dwCoInit!=COINIT_APARTMENTTHREADED)
264   {
265     FIXME(":(%p,%x): unsupported flag %x\n", lpReserved, (int)dwCoInit, (int)dwCoInit);
266     /* Hope for the best and continue anyway */
267   }
268
269   /*
270    * Check the lock count. If this is the first time going through the initialize
271    * process, we have to initialize the libraries.
272    *
273    * And crank-up that lock count.
274    */
275   if (InterlockedExchangeAdd(&s_COMLockCount,1)==0)
276   {
277     /*
278      * Initialize the various COM libraries and data structures.
279      */
280     TRACE("() - Initializing the COM libraries\n");
281
282
283     RunningObjectTableImpl_Initialize();
284
285     hr = S_OK;
286   }
287   else
288     hr = S_FALSE;
289
290   return hr;
291 }
292
293 /***********************************************************************
294  *           CoUninitialize   [COMPOBJ.3]
295  * Don't know what it does.
296  * 3-Nov-98 -- this was originally misspelled, I changed it to what I
297  *   believe is the correct spelling
298  */
299 void WINAPI CoUninitialize16(void)
300 {
301   TRACE("()\n");
302   CoFreeAllLibraries();
303 }
304
305 /***********************************************************************
306  *           CoUninitialize   [OLE32.47]
307  *
308  * This method will release the COM libraries.
309  *
310  * See the windows documentation for more details.
311  */
312 void WINAPI CoUninitialize(void)
313 {
314   LONG lCOMRefCnt;
315   TRACE("()\n");
316
317   /*
318    * Decrease the reference count.
319    * If we are back to 0 locks on the COM library, make sure we free
320    * all the associated data structures.
321    */
322   lCOMRefCnt = InterlockedExchangeAdd(&s_COMLockCount,-1);
323   if (lCOMRefCnt==1)
324   {
325     /*
326      * Release the various COM libraries and data structures.
327      */
328     TRACE("() - Releasing the COM libraries\n");
329
330     RunningObjectTableImpl_UnInitialize();
331     /*
332      * Release the references to the registered class objects.
333      */
334     COM_RevokeAllClasses();
335
336     /*
337      * This will free the loaded COM Dlls.
338      */
339     CoFreeAllLibraries();
340
341     /*
342      * This will free list of external references to COM objects.
343      */
344     COM_ExternalLockFreeList();
345
346   }
347   else if (lCOMRefCnt<1) {
348     ERR( "CoUninitialize() - not CoInitialized.\n" );
349     InterlockedExchangeAdd(&s_COMLockCount,1); /* restore the lock count. */
350   }
351 }
352
353 /***********************************************************************
354  *           CoGetMalloc    [COMPOBJ.4]
355  * RETURNS
356  *      The current win16 IMalloc
357  */
358 HRESULT WINAPI CoGetMalloc16(
359         DWORD dwMemContext,     /* [in] unknown */
360         LPMALLOC16 * lpMalloc   /* [out] current win16 malloc interface */
361 ) {
362     if(!currentMalloc16)
363         currentMalloc16 = IMalloc16_Constructor();
364     *lpMalloc = currentMalloc16;
365     return S_OK;
366 }
367
368 /******************************************************************************
369  *              CoGetMalloc     [OLE32.20]
370  *
371  * RETURNS
372  *      The current win32 IMalloc
373  */
374 HRESULT WINAPI CoGetMalloc(
375         DWORD dwMemContext,     /* [in] unknown */
376         LPMALLOC *lpMalloc      /* [out] current win32 malloc interface */
377 ) {
378     if(!currentMalloc32)
379         currentMalloc32 = IMalloc_Constructor();
380     *lpMalloc = currentMalloc32;
381     return S_OK;
382 }
383
384 /***********************************************************************
385  *           CoCreateStandardMalloc [COMPOBJ.71]
386  */
387 HRESULT WINAPI CoCreateStandardMalloc16(DWORD dwMemContext,
388                                           LPMALLOC16 *lpMalloc)
389 {
390     /* FIXME: docu says we shouldn't return the same allocator as in
391      * CoGetMalloc16 */
392     *lpMalloc = IMalloc16_Constructor();
393     return S_OK;
394 }
395
396 /******************************************************************************
397  *              CoDisconnectObject      [COMPOBJ.15]
398  *              CoDisconnectObject      [OLE32.8]
399  */
400 HRESULT WINAPI CoDisconnectObject( LPUNKNOWN lpUnk, DWORD reserved )
401 {
402     TRACE("(%p, %lx)\n",lpUnk,reserved);
403     return S_OK;
404 }
405
406 /***********************************************************************
407  *           IsEqualGUID [COMPOBJ.18]
408  *
409  * Compares two Unique Identifiers.
410  *
411  * RETURNS
412  *      TRUE if equal
413  */
414 BOOL16 WINAPI IsEqualGUID16(
415         GUID* g1,       /* [in] unique id 1 */
416         GUID* g2        /* [in] unique id 2 */
417 ) {
418     return !memcmp( g1, g2, sizeof(GUID) );
419 }
420
421 /******************************************************************************
422  *              CLSIDFromString [COMPOBJ.20]
423  * Converts a unique identifier from its string representation into
424  * the GUID struct.
425  *
426  * Class id: DWORD-WORD-WORD-BYTES[2]-BYTES[6]
427  *
428  * RETURNS
429  *      the converted GUID
430  */
431 HRESULT WINAPI CLSIDFromString16(
432         LPCOLESTR16 idstr,      /* [in] string representation of guid */
433         CLSID *id               /* [out] GUID converted from string */
434 ) {
435   BYTE *s = (BYTE *) idstr;
436   BYTE *p;
437   int   i;
438   BYTE table[256];
439
440   if (!s)
441           s = "{00000000-0000-0000-0000-000000000000}";
442   else {  /* validate the CLSID string */
443
444       if (strlen(s) != 38)
445           return CO_E_CLASSSTRING;
446
447       if ((s[0]!='{') || (s[9]!='-') || (s[14]!='-') || (s[19]!='-') || (s[24]!='-') || (s[37]!='}'))
448           return CO_E_CLASSSTRING;
449
450       for (i=1; i<37; i++)
451       {
452           if ((i == 9)||(i == 14)||(i == 19)||(i == 24)) continue;
453           if (!(((s[i] >= '0') && (s[i] <= '9'))  ||
454                 ((s[i] >= 'a') && (s[i] <= 'f'))  ||
455                 ((s[i] >= 'A') && (s[i] <= 'F')))
456              )
457               return CO_E_CLASSSTRING;
458       }
459   }
460
461   TRACE("%s -> %p\n", s, id);
462
463   /* quick lookup table */
464   memset(table, 0, 256);
465
466   for (i = 0; i < 10; i++) {
467     table['0' + i] = i;
468   }
469   for (i = 0; i < 6; i++) {
470     table['A' + i] = i+10;
471     table['a' + i] = i+10;
472   }
473
474   /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
475
476   p = (BYTE *) id;
477
478   s++;  /* skip leading brace  */
479   for (i = 0; i < 4; i++) {
480     p[3 - i] = table[*s]<<4 | table[*(s+1)];
481     s += 2;
482   }
483   p += 4;
484   s++;  /* skip - */
485
486   for (i = 0; i < 2; i++) {
487     p[1-i] = table[*s]<<4 | table[*(s+1)];
488     s += 2;
489   }
490   p += 2;
491   s++;  /* skip - */
492
493   for (i = 0; i < 2; i++) {
494     p[1-i] = table[*s]<<4 | table[*(s+1)];
495     s += 2;
496   }
497   p += 2;
498   s++;  /* skip - */
499
500   /* these are just sequential bytes */
501   for (i = 0; i < 2; i++) {
502     *p++ = table[*s]<<4 | table[*(s+1)];
503     s += 2;
504   }
505   s++;  /* skip - */
506
507   for (i = 0; i < 6; i++) {
508     *p++ = table[*s]<<4 | table[*(s+1)];
509     s += 2;
510   }
511
512   return S_OK;
513 }
514
515 /******************************************************************************
516  *              CoCreateGuid[OLE32.6]
517  *
518  */
519 HRESULT WINAPI CoCreateGuid(
520         GUID *pguid /* [out] points to the GUID to initialize */
521 ) {
522     return UuidCreate(pguid);
523 }
524
525 /******************************************************************************
526  *              CLSIDFromString [OLE32.3]
527  *              IIDFromString   [OLE32.74]
528  * Converts a unique identifier from its string representation into
529  * the GUID struct.
530  *
531  * UNDOCUMENTED
532  *      If idstr is not a valid CLSID string then it gets treated as a ProgID
533  *
534  * RETURNS
535  *      the converted GUID
536  */
537 HRESULT WINAPI CLSIDFromString(
538         LPCOLESTR idstr,        /* [in] string representation of GUID */
539         CLSID *id )             /* [out] GUID represented by above string */
540 {
541     char xid[40];
542     HRESULT ret;
543
544     if (!WideCharToMultiByte( CP_ACP, 0, idstr, -1, xid, sizeof(xid), NULL, NULL ))
545         return CO_E_CLASSSTRING;
546     ret = CLSIDFromString16(xid,id);
547     if(ret != S_OK) { /* It appears a ProgID is also valid */
548         ret = CLSIDFromProgID(idstr, id);
549     }
550     return ret;
551 }
552
553 /******************************************************************************
554  *              WINE_StringFromCLSID    [Internal]
555  * Converts a GUID into the respective string representation.
556  *
557  * NOTES
558  *
559  * RETURNS
560  *      the string representation and HRESULT
561  */
562 static HRESULT WINE_StringFromCLSID(
563         const CLSID *id,        /* [in] GUID to be converted */
564         LPSTR idstr             /* [out] pointer to buffer to contain converted guid */
565 ) {
566   static const char *hex = "0123456789ABCDEF";
567   char *s;
568   int   i;
569
570   if (!id)
571         { ERR("called with id=Null\n");
572           *idstr = 0x00;
573           return E_FAIL;
574         }
575
576   sprintf(idstr, "{%08lX-%04X-%04X-%02X%02X-",
577           id->Data1, id->Data2, id->Data3,
578           id->Data4[0], id->Data4[1]);
579   s = &idstr[25];
580
581   /* 6 hex bytes */
582   for (i = 2; i < 8; i++) {
583     *s++ = hex[id->Data4[i]>>4];
584     *s++ = hex[id->Data4[i] & 0xf];
585   }
586
587   *s++ = '}';
588   *s++ = '\0';
589
590   TRACE("%p->%s\n", id, idstr);
591
592   return S_OK;
593 }
594
595 /******************************************************************************
596  *              StringFromCLSID [COMPOBJ.19]
597  * Converts a GUID into the respective string representation.
598  * The target string is allocated using the OLE IMalloc.
599  * RETURNS
600  *      the string representation and HRESULT
601  */
602 HRESULT WINAPI StringFromCLSID16(
603         REFCLSID id,            /* [in] the GUID to be converted */
604         LPOLESTR16 *idstr       /* [out] a pointer to a to-be-allocated segmented pointer pointing to the resulting string */
605
606 ) {
607     extern BOOL WINAPI K32WOWCallback16Ex( DWORD vpfn16, DWORD dwFlags,
608                                            DWORD cbArgs, LPVOID pArgs, LPDWORD pdwRetCode );
609     LPMALLOC16  mllc;
610     HRESULT     ret;
611     DWORD       args[2];
612
613     ret = CoGetMalloc16(0,&mllc);
614     if (ret) return ret;
615
616     args[0] = (DWORD)mllc;
617     args[1] = 40;
618
619     /* No need for a Callback entry, we have WOWCallback16Ex which does
620      * everything we need.
621      */
622     if (!K32WOWCallback16Ex(
623         (DWORD)((ICOM_VTABLE(IMalloc16)*)MapSL(
624             (SEGPTR)ICOM_VTBL(((LPMALLOC16)MapSL((SEGPTR)mllc))))
625         )->Alloc,
626         WCB16_CDECL,
627         2*sizeof(DWORD),
628         (LPVOID)args,
629         (LPDWORD)idstr
630     )) {
631         WARN("CallTo16 IMalloc16 failed\n");
632         return E_FAIL;
633     }
634     return WINE_StringFromCLSID(id,MapSL((SEGPTR)*idstr));
635 }
636
637 /******************************************************************************
638  *              StringFromCLSID [OLE32.151]
639  *              StringFromIID   [OLE32.153]
640  * Converts a GUID into the respective string representation.
641  * The target string is allocated using the OLE IMalloc.
642  * RETURNS
643  *      the string representation and HRESULT
644  */
645 HRESULT WINAPI StringFromCLSID(
646         REFCLSID id,            /* [in] the GUID to be converted */
647         LPOLESTR *idstr /* [out] a pointer to a to-be-allocated pointer pointing to the resulting string */
648 ) {
649         char            buf[80];
650         HRESULT       ret;
651         LPMALLOC        mllc;
652
653         if ((ret=CoGetMalloc(0,&mllc)))
654                 return ret;
655
656         ret=WINE_StringFromCLSID(id,buf);
657         if (!ret) {
658             DWORD len = MultiByteToWideChar( CP_ACP, 0, buf, -1, NULL, 0 );
659             *idstr = IMalloc_Alloc( mllc, len * sizeof(WCHAR) );
660             MultiByteToWideChar( CP_ACP, 0, buf, -1, *idstr, len );
661         }
662         return ret;
663 }
664
665 /******************************************************************************
666  *              StringFromGUID2 [COMPOBJ.76]
667  *              StringFromGUID2 [OLE32.152]
668  *
669  * Converts a global unique identifier into a string of an API-
670  * specified fixed format. (The usual {.....} stuff.)
671  *
672  * RETURNS
673  *      The (UNICODE) string representation of the GUID in 'str'
674  *      The length of the resulting string, 0 if there was any problem.
675  */
676 INT WINAPI
677 StringFromGUID2(REFGUID id, LPOLESTR str, INT cmax)
678 {
679   char          xguid[80];
680
681   if (WINE_StringFromCLSID(id,xguid))
682         return 0;
683   return MultiByteToWideChar( CP_ACP, 0, xguid, -1, str, cmax );
684 }
685
686 /******************************************************************************
687  * ProgIDFromCLSID [OLE32.133]
688  * Converts a class id into the respective Program ID. (By using a registry lookup)
689  * RETURNS S_OK on success
690  * riid associated with the progid
691  */
692
693 HRESULT WINAPI ProgIDFromCLSID(
694   REFCLSID clsid, /* [in] class id as found in registry */
695   LPOLESTR *lplpszProgID/* [out] associated Prog ID */
696 )
697 {
698   char     strCLSID[50], *buf, *buf2;
699   DWORD    buf2len;
700   HKEY     xhkey;
701   LPMALLOC mllc;
702   HRESULT  ret = S_OK;
703
704   WINE_StringFromCLSID(clsid, strCLSID);
705
706   buf = HeapAlloc(GetProcessHeap(), 0, strlen(strCLSID)+14);
707   sprintf(buf,"CLSID\\%s\\ProgID", strCLSID);
708   if (RegOpenKeyA(HKEY_CLASSES_ROOT, buf, &xhkey))
709     ret = REGDB_E_CLASSNOTREG;
710
711   HeapFree(GetProcessHeap(), 0, buf);
712
713   if (ret == S_OK)
714   {
715     buf2 = HeapAlloc(GetProcessHeap(), 0, 255);
716     buf2len = 255;
717     if (RegQueryValueA(xhkey, NULL, buf2, &buf2len))
718       ret = REGDB_E_CLASSNOTREG;
719
720     if (ret == S_OK)
721     {
722       if (CoGetMalloc(0,&mllc))
723         ret = E_OUTOFMEMORY;
724       else
725       {
726           DWORD len = MultiByteToWideChar( CP_ACP, 0, buf2, -1, NULL, 0 );
727           *lplpszProgID = IMalloc_Alloc(mllc, len * sizeof(WCHAR) );
728           MultiByteToWideChar( CP_ACP, 0, buf2, -1, *lplpszProgID, len );
729       }
730     }
731     HeapFree(GetProcessHeap(), 0, buf2);
732   }
733
734   RegCloseKey(xhkey);
735   return ret;
736 }
737
738 /******************************************************************************
739  *              CLSIDFromProgID [COMPOBJ.61]
740  * Converts a program id into the respective GUID. (By using a registry lookup)
741  * RETURNS
742  *      riid associated with the progid
743  */
744 HRESULT WINAPI CLSIDFromProgID16(
745         LPCOLESTR16 progid,     /* [in] program id as found in registry */
746         LPCLSID riid            /* [out] associated CLSID */
747 ) {
748         char    *buf,buf2[80];
749         DWORD   buf2len;
750         HRESULT err;
751         HKEY    xhkey;
752
753         buf = HeapAlloc(GetProcessHeap(),0,strlen(progid)+8);
754         sprintf(buf,"%s\\CLSID",progid);
755         if ((err=RegOpenKeyA(HKEY_CLASSES_ROOT,buf,&xhkey))) {
756                 HeapFree(GetProcessHeap(),0,buf);
757                 return CO_E_CLASSSTRING;
758         }
759         HeapFree(GetProcessHeap(),0,buf);
760         buf2len = sizeof(buf2);
761         if ((err=RegQueryValueA(xhkey,NULL,buf2,&buf2len))) {
762                 RegCloseKey(xhkey);
763                 return CO_E_CLASSSTRING;
764         }
765         RegCloseKey(xhkey);
766         return CLSIDFromString16(buf2,riid);
767 }
768
769 /******************************************************************************
770  *              CLSIDFromProgID [OLE32.2]
771  * Converts a program id into the respective GUID. (By using a registry lookup)
772  * RETURNS
773  *      riid associated with the progid
774  */
775 HRESULT WINAPI CLSIDFromProgID(
776         LPCOLESTR progid,       /* [in] program id as found in registry */
777         LPCLSID riid )          /* [out] associated CLSID */
778 {
779     static const WCHAR clsidW[] = { '\\','C','L','S','I','D',0 };
780     char buf2[80];
781     DWORD buf2len = sizeof(buf2);
782     HKEY xhkey;
783
784     WCHAR *buf = HeapAlloc( GetProcessHeap(),0,(strlenW(progid)+8) * sizeof(WCHAR) );
785     strcpyW( buf, progid );
786     strcatW( buf, clsidW );
787     if (RegOpenKeyW(HKEY_CLASSES_ROOT,buf,&xhkey))
788     {
789         HeapFree(GetProcessHeap(),0,buf);
790         return CO_E_CLASSSTRING;
791     }
792     HeapFree(GetProcessHeap(),0,buf);
793
794     if (RegQueryValueA(xhkey,NULL,buf2,&buf2len))
795     {
796         RegCloseKey(xhkey);
797         return CO_E_CLASSSTRING;
798     }
799     RegCloseKey(xhkey);
800     return CLSIDFromString16(buf2,riid);
801 }
802
803
804
805 /*****************************************************************************
806  *             CoGetPSClsid [OLE32.22]
807  *
808  * This function returns the CLSID of the DLL that implements the proxy and stub
809  * for the specified interface.
810  *
811  * It determines this by searching the
812  * HKEY_CLASSES_ROOT\Interface\{string form of riid}\ProxyStubClsid32 in the registry
813  * and any interface id registered by CoRegisterPSClsid within the current process.
814  *
815  * FIXME: We only search the registry, not ids registered with CoRegisterPSClsid.
816  */
817 HRESULT WINAPI CoGetPSClsid(
818           REFIID riid,     /* [in]  Interface whose proxy/stub CLSID is to be returned */
819           CLSID *pclsid )    /* [out] Where to store returned proxy/stub CLSID */
820 {
821     char *buf, buf2[40];
822     DWORD buf2len;
823     HKEY xhkey;
824
825     TRACE("() riid=%s, pclsid=%p\n", debugstr_guid(riid), pclsid);
826
827     /* Get the input iid as a string */
828     WINE_StringFromCLSID(riid, buf2);
829     /* Allocate memory for the registry key we will construct.
830        (length of iid string plus constant length of static text */
831     buf = HeapAlloc(GetProcessHeap(), 0, strlen(buf2)+27);
832     if (buf == NULL)
833     {
834        return (E_OUTOFMEMORY);
835     }
836
837     /* Construct the registry key we want */
838     sprintf(buf,"Interface\\%s\\ProxyStubClsid32", buf2);
839
840     /* Open the key.. */
841     if (RegOpenKeyA(HKEY_CLASSES_ROOT, buf, &xhkey))
842     {
843        HeapFree(GetProcessHeap(),0,buf);
844        return (E_INVALIDARG);
845     }
846     HeapFree(GetProcessHeap(),0,buf);
847
848     /* ... Once we have the key, query the registry to get the
849        value of CLSID as a string, and convert it into a
850        proper CLSID structure to be passed back to the app */
851     buf2len = sizeof(buf2);
852     if ( (RegQueryValueA(xhkey,NULL,buf2,&buf2len)) )
853     {
854        RegCloseKey(xhkey);
855        return E_INVALIDARG;
856     }
857     RegCloseKey(xhkey);
858
859     /* We have the CLSid we want back from the registry as a string, so
860        lets convert it into a CLSID structure */
861     if ( (CLSIDFromString16(buf2,pclsid)) != NOERROR)
862     {
863        return E_INVALIDARG;
864     }
865
866     TRACE ("() Returning CLSID=%s\n", debugstr_guid(pclsid));
867     return (S_OK);
868 }
869
870
871
872 /***********************************************************************
873  *              WriteClassStm (OLE32.159)
874  *
875  * This function write a CLSID on stream
876  */
877 HRESULT WINAPI WriteClassStm(IStream *pStm,REFCLSID rclsid)
878 {
879     TRACE("(%p,%p)\n",pStm,rclsid);
880
881     if (rclsid==NULL)
882         return E_INVALIDARG;
883
884     return IStream_Write(pStm,rclsid,sizeof(CLSID),NULL);
885 }
886
887 /***********************************************************************
888  *              ReadClassStm (OLE32.135)
889  *
890  * This function read a CLSID from a stream
891  */
892 HRESULT WINAPI ReadClassStm(IStream *pStm,CLSID *pclsid)
893 {
894     ULONG nbByte;
895     HRESULT res;
896
897     TRACE("(%p,%p)\n",pStm,pclsid);
898
899     if (pclsid==NULL)
900         return E_INVALIDARG;
901
902     res = IStream_Read(pStm,(void*)pclsid,sizeof(CLSID),&nbByte);
903
904     if (FAILED(res))
905         return res;
906
907     if (nbByte != sizeof(CLSID))
908         return S_FALSE;
909     else
910         return S_OK;
911 }
912
913 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
914 /***********************************************************************
915  *           LookupETask (COMPOBJ.94)
916  */
917 HRESULT WINAPI LookupETask16(HTASK16 *hTask,LPVOID p) {
918         FIXME("(%p,%p),stub!\n",hTask,p);
919         if ((*hTask = GetCurrentTask()) == hETask) {
920                 memcpy(p, Table_ETask, sizeof(Table_ETask));
921         }
922         return 0;
923 }
924
925 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
926 /***********************************************************************
927  *           SetETask (COMPOBJ.95)
928  */
929 HRESULT WINAPI SetETask16(HTASK16 hTask, LPVOID p) {
930         FIXME("(%04x,%p),stub!\n",hTask,p);
931         hETask = hTask;
932         return 0;
933 }
934
935 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
936 /***********************************************************************
937  *           CALLOBJECTINWOW (COMPOBJ.201)
938  */
939 HRESULT WINAPI CallObjectInWOW(LPVOID p1,LPVOID p2) {
940         FIXME("(%p,%p),stub!\n",p1,p2);
941         return 0;
942 }
943
944 /******************************************************************************
945  *              CoRegisterClassObject   [COMPOBJ.5]
946  *
947  * Don't know where it registers it ...
948  */
949 HRESULT WINAPI CoRegisterClassObject16(
950         REFCLSID rclsid,
951         LPUNKNOWN pUnk,
952         DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
953         DWORD flags,        /* [in] REGCLS flags indicating how connections are made */
954         LPDWORD lpdwRegister
955 ) {
956         char    buf[80];
957
958         WINE_StringFromCLSID(rclsid,buf);
959
960         FIXME("(%s,%p,0x%08lx,0x%08lx,%p),stub\n",
961                 buf,pUnk,dwClsContext,flags,lpdwRegister
962         );
963         return 0;
964 }
965
966
967 /******************************************************************************
968  *      CoRevokeClassObject [COMPOBJ.6]
969  *
970  */
971 HRESULT WINAPI CoRevokeClassObject16(DWORD dwRegister) /* [in] token on class obj */
972 {
973     FIXME("(0x%08lx),stub!\n", dwRegister);
974     return 0;
975 }
976
977 /******************************************************************************
978  *      CoFileTimeToDosDateTime [COMPOBJ.30]
979  */
980 BOOL16 WINAPI CoFileTimeToDosDateTime16(const FILETIME *ft, LPWORD lpDosDate, LPWORD lpDosTime)
981 {
982     return FileTimeToDosDateTime(ft, lpDosDate, lpDosTime);
983 }
984
985 /******************************************************************************
986  *      CoDosDateTimeToFileTime [COMPOBJ.31]
987  */
988 BOOL16 WINAPI CoDosDateTimeToFileTime16(WORD wDosDate, WORD wDosTime, FILETIME *ft)
989 {
990     return DosDateTimeToFileTime(wDosDate, wDosTime, ft);
991 }
992
993 /***
994  * COM_GetRegisteredClassObject
995  *
996  * This internal method is used to scan the registered class list to
997  * find a class object.
998  *
999  * Params:
1000  *   rclsid        Class ID of the class to find.
1001  *   dwClsContext  Class context to match.
1002  *   ppv           [out] returns a pointer to the class object. Complying
1003  *                 to normal COM usage, this method will increase the
1004  *                 reference count on this object.
1005  */
1006 static HRESULT COM_GetRegisteredClassObject(
1007         REFCLSID    rclsid,
1008         DWORD       dwClsContext,
1009         LPUNKNOWN*  ppUnk)
1010 {
1011   HRESULT hr = S_FALSE;
1012   RegisteredClass* curClass;
1013
1014   EnterCriticalSection( &csRegisteredClassList );
1015
1016   /*
1017    * Sanity check
1018    */
1019   assert(ppUnk!=0);
1020
1021   /*
1022    * Iterate through the whole list and try to match the class ID.
1023    */
1024   curClass = firstRegisteredClass;
1025
1026   while (curClass != 0)
1027   {
1028     /*
1029      * Check if we have a match on the class ID.
1030      */
1031     if (IsEqualGUID(&(curClass->classIdentifier), rclsid))
1032     {
1033       /*
1034        * Since we don't do out-of process or DCOM just right away, let's ignore the
1035        * class context.
1036        */
1037
1038       /*
1039        * We have a match, return the pointer to the class object.
1040        */
1041       *ppUnk = curClass->classObject;
1042
1043       IUnknown_AddRef(curClass->classObject);
1044
1045       hr = S_OK;
1046       goto end;
1047     }
1048
1049     /*
1050      * Step to the next class in the list.
1051      */
1052     curClass = curClass->nextClass;
1053   }
1054
1055 end:
1056   LeaveCriticalSection( &csRegisteredClassList );
1057   /*
1058    * If we get to here, we haven't found our class.
1059    */
1060   return hr;
1061 }
1062
1063 /******************************************************************************
1064  *              CoRegisterClassObject   [OLE32.36]
1065  *
1066  * This method will register the class object for a given class ID.
1067  *
1068  * See the Windows documentation for more details.
1069  */
1070 HRESULT WINAPI CoRegisterClassObject(
1071         REFCLSID rclsid,
1072         LPUNKNOWN pUnk,
1073         DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
1074         DWORD flags,        /* [in] REGCLS flags indicating how connections are made */
1075         LPDWORD lpdwRegister
1076 )
1077 {
1078   RegisteredClass* newClass;
1079   LPUNKNOWN        foundObject;
1080   HRESULT          hr;
1081     char buf[80];
1082
1083     WINE_StringFromCLSID(rclsid,buf);
1084
1085   TRACE("(%s,%p,0x%08lx,0x%08lx,%p)\n",
1086         buf,pUnk,dwClsContext,flags,lpdwRegister);
1087
1088   /*
1089    * Perform a sanity check on the parameters
1090    */
1091   if ( (lpdwRegister==0) || (pUnk==0) )
1092   {
1093     return E_INVALIDARG;
1094   }
1095
1096   /*
1097    * Initialize the cookie (out parameter)
1098    */
1099   *lpdwRegister = 0;
1100
1101   /*
1102    * First, check if the class is already registered.
1103    * If it is, this should cause an error.
1104    */
1105   hr = COM_GetRegisteredClassObject(rclsid, dwClsContext, &foundObject);
1106
1107   if (hr == S_OK)
1108   {
1109     /*
1110      * The COM_GetRegisteredClassObject increased the reference count on the
1111      * object so it has to be released.
1112      */
1113     IUnknown_Release(foundObject);
1114
1115     return CO_E_OBJISREG;
1116   }
1117
1118   /*
1119    * If it is not registered, we must create a new entry for this class and
1120    * append it to the registered class list.
1121    * We use the address of the chain node as the cookie since we are sure it's
1122    * unique.
1123    */
1124   newClass = HeapAlloc(GetProcessHeap(), 0, sizeof(RegisteredClass));
1125   if ( newClass == NULL )
1126     return E_OUTOFMEMORY;
1127
1128   EnterCriticalSection( &csRegisteredClassList );
1129   /*
1130    * Initialize the node.
1131    */
1132   newClass->classIdentifier = *rclsid;
1133   newClass->runContext      = dwClsContext;
1134   newClass->connectFlags    = flags;
1135   newClass->dwCookie        = (DWORD)newClass;
1136   newClass->nextClass       = firstRegisteredClass;
1137
1138   /*
1139    * Since we're making a copy of the object pointer, we have to increase its
1140    * reference count.
1141    */
1142   newClass->classObject     = pUnk;
1143   IUnknown_AddRef(newClass->classObject);
1144
1145   firstRegisteredClass = newClass;
1146
1147   LeaveCriticalSection( &csRegisteredClassList );
1148
1149   /*
1150    * Assign the out parameter (cookie)
1151    */
1152   *lpdwRegister = newClass->dwCookie;
1153
1154   /*
1155    * We're successful Yippee!
1156    */
1157   return S_OK;
1158 }
1159
1160 /***********************************************************************
1161  *           CoRevokeClassObject [OLE32.40]
1162  *
1163  * This method will remove a class object from the class registry
1164  *
1165  * See the Windows documentation for more details.
1166  */
1167 HRESULT WINAPI CoRevokeClassObject(
1168         DWORD dwRegister)
1169 {
1170   HRESULT hr = E_INVALIDARG;
1171   RegisteredClass** prevClassLink;
1172   RegisteredClass*  curClass;
1173
1174   TRACE("(%08lx)\n",dwRegister);
1175
1176   EnterCriticalSection( &csRegisteredClassList );
1177
1178   /*
1179    * Iterate through the whole list and try to match the cookie.
1180    */
1181   curClass      = firstRegisteredClass;
1182   prevClassLink = &firstRegisteredClass;
1183
1184   while (curClass != 0)
1185   {
1186     /*
1187      * Check if we have a match on the cookie.
1188      */
1189     if (curClass->dwCookie == dwRegister)
1190     {
1191       /*
1192        * Remove the class from the chain.
1193        */
1194       *prevClassLink = curClass->nextClass;
1195
1196       /*
1197        * Release the reference to the class object.
1198        */
1199       IUnknown_Release(curClass->classObject);
1200
1201       /*
1202        * Free the memory used by the chain node.
1203        */
1204       HeapFree(GetProcessHeap(), 0, curClass);
1205
1206       hr = S_OK;
1207       goto end;
1208     }
1209
1210     /*
1211      * Step to the next class in the list.
1212      */
1213     prevClassLink = &(curClass->nextClass);
1214     curClass      = curClass->nextClass;
1215   }
1216
1217 end:
1218   LeaveCriticalSection( &csRegisteredClassList );
1219   /*
1220    * If we get to here, we haven't found our class.
1221    */
1222   return hr;
1223 }
1224
1225 /***********************************************************************
1226  *           CoGetClassObject [COMPOBJ.7]
1227  *           CoGetClassObject [OLE32.16]
1228  *
1229  * FIXME.  If request allows of several options and there is a failure 
1230  *         with one (other than not being registered) do we try the
1231  *         others or return failure?  (E.g. inprocess is registered but
1232  *         the DLL is not found but the server version works)
1233  */
1234 HRESULT WINAPI CoGetClassObject(
1235     REFCLSID rclsid, DWORD dwClsContext, COSERVERINFO *pServerInfo,
1236     REFIID iid, LPVOID *ppv
1237 ) {
1238     LPUNKNOWN   regClassObject;
1239     HRESULT     hres = E_UNEXPECTED;
1240     char        xclsid[80];
1241     WCHAR ProviderName[MAX_PATH+1];
1242     DWORD ProviderNameLen = sizeof(ProviderName);
1243     HINSTANCE hLibrary;
1244     typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid,
1245                              REFIID iid, LPVOID *ppv);
1246     DllGetClassObjectFunc DllGetClassObject;
1247     HKEY key;
1248     char buf[200];
1249
1250     WINE_StringFromCLSID((LPCLSID)rclsid,xclsid);
1251
1252     TRACE("\n\tCLSID:\t%s,\n\tIID:\t%s\n",
1253         debugstr_guid(rclsid),
1254         debugstr_guid(iid)
1255     );
1256
1257     if (pServerInfo) {
1258         FIXME("\tpServerInfo: name=%s\n",debugstr_w(pServerInfo->pwszName));
1259         FIXME("\t\tpAuthInfo=%p\n",pServerInfo->pAuthInfo);
1260     }
1261
1262     /*
1263      * First, try and see if we can't match the class ID with one of the
1264      * registered classes.
1265      */
1266     if (S_OK == COM_GetRegisteredClassObject(rclsid, dwClsContext, &regClassObject))
1267     {
1268       /*
1269        * Get the required interface from the retrieved pointer.
1270        */
1271       hres = IUnknown_QueryInterface(regClassObject, iid, ppv);
1272
1273       /*
1274        * Since QI got another reference on the pointer, we want to release the
1275        * one we already have. If QI was unsuccessful, this will release the object. This
1276        * is good since we are not returning it in the "out" parameter.
1277        */
1278       IUnknown_Release(regClassObject);
1279
1280       return hres;
1281     }
1282
1283     /* Then try for in-process */
1284     if ((CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER) & dwClsContext)
1285     {
1286         memset(ProviderName,0,sizeof(ProviderName));
1287         sprintf(buf,"CLSID\\%s\\InprocServer32",xclsid);
1288         if (((hres = RegOpenKeyExA(HKEY_CLASSES_ROOT, buf, 0, KEY_READ, &key)) != ERROR_SUCCESS) ||
1289             ((hres = RegQueryValueExW(key,NULL,NULL,NULL,(LPBYTE)ProviderName,&ProviderNameLen)),
1290              RegCloseKey (key),
1291              hres != ERROR_SUCCESS))
1292         {
1293             hres = REGDB_E_CLASSNOTREG;
1294         }
1295         /* Don't ask me.  MSDN says that CoGetClassObject does NOT call CoLoadLibrary */
1296         else if ((hLibrary = CoLoadLibrary(ProviderName, TRUE)) == 0)
1297         {
1298             FIXME("couldn't load InprocServer32 dll %s\n", debugstr_w(ProviderName));
1299             hres = E_ACCESSDENIED; /* or should this be CO_E_DLLNOTFOUND? */
1300         }
1301         else if (!(DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress(hLibrary, "DllGetClassObject")))
1302         {
1303             /* not sure if this should be called here CoFreeLibrary(hLibrary);*/
1304             FIXME("couldn't find function DllGetClassObject in %s\n", debugstr_w(ProviderName));
1305             hres = E_ACCESSDENIED;
1306         }
1307         else
1308         {
1309             /* Ask the DLL for its class object. (there was a note here about
1310              * class factories but this is good.
1311              */
1312             return DllGetClassObject(rclsid, iid, ppv);
1313         }
1314     }
1315     
1316
1317     /* Finally try out of process */
1318     /* out of process and remote servers not supported yet */
1319     if (CLSCTX_LOCAL_SERVER & dwClsContext)
1320     {
1321         memset(ProviderName,0,sizeof(ProviderName));
1322         sprintf(buf,"CLSID\\%s\\LocalServer32",xclsid);
1323         if (((hres = RegOpenKeyExA(HKEY_CLASSES_ROOT, buf, 0, KEY_READ, &key)) != ERROR_SUCCESS) ||
1324             ((hres = RegQueryValueExW(key,NULL,NULL,NULL,(LPBYTE)ProviderName,&ProviderNameLen)),
1325              RegCloseKey (key),
1326              hres != ERROR_SUCCESS))
1327         {
1328             hres = REGDB_E_CLASSNOTREG;
1329         }
1330         else
1331         {
1332             /* CO_E_APPNOTFOUND if no exe */
1333             FIXME("CLSCTX_LOCAL_SERVER %s registered but not yet supported!\n",debugstr_w(ProviderName));
1334             hres = E_ACCESSDENIED;
1335         }
1336     }
1337
1338     if (CLSCTX_REMOTE_SERVER & dwClsContext)
1339     {
1340         FIXME ("CLSCTX_REMOTE_SERVER not supported\n");
1341         hres = E_ACCESSDENIED;
1342     }
1343
1344     return hres;
1345 }
1346 /***********************************************************************
1347  *        CoResumeClassObjects (OLE32.173)
1348  *
1349  * Resumes classobjects registered with REGCLS suspended
1350  */
1351 HRESULT WINAPI CoResumeClassObjects(void)
1352 {
1353         FIXME("\n");
1354         return S_OK;
1355 }
1356
1357 /***********************************************************************
1358  *        GetClassFile (OLE32.67)
1359  *
1360  * This function supplies the CLSID associated with the given filename.
1361  */
1362 HRESULT WINAPI GetClassFile(LPCOLESTR filePathName,CLSID *pclsid)
1363 {
1364     IStorage *pstg=0;
1365     HRESULT res;
1366     int nbElm=0,length=0,i=0;
1367     LONG sizeProgId=20;
1368     LPOLESTR *pathDec=0,absFile=0,progId=0;
1369     WCHAR extention[100]={0};
1370
1371     TRACE("()\n");
1372
1373     /* if the file contain a storage object the return the CLSID writen by IStorage_SetClass method*/
1374     if((StgIsStorageFile(filePathName))==S_OK){
1375
1376         res=StgOpenStorage(filePathName,NULL,STGM_READ | STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
1377
1378         if (SUCCEEDED(res))
1379             res=ReadClassStg(pstg,pclsid);
1380
1381         IStorage_Release(pstg);
1382
1383         return res;
1384     }
1385     /* if the file is not a storage object then attemps to match various bits in the file against a
1386        pattern in the registry. this case is not frequently used ! so I present only the psodocode for
1387        this case
1388
1389      for(i=0;i<nFileTypes;i++)
1390
1391         for(i=0;j<nPatternsForType;j++){
1392
1393             PATTERN pat;
1394             HANDLE  hFile;
1395
1396             pat=ReadPatternFromRegistry(i,j);
1397             hFile=CreateFileW(filePathName,,,,,,hFile);
1398             SetFilePosition(hFile,pat.offset);
1399             ReadFile(hFile,buf,pat.size,NULL,NULL);
1400             if (memcmp(buf&pat.mask,pat.pattern.pat.size)==0){
1401
1402                 *pclsid=ReadCLSIDFromRegistry(i);
1403                 return S_OK;
1404             }
1405         }
1406      */
1407
1408     /* if the obove strategies fail then search for the extension key in the registry */
1409
1410     /* get the last element (absolute file) in the path name */
1411     nbElm=FileMonikerImpl_DecomposePath(filePathName,&pathDec);
1412     absFile=pathDec[nbElm-1];
1413
1414     /* failed if the path represente a directory and not an absolute file name*/
1415     if (lstrcmpW(absFile,(LPOLESTR)"\\"))
1416         return MK_E_INVALIDEXTENSION;
1417
1418     /* get the extension of the file */
1419     length=lstrlenW(absFile);
1420     for(i=length-1; ( (i>=0) && (extention[i]=absFile[i]) );i--);
1421
1422     /* get the progId associated to the extension */
1423     progId=CoTaskMemAlloc(sizeProgId);
1424
1425     res=RegQueryValueW(HKEY_CLASSES_ROOT,extention,progId,&sizeProgId);
1426
1427     if (res==ERROR_MORE_DATA){
1428
1429         progId = CoTaskMemRealloc(progId,sizeProgId);
1430         res=RegQueryValueW(HKEY_CLASSES_ROOT,extention,progId,&sizeProgId);
1431     }
1432     if (res==ERROR_SUCCESS)
1433         /* return the clsid associated to the progId */
1434         res= CLSIDFromProgID(progId,pclsid);
1435
1436     for(i=0; pathDec[i]!=NULL;i++)
1437         CoTaskMemFree(pathDec[i]);
1438     CoTaskMemFree(pathDec);
1439
1440     CoTaskMemFree(progId);
1441
1442     if (res==ERROR_SUCCESS)
1443         return res;
1444
1445     return MK_E_INVALIDEXTENSION;
1446 }
1447 /******************************************************************************
1448  *              CoRegisterMessageFilter [COMPOBJ.27]
1449  */
1450 HRESULT WINAPI CoRegisterMessageFilter16(
1451         LPMESSAGEFILTER lpMessageFilter,
1452         LPMESSAGEFILTER *lplpMessageFilter
1453 ) {
1454         FIXME("(%p,%p),stub!\n",lpMessageFilter,lplpMessageFilter);
1455         return 0;
1456 }
1457
1458 /***********************************************************************
1459  *           CoCreateInstance [COMPOBJ.13]
1460  *           CoCreateInstance [OLE32.7]
1461  */
1462 HRESULT WINAPI CoCreateInstance(
1463         REFCLSID rclsid,
1464         LPUNKNOWN pUnkOuter,
1465         DWORD dwClsContext,
1466         REFIID iid,
1467         LPVOID *ppv)
1468 {
1469         HRESULT hres;
1470         LPCLASSFACTORY lpclf = 0;
1471
1472   /*
1473    * Sanity check
1474    */
1475   if (ppv==0)
1476     return E_POINTER;
1477
1478   /*
1479    * Initialize the "out" parameter
1480    */
1481   *ppv = 0;
1482
1483   /*
1484    * Get a class factory to construct the object we want.
1485    */
1486   hres = CoGetClassObject(rclsid,
1487                           dwClsContext,
1488                           NULL,
1489                           &IID_IClassFactory,
1490                           (LPVOID)&lpclf);
1491
1492   if (FAILED(hres)) {
1493     FIXME("no classfactory created for CLSID %s, hres is 0x%08lx\n",
1494           debugstr_guid(rclsid),hres);
1495     return hres;
1496   }
1497
1498   /*
1499    * Create the object and don't forget to release the factory
1500    */
1501         hres = IClassFactory_CreateInstance(lpclf, pUnkOuter, iid, ppv);
1502         IClassFactory_Release(lpclf);
1503         if(FAILED(hres))
1504           FIXME("no instance created for interface %s of class %s, hres is 0x%08lx\n",
1505                 debugstr_guid(iid), debugstr_guid(rclsid),hres);
1506
1507         return hres;
1508 }
1509
1510 /***********************************************************************
1511  *           CoCreateInstanceEx [OLE32.165]
1512  */
1513 HRESULT WINAPI CoCreateInstanceEx(
1514   REFCLSID      rclsid,
1515   LPUNKNOWN     pUnkOuter,
1516   DWORD         dwClsContext,
1517   COSERVERINFO* pServerInfo,
1518   ULONG         cmq,
1519   MULTI_QI*     pResults)
1520 {
1521   IUnknown* pUnk = NULL;
1522   HRESULT   hr;
1523   ULONG     index;
1524   int       successCount = 0;
1525
1526   /*
1527    * Sanity check
1528    */
1529   if ( (cmq==0) || (pResults==NULL))
1530     return E_INVALIDARG;
1531
1532   if (pServerInfo!=NULL)
1533     FIXME("() non-NULL pServerInfo not supported!\n");
1534
1535   /*
1536    * Initialize all the "out" parameters.
1537    */
1538   for (index = 0; index < cmq; index++)
1539   {
1540     pResults[index].pItf = NULL;
1541     pResults[index].hr   = E_NOINTERFACE;
1542   }
1543
1544   /*
1545    * Get the object and get its IUnknown pointer.
1546    */
1547   hr = CoCreateInstance(rclsid,
1548                         pUnkOuter,
1549                         dwClsContext,
1550                         &IID_IUnknown,
1551                         (VOID**)&pUnk);
1552
1553   if (hr)
1554     return hr;
1555
1556   /*
1557    * Then, query for all the interfaces requested.
1558    */
1559   for (index = 0; index < cmq; index++)
1560   {
1561     pResults[index].hr = IUnknown_QueryInterface(pUnk,
1562                                                  pResults[index].pIID,
1563                                                  (VOID**)&(pResults[index].pItf));
1564
1565     if (pResults[index].hr == S_OK)
1566       successCount++;
1567   }
1568
1569   /*
1570    * Release our temporary unknown pointer.
1571    */
1572   IUnknown_Release(pUnk);
1573
1574   if (successCount == 0)
1575     return E_NOINTERFACE;
1576
1577   if (successCount!=cmq)
1578     return CO_S_NOTALLINTERFACES;
1579
1580   return S_OK;
1581 }
1582
1583 /***********************************************************************
1584  *           CoFreeLibrary [OLE32.13]
1585  */
1586 void WINAPI CoFreeLibrary(HINSTANCE hLibrary)
1587 {
1588     OpenDll *ptr, *prev;
1589     OpenDll *tmp;
1590
1591     EnterCriticalSection( &csOpenDllList );
1592
1593     /* lookup library in linked list */
1594     prev = NULL;
1595     for (ptr = openDllList; ptr != NULL; ptr=ptr->next) {
1596         if (ptr->hLibrary == hLibrary) {
1597             break;
1598         }
1599         prev = ptr;
1600     }
1601
1602     if (ptr == NULL) {
1603         /* shouldn't happen if user passed in a valid hLibrary */
1604         goto end;
1605     }
1606     /* assert: ptr points to the library entry to free */
1607
1608     /* free library and remove node from list */
1609     FreeLibrary(hLibrary);
1610     if (ptr == openDllList) {
1611         tmp = openDllList->next;
1612         HeapFree(GetProcessHeap(), 0, openDllList);
1613         openDllList = tmp;
1614     } else {
1615         tmp = ptr->next;
1616         HeapFree(GetProcessHeap(), 0, ptr);
1617         prev->next = tmp;
1618     }
1619 end:
1620     LeaveCriticalSection( &csOpenDllList );
1621 }
1622
1623
1624 /***********************************************************************
1625  *           CoFreeAllLibraries [OLE32.12]
1626  */
1627 void WINAPI CoFreeAllLibraries(void)
1628 {
1629     OpenDll *ptr, *tmp;
1630
1631     EnterCriticalSection( &csOpenDllList );
1632
1633     for (ptr = openDllList; ptr != NULL; ) {
1634         tmp=ptr->next;
1635         CoFreeLibrary(ptr->hLibrary);
1636         ptr = tmp;
1637     }
1638
1639     LeaveCriticalSection( &csOpenDllList );
1640 }
1641
1642
1643
1644 /***********************************************************************
1645  *           CoFreeUnusedLibraries [COMPOBJ.17]
1646  *           CoFreeUnusedLibraries [OLE32.14]
1647  */
1648 void WINAPI CoFreeUnusedLibraries(void)
1649 {
1650     OpenDll *ptr, *tmp;
1651     typedef HRESULT(*DllCanUnloadNowFunc)(void);
1652     DllCanUnloadNowFunc DllCanUnloadNow;
1653
1654     EnterCriticalSection( &csOpenDllList );
1655
1656     for (ptr = openDllList; ptr != NULL; ) {
1657         DllCanUnloadNow = (DllCanUnloadNowFunc)
1658             GetProcAddress(ptr->hLibrary, "DllCanUnloadNow");
1659
1660         if ( (DllCanUnloadNow != NULL) &&
1661              (DllCanUnloadNow() == S_OK) ) {
1662             tmp=ptr->next;
1663             CoFreeLibrary(ptr->hLibrary);
1664             ptr = tmp;
1665         } else {
1666             ptr=ptr->next;
1667         }
1668     }
1669
1670     LeaveCriticalSection( &csOpenDllList );
1671 }
1672
1673 /***********************************************************************
1674  *           CoFileTimeNow [COMPOBJ.82]
1675  *           CoFileTimeNow [OLE32.10]
1676  *
1677  * RETURNS
1678  *      the current system time in lpFileTime
1679  */
1680 HRESULT WINAPI CoFileTimeNow( FILETIME *lpFileTime ) /* [out] the current time */
1681 {
1682     GetSystemTimeAsFileTime( lpFileTime );
1683     return S_OK;
1684 }
1685
1686 /***********************************************************************
1687  *           CoTaskMemAlloc (OLE32.43)
1688  * RETURNS
1689  *      pointer to newly allocated block
1690  */
1691 LPVOID WINAPI CoTaskMemAlloc(
1692         ULONG size      /* [in] size of memoryblock to be allocated */
1693 ) {
1694     LPMALLOC    lpmalloc;
1695     HRESULT     ret = CoGetMalloc(0,&lpmalloc);
1696
1697     if (FAILED(ret))
1698         return NULL;
1699
1700     return IMalloc_Alloc(lpmalloc,size);
1701 }
1702 /***********************************************************************
1703  *           CoTaskMemFree (OLE32.44)
1704  */
1705 VOID WINAPI CoTaskMemFree(
1706         LPVOID ptr      /* [in] pointer to be freed */
1707 ) {
1708     LPMALLOC    lpmalloc;
1709     HRESULT     ret = CoGetMalloc(0,&lpmalloc);
1710
1711     if (FAILED(ret))
1712       return;
1713
1714     IMalloc_Free(lpmalloc, ptr);
1715 }
1716
1717 /***********************************************************************
1718  *           CoTaskMemRealloc (OLE32.45)
1719  * RETURNS
1720  *      pointer to newly allocated block
1721  */
1722 LPVOID WINAPI CoTaskMemRealloc(
1723   LPVOID pvOld,
1724   ULONG  size)  /* [in] size of memoryblock to be allocated */
1725 {
1726   LPMALLOC lpmalloc;
1727   HRESULT  ret = CoGetMalloc(0,&lpmalloc);
1728
1729   if (FAILED(ret))
1730     return NULL;
1731
1732   return IMalloc_Realloc(lpmalloc, pvOld, size);
1733 }
1734
1735 /***********************************************************************
1736  *           CoLoadLibrary (OLE32.30)
1737  */
1738 HINSTANCE WINAPI CoLoadLibrary(LPOLESTR lpszLibName, BOOL bAutoFree)
1739 {
1740     HINSTANCE hLibrary;
1741     OpenDll *ptr;
1742     OpenDll *tmp;
1743
1744     TRACE("(%s, %d)\n", debugstr_w(lpszLibName), bAutoFree);
1745
1746     hLibrary = LoadLibraryExW(lpszLibName, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
1747
1748     if (!bAutoFree)
1749         return hLibrary;
1750
1751     EnterCriticalSection( &csOpenDllList );
1752
1753     if (openDllList == NULL) {
1754         /* empty list -- add first node */
1755         openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
1756         openDllList->hLibrary=hLibrary;
1757         openDllList->next = NULL;
1758     } else {
1759         /* search for this dll */
1760         int found = FALSE;
1761         for (ptr = openDllList; ptr->next != NULL; ptr=ptr->next) {
1762             if (ptr->hLibrary == hLibrary) {
1763                 found = TRUE;
1764                 break;
1765             }
1766         }
1767         if (!found) {
1768             /* dll not found, add it */
1769             tmp = openDllList;
1770             openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
1771             openDllList->hLibrary = hLibrary;
1772             openDllList->next = tmp;
1773         }
1774     }
1775
1776     LeaveCriticalSection( &csOpenDllList );
1777
1778     return hLibrary;
1779 }
1780
1781 /***********************************************************************
1782  *           CoInitializeWOW (OLE32.27)
1783  */
1784 HRESULT WINAPI CoInitializeWOW(DWORD x,DWORD y) {
1785     FIXME("(0x%08lx,0x%08lx),stub!\n",x,y);
1786     return 0;
1787 }
1788
1789 /******************************************************************************
1790  *              CoLockObjectExternal    [COMPOBJ.63]
1791  */
1792 HRESULT WINAPI CoLockObjectExternal16(
1793     LPUNKNOWN pUnk,             /* [in] object to be locked */
1794     BOOL16 fLock,               /* [in] do lock */
1795     BOOL16 fLastUnlockReleases  /* [in] ? */
1796 ) {
1797     FIXME("(%p,%d,%d),stub!\n",pUnk,fLock,fLastUnlockReleases);
1798     return S_OK;
1799 }
1800
1801 /******************************************************************************
1802  *              CoLockObjectExternal    [OLE32.31]
1803  */
1804 HRESULT WINAPI CoLockObjectExternal(
1805     LPUNKNOWN pUnk,             /* [in] object to be locked */
1806     BOOL fLock,         /* [in] do lock */
1807     BOOL fLastUnlockReleases) /* [in] unlock all */
1808 {
1809
1810   if (fLock)
1811   {
1812     /*
1813      * Increment the external lock coutner, COM_ExternalLockAddRef also
1814      * increment the object's internal lock counter.
1815      */
1816     COM_ExternalLockAddRef( pUnk);
1817   }
1818   else
1819   {
1820     /*
1821      * Decrement the external lock coutner, COM_ExternalLockRelease also
1822      * decrement the object's internal lock counter.
1823      */
1824     COM_ExternalLockRelease( pUnk, fLastUnlockReleases);
1825   }
1826
1827     return S_OK;
1828 }
1829
1830 /***********************************************************************
1831  *           CoGetState [COMPOBJ.115]
1832  */
1833 HRESULT WINAPI CoGetState16(LPDWORD state)
1834 {
1835     FIXME("(%p),stub!\n", state);
1836     *state = 0;
1837     return S_OK;
1838 }
1839 /***********************************************************************
1840  *           CoSetState [OLE32.42]
1841  */
1842 HRESULT WINAPI CoSetState(LPDWORD state)
1843 {
1844     FIXME("(%p),stub!\n", state);
1845     if (state) *state = 0;
1846     return S_OK;
1847 }
1848 /***********************************************************************
1849  *          CoCreateFreeThreadedMarshaler [OLE32.5]
1850  */
1851 HRESULT WINAPI CoCreateFreeThreadedMarshaler (LPUNKNOWN punkOuter, LPUNKNOWN* ppunkMarshal)
1852 {
1853    FIXME ("(%p %p): stub\n", punkOuter, ppunkMarshal);
1854
1855    return S_OK;
1856 }
1857
1858
1859 /***********************************************************************
1860  *           DllGetClassObject [OLE32.63]
1861  */
1862 HRESULT WINAPI OLE32_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
1863 {
1864         FIXME("\n\tCLSID:\t%s,\n\tIID:\t%s\n",debugstr_guid(rclsid),debugstr_guid(iid));
1865         *ppv = NULL;
1866         return CLASS_E_CLASSNOTAVAILABLE;
1867 }
1868
1869
1870 /***
1871  * COM_RevokeAllClasses
1872  *
1873  * This method is called when the COM libraries are uninitialized to
1874  * release all the references to the class objects registered with
1875  * the library
1876  */
1877 static void COM_RevokeAllClasses()
1878 {
1879   EnterCriticalSection( &csRegisteredClassList );
1880
1881   while (firstRegisteredClass!=0)
1882   {
1883     CoRevokeClassObject(firstRegisteredClass->dwCookie);
1884   }
1885
1886   LeaveCriticalSection( &csRegisteredClassList );
1887 }
1888
1889 /****************************************************************************
1890  *  COM External Lock methods implementation
1891  */
1892
1893 /****************************************************************************
1894  * Public - Method that increments the count for a IUnknown* in the linked
1895  * list.  The item is inserted if not already in the list.
1896  */
1897 static void COM_ExternalLockAddRef(
1898   IUnknown *pUnk)
1899 {
1900   COM_ExternalLock *externalLock = COM_ExternalLockFind(pUnk);
1901
1902   /*
1903    * Add an external lock to the object. If it was already externally
1904    * locked, just increase the reference count. If it was not.
1905    * add the item to the list.
1906    */
1907   if ( externalLock == EL_NOT_FOUND )
1908     COM_ExternalLockInsert(pUnk);
1909   else
1910     externalLock->uRefCount++;
1911
1912   /*
1913    * Add an internal lock to the object
1914    */
1915   IUnknown_AddRef(pUnk);
1916 }
1917
1918 /****************************************************************************
1919  * Public - Method that decrements the count for a IUnknown* in the linked
1920  * list.  The item is removed from the list if its count end up at zero or if
1921  * bRelAll is TRUE.
1922  */
1923 static void COM_ExternalLockRelease(
1924   IUnknown *pUnk,
1925   BOOL   bRelAll)
1926 {
1927   COM_ExternalLock *externalLock = COM_ExternalLockFind(pUnk);
1928
1929   if ( externalLock != EL_NOT_FOUND )
1930   {
1931     do
1932     {
1933       externalLock->uRefCount--;  /* release external locks      */
1934       IUnknown_Release(pUnk);     /* release local locks as well */
1935
1936       if ( bRelAll == FALSE )
1937         break;  /* perform single release */
1938
1939     } while ( externalLock->uRefCount > 0 );
1940
1941     if ( externalLock->uRefCount == 0 )  /* get rid of the list entry */
1942       COM_ExternalLockDelete(externalLock);
1943   }
1944 }
1945 /****************************************************************************
1946  * Public - Method that frees the content of the list.
1947  */
1948 static void COM_ExternalLockFreeList()
1949 {
1950   COM_ExternalLock *head;
1951
1952   head = elList.head;                 /* grab it by the head             */
1953   while ( head != EL_END_OF_LIST )
1954   {
1955     COM_ExternalLockDelete(head);     /* get rid of the head stuff       */
1956
1957     head = elList.head;               /* get the new head...             */
1958   }
1959 }
1960
1961 /****************************************************************************
1962  * Public - Method that dump the content of the list.
1963  */
1964 void COM_ExternalLockDump()
1965 {
1966   COM_ExternalLock *current = elList.head;
1967
1968   DPRINTF("\nExternal lock list contains:\n");
1969
1970   while ( current != EL_END_OF_LIST )
1971   {
1972       DPRINTF( "\t%p with %lu references count.\n", current->pUnk, current->uRefCount);
1973
1974     /* Skip to the next item */
1975     current = current->next;
1976   }
1977
1978 }
1979
1980 /****************************************************************************
1981  * Internal - Find a IUnknown* in the linked list
1982  */
1983 static COM_ExternalLock* COM_ExternalLockFind(
1984   IUnknown *pUnk)
1985 {
1986   return COM_ExternalLockLocate(elList.head, pUnk);
1987 }
1988
1989 /****************************************************************************
1990  * Internal - Recursivity agent for IUnknownExternalLockList_Find
1991  */
1992 static COM_ExternalLock* COM_ExternalLockLocate(
1993   COM_ExternalLock *element,
1994   IUnknown         *pUnk)
1995 {
1996   if ( element == EL_END_OF_LIST )
1997     return EL_NOT_FOUND;
1998
1999   else if ( element->pUnk == pUnk )    /* We found it */
2000     return element;
2001
2002   else                                 /* Not the right guy, keep on looking */
2003     return COM_ExternalLockLocate( element->next, pUnk);
2004 }
2005
2006 /****************************************************************************
2007  * Internal - Insert a new IUnknown* to the linked list
2008  */
2009 static BOOL COM_ExternalLockInsert(
2010   IUnknown *pUnk)
2011 {
2012   COM_ExternalLock *newLock      = NULL;
2013   COM_ExternalLock *previousHead = NULL;
2014
2015   /*
2016    * Allocate space for the new storage object
2017    */
2018   newLock = HeapAlloc(GetProcessHeap(), 0, sizeof(COM_ExternalLock));
2019
2020   if (newLock!=NULL)
2021   {
2022     if ( elList.head == EL_END_OF_LIST )
2023     {
2024       elList.head = newLock;    /* The list is empty */
2025     }
2026     else
2027     {
2028       /*
2029        * insert does it at the head
2030        */
2031       previousHead  = elList.head;
2032       elList.head = newLock;
2033     }
2034
2035     /*
2036      * Set new list item data member
2037      */
2038     newLock->pUnk      = pUnk;
2039     newLock->uRefCount = 1;
2040     newLock->next      = previousHead;
2041
2042     return TRUE;
2043   }
2044   else
2045     return FALSE;
2046 }
2047
2048 /****************************************************************************
2049  * Internal - Method that removes an item from the linked list.
2050  */
2051 static void COM_ExternalLockDelete(
2052   COM_ExternalLock *itemList)
2053 {
2054   COM_ExternalLock *current = elList.head;
2055
2056   if ( current == itemList )
2057   {
2058     /*
2059      * this section handles the deletion of the first node
2060      */
2061     elList.head = itemList->next;
2062     HeapFree( GetProcessHeap(), 0, itemList);
2063   }
2064   else
2065   {
2066     do
2067     {
2068       if ( current->next == itemList )   /* We found the item to free  */
2069       {
2070         current->next = itemList->next;  /* readjust the list pointers */
2071
2072         HeapFree( GetProcessHeap(), 0, itemList);
2073         break;
2074       }
2075
2076       /* Skip to the next item */
2077       current = current->next;
2078
2079     } while ( current != EL_END_OF_LIST );
2080   }
2081 }
2082
2083 /***********************************************************************
2084  *      DllEntryPoint                   [COMPOBJ.116]
2085  *
2086  *    Initialization code for the COMPOBJ DLL
2087  *
2088  * RETURNS:
2089  */
2090 BOOL WINAPI COMPOBJ_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst, WORD ds, WORD HeapSize, DWORD res1, WORD res2)
2091 {
2092         TRACE("(%08lx, %04x, %04x, %04x, %08lx, %04x)\n", Reason, hInst, ds, HeapSize,
2093  res1, res2);
2094         switch(Reason)
2095         {
2096         case DLL_PROCESS_ATTACH:
2097                 if (!COMPOBJ_Attach++) COMPOBJ_hInstance = hInst;
2098                 break;
2099
2100         case DLL_PROCESS_DETACH:
2101                 if(!--COMPOBJ_Attach)
2102                         COMPOBJ_hInstance = 0;
2103                 break;
2104         }
2105         return TRUE;
2106 }
2107
2108 /******************************************************************************
2109  *              OleGetAutoConvert        [OLE32.104]
2110  */
2111 HRESULT WINAPI OleGetAutoConvert(REFCLSID clsidOld, LPCLSID pClsidNew)
2112 {
2113     HKEY hkey = 0;
2114     char buf[200];
2115     WCHAR wbuf[200];
2116     DWORD len;
2117     HRESULT res = S_OK;
2118
2119     sprintf(buf,"CLSID\\");WINE_StringFromCLSID(clsidOld,&buf[6]);
2120     if (RegOpenKeyA(HKEY_CLASSES_ROOT,buf,&hkey))
2121     {
2122         res = REGDB_E_CLASSNOTREG;
2123         goto done;
2124     }
2125     len = 200;
2126     /* we can just query for the default value of AutoConvertTo key like that,
2127        without opening the AutoConvertTo key and querying for NULL (default) */
2128     if (RegQueryValueA(hkey,"AutoConvertTo",buf,&len))
2129     {
2130         res = REGDB_E_KEYMISSING;
2131         goto done;
2132     }
2133     MultiByteToWideChar( CP_ACP, 0, buf, -1, wbuf, sizeof(wbuf)/sizeof(WCHAR) );
2134     CLSIDFromString(wbuf,pClsidNew);
2135 done:
2136   if (hkey) RegCloseKey(hkey);
2137
2138   return res;
2139 }
2140
2141 /******************************************************************************
2142  *              OleSetAutoConvert        [OLE32.126]
2143  */
2144 HRESULT WINAPI OleSetAutoConvert(REFCLSID clsidOld, REFCLSID clsidNew)
2145 {
2146     HKEY hkey = 0;
2147     char buf[200], szClsidNew[200];
2148     HRESULT res = S_OK;
2149
2150     TRACE("(%s,%s)\n", debugstr_guid(clsidOld), debugstr_guid(clsidNew));
2151     sprintf(buf,"CLSID\\");WINE_StringFromCLSID(clsidOld,&buf[6]);
2152     WINE_StringFromCLSID(clsidNew, szClsidNew);
2153     if (RegOpenKeyA(HKEY_CLASSES_ROOT,buf,&hkey))
2154     {
2155         res = REGDB_E_CLASSNOTREG;
2156         goto done;
2157     }
2158     if (RegSetValueA(hkey, "AutoConvertTo", REG_SZ, szClsidNew, strlen(szClsidNew)+1))
2159     {
2160         res = REGDB_E_WRITEREGDB;
2161         goto done;
2162     }
2163
2164 done:
2165     if (hkey) RegCloseKey(hkey);
2166     return res;
2167 }
2168
2169 /******************************************************************************
2170  *              CoTreatAsClass        [OLE32.46]
2171  */
2172 HRESULT WINAPI CoTreatAsClass(REFCLSID clsidOld, REFCLSID clsidNew)
2173 {
2174     HKEY hkey = 0;
2175     char buf[200], szClsidNew[200];
2176     HRESULT res = S_OK;
2177
2178     FIXME("(%s,%s)\n", debugstr_guid(clsidOld), debugstr_guid(clsidNew));
2179     sprintf(buf,"CLSID\\");WINE_StringFromCLSID(clsidOld,&buf[6]);
2180     WINE_StringFromCLSID(clsidNew, szClsidNew);
2181     if (RegOpenKeyA(HKEY_CLASSES_ROOT,buf,&hkey))
2182     {
2183         res = REGDB_E_CLASSNOTREG;
2184         goto done;
2185     }
2186     if (RegSetValueA(hkey, "AutoTreatAs", REG_SZ, szClsidNew, strlen(szClsidNew)+1))
2187     {
2188         res = REGDB_E_WRITEREGDB;
2189         goto done;
2190     }
2191
2192 done:
2193     if (hkey) RegCloseKey(hkey);
2194     return res;
2195 }
2196
2197
2198 /***********************************************************************
2199  *           IsEqualGUID [OLE32.76]
2200  *
2201  * Compares two Unique Identifiers.
2202  *
2203  * RETURNS
2204  *      TRUE if equal
2205  */
2206 #undef IsEqualGUID
2207 BOOL WINAPI IsEqualGUID(
2208      REFGUID rguid1, /* [in] unique id 1 */
2209      REFGUID rguid2  /* [in] unique id 2 */
2210      )
2211 {
2212     return !memcmp(rguid1,rguid2,sizeof(GUID));
2213 }