Added the DFCS_{HOT,TRANSPARENT} definitions.
[wine/gsoc_dplay.git] / dlls / ole32 / compobj.c
blobe58b00a3a2b838b1748510f6627b2d3dcb16107b
1 /*
2 * COMPOBJ library
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
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "config.h"
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <assert.h>
32 #include "windef.h"
33 #include "objbase.h"
34 #include "ole2.h"
35 #include "ole2ver.h"
36 #include "rpc.h"
37 #include "winerror.h"
38 #include "winreg.h"
39 #include "wownt32.h"
40 #include "wtypes.h"
41 #include "wine/unicode.h"
42 #include "wine/obj_base.h"
43 #include "wine/obj_clientserver.h"
44 #include "wine/obj_misc.h"
45 #include "wine/obj_marshal.h"
46 #include "wine/obj_storage.h"
47 #include "wine/obj_channel.h"
48 #include "wine/winbase16.h"
49 #include "compobj_private.h"
50 #include "ifs.h"
52 #include "wine/debug.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(ole);
56 /****************************************************************************
57 * COM External Lock structures and methods declaration
59 * This api provides a linked list to managed external references to
60 * COM objects.
62 * The public interface consists of three calls:
63 * COM_ExternalLockAddRef
64 * COM_ExternalLockRelease
65 * COM_ExternalLockFreeList
68 #define EL_END_OF_LIST 0
69 #define EL_NOT_FOUND 0
72 * Declaration of the static structure that manage the
73 * external lock to COM objects.
75 typedef struct COM_ExternalLock COM_ExternalLock;
76 typedef struct COM_ExternalLockList COM_ExternalLockList;
78 struct COM_ExternalLock
80 IUnknown *pUnk; /* IUnknown referenced */
81 ULONG uRefCount; /* external lock counter to IUnknown object*/
82 COM_ExternalLock *next; /* Pointer to next element in list */
85 struct COM_ExternalLockList
87 COM_ExternalLock *head; /* head of list */
91 * Declaration and initialization of the static structure that manages
92 * the external lock to COM objects.
94 static COM_ExternalLockList elList = { EL_END_OF_LIST };
97 * Public Interface to the external lock list
99 static void COM_ExternalLockFreeList();
100 static void COM_ExternalLockAddRef(IUnknown *pUnk);
101 static void COM_ExternalLockRelease(IUnknown *pUnk, BOOL bRelAll);
102 void COM_ExternalLockDump(); /* testing purposes, not static to avoid warning */
105 * Private methods used to managed the linked list
107 static BOOL COM_ExternalLockInsert(
108 IUnknown *pUnk);
110 static void COM_ExternalLockDelete(
111 COM_ExternalLock *element);
113 static COM_ExternalLock* COM_ExternalLockFind(
114 IUnknown *pUnk);
116 static COM_ExternalLock* COM_ExternalLockLocate(
117 COM_ExternalLock *element,
118 IUnknown *pUnk);
120 /****************************************************************************
121 * This section defines variables internal to the COM module.
123 * TODO: Most of these things will have to be made thread-safe.
125 HINSTANCE16 COMPOBJ_hInstance = 0;
126 HINSTANCE COMPOBJ_hInstance32 = 0;
127 static int COMPOBJ_Attach = 0;
129 LPMALLOC16 currentMalloc16=NULL;
130 LPMALLOC currentMalloc32=NULL;
132 HTASK16 hETask = 0;
133 WORD Table_ETask[62];
136 * This lock count counts the number of times CoInitialize is called. It is
137 * decreased every time CoUninitialize is called. When it hits 0, the COM
138 * libraries are freed
140 static LONG s_COMLockCount = 0;
143 * This linked list contains the list of registered class objects. These
144 * are mostly used to register the factories for out-of-proc servers of OLE
145 * objects.
147 * TODO: Make this data structure aware of inter-process communication. This
148 * means that parts of this will be exported to the Wine Server.
150 typedef struct tagRegisteredClass
152 CLSID classIdentifier;
153 LPUNKNOWN classObject;
154 DWORD runContext;
155 DWORD connectFlags;
156 DWORD dwCookie;
157 HANDLE hThread; /* only for localserver */
158 struct tagRegisteredClass* nextClass;
159 } RegisteredClass;
161 static CRITICAL_SECTION csRegisteredClassList;
162 static RegisteredClass* firstRegisteredClass = NULL;
164 /* this open DLL table belongs in a per process table, but my guess is that
165 * it shouldn't live in the kernel, so I'll put them out here in DLL
166 * space assuming that there is one OLE32 per process.
168 typedef struct tagOpenDll {
169 HINSTANCE hLibrary;
170 struct tagOpenDll *next;
171 } OpenDll;
173 static CRITICAL_SECTION csOpenDllList;
174 static OpenDll *openDllList = NULL; /* linked list of open dlls */
176 /*****************************************************************************
177 * This section contains prototypes to internal methods for this
178 * module
180 static HRESULT COM_GetRegisteredClassObject(REFCLSID rclsid,
181 DWORD dwClsContext,
182 LPUNKNOWN* ppUnk);
184 static void COM_RevokeAllClasses();
187 /******************************************************************************
188 * Initialize/Uninitialize critical sections.
190 void COMPOBJ_InitProcess( void )
192 InitializeCriticalSection( &csRegisteredClassList );
193 InitializeCriticalSection( &csOpenDllList );
196 void COMPOBJ_UninitProcess( void )
198 DeleteCriticalSection( &csRegisteredClassList );
199 DeleteCriticalSection( &csOpenDllList );
202 /******************************************************************************
203 * CoBuildVersion [COMPOBJ.1]
204 * CoBuildVersion [OLE32.4]
206 * RETURNS
207 * Current build version, hiword is majornumber, loword is minornumber
209 DWORD WINAPI CoBuildVersion(void)
211 TRACE("Returning version %d, build %d.\n", rmm, rup);
212 return (rmm<<16)+rup;
215 /******************************************************************************
216 * CoInitialize [COMPOBJ.2]
217 * Set the win16 IMalloc used for memory management
219 HRESULT WINAPI CoInitialize16(
220 LPVOID lpReserved /* [in] pointer to win16 malloc interface */
222 currentMalloc16 = (LPMALLOC16)lpReserved;
223 return S_OK;
226 /******************************************************************************
227 * CoInitialize [OLE32.26]
229 * Initializes the COM libraries.
231 * See CoInitializeEx
233 HRESULT WINAPI CoInitialize(
234 LPVOID lpReserved /* [in] pointer to win32 malloc interface
235 (obsolete, should be NULL) */
239 * Just delegate to the newer method.
241 return CoInitializeEx(lpReserved, COINIT_APARTMENTTHREADED);
244 /******************************************************************************
245 * CoInitializeEx [OLE32.163]
247 * Initializes the COM libraries. The behavior used to set the win32 IMalloc
248 * used for memory management is obsolete.
250 * RETURNS
251 * S_OK if successful,
252 * S_FALSE if this function was called already.
253 * RPC_E_CHANGED_MODE if a previous call to CoInitialize specified another
254 * threading model.
256 * BUGS
257 * Only the single threaded model is supported. As a result RPC_E_CHANGED_MODE
258 * is never returned.
260 * See the windows documentation for more details.
262 HRESULT WINAPI CoInitializeEx(
263 LPVOID lpReserved, /* [in] pointer to win32 malloc interface
264 (obsolete, should be NULL) */
265 DWORD dwCoInit /* [in] A value from COINIT specifies the threading model */
268 HRESULT hr;
270 TRACE("(%p, %x)\n", lpReserved, (int)dwCoInit);
272 if (lpReserved!=NULL)
274 ERR("(%p, %x) - Bad parameter passed-in %p, must be an old Windows Application\n", lpReserved, (int)dwCoInit, lpReserved);
278 * Check for unsupported features.
280 if (dwCoInit!=COINIT_APARTMENTTHREADED)
282 FIXME(":(%p,%x): unsupported flag %x\n", lpReserved, (int)dwCoInit, (int)dwCoInit);
283 /* Hope for the best and continue anyway */
287 * Check the lock count. If this is the first time going through the initialize
288 * process, we have to initialize the libraries.
290 * And crank-up that lock count.
292 if (InterlockedExchangeAdd(&s_COMLockCount,1)==0)
295 * Initialize the various COM libraries and data structures.
297 TRACE("() - Initializing the COM libraries\n");
300 RunningObjectTableImpl_Initialize();
302 hr = S_OK;
304 else
305 hr = S_FALSE;
307 return hr;
310 /***********************************************************************
311 * CoUninitialize [COMPOBJ.3]
312 * Don't know what it does.
313 * 3-Nov-98 -- this was originally misspelled, I changed it to what I
314 * believe is the correct spelling
316 void WINAPI CoUninitialize16(void)
318 TRACE("()\n");
319 CoFreeAllLibraries();
322 /***********************************************************************
323 * CoUninitialize [OLE32.47]
325 * This method will release the COM libraries.
327 * See the windows documentation for more details.
329 void WINAPI CoUninitialize(void)
331 LONG lCOMRefCnt;
332 TRACE("()\n");
335 * Decrease the reference count.
336 * If we are back to 0 locks on the COM library, make sure we free
337 * all the associated data structures.
339 lCOMRefCnt = InterlockedExchangeAdd(&s_COMLockCount,-1);
340 if (lCOMRefCnt==1)
343 * Release the various COM libraries and data structures.
345 TRACE("() - Releasing the COM libraries\n");
347 RunningObjectTableImpl_UnInitialize();
349 * Release the references to the registered class objects.
351 COM_RevokeAllClasses();
354 * This will free the loaded COM Dlls.
356 CoFreeAllLibraries();
359 * This will free list of external references to COM objects.
361 COM_ExternalLockFreeList();
364 else if (lCOMRefCnt<1) {
365 ERR( "CoUninitialize() - not CoInitialized.\n" );
366 InterlockedExchangeAdd(&s_COMLockCount,1); /* restore the lock count. */
370 /***********************************************************************
371 * CoGetMalloc [COMPOBJ.4]
372 * RETURNS
373 * The current win16 IMalloc
375 HRESULT WINAPI CoGetMalloc16(
376 DWORD dwMemContext, /* [in] unknown */
377 LPMALLOC16 * lpMalloc /* [out] current win16 malloc interface */
379 if(!currentMalloc16)
380 currentMalloc16 = IMalloc16_Constructor();
381 *lpMalloc = currentMalloc16;
382 return S_OK;
385 /******************************************************************************
386 * CoGetMalloc [OLE32.20]
388 * RETURNS
389 * The current win32 IMalloc
391 HRESULT WINAPI CoGetMalloc(
392 DWORD dwMemContext, /* [in] unknown */
393 LPMALLOC *lpMalloc /* [out] current win32 malloc interface */
395 if(!currentMalloc32)
396 currentMalloc32 = IMalloc_Constructor();
397 *lpMalloc = currentMalloc32;
398 return S_OK;
401 /***********************************************************************
402 * CoCreateStandardMalloc [COMPOBJ.71]
404 HRESULT WINAPI CoCreateStandardMalloc16(DWORD dwMemContext,
405 LPMALLOC16 *lpMalloc)
407 /* FIXME: docu says we shouldn't return the same allocator as in
408 * CoGetMalloc16 */
409 *lpMalloc = IMalloc16_Constructor();
410 return S_OK;
413 /******************************************************************************
414 * CoDisconnectObject [COMPOBJ.15]
415 * CoDisconnectObject [OLE32.8]
417 HRESULT WINAPI CoDisconnectObject( LPUNKNOWN lpUnk, DWORD reserved )
419 TRACE("(%p, %lx)\n",lpUnk,reserved);
420 return S_OK;
423 /***********************************************************************
424 * IsEqualGUID [COMPOBJ.18]
426 * Compares two Unique Identifiers.
428 * RETURNS
429 * TRUE if equal
431 BOOL16 WINAPI IsEqualGUID16(
432 GUID* g1, /* [in] unique id 1 */
433 GUID* g2 /* [in] unique id 2 */
435 return !memcmp( g1, g2, sizeof(GUID) );
438 /******************************************************************************
439 * CLSIDFromString [COMPOBJ.20]
440 * Converts a unique identifier from its string representation into
441 * the GUID struct.
443 * Class id: DWORD-WORD-WORD-BYTES[2]-BYTES[6]
445 * RETURNS
446 * the converted GUID
448 HRESULT WINAPI CLSIDFromString16(
449 LPCOLESTR16 idstr, /* [in] string representation of guid */
450 CLSID *id /* [out] GUID converted from string */
452 BYTE *s = (BYTE *) idstr;
453 BYTE *p;
454 int i;
455 BYTE table[256];
457 if (!s)
458 s = "{00000000-0000-0000-0000-000000000000}";
459 else { /* validate the CLSID string */
461 if (strlen(s) != 38)
462 return CO_E_CLASSSTRING;
464 if ((s[0]!='{') || (s[9]!='-') || (s[14]!='-') || (s[19]!='-') || (s[24]!='-') || (s[37]!='}'))
465 return CO_E_CLASSSTRING;
467 for (i=1; i<37; i++)
469 if ((i == 9)||(i == 14)||(i == 19)||(i == 24)) continue;
470 if (!(((s[i] >= '0') && (s[i] <= '9')) ||
471 ((s[i] >= 'a') && (s[i] <= 'f')) ||
472 ((s[i] >= 'A') && (s[i] <= 'F')))
474 return CO_E_CLASSSTRING;
478 TRACE("%s -> %p\n", s, id);
480 /* quick lookup table */
481 memset(table, 0, 256);
483 for (i = 0; i < 10; i++) {
484 table['0' + i] = i;
486 for (i = 0; i < 6; i++) {
487 table['A' + i] = i+10;
488 table['a' + i] = i+10;
491 /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
493 p = (BYTE *) id;
495 s++; /* skip leading brace */
496 for (i = 0; i < 4; i++) {
497 p[3 - i] = table[*s]<<4 | table[*(s+1)];
498 s += 2;
500 p += 4;
501 s++; /* skip - */
503 for (i = 0; i < 2; i++) {
504 p[1-i] = table[*s]<<4 | table[*(s+1)];
505 s += 2;
507 p += 2;
508 s++; /* skip - */
510 for (i = 0; i < 2; i++) {
511 p[1-i] = table[*s]<<4 | table[*(s+1)];
512 s += 2;
514 p += 2;
515 s++; /* skip - */
517 /* these are just sequential bytes */
518 for (i = 0; i < 2; i++) {
519 *p++ = table[*s]<<4 | table[*(s+1)];
520 s += 2;
522 s++; /* skip - */
524 for (i = 0; i < 6; i++) {
525 *p++ = table[*s]<<4 | table[*(s+1)];
526 s += 2;
529 return S_OK;
532 /******************************************************************************
533 * CoCreateGuid[OLE32.6]
536 HRESULT WINAPI CoCreateGuid(
537 GUID *pguid /* [out] points to the GUID to initialize */
539 return UuidCreate(pguid);
542 /******************************************************************************
543 * CLSIDFromString [OLE32.3]
544 * IIDFromString [OLE32.74]
545 * Converts a unique identifier from its string representation into
546 * the GUID struct.
548 * UNDOCUMENTED
549 * If idstr is not a valid CLSID string then it gets treated as a ProgID
551 * RETURNS
552 * the converted GUID
554 HRESULT WINAPI CLSIDFromString(
555 LPCOLESTR idstr, /* [in] string representation of GUID */
556 CLSID *id ) /* [out] GUID represented by above string */
558 char xid[40];
559 HRESULT ret;
561 if (!WideCharToMultiByte( CP_ACP, 0, idstr, -1, xid, sizeof(xid), NULL, NULL ))
562 return CO_E_CLASSSTRING;
563 ret = CLSIDFromString16(xid,id);
564 if(ret != S_OK) { /* It appears a ProgID is also valid */
565 ret = CLSIDFromProgID(idstr, id);
567 return ret;
570 /******************************************************************************
571 * WINE_StringFromCLSID [Internal]
572 * Converts a GUID into the respective string representation.
574 * NOTES
576 * RETURNS
577 * the string representation and HRESULT
579 HRESULT WINE_StringFromCLSID(
580 const CLSID *id, /* [in] GUID to be converted */
581 LPSTR idstr /* [out] pointer to buffer to contain converted guid */
583 static const char *hex = "0123456789ABCDEF";
584 char *s;
585 int i;
587 if (!id)
588 { ERR("called with id=Null\n");
589 *idstr = 0x00;
590 return E_FAIL;
593 sprintf(idstr, "{%08lX-%04X-%04X-%02X%02X-",
594 id->Data1, id->Data2, id->Data3,
595 id->Data4[0], id->Data4[1]);
596 s = &idstr[25];
598 /* 6 hex bytes */
599 for (i = 2; i < 8; i++) {
600 *s++ = hex[id->Data4[i]>>4];
601 *s++ = hex[id->Data4[i] & 0xf];
604 *s++ = '}';
605 *s++ = '\0';
607 TRACE("%p->%s\n", id, idstr);
609 return S_OK;
612 /******************************************************************************
613 * StringFromCLSID [COMPOBJ.19]
614 * Converts a GUID into the respective string representation.
615 * The target string is allocated using the OLE IMalloc.
616 * RETURNS
617 * the string representation and HRESULT
619 HRESULT WINAPI StringFromCLSID16(
620 REFCLSID id, /* [in] the GUID to be converted */
621 LPOLESTR16 *idstr /* [out] a pointer to a to-be-allocated segmented pointer pointing to the resulting string */
624 extern BOOL WINAPI K32WOWCallback16Ex( DWORD vpfn16, DWORD dwFlags,
625 DWORD cbArgs, LPVOID pArgs, LPDWORD pdwRetCode );
626 LPMALLOC16 mllc;
627 HRESULT ret;
628 DWORD args[2];
630 ret = CoGetMalloc16(0,&mllc);
631 if (ret) return ret;
633 args[0] = (DWORD)mllc;
634 args[1] = 40;
636 /* No need for a Callback entry, we have WOWCallback16Ex which does
637 * everything we need.
639 if (!K32WOWCallback16Ex(
640 (DWORD)((ICOM_VTABLE(IMalloc16)*)MapSL(
641 (SEGPTR)ICOM_VTBL(((LPMALLOC16)MapSL((SEGPTR)mllc))))
642 )->Alloc,
643 WCB16_CDECL,
644 2*sizeof(DWORD),
645 (LPVOID)args,
646 (LPDWORD)idstr
647 )) {
648 WARN("CallTo16 IMalloc16 failed\n");
649 return E_FAIL;
651 return WINE_StringFromCLSID(id,MapSL((SEGPTR)*idstr));
654 /******************************************************************************
655 * StringFromCLSID [OLE32.151]
656 * StringFromIID [OLE32.153]
657 * Converts a GUID into the respective string representation.
658 * The target string is allocated using the OLE IMalloc.
659 * RETURNS
660 * the string representation and HRESULT
662 HRESULT WINAPI StringFromCLSID(
663 REFCLSID id, /* [in] the GUID to be converted */
664 LPOLESTR *idstr /* [out] a pointer to a to-be-allocated pointer pointing to the resulting string */
666 char buf[80];
667 HRESULT ret;
668 LPMALLOC mllc;
670 if ((ret=CoGetMalloc(0,&mllc)))
671 return ret;
673 ret=WINE_StringFromCLSID(id,buf);
674 if (!ret) {
675 DWORD len = MultiByteToWideChar( CP_ACP, 0, buf, -1, NULL, 0 );
676 *idstr = IMalloc_Alloc( mllc, len * sizeof(WCHAR) );
677 MultiByteToWideChar( CP_ACP, 0, buf, -1, *idstr, len );
679 return ret;
682 /******************************************************************************
683 * StringFromGUID2 [COMPOBJ.76]
684 * StringFromGUID2 [OLE32.152]
686 * Converts a global unique identifier into a string of an API-
687 * specified fixed format. (The usual {.....} stuff.)
689 * RETURNS
690 * The (UNICODE) string representation of the GUID in 'str'
691 * The length of the resulting string, 0 if there was any problem.
693 INT WINAPI
694 StringFromGUID2(REFGUID id, LPOLESTR str, INT cmax)
696 char xguid[80];
698 if (WINE_StringFromCLSID(id,xguid))
699 return 0;
700 return MultiByteToWideChar( CP_ACP, 0, xguid, -1, str, cmax );
703 /******************************************************************************
704 * ProgIDFromCLSID [OLE32.133]
705 * Converts a class id into the respective Program ID. (By using a registry lookup)
706 * RETURNS S_OK on success
707 * riid associated with the progid
710 HRESULT WINAPI ProgIDFromCLSID(
711 REFCLSID clsid, /* [in] class id as found in registry */
712 LPOLESTR *lplpszProgID/* [out] associated Prog ID */
715 char strCLSID[50], *buf, *buf2;
716 DWORD buf2len;
717 HKEY xhkey;
718 LPMALLOC mllc;
719 HRESULT ret = S_OK;
721 WINE_StringFromCLSID(clsid, strCLSID);
723 buf = HeapAlloc(GetProcessHeap(), 0, strlen(strCLSID)+14);
724 sprintf(buf,"CLSID\\%s\\ProgID", strCLSID);
725 if (RegOpenKeyA(HKEY_CLASSES_ROOT, buf, &xhkey))
726 ret = REGDB_E_CLASSNOTREG;
728 HeapFree(GetProcessHeap(), 0, buf);
730 if (ret == S_OK)
732 buf2 = HeapAlloc(GetProcessHeap(), 0, 255);
733 buf2len = 255;
734 if (RegQueryValueA(xhkey, NULL, buf2, &buf2len))
735 ret = REGDB_E_CLASSNOTREG;
737 if (ret == S_OK)
739 if (CoGetMalloc(0,&mllc))
740 ret = E_OUTOFMEMORY;
741 else
743 DWORD len = MultiByteToWideChar( CP_ACP, 0, buf2, -1, NULL, 0 );
744 *lplpszProgID = IMalloc_Alloc(mllc, len * sizeof(WCHAR) );
745 MultiByteToWideChar( CP_ACP, 0, buf2, -1, *lplpszProgID, len );
748 HeapFree(GetProcessHeap(), 0, buf2);
751 RegCloseKey(xhkey);
752 return ret;
755 /******************************************************************************
756 * CLSIDFromProgID [COMPOBJ.61]
757 * Converts a program id into the respective GUID. (By using a registry lookup)
758 * RETURNS
759 * riid associated with the progid
761 HRESULT WINAPI CLSIDFromProgID16(
762 LPCOLESTR16 progid, /* [in] program id as found in registry */
763 LPCLSID riid /* [out] associated CLSID */
765 char *buf,buf2[80];
766 DWORD buf2len;
767 HRESULT err;
768 HKEY xhkey;
770 buf = HeapAlloc(GetProcessHeap(),0,strlen(progid)+8);
771 sprintf(buf,"%s\\CLSID",progid);
772 if ((err=RegOpenKeyA(HKEY_CLASSES_ROOT,buf,&xhkey))) {
773 HeapFree(GetProcessHeap(),0,buf);
774 return CO_E_CLASSSTRING;
776 HeapFree(GetProcessHeap(),0,buf);
777 buf2len = sizeof(buf2);
778 if ((err=RegQueryValueA(xhkey,NULL,buf2,&buf2len))) {
779 RegCloseKey(xhkey);
780 return CO_E_CLASSSTRING;
782 RegCloseKey(xhkey);
783 return CLSIDFromString16(buf2,riid);
786 /******************************************************************************
787 * CLSIDFromProgID [OLE32.2]
788 * Converts a program id into the respective GUID. (By using a registry lookup)
789 * RETURNS
790 * riid associated with the progid
792 HRESULT WINAPI CLSIDFromProgID(
793 LPCOLESTR progid, /* [in] program id as found in registry */
794 LPCLSID riid ) /* [out] associated CLSID */
796 static const WCHAR clsidW[] = { '\\','C','L','S','I','D',0 };
797 char buf2[80];
798 DWORD buf2len = sizeof(buf2);
799 HKEY xhkey;
801 WCHAR *buf = HeapAlloc( GetProcessHeap(),0,(strlenW(progid)+8) * sizeof(WCHAR) );
802 strcpyW( buf, progid );
803 strcatW( buf, clsidW );
804 if (RegOpenKeyW(HKEY_CLASSES_ROOT,buf,&xhkey))
806 HeapFree(GetProcessHeap(),0,buf);
807 return CO_E_CLASSSTRING;
809 HeapFree(GetProcessHeap(),0,buf);
811 if (RegQueryValueA(xhkey,NULL,buf2,&buf2len))
813 RegCloseKey(xhkey);
814 return CO_E_CLASSSTRING;
816 RegCloseKey(xhkey);
817 return CLSIDFromString16(buf2,riid);
822 /*****************************************************************************
823 * CoGetPSClsid [OLE32.22]
825 * This function returns the CLSID of the DLL that implements the proxy and stub
826 * for the specified interface.
828 * It determines this by searching the
829 * HKEY_CLASSES_ROOT\Interface\{string form of riid}\ProxyStubClsid32 in the registry
830 * and any interface id registered by CoRegisterPSClsid within the current process.
832 * FIXME: We only search the registry, not ids registered with CoRegisterPSClsid.
834 HRESULT WINAPI CoGetPSClsid(
835 REFIID riid, /* [in] Interface whose proxy/stub CLSID is to be returned */
836 CLSID *pclsid ) /* [out] Where to store returned proxy/stub CLSID */
838 char *buf, buf2[40];
839 DWORD buf2len;
840 HKEY xhkey;
842 TRACE("() riid=%s, pclsid=%p\n", debugstr_guid(riid), pclsid);
844 /* Get the input iid as a string */
845 WINE_StringFromCLSID(riid, buf2);
846 /* Allocate memory for the registry key we will construct.
847 (length of iid string plus constant length of static text */
848 buf = HeapAlloc(GetProcessHeap(), 0, strlen(buf2)+27);
849 if (buf == NULL)
851 return (E_OUTOFMEMORY);
854 /* Construct the registry key we want */
855 sprintf(buf,"Interface\\%s\\ProxyStubClsid32", buf2);
857 /* Open the key.. */
858 if (RegOpenKeyA(HKEY_CLASSES_ROOT, buf, &xhkey))
860 HeapFree(GetProcessHeap(),0,buf);
861 return (E_INVALIDARG);
863 HeapFree(GetProcessHeap(),0,buf);
865 /* ... Once we have the key, query the registry to get the
866 value of CLSID as a string, and convert it into a
867 proper CLSID structure to be passed back to the app */
868 buf2len = sizeof(buf2);
869 if ( (RegQueryValueA(xhkey,NULL,buf2,&buf2len)) )
871 RegCloseKey(xhkey);
872 return E_INVALIDARG;
874 RegCloseKey(xhkey);
876 /* We have the CLSid we want back from the registry as a string, so
877 lets convert it into a CLSID structure */
878 if ( (CLSIDFromString16(buf2,pclsid)) != NOERROR)
880 return E_INVALIDARG;
883 TRACE ("() Returning CLSID=%s\n", debugstr_guid(pclsid));
884 return (S_OK);
889 /***********************************************************************
890 * WriteClassStm (OLE32.159)
892 * This function write a CLSID on stream
894 HRESULT WINAPI WriteClassStm(IStream *pStm,REFCLSID rclsid)
896 TRACE("(%p,%p)\n",pStm,rclsid);
898 if (rclsid==NULL)
899 return E_INVALIDARG;
901 return IStream_Write(pStm,rclsid,sizeof(CLSID),NULL);
904 /***********************************************************************
905 * ReadClassStm (OLE32.135)
907 * This function read a CLSID from a stream
909 HRESULT WINAPI ReadClassStm(IStream *pStm,CLSID *pclsid)
911 ULONG nbByte;
912 HRESULT res;
914 TRACE("(%p,%p)\n",pStm,pclsid);
916 if (pclsid==NULL)
917 return E_INVALIDARG;
919 res = IStream_Read(pStm,(void*)pclsid,sizeof(CLSID),&nbByte);
921 if (FAILED(res))
922 return res;
924 if (nbByte != sizeof(CLSID))
925 return S_FALSE;
926 else
927 return S_OK;
930 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
931 /***********************************************************************
932 * LookupETask (COMPOBJ.94)
934 HRESULT WINAPI LookupETask16(HTASK16 *hTask,LPVOID p) {
935 FIXME("(%p,%p),stub!\n",hTask,p);
936 if ((*hTask = GetCurrentTask()) == hETask) {
937 memcpy(p, Table_ETask, sizeof(Table_ETask));
939 return 0;
942 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
943 /***********************************************************************
944 * SetETask (COMPOBJ.95)
946 HRESULT WINAPI SetETask16(HTASK16 hTask, LPVOID p) {
947 FIXME("(%04x,%p),stub!\n",hTask,p);
948 hETask = hTask;
949 return 0;
952 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
953 /***********************************************************************
954 * CALLOBJECTINWOW (COMPOBJ.201)
956 HRESULT WINAPI CallObjectInWOW(LPVOID p1,LPVOID p2) {
957 FIXME("(%p,%p),stub!\n",p1,p2);
958 return 0;
961 /******************************************************************************
962 * CoRegisterClassObject [COMPOBJ.5]
964 * Don't know where it registers it ...
966 HRESULT WINAPI CoRegisterClassObject16(
967 REFCLSID rclsid,
968 LPUNKNOWN pUnk,
969 DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
970 DWORD flags, /* [in] REGCLS flags indicating how connections are made */
971 LPDWORD lpdwRegister
973 char buf[80];
975 WINE_StringFromCLSID(rclsid,buf);
977 FIXME("(%s,%p,0x%08lx,0x%08lx,%p),stub\n",
978 buf,pUnk,dwClsContext,flags,lpdwRegister
980 return 0;
984 /******************************************************************************
985 * CoRevokeClassObject [COMPOBJ.6]
988 HRESULT WINAPI CoRevokeClassObject16(DWORD dwRegister) /* [in] token on class obj */
990 FIXME("(0x%08lx),stub!\n", dwRegister);
991 return 0;
994 /******************************************************************************
995 * CoFileTimeToDosDateTime [COMPOBJ.30]
997 BOOL16 WINAPI CoFileTimeToDosDateTime16(const FILETIME *ft, LPWORD lpDosDate, LPWORD lpDosTime)
999 return FileTimeToDosDateTime(ft, lpDosDate, lpDosTime);
1002 /******************************************************************************
1003 * CoDosDateTimeToFileTime [COMPOBJ.31]
1005 BOOL16 WINAPI CoDosDateTimeToFileTime16(WORD wDosDate, WORD wDosTime, FILETIME *ft)
1007 return DosDateTimeToFileTime(wDosDate, wDosTime, ft);
1010 /***
1011 * COM_GetRegisteredClassObject
1013 * This internal method is used to scan the registered class list to
1014 * find a class object.
1016 * Params:
1017 * rclsid Class ID of the class to find.
1018 * dwClsContext Class context to match.
1019 * ppv [out] returns a pointer to the class object. Complying
1020 * to normal COM usage, this method will increase the
1021 * reference count on this object.
1023 static HRESULT COM_GetRegisteredClassObject(
1024 REFCLSID rclsid,
1025 DWORD dwClsContext,
1026 LPUNKNOWN* ppUnk)
1028 HRESULT hr = S_FALSE;
1029 RegisteredClass* curClass;
1031 EnterCriticalSection( &csRegisteredClassList );
1034 * Sanity check
1036 assert(ppUnk!=0);
1039 * Iterate through the whole list and try to match the class ID.
1041 curClass = firstRegisteredClass;
1043 while (curClass != 0)
1046 * Check if we have a match on the class ID.
1048 if (IsEqualGUID(&(curClass->classIdentifier), rclsid))
1051 * Since we don't do out-of process or DCOM just right away, let's ignore the
1052 * class context.
1056 * We have a match, return the pointer to the class object.
1058 *ppUnk = curClass->classObject;
1060 IUnknown_AddRef(curClass->classObject);
1062 hr = S_OK;
1063 goto end;
1067 * Step to the next class in the list.
1069 curClass = curClass->nextClass;
1072 end:
1073 LeaveCriticalSection( &csRegisteredClassList );
1075 * If we get to here, we haven't found our class.
1077 return hr;
1080 static DWORD WINAPI
1081 _LocalServerThread(LPVOID param) {
1082 HANDLE hPipe;
1083 char pipefn[200];
1084 RegisteredClass *newClass = (RegisteredClass*)param;
1085 HRESULT hres;
1086 IStream *pStm;
1087 STATSTG ststg;
1088 unsigned char *buffer;
1089 int buflen;
1090 IClassFactory *classfac;
1091 LARGE_INTEGER seekto;
1092 ULARGE_INTEGER newpos;
1093 ULONG res;
1095 TRACE("Starting threader for %s.\n",debugstr_guid(&newClass->classIdentifier));
1096 strcpy(pipefn,PIPEPREF);
1097 WINE_StringFromCLSID(&newClass->classIdentifier,pipefn+strlen(PIPEPREF));
1099 hres = IUnknown_QueryInterface(newClass->classObject,&IID_IClassFactory,(LPVOID*)&classfac);
1100 if (hres) return hres;
1102 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
1103 if (hres) {
1104 FIXME("Failed to create stream on hglobal.\n");
1105 return hres;
1107 hres = CoMarshalInterface(pStm,&IID_IClassFactory,(LPVOID)classfac,0,NULL,0);
1108 if (hres) {
1109 FIXME("CoMarshalInterface failed, %lx!\n",hres);
1110 return hres;
1112 hres = IStream_Stat(pStm,&ststg,0);
1113 if (hres) return hres;
1115 buflen = ststg.cbSize.s.LowPart;
1116 buffer = HeapAlloc(GetProcessHeap(),0,buflen);
1117 seekto.s.LowPart = 0;
1118 seekto.s.HighPart = 0;
1119 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
1120 if (hres) {
1121 FIXME("IStream_Seek failed, %lx\n",hres);
1122 return hres;
1124 hres = IStream_Read(pStm,buffer,buflen,&res);
1125 if (hres) {
1126 FIXME("Stream Read failed, %lx\n",hres);
1127 return hres;
1129 IStream_Release(pStm);
1131 while (1) {
1132 hPipe = CreateNamedPipeA(
1133 pipefn,
1134 PIPE_ACCESS_DUPLEX,
1135 PIPE_TYPE_BYTE|PIPE_WAIT,
1136 PIPE_UNLIMITED_INSTANCES,
1137 4096,
1138 4096,
1139 NMPWAIT_USE_DEFAULT_WAIT,
1140 NULL
1142 if (hPipe == INVALID_HANDLE_VALUE) {
1143 FIXME("pipe creation failed for %s, le is %lx\n",pipefn,GetLastError());
1144 return 1;
1146 if (!ConnectNamedPipe(hPipe,NULL)) {
1147 ERR("Failure during ConnectNamedPipe %lx, ABORT!\n",GetLastError());
1148 CloseHandle(hPipe);
1149 continue;
1151 WriteFile(hPipe,buffer,buflen,&res,NULL);
1152 CloseHandle(hPipe);
1154 return 0;
1157 /******************************************************************************
1158 * CoRegisterClassObject [OLE32.36]
1160 * This method will register the class object for a given class ID.
1162 * See the Windows documentation for more details.
1164 HRESULT WINAPI CoRegisterClassObject(
1165 REFCLSID rclsid,
1166 LPUNKNOWN pUnk,
1167 DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
1168 DWORD flags, /* [in] REGCLS flags indicating how connections are made */
1169 LPDWORD lpdwRegister
1172 RegisteredClass* newClass;
1173 LPUNKNOWN foundObject;
1174 HRESULT hr;
1176 TRACE("(%s,%p,0x%08lx,0x%08lx,%p)\n",
1177 debugstr_guid(rclsid),pUnk,dwClsContext,flags,lpdwRegister);
1179 if ( (lpdwRegister==0) || (pUnk==0) )
1180 return E_INVALIDARG;
1182 *lpdwRegister = 0;
1185 * First, check if the class is already registered.
1186 * If it is, this should cause an error.
1188 hr = COM_GetRegisteredClassObject(rclsid, dwClsContext, &foundObject);
1189 if (hr == S_OK) {
1190 IUnknown_Release(foundObject);
1191 return CO_E_OBJISREG;
1194 newClass = HeapAlloc(GetProcessHeap(), 0, sizeof(RegisteredClass));
1195 if ( newClass == NULL )
1196 return E_OUTOFMEMORY;
1198 EnterCriticalSection( &csRegisteredClassList );
1200 newClass->classIdentifier = *rclsid;
1201 newClass->runContext = dwClsContext;
1202 newClass->connectFlags = flags;
1204 * Use the address of the chain node as the cookie since we are sure it's
1205 * unique.
1207 newClass->dwCookie = (DWORD)newClass;
1208 newClass->nextClass = firstRegisteredClass;
1211 * Since we're making a copy of the object pointer, we have to increase its
1212 * reference count.
1214 newClass->classObject = pUnk;
1215 IUnknown_AddRef(newClass->classObject);
1217 firstRegisteredClass = newClass;
1218 LeaveCriticalSection( &csRegisteredClassList );
1220 *lpdwRegister = newClass->dwCookie;
1222 if (dwClsContext & CLSCTX_LOCAL_SERVER) {
1223 DWORD tid;
1225 STUBMGR_Start();
1226 newClass->hThread=CreateThread(NULL,0,_LocalServerThread,newClass,0,&tid);
1228 return S_OK;
1231 /***********************************************************************
1232 * CoRevokeClassObject [OLE32.40]
1234 * This method will remove a class object from the class registry
1236 * See the Windows documentation for more details.
1238 HRESULT WINAPI CoRevokeClassObject(
1239 DWORD dwRegister)
1241 HRESULT hr = E_INVALIDARG;
1242 RegisteredClass** prevClassLink;
1243 RegisteredClass* curClass;
1245 TRACE("(%08lx)\n",dwRegister);
1247 EnterCriticalSection( &csRegisteredClassList );
1250 * Iterate through the whole list and try to match the cookie.
1252 curClass = firstRegisteredClass;
1253 prevClassLink = &firstRegisteredClass;
1255 while (curClass != 0)
1258 * Check if we have a match on the cookie.
1260 if (curClass->dwCookie == dwRegister)
1263 * Remove the class from the chain.
1265 *prevClassLink = curClass->nextClass;
1268 * Release the reference to the class object.
1270 IUnknown_Release(curClass->classObject);
1273 * Free the memory used by the chain node.
1275 HeapFree(GetProcessHeap(), 0, curClass);
1277 hr = S_OK;
1278 goto end;
1282 * Step to the next class in the list.
1284 prevClassLink = &(curClass->nextClass);
1285 curClass = curClass->nextClass;
1288 end:
1289 LeaveCriticalSection( &csRegisteredClassList );
1291 * If we get to here, we haven't found our class.
1293 return hr;
1296 static HRESULT WINAPI Remote_CoGetClassObject(
1297 REFCLSID rclsid, DWORD dwClsContext, COSERVERINFO *pServerInfo,
1298 REFIID iid, LPVOID *ppv
1300 HKEY key;
1301 char buf[200];
1302 HRESULT hres = E_UNEXPECTED;
1303 char xclsid[80];
1304 WCHAR dllName[MAX_PATH+1];
1305 DWORD dllNameLen = sizeof(dllName);
1306 STARTUPINFOW sinfo;
1307 PROCESS_INFORMATION pinfo;
1309 WINE_StringFromCLSID((LPCLSID)rclsid,xclsid);
1311 sprintf(buf,"CLSID\\%s\\LocalServer32",xclsid);
1312 hres = RegOpenKeyExA(HKEY_CLASSES_ROOT, buf, 0, KEY_READ, &key);
1314 if (hres != ERROR_SUCCESS)
1315 return REGDB_E_CLASSNOTREG;
1317 memset(dllName,0,sizeof(dllName));
1318 hres= RegQueryValueExW(key,NULL,NULL,NULL,(LPBYTE)dllName,&dllNameLen);
1319 if (hres)
1320 return REGDB_E_CLASSNOTREG; /* FIXME: check retval */
1321 RegCloseKey(key);
1323 TRACE("found LocalServer32 exe %s\n", debugstr_w(dllName));
1325 memset(&sinfo,0,sizeof(sinfo));
1326 sinfo.cb = sizeof(sinfo);
1327 if (!CreateProcessW(NULL,dllName,NULL,NULL,FALSE,0,NULL,NULL,&sinfo,&pinfo))
1328 return E_FAIL;
1329 return create_marshalled_proxy(rclsid,iid,ppv);
1332 /***********************************************************************
1333 * CoGetClassObject [COMPOBJ.7]
1334 * CoGetClassObject [OLE32.16]
1336 * FIXME. If request allows of several options and there is a failure
1337 * with one (other than not being registered) do we try the
1338 * others or return failure? (E.g. inprocess is registered but
1339 * the DLL is not found but the server version works)
1341 HRESULT WINAPI CoGetClassObject(
1342 REFCLSID rclsid, DWORD dwClsContext, COSERVERINFO *pServerInfo,
1343 REFIID iid, LPVOID *ppv
1345 LPUNKNOWN regClassObject;
1346 HRESULT hres = E_UNEXPECTED;
1347 char xclsid[80];
1348 WCHAR ProviderName[MAX_PATH+1];
1349 DWORD ProviderNameLen = sizeof(ProviderName);
1350 HINSTANCE hLibrary;
1351 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid,
1352 REFIID iid, LPVOID *ppv);
1353 DllGetClassObjectFunc DllGetClassObject;
1354 HKEY key;
1355 char buf[200];
1357 WINE_StringFromCLSID((LPCLSID)rclsid,xclsid);
1359 TRACE("\n\tCLSID:\t%s,\n\tIID:\t%s\n",
1360 debugstr_guid(rclsid),
1361 debugstr_guid(iid)
1364 if (pServerInfo) {
1365 FIXME("\tpServerInfo: name=%s\n",debugstr_w(pServerInfo->pwszName));
1366 FIXME("\t\tpAuthInfo=%p\n",pServerInfo->pAuthInfo);
1370 * First, try and see if we can't match the class ID with one of the
1371 * registered classes.
1373 if (S_OK == COM_GetRegisteredClassObject(rclsid, dwClsContext, &regClassObject))
1376 * Get the required interface from the retrieved pointer.
1378 hres = IUnknown_QueryInterface(regClassObject, iid, ppv);
1381 * Since QI got another reference on the pointer, we want to release the
1382 * one we already have. If QI was unsuccessful, this will release the object. This
1383 * is good since we are not returning it in the "out" parameter.
1385 IUnknown_Release(regClassObject);
1387 return hres;
1390 if (((CLSCTX_LOCAL_SERVER) & dwClsContext)
1391 && !((CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER) & dwClsContext))
1392 return Remote_CoGetClassObject(rclsid,dwClsContext,pServerInfo,iid,ppv);
1394 /* remote servers not supported yet */
1395 if ( ((CLSCTX_REMOTE_SERVER) & dwClsContext)
1396 && !((CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER) & dwClsContext)
1398 FIXME("CLSCTX_REMOTE_SERVER not supported!\n");
1399 return E_NOINTERFACE;
1402 if ((CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER) & dwClsContext) {
1403 HKEY key;
1404 char buf[200];
1406 memset(ProviderName,0,sizeof(ProviderName));
1407 sprintf(buf,"CLSID\\%s\\InprocServer32",xclsid);
1408 if (((hres = RegOpenKeyExA(HKEY_CLASSES_ROOT, buf, 0, KEY_READ, &key)) != ERROR_SUCCESS) ||
1409 ((hres = RegQueryValueExW(key,NULL,NULL,NULL,(LPBYTE)ProviderName,&ProviderNameLen)),
1410 RegCloseKey (key),
1411 hres != ERROR_SUCCESS))
1413 hres = REGDB_E_CLASSNOTREG;
1415 /* Don't ask me. MSDN says that CoGetClassObject does NOT call CoLoadLibrary */
1416 else if ((hLibrary = CoLoadLibrary(ProviderName, TRUE)) == 0)
1418 FIXME("couldn't load InprocServer32 dll %s\n", debugstr_w(ProviderName));
1419 hres = E_ACCESSDENIED; /* or should this be CO_E_DLLNOTFOUND? */
1421 else if (!(DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress(hLibrary, "DllGetClassObject")))
1423 /* not sure if this should be called here CoFreeLibrary(hLibrary);*/
1424 FIXME("couldn't find function DllGetClassObject in %s\n", debugstr_w(ProviderName));
1425 hres = E_ACCESSDENIED;
1427 else
1429 /* Ask the DLL for its class object. (there was a note here about
1430 * class factories but this is good.
1432 return DllGetClassObject(rclsid, iid, ppv);
1437 /* Finally try out of process */
1438 /* out of process and remote servers not supported yet */
1439 if (CLSCTX_LOCAL_SERVER & dwClsContext)
1441 memset(ProviderName,0,sizeof(ProviderName));
1442 sprintf(buf,"CLSID\\%s\\LocalServer32",xclsid);
1443 if (((hres = RegOpenKeyExA(HKEY_CLASSES_ROOT, buf, 0, KEY_READ, &key)) != ERROR_SUCCESS) ||
1444 ((hres = RegQueryValueExW(key,NULL,NULL,NULL,(LPBYTE)ProviderName,&ProviderNameLen)),
1445 RegCloseKey (key),
1446 hres != ERROR_SUCCESS))
1448 hres = REGDB_E_CLASSNOTREG;
1450 else
1452 /* CO_E_APPNOTFOUND if no exe */
1453 FIXME("CLSCTX_LOCAL_SERVER %s registered but not yet supported!\n",debugstr_w(ProviderName));
1454 hres = E_ACCESSDENIED;
1458 if (CLSCTX_REMOTE_SERVER & dwClsContext)
1460 FIXME ("CLSCTX_REMOTE_SERVER not supported\n");
1461 hres = E_ACCESSDENIED;
1464 return hres;
1466 /***********************************************************************
1467 * CoResumeClassObjects (OLE32.173)
1469 * Resumes classobjects registered with REGCLS suspended
1471 HRESULT WINAPI CoResumeClassObjects(void)
1473 FIXME("\n");
1474 return S_OK;
1477 /***********************************************************************
1478 * GetClassFile (OLE32.67)
1480 * This function supplies the CLSID associated with the given filename.
1482 HRESULT WINAPI GetClassFile(LPCOLESTR filePathName,CLSID *pclsid)
1484 IStorage *pstg=0;
1485 HRESULT res;
1486 int nbElm=0,length=0,i=0;
1487 LONG sizeProgId=20;
1488 LPOLESTR *pathDec=0,absFile=0,progId=0;
1489 WCHAR extention[100]={0};
1491 TRACE("()\n");
1493 /* if the file contain a storage object the return the CLSID writen by IStorage_SetClass method*/
1494 if((StgIsStorageFile(filePathName))==S_OK){
1496 res=StgOpenStorage(filePathName,NULL,STGM_READ | STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
1498 if (SUCCEEDED(res))
1499 res=ReadClassStg(pstg,pclsid);
1501 IStorage_Release(pstg);
1503 return res;
1505 /* if the file is not a storage object then attemps to match various bits in the file against a
1506 pattern in the registry. this case is not frequently used ! so I present only the psodocode for
1507 this case
1509 for(i=0;i<nFileTypes;i++)
1511 for(i=0;j<nPatternsForType;j++){
1513 PATTERN pat;
1514 HANDLE hFile;
1516 pat=ReadPatternFromRegistry(i,j);
1517 hFile=CreateFileW(filePathName,,,,,,hFile);
1518 SetFilePosition(hFile,pat.offset);
1519 ReadFile(hFile,buf,pat.size,NULL,NULL);
1520 if (memcmp(buf&pat.mask,pat.pattern.pat.size)==0){
1522 *pclsid=ReadCLSIDFromRegistry(i);
1523 return S_OK;
1528 /* if the obove strategies fail then search for the extension key in the registry */
1530 /* get the last element (absolute file) in the path name */
1531 nbElm=FileMonikerImpl_DecomposePath(filePathName,&pathDec);
1532 absFile=pathDec[nbElm-1];
1534 /* failed if the path represente a directory and not an absolute file name*/
1535 if (lstrcmpW(absFile,(LPOLESTR)"\\"))
1536 return MK_E_INVALIDEXTENSION;
1538 /* get the extension of the file */
1539 length=lstrlenW(absFile);
1540 for(i=length-1; ( (i>=0) && (extention[i]=absFile[i]) );i--);
1542 /* get the progId associated to the extension */
1543 progId=CoTaskMemAlloc(sizeProgId);
1545 res=RegQueryValueW(HKEY_CLASSES_ROOT,extention,progId,&sizeProgId);
1547 if (res==ERROR_MORE_DATA){
1549 progId = CoTaskMemRealloc(progId,sizeProgId);
1550 res=RegQueryValueW(HKEY_CLASSES_ROOT,extention,progId,&sizeProgId);
1552 if (res==ERROR_SUCCESS)
1553 /* return the clsid associated to the progId */
1554 res= CLSIDFromProgID(progId,pclsid);
1556 for(i=0; pathDec[i]!=NULL;i++)
1557 CoTaskMemFree(pathDec[i]);
1558 CoTaskMemFree(pathDec);
1560 CoTaskMemFree(progId);
1562 if (res==ERROR_SUCCESS)
1563 return res;
1565 return MK_E_INVALIDEXTENSION;
1567 /******************************************************************************
1568 * CoRegisterMessageFilter [COMPOBJ.27]
1570 HRESULT WINAPI CoRegisterMessageFilter16(
1571 LPMESSAGEFILTER lpMessageFilter,
1572 LPMESSAGEFILTER *lplpMessageFilter
1574 FIXME("(%p,%p),stub!\n",lpMessageFilter,lplpMessageFilter);
1575 return 0;
1578 /***********************************************************************
1579 * CoCreateInstance [COMPOBJ.13]
1580 * CoCreateInstance [OLE32.7]
1582 HRESULT WINAPI CoCreateInstance(
1583 REFCLSID rclsid,
1584 LPUNKNOWN pUnkOuter,
1585 DWORD dwClsContext,
1586 REFIID iid,
1587 LPVOID *ppv)
1589 HRESULT hres;
1590 LPCLASSFACTORY lpclf = 0;
1593 * Sanity check
1595 if (ppv==0)
1596 return E_POINTER;
1599 * Initialize the "out" parameter
1601 *ppv = 0;
1604 * Get a class factory to construct the object we want.
1606 hres = CoGetClassObject(rclsid,
1607 dwClsContext,
1608 NULL,
1609 &IID_IClassFactory,
1610 (LPVOID)&lpclf);
1612 if (FAILED(hres)) {
1613 FIXME("no classfactory created for CLSID %s, hres is 0x%08lx\n",
1614 debugstr_guid(rclsid),hres);
1615 return hres;
1619 * Create the object and don't forget to release the factory
1621 hres = IClassFactory_CreateInstance(lpclf, pUnkOuter, iid, ppv);
1622 IClassFactory_Release(lpclf);
1623 if(FAILED(hres))
1624 FIXME("no instance created for interface %s of class %s, hres is 0x%08lx\n",
1625 debugstr_guid(iid), debugstr_guid(rclsid),hres);
1627 return hres;
1630 /***********************************************************************
1631 * CoCreateInstanceEx [OLE32.165]
1633 HRESULT WINAPI CoCreateInstanceEx(
1634 REFCLSID rclsid,
1635 LPUNKNOWN pUnkOuter,
1636 DWORD dwClsContext,
1637 COSERVERINFO* pServerInfo,
1638 ULONG cmq,
1639 MULTI_QI* pResults)
1641 IUnknown* pUnk = NULL;
1642 HRESULT hr;
1643 ULONG index;
1644 int successCount = 0;
1647 * Sanity check
1649 if ( (cmq==0) || (pResults==NULL))
1650 return E_INVALIDARG;
1652 if (pServerInfo!=NULL)
1653 FIXME("() non-NULL pServerInfo not supported!\n");
1656 * Initialize all the "out" parameters.
1658 for (index = 0; index < cmq; index++)
1660 pResults[index].pItf = NULL;
1661 pResults[index].hr = E_NOINTERFACE;
1665 * Get the object and get its IUnknown pointer.
1667 hr = CoCreateInstance(rclsid,
1668 pUnkOuter,
1669 dwClsContext,
1670 &IID_IUnknown,
1671 (VOID**)&pUnk);
1673 if (hr)
1674 return hr;
1677 * Then, query for all the interfaces requested.
1679 for (index = 0; index < cmq; index++)
1681 pResults[index].hr = IUnknown_QueryInterface(pUnk,
1682 pResults[index].pIID,
1683 (VOID**)&(pResults[index].pItf));
1685 if (pResults[index].hr == S_OK)
1686 successCount++;
1690 * Release our temporary unknown pointer.
1692 IUnknown_Release(pUnk);
1694 if (successCount == 0)
1695 return E_NOINTERFACE;
1697 if (successCount!=cmq)
1698 return CO_S_NOTALLINTERFACES;
1700 return S_OK;
1703 /***********************************************************************
1704 * CoFreeLibrary [OLE32.13]
1706 void WINAPI CoFreeLibrary(HINSTANCE hLibrary)
1708 OpenDll *ptr, *prev;
1709 OpenDll *tmp;
1711 EnterCriticalSection( &csOpenDllList );
1713 /* lookup library in linked list */
1714 prev = NULL;
1715 for (ptr = openDllList; ptr != NULL; ptr=ptr->next) {
1716 if (ptr->hLibrary == hLibrary) {
1717 break;
1719 prev = ptr;
1722 if (ptr == NULL) {
1723 /* shouldn't happen if user passed in a valid hLibrary */
1724 goto end;
1726 /* assert: ptr points to the library entry to free */
1728 /* free library and remove node from list */
1729 FreeLibrary(hLibrary);
1730 if (ptr == openDllList) {
1731 tmp = openDllList->next;
1732 HeapFree(GetProcessHeap(), 0, openDllList);
1733 openDllList = tmp;
1734 } else {
1735 tmp = ptr->next;
1736 HeapFree(GetProcessHeap(), 0, ptr);
1737 prev->next = tmp;
1739 end:
1740 LeaveCriticalSection( &csOpenDllList );
1744 /***********************************************************************
1745 * CoFreeAllLibraries [OLE32.12]
1747 void WINAPI CoFreeAllLibraries(void)
1749 OpenDll *ptr, *tmp;
1751 EnterCriticalSection( &csOpenDllList );
1753 for (ptr = openDllList; ptr != NULL; ) {
1754 tmp=ptr->next;
1755 CoFreeLibrary(ptr->hLibrary);
1756 ptr = tmp;
1759 LeaveCriticalSection( &csOpenDllList );
1764 /***********************************************************************
1765 * CoFreeUnusedLibraries [COMPOBJ.17]
1766 * CoFreeUnusedLibraries [OLE32.14]
1768 void WINAPI CoFreeUnusedLibraries(void)
1770 OpenDll *ptr, *tmp;
1771 typedef HRESULT(*DllCanUnloadNowFunc)(void);
1772 DllCanUnloadNowFunc DllCanUnloadNow;
1774 EnterCriticalSection( &csOpenDllList );
1776 for (ptr = openDllList; ptr != NULL; ) {
1777 DllCanUnloadNow = (DllCanUnloadNowFunc)
1778 GetProcAddress(ptr->hLibrary, "DllCanUnloadNow");
1780 if ( (DllCanUnloadNow != NULL) &&
1781 (DllCanUnloadNow() == S_OK) ) {
1782 tmp=ptr->next;
1783 CoFreeLibrary(ptr->hLibrary);
1784 ptr = tmp;
1785 } else {
1786 ptr=ptr->next;
1790 LeaveCriticalSection( &csOpenDllList );
1793 /***********************************************************************
1794 * CoFileTimeNow [COMPOBJ.82]
1795 * CoFileTimeNow [OLE32.10]
1797 * RETURNS
1798 * the current system time in lpFileTime
1800 HRESULT WINAPI CoFileTimeNow( FILETIME *lpFileTime ) /* [out] the current time */
1802 GetSystemTimeAsFileTime( lpFileTime );
1803 return S_OK;
1806 /***********************************************************************
1807 * CoTaskMemAlloc (OLE32.43)
1808 * RETURNS
1809 * pointer to newly allocated block
1811 LPVOID WINAPI CoTaskMemAlloc(
1812 ULONG size /* [in] size of memoryblock to be allocated */
1814 LPMALLOC lpmalloc;
1815 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1817 if (FAILED(ret))
1818 return NULL;
1820 return IMalloc_Alloc(lpmalloc,size);
1822 /***********************************************************************
1823 * CoTaskMemFree (OLE32.44)
1825 VOID WINAPI CoTaskMemFree(
1826 LPVOID ptr /* [in] pointer to be freed */
1828 LPMALLOC lpmalloc;
1829 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1831 if (FAILED(ret))
1832 return;
1834 IMalloc_Free(lpmalloc, ptr);
1837 /***********************************************************************
1838 * CoTaskMemRealloc (OLE32.45)
1839 * RETURNS
1840 * pointer to newly allocated block
1842 LPVOID WINAPI CoTaskMemRealloc(
1843 LPVOID pvOld,
1844 ULONG size) /* [in] size of memoryblock to be allocated */
1846 LPMALLOC lpmalloc;
1847 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1849 if (FAILED(ret))
1850 return NULL;
1852 return IMalloc_Realloc(lpmalloc, pvOld, size);
1855 /***********************************************************************
1856 * CoLoadLibrary (OLE32.30)
1858 HINSTANCE WINAPI CoLoadLibrary(LPOLESTR lpszLibName, BOOL bAutoFree)
1860 HINSTANCE hLibrary;
1861 OpenDll *ptr;
1862 OpenDll *tmp;
1864 TRACE("(%s, %d)\n", debugstr_w(lpszLibName), bAutoFree);
1866 hLibrary = LoadLibraryExW(lpszLibName, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
1868 if (!bAutoFree)
1869 return hLibrary;
1871 EnterCriticalSection( &csOpenDllList );
1873 if (openDllList == NULL) {
1874 /* empty list -- add first node */
1875 openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
1876 openDllList->hLibrary=hLibrary;
1877 openDllList->next = NULL;
1878 } else {
1879 /* search for this dll */
1880 int found = FALSE;
1881 for (ptr = openDllList; ptr->next != NULL; ptr=ptr->next) {
1882 if (ptr->hLibrary == hLibrary) {
1883 found = TRUE;
1884 break;
1887 if (!found) {
1888 /* dll not found, add it */
1889 tmp = openDllList;
1890 openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
1891 openDllList->hLibrary = hLibrary;
1892 openDllList->next = tmp;
1896 LeaveCriticalSection( &csOpenDllList );
1898 return hLibrary;
1901 /***********************************************************************
1902 * CoInitializeWOW (OLE32.27)
1904 HRESULT WINAPI CoInitializeWOW(DWORD x,DWORD y) {
1905 FIXME("(0x%08lx,0x%08lx),stub!\n",x,y);
1906 return 0;
1909 /******************************************************************************
1910 * CoLockObjectExternal [COMPOBJ.63]
1912 HRESULT WINAPI CoLockObjectExternal16(
1913 LPUNKNOWN pUnk, /* [in] object to be locked */
1914 BOOL16 fLock, /* [in] do lock */
1915 BOOL16 fLastUnlockReleases /* [in] ? */
1917 FIXME("(%p,%d,%d),stub!\n",pUnk,fLock,fLastUnlockReleases);
1918 return S_OK;
1921 /******************************************************************************
1922 * CoLockObjectExternal [OLE32.31]
1924 HRESULT WINAPI CoLockObjectExternal(
1925 LPUNKNOWN pUnk, /* [in] object to be locked */
1926 BOOL fLock, /* [in] do lock */
1927 BOOL fLastUnlockReleases) /* [in] unlock all */
1930 if (fLock)
1933 * Increment the external lock coutner, COM_ExternalLockAddRef also
1934 * increment the object's internal lock counter.
1936 COM_ExternalLockAddRef( pUnk);
1938 else
1941 * Decrement the external lock coutner, COM_ExternalLockRelease also
1942 * decrement the object's internal lock counter.
1944 COM_ExternalLockRelease( pUnk, fLastUnlockReleases);
1947 return S_OK;
1950 /***********************************************************************
1951 * CoGetState [COMPOBJ.115]
1953 HRESULT WINAPI CoGetState16(LPDWORD state)
1955 FIXME("(%p),stub!\n", state);
1956 *state = 0;
1957 return S_OK;
1959 /***********************************************************************
1960 * CoSetState [OLE32.42]
1962 HRESULT WINAPI CoSetState(LPDWORD state)
1964 FIXME("(%p),stub!\n", state);
1965 if (state) *state = 0;
1966 return S_OK;
1968 /***********************************************************************
1969 * CoCreateFreeThreadedMarshaler [OLE32.5]
1971 HRESULT WINAPI CoCreateFreeThreadedMarshaler (LPUNKNOWN punkOuter, LPUNKNOWN* ppunkMarshal)
1973 FIXME ("(%p %p): stub\n", punkOuter, ppunkMarshal);
1975 return S_OK;
1978 /***
1979 * COM_RevokeAllClasses
1981 * This method is called when the COM libraries are uninitialized to
1982 * release all the references to the class objects registered with
1983 * the library
1985 static void COM_RevokeAllClasses()
1987 EnterCriticalSection( &csRegisteredClassList );
1989 while (firstRegisteredClass!=0)
1991 CoRevokeClassObject(firstRegisteredClass->dwCookie);
1994 LeaveCriticalSection( &csRegisteredClassList );
1997 /****************************************************************************
1998 * COM External Lock methods implementation
2001 /****************************************************************************
2002 * Public - Method that increments the count for a IUnknown* in the linked
2003 * list. The item is inserted if not already in the list.
2005 static void COM_ExternalLockAddRef(
2006 IUnknown *pUnk)
2008 COM_ExternalLock *externalLock = COM_ExternalLockFind(pUnk);
2011 * Add an external lock to the object. If it was already externally
2012 * locked, just increase the reference count. If it was not.
2013 * add the item to the list.
2015 if ( externalLock == EL_NOT_FOUND )
2016 COM_ExternalLockInsert(pUnk);
2017 else
2018 externalLock->uRefCount++;
2021 * Add an internal lock to the object
2023 IUnknown_AddRef(pUnk);
2026 /****************************************************************************
2027 * Public - Method that decrements the count for a IUnknown* in the linked
2028 * list. The item is removed from the list if its count end up at zero or if
2029 * bRelAll is TRUE.
2031 static void COM_ExternalLockRelease(
2032 IUnknown *pUnk,
2033 BOOL bRelAll)
2035 COM_ExternalLock *externalLock = COM_ExternalLockFind(pUnk);
2037 if ( externalLock != EL_NOT_FOUND )
2041 externalLock->uRefCount--; /* release external locks */
2042 IUnknown_Release(pUnk); /* release local locks as well */
2044 if ( bRelAll == FALSE )
2045 break; /* perform single release */
2047 } while ( externalLock->uRefCount > 0 );
2049 if ( externalLock->uRefCount == 0 ) /* get rid of the list entry */
2050 COM_ExternalLockDelete(externalLock);
2053 /****************************************************************************
2054 * Public - Method that frees the content of the list.
2056 static void COM_ExternalLockFreeList()
2058 COM_ExternalLock *head;
2060 head = elList.head; /* grab it by the head */
2061 while ( head != EL_END_OF_LIST )
2063 COM_ExternalLockDelete(head); /* get rid of the head stuff */
2065 head = elList.head; /* get the new head... */
2069 /****************************************************************************
2070 * Public - Method that dump the content of the list.
2072 void COM_ExternalLockDump()
2074 COM_ExternalLock *current = elList.head;
2076 DPRINTF("\nExternal lock list contains:\n");
2078 while ( current != EL_END_OF_LIST )
2080 DPRINTF( "\t%p with %lu references count.\n", current->pUnk, current->uRefCount);
2082 /* Skip to the next item */
2083 current = current->next;
2088 /****************************************************************************
2089 * Internal - Find a IUnknown* in the linked list
2091 static COM_ExternalLock* COM_ExternalLockFind(
2092 IUnknown *pUnk)
2094 return COM_ExternalLockLocate(elList.head, pUnk);
2097 /****************************************************************************
2098 * Internal - Recursivity agent for IUnknownExternalLockList_Find
2100 static COM_ExternalLock* COM_ExternalLockLocate(
2101 COM_ExternalLock *element,
2102 IUnknown *pUnk)
2104 if ( element == EL_END_OF_LIST )
2105 return EL_NOT_FOUND;
2107 else if ( element->pUnk == pUnk ) /* We found it */
2108 return element;
2110 else /* Not the right guy, keep on looking */
2111 return COM_ExternalLockLocate( element->next, pUnk);
2114 /****************************************************************************
2115 * Internal - Insert a new IUnknown* to the linked list
2117 static BOOL COM_ExternalLockInsert(
2118 IUnknown *pUnk)
2120 COM_ExternalLock *newLock = NULL;
2121 COM_ExternalLock *previousHead = NULL;
2124 * Allocate space for the new storage object
2126 newLock = HeapAlloc(GetProcessHeap(), 0, sizeof(COM_ExternalLock));
2128 if (newLock!=NULL)
2130 if ( elList.head == EL_END_OF_LIST )
2132 elList.head = newLock; /* The list is empty */
2134 else
2137 * insert does it at the head
2139 previousHead = elList.head;
2140 elList.head = newLock;
2144 * Set new list item data member
2146 newLock->pUnk = pUnk;
2147 newLock->uRefCount = 1;
2148 newLock->next = previousHead;
2150 return TRUE;
2152 else
2153 return FALSE;
2156 /****************************************************************************
2157 * Internal - Method that removes an item from the linked list.
2159 static void COM_ExternalLockDelete(
2160 COM_ExternalLock *itemList)
2162 COM_ExternalLock *current = elList.head;
2164 if ( current == itemList )
2167 * this section handles the deletion of the first node
2169 elList.head = itemList->next;
2170 HeapFree( GetProcessHeap(), 0, itemList);
2172 else
2176 if ( current->next == itemList ) /* We found the item to free */
2178 current->next = itemList->next; /* readjust the list pointers */
2180 HeapFree( GetProcessHeap(), 0, itemList);
2181 break;
2184 /* Skip to the next item */
2185 current = current->next;
2187 } while ( current != EL_END_OF_LIST );
2191 /***********************************************************************
2192 * DllEntryPoint [COMPOBJ.116]
2194 * Initialization code for the COMPOBJ DLL
2196 * RETURNS:
2198 BOOL WINAPI COMPOBJ_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst, WORD ds, WORD HeapSize, DWORD res1, WORD res2)
2200 TRACE("(%08lx, %04x, %04x, %04x, %08lx, %04x)\n", Reason, hInst, ds, HeapSize,
2201 res1, res2);
2202 switch(Reason)
2204 case DLL_PROCESS_ATTACH:
2205 if (!COMPOBJ_Attach++) COMPOBJ_hInstance = hInst;
2206 break;
2208 case DLL_PROCESS_DETACH:
2209 if(!--COMPOBJ_Attach)
2210 COMPOBJ_hInstance = 0;
2211 break;
2213 return TRUE;
2216 /******************************************************************************
2217 * OleGetAutoConvert [OLE32.104]
2219 HRESULT WINAPI OleGetAutoConvert(REFCLSID clsidOld, LPCLSID pClsidNew)
2221 HKEY hkey = 0;
2222 char buf[200];
2223 WCHAR wbuf[200];
2224 DWORD len;
2225 HRESULT res = S_OK;
2227 sprintf(buf,"CLSID\\");WINE_StringFromCLSID(clsidOld,&buf[6]);
2228 if (RegOpenKeyA(HKEY_CLASSES_ROOT,buf,&hkey))
2230 res = REGDB_E_CLASSNOTREG;
2231 goto done;
2233 len = 200;
2234 /* we can just query for the default value of AutoConvertTo key like that,
2235 without opening the AutoConvertTo key and querying for NULL (default) */
2236 if (RegQueryValueA(hkey,"AutoConvertTo",buf,&len))
2238 res = REGDB_E_KEYMISSING;
2239 goto done;
2241 MultiByteToWideChar( CP_ACP, 0, buf, -1, wbuf, sizeof(wbuf)/sizeof(WCHAR) );
2242 CLSIDFromString(wbuf,pClsidNew);
2243 done:
2244 if (hkey) RegCloseKey(hkey);
2246 return res;
2249 /******************************************************************************
2250 * OleSetAutoConvert [OLE32.126]
2252 HRESULT WINAPI OleSetAutoConvert(REFCLSID clsidOld, REFCLSID clsidNew)
2254 HKEY hkey = 0;
2255 char buf[200], szClsidNew[200];
2256 HRESULT res = S_OK;
2258 TRACE("(%s,%s)\n", debugstr_guid(clsidOld), debugstr_guid(clsidNew));
2259 sprintf(buf,"CLSID\\");WINE_StringFromCLSID(clsidOld,&buf[6]);
2260 WINE_StringFromCLSID(clsidNew, szClsidNew);
2261 if (RegOpenKeyA(HKEY_CLASSES_ROOT,buf,&hkey))
2263 res = REGDB_E_CLASSNOTREG;
2264 goto done;
2266 if (RegSetValueA(hkey, "AutoConvertTo", REG_SZ, szClsidNew, strlen(szClsidNew)+1))
2268 res = REGDB_E_WRITEREGDB;
2269 goto done;
2272 done:
2273 if (hkey) RegCloseKey(hkey);
2274 return res;
2277 /******************************************************************************
2278 * CoTreatAsClass [OLE32.46]
2280 HRESULT WINAPI CoTreatAsClass(REFCLSID clsidOld, REFCLSID clsidNew)
2282 HKEY hkey = 0;
2283 char buf[200], szClsidNew[200];
2284 HRESULT res = S_OK;
2286 FIXME("(%s,%s)\n", debugstr_guid(clsidOld), debugstr_guid(clsidNew));
2287 sprintf(buf,"CLSID\\");WINE_StringFromCLSID(clsidOld,&buf[6]);
2288 WINE_StringFromCLSID(clsidNew, szClsidNew);
2289 if (RegOpenKeyA(HKEY_CLASSES_ROOT,buf,&hkey))
2291 res = REGDB_E_CLASSNOTREG;
2292 goto done;
2294 if (RegSetValueA(hkey, "AutoTreatAs", REG_SZ, szClsidNew, strlen(szClsidNew)+1))
2296 res = REGDB_E_WRITEREGDB;
2297 goto done;
2300 done:
2301 if (hkey) RegCloseKey(hkey);
2302 return res;
2306 /***********************************************************************
2307 * IsEqualGUID [OLE32.76]
2309 * Compares two Unique Identifiers.
2311 * RETURNS
2312 * TRUE if equal
2314 #undef IsEqualGUID
2315 BOOL WINAPI IsEqualGUID(
2316 REFGUID rguid1, /* [in] unique id 1 */
2317 REFGUID rguid2 /* [in] unique id 2 */
2320 return !memcmp(rguid1,rguid2,sizeof(GUID));