4 * Copyright 1995 Martin von Loewis
5 * Copyright 1998 Justin Bradford
6 * Copyright 1999 Francis Beaudet
7 * Copyright 1999 Sylvain St-Germain
19 #include "wine/winbase16.h"
24 #include "debugtools.h"
31 #include "wine/obj_base.h"
32 #include "wine/obj_misc.h"
33 #include "wine/obj_storage.h"
34 #include "wine/obj_clientserver.h"
39 DEFAULT_DEBUG_CHANNEL(ole);
41 /****************************************************************************
42 * COM External Lock structures and methods declaration
44 * This api provides a linked list to managed external references to
47 * The public interface consists of three calls:
48 * COM_ExternalLockAddRef
49 * COM_ExternalLockRelease
50 * COM_ExternalLockFreeList
53 #define EL_END_OF_LIST 0
54 #define EL_NOT_FOUND 0
57 * Declaration of the static structure that manage the
58 * external lock to COM objects.
60 typedef struct COM_ExternalLock COM_ExternalLock;
61 typedef struct COM_ExternalLockList COM_ExternalLockList;
63 struct COM_ExternalLock
65 IUnknown *pUnk; /* IUnknown referenced */
66 ULONG uRefCount; /* external lock counter to IUnknown object*/
67 COM_ExternalLock *next; /* Pointer to next element in list */
70 struct COM_ExternalLockList
72 COM_ExternalLock *head; /* head of list */
76 * Declaration and initialization of the static structure that manages
77 * the external lock to COM objects.
79 static COM_ExternalLockList elList = { EL_END_OF_LIST };
82 * Public Interface to the external lock list
84 static void COM_ExternalLockFreeList();
85 static void COM_ExternalLockAddRef(IUnknown *pUnk);
86 static void COM_ExternalLockRelease(IUnknown *pUnk, BOOL bRelAll);
87 void COM_ExternalLockDump(); /* testing purposes, not static to avoid warning */
90 * Private methods used to managed the linked list
92 static BOOL COM_ExternalLockInsert(
95 static void COM_ExternalLockDelete(
96 COM_ExternalLock *element);
98 static COM_ExternalLock* COM_ExternalLockFind(
101 static COM_ExternalLock* COM_ExternalLockLocate(
102 COM_ExternalLock *element,
105 /****************************************************************************
106 * This section defines variables internal to the COM module.
108 * TODO: Most of these things will have to be made thread-safe.
110 HINSTANCE16 COMPOBJ_hInstance = 0;
111 HINSTANCE COMPOBJ_hInstance32 = 0;
112 static int COMPOBJ_Attach = 0;
114 LPMALLOC16 currentMalloc16=NULL;
115 LPMALLOC currentMalloc32=NULL;
118 WORD Table_ETask[62];
121 * This lock count counts the number of times CoInitialize is called. It is
122 * decreased every time CoUninitialize is called. When it hits 0, the COM
123 * libraries are freed
125 static ULONG s_COMLockCount = 0;
128 * This linked list contains the list of registered class objects. These
129 * are mostly used to register the factories for out-of-proc servers of OLE
132 * TODO: Make this data structure aware of inter-process communication. This
133 * means that parts of this will be exported to the Wine Server.
135 typedef struct tagRegisteredClass
137 CLSID classIdentifier;
138 LPUNKNOWN classObject;
142 struct tagRegisteredClass* nextClass;
145 static RegisteredClass* firstRegisteredClass = NULL;
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.
151 typedef struct tagOpenDll {
153 struct tagOpenDll *next;
156 static OpenDll *openDllList = NULL; /* linked list of open dlls */
158 /*****************************************************************************
159 * This section contains prototypes to internal methods for this
162 static HRESULT COM_GetRegisteredClassObject(REFCLSID rclsid,
166 static void COM_RevokeAllClasses();
169 /******************************************************************************
170 * CoBuildVersion [COMPOBJ.1]
173 * Current build version, hiword is majornumber, loword is minornumber
175 DWORD WINAPI CoBuildVersion(void)
177 TRACE("Returning version %d, build %d.\n", rmm, rup);
178 return (rmm<<16)+rup;
181 /******************************************************************************
182 * CoInitialize16 [COMPOBJ.2]
183 * Set the win16 IMalloc used for memory management
185 HRESULT WINAPI CoInitialize16(
186 LPVOID lpReserved /* [in] pointer to win16 malloc interface */
188 currentMalloc16 = (LPMALLOC16)lpReserved;
192 /******************************************************************************
193 * CoInitialize [OLE32.26]
195 * Initializes the COM libraries.
199 HRESULT WINAPI CoInitialize(
200 LPVOID lpReserved /* [in] pointer to win32 malloc interface
201 (obsolete, should be NULL) */
205 * Just delegate to the newer method.
207 return CoInitializeEx(lpReserved, COINIT_APARTMENTTHREADED);
210 /******************************************************************************
211 * CoInitializeEx [OLE32.163]
213 * Initializes the COM libraries. The behavior used to set the win32 IMalloc
214 * used for memory management is obsolete.
217 * S_OK if successful,
218 * S_FALSE if this function was called already.
219 * RPC_E_CHANGED_MODE if a previous call to CoInitialize specified another
223 * Only the single threaded model is supported. As a result RPC_E_CHANGED_MODE
226 * See the windows documentation for more details.
228 HRESULT WINAPI CoInitializeEx(
229 LPVOID lpReserved, /* [in] pointer to win32 malloc interface
230 (obsolete, should be NULL) */
231 DWORD dwCoInit /* [in] A value from COINIT specifies the threading model */
236 TRACE("(%p, %x)\n", lpReserved, (int)dwCoInit);
238 if (lpReserved!=NULL)
240 ERR("(%p, %x) - Bad parameter passed-in %p, must be an old Windows Application\n", lpReserved, (int)dwCoInit, lpReserved);
244 * Check for unsupported features.
246 if (dwCoInit!=COINIT_APARTMENTTHREADED)
248 FIXME(":(%p,%x): unsupported flag %x\n", lpReserved, (int)dwCoInit, (int)dwCoInit);
249 /* Hope for the best and continue anyway */
253 * Check the lock count. If this is the first time going through the initialize
254 * process, we have to initialize the libraries.
256 if (s_COMLockCount==0)
259 * Initialize the various COM libraries and data structures.
261 TRACE("() - Initializing the COM libraries\n");
263 RunningObjectTableImpl_Initialize();
271 * Crank-up that lock count.
278 /***********************************************************************
279 * CoUninitialize16 [COMPOBJ.3]
280 * Don't know what it does.
281 * 3-Nov-98 -- this was originally misspelled, I changed it to what I
282 * believe is the correct spelling
284 void WINAPI CoUninitialize16(void)
287 CoFreeAllLibraries();
290 /***********************************************************************
291 * CoUninitialize [OLE32.47]
293 * This method will release the COM libraries.
295 * See the windows documentation for more details.
297 void WINAPI CoUninitialize(void)
302 * Decrease the reference count.
307 * If we are back to 0 locks on the COM library, make sure we free
308 * all the associated data structures.
310 if (s_COMLockCount==0)
313 * Release the various COM libraries and data structures.
315 TRACE("() - Releasing the COM libraries\n");
317 RunningObjectTableImpl_UnInitialize();
319 * Release the references to the registered class objects.
321 COM_RevokeAllClasses();
324 * This will free the loaded COM Dlls.
326 CoFreeAllLibraries();
329 * This will free list of external references to COM objects.
331 COM_ExternalLockFreeList();
335 /***********************************************************************
336 * CoGetMalloc16 [COMPOBJ.4]
338 * The current win16 IMalloc
340 HRESULT WINAPI CoGetMalloc16(
341 DWORD dwMemContext, /* [in] unknown */
342 LPMALLOC16 * lpMalloc /* [out] current win16 malloc interface */
345 currentMalloc16 = IMalloc16_Constructor();
346 *lpMalloc = currentMalloc16;
350 /******************************************************************************
351 * CoGetMalloc [OLE32.20]
354 * The current win32 IMalloc
356 HRESULT WINAPI CoGetMalloc(
357 DWORD dwMemContext, /* [in] unknown */
358 LPMALLOC *lpMalloc /* [out] current win32 malloc interface */
361 currentMalloc32 = IMalloc_Constructor();
362 *lpMalloc = currentMalloc32;
366 /***********************************************************************
367 * CoCreateStandardMalloc16 [COMPOBJ.71]
369 HRESULT WINAPI CoCreateStandardMalloc16(DWORD dwMemContext,
370 LPMALLOC16 *lpMalloc)
372 /* FIXME: docu says we shouldn't return the same allocator as in
374 *lpMalloc = IMalloc16_Constructor();
378 /******************************************************************************
379 * CoDisconnectObject [COMPOBJ.15]
381 HRESULT WINAPI CoDisconnectObject( LPUNKNOWN lpUnk, DWORD reserved )
383 TRACE("%p %lx\n",lpUnk,reserved);
387 /***********************************************************************
388 * IsEqualGUID16 [COMPOBJ.18]
390 * Compares two Unique Identifiers.
395 BOOL16 WINAPI IsEqualGUID16(
396 GUID* g1, /* [in] unique id 1 */
397 GUID* g2 /* [in] unique id 2 */
399 return !memcmp( g1, g2, sizeof(GUID) );
402 /***********************************************************************
403 * IsEqualGUID32 [OLE32.76]
405 * Compares two Unique Identifiers.
410 BOOL WINAPI IsEqualGUID32(
411 REFGUID rguid1, /* [in] unique id 1 */
412 REFGUID rguid2 /* [in] unique id 2 */
415 return !memcmp(rguid1,rguid2,sizeof(GUID));
418 /******************************************************************************
419 * CLSIDFromString16 [COMPOBJ.20]
420 * Converts a unique identifier from its string representation into
423 * Class id: DWORD-WORD-WORD-BYTES[2]-BYTES[6]
428 HRESULT WINAPI CLSIDFromString16(
429 LPCOLESTR16 idstr, /* [in] string representation of guid */
430 CLSID *id /* [out] GUID converted from string */
432 BYTE *s = (BYTE *) idstr;
438 s = "{00000000-0000-0000-0000-000000000000}";
439 else { /* validate the CLSID string */
442 return CO_E_CLASSSTRING;
444 if ((s[0]!='{') || (s[9]!='-') || (s[14]!='-') || (s[19]!='-') || (s[24]!='-') || (s[37]!='}'))
445 return CO_E_CLASSSTRING;
449 if ((i == 9)||(i == 14)||(i == 19)||(i == 24)) continue;
450 if (!(((s[i] >= '0') && (s[i] <= '9')) ||
451 ((s[i] >= 'a') && (s[i] <= 'f')) ||
452 ((s[i] >= 'A') && (s[i] <= 'F')))
454 return CO_E_CLASSSTRING;
458 TRACE("%s -> %p\n", s, id);
460 /* quick lookup table */
461 memset(table, 0, 256);
463 for (i = 0; i < 10; i++) {
466 for (i = 0; i < 6; i++) {
467 table['A' + i] = i+10;
468 table['a' + i] = i+10;
471 /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
475 s++; /* skip leading brace */
476 for (i = 0; i < 4; i++) {
477 p[3 - i] = table[*s]<<4 | table[*(s+1)];
483 for (i = 0; i < 2; i++) {
484 p[1-i] = table[*s]<<4 | table[*(s+1)];
490 for (i = 0; i < 2; i++) {
491 p[1-i] = table[*s]<<4 | table[*(s+1)];
497 /* these are just sequential bytes */
498 for (i = 0; i < 2; i++) {
499 *p++ = table[*s]<<4 | table[*(s+1)];
504 for (i = 0; i < 6; i++) {
505 *p++ = table[*s]<<4 | table[*(s+1)];
512 /******************************************************************************
513 * CoCreateGuid[OLE32.6]
516 HRESULT WINAPI CoCreateGuid(
517 GUID *pguid /* [out] points to the GUID to initialize */
519 return UuidCreate(pguid);
522 /******************************************************************************
523 * CLSIDFromString [OLE32.3]
524 * Converts a unique identifier from its string representation into
528 * If idstr is not a valid CLSID string then it gets treated as a ProgID
533 HRESULT WINAPI CLSIDFromString(
534 LPCOLESTR idstr, /* [in] string representation of GUID */
535 CLSID *id /* [out] GUID represented by above string */
537 LPOLESTR16 xid = HEAP_strdupWtoA(GetProcessHeap(),0,idstr);
538 OLESTATUS ret = CLSIDFromString16(xid,id);
540 HeapFree(GetProcessHeap(),0,xid);
541 if(ret != S_OK) { /* It appears a ProgID is also valid */
542 ret = CLSIDFromProgID(idstr, id);
547 /******************************************************************************
548 * WINE_StringFromCLSID [Internal]
549 * Converts a GUID into the respective string representation.
554 * the string representation and OLESTATUS
556 static HRESULT WINE_StringFromCLSID(
557 const CLSID *id, /* [in] GUID to be converted */
558 LPSTR idstr /* [out] pointer to buffer to contain converted guid */
560 static const char *hex = "0123456789ABCDEF";
565 { ERR("called with id=Null\n");
570 sprintf(idstr, "{%08lX-%04X-%04X-%02X%02X-",
571 id->Data1, id->Data2, id->Data3,
572 id->Data4[0], id->Data4[1]);
576 for (i = 2; i < 8; i++) {
577 *s++ = hex[id->Data4[i]>>4];
578 *s++ = hex[id->Data4[i] & 0xf];
584 TRACE("%p->%s\n", id, idstr);
589 /******************************************************************************
590 * StringFromCLSID16 [COMPOBJ.19]
591 * Converts a GUID into the respective string representation.
592 * The target string is allocated using the OLE IMalloc.
594 * the string representation and OLESTATUS
596 HRESULT WINAPI StringFromCLSID16(
597 REFCLSID id, /* [in] the GUID to be converted */
598 LPOLESTR16 *idstr /* [out] a pointer to a to-be-allocated segmented pointer pointing to the resulting string */
605 ret = CoGetMalloc16(0,&mllc);
608 args[0] = (DWORD)mllc;
611 /* No need for a Callback entry, we have WOWCallback16Ex which does
612 * everything we need.
614 if (!WOWCallback16Ex(
615 (DWORD)((ICOM_VTABLE(IMalloc16)*)PTR_SEG_TO_LIN(
616 ICOM_VTBL(((LPMALLOC16)PTR_SEG_TO_LIN(mllc))))
623 WARN("CallTo16 IMalloc16 failed\n");
626 return WINE_StringFromCLSID(id,PTR_SEG_TO_LIN(*idstr));
629 /******************************************************************************
630 * StringFromCLSID [OLE32.151]
631 * Converts a GUID into the respective string representation.
632 * The target string is allocated using the OLE IMalloc.
634 * the string representation and OLESTATUS
636 HRESULT WINAPI StringFromCLSID(
637 REFCLSID id, /* [in] the GUID to be converted */
638 LPOLESTR *idstr /* [out] a pointer to a to-be-allocated pointer pointing to the resulting string */
644 if ((ret=CoGetMalloc(0,&mllc)))
647 ret=WINE_StringFromCLSID(id,buf);
649 *idstr = IMalloc_Alloc(mllc,strlen(buf)*2+2);
650 lstrcpyAtoW(*idstr,buf);
655 /******************************************************************************
656 * StringFromGUID2 [COMPOBJ.76] [OLE32.152]
658 * Converts a global unique identifier into a string of an API-
659 * specified fixed format. (The usual {.....} stuff.)
662 * The (UNICODE) string representation of the GUID in 'str'
663 * The length of the resulting string, 0 if there was any problem.
666 StringFromGUID2(REFGUID id, LPOLESTR str, INT cmax)
670 if (WINE_StringFromCLSID(id,xguid))
672 if (strlen(xguid)>=cmax)
674 lstrcpyAtoW(str,xguid);
675 return strlen(xguid) + 1;
678 /******************************************************************************
679 * ProgIDFromCLSID [OLE32.133]
680 * Converts a class id into the respective Program ID. (By using a registry lookup)
681 * RETURNS S_OK on success
682 * riid associated with the progid
685 HRESULT WINAPI ProgIDFromCLSID(
686 REFCLSID clsid, /* [in] class id as found in registry */
687 LPOLESTR *lplpszProgID/* [out] associated Prog ID */
690 char strCLSID[50], *buf, *buf2;
696 WINE_StringFromCLSID(clsid, strCLSID);
698 buf = HeapAlloc(GetProcessHeap(), 0, strlen(strCLSID)+14);
699 sprintf(buf,"CLSID\\%s\\ProgID", strCLSID);
700 if (RegOpenKeyA(HKEY_CLASSES_ROOT, buf, &xhkey))
701 ret = REGDB_E_CLASSNOTREG;
703 HeapFree(GetProcessHeap(), 0, buf);
707 buf2 = HeapAlloc(GetProcessHeap(), 0, 255);
709 if (RegQueryValueA(xhkey, NULL, buf2, &buf2len))
710 ret = REGDB_E_CLASSNOTREG;
714 if (CoGetMalloc(0,&mllc))
718 *lplpszProgID = IMalloc_Alloc(mllc, (buf2len+1)*2);
719 lstrcpyAtoW(*lplpszProgID, buf2);
722 HeapFree(GetProcessHeap(), 0, buf2);
729 /******************************************************************************
730 * CLSIDFromProgID16 [COMPOBJ.61]
731 * Converts a program id into the respective GUID. (By using a registry lookup)
733 * riid associated with the progid
735 HRESULT WINAPI CLSIDFromProgID16(
736 LPCOLESTR16 progid, /* [in] program id as found in registry */
737 LPCLSID riid /* [out] associated CLSID */
744 buf = HeapAlloc(GetProcessHeap(),0,strlen(progid)+8);
745 sprintf(buf,"%s\\CLSID",progid);
746 if ((err=RegOpenKeyA(HKEY_CLASSES_ROOT,buf,&xhkey))) {
747 HeapFree(GetProcessHeap(),0,buf);
748 return CO_E_CLASSSTRING;
750 HeapFree(GetProcessHeap(),0,buf);
751 buf2len = sizeof(buf2);
752 if ((err=RegQueryValueA(xhkey,NULL,buf2,&buf2len))) {
754 return CO_E_CLASSSTRING;
757 return CLSIDFromString16(buf2,riid);
760 /******************************************************************************
761 * CLSIDFromProgID [OLE32.2]
762 * Converts a program id into the respective GUID. (By using a registry lookup)
764 * riid associated with the progid
766 HRESULT WINAPI CLSIDFromProgID(
767 LPCOLESTR progid, /* [in] program id as found in registry */
768 LPCLSID riid /* [out] associated CLSID */
770 LPOLESTR16 pid = HEAP_strdupWtoA(GetProcessHeap(),0,progid);
771 OLESTATUS ret = CLSIDFromProgID16(pid,riid);
773 HeapFree(GetProcessHeap(),0,pid);
777 /***********************************************************************
780 * This function write a CLSID on stream
782 HRESULT WINAPI WriteClassStm(IStream *pStm,REFCLSID rclsid)
784 TRACE("(%p,%p)\n",pStm,rclsid);
789 return IStream_Write(pStm,rclsid,sizeof(CLSID),NULL);
792 /***********************************************************************
795 * This function read a CLSID from a stream
797 HRESULT WINAPI ReadClassStm(IStream *pStm,REFCLSID rclsid)
802 TRACE("(%p,%p)\n",pStm,rclsid);
807 res = IStream_Read(pStm,(void*)rclsid,sizeof(CLSID),&nbByte);
812 if (nbByte != sizeof(CLSID))
818 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
819 /***********************************************************************
820 * LookupETask (COMPOBJ.94)
822 OLESTATUS WINAPI LookupETask16(HTASK16 *hTask,LPVOID p) {
823 FIXME("(%p,%p),stub!\n",hTask,p);
824 if ((*hTask = GetCurrentTask()) == hETask) {
825 memcpy(p, Table_ETask, sizeof(Table_ETask));
830 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
831 /***********************************************************************
832 * SetETask (COMPOBJ.95)
834 OLESTATUS WINAPI SetETask16(HTASK16 hTask, LPVOID p) {
835 FIXME("(%04x,%p),stub!\n",hTask,p);
840 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
841 /***********************************************************************
842 * CallObjectInWOW (COMPOBJ.201)
844 OLESTATUS WINAPI CallObjectInWOW(LPVOID p1,LPVOID p2) {
845 FIXME("(%p,%p),stub!\n",p1,p2);
849 /******************************************************************************
850 * CoRegisterClassObject16 [COMPOBJ.5]
852 * Don't know where it registers it ...
854 HRESULT WINAPI CoRegisterClassObject16(
857 DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
858 DWORD flags, /* [in] REGCLS flags indicating how connections are made */
863 WINE_StringFromCLSID(rclsid,buf);
865 FIXME("(%s,%p,0x%08lx,0x%08lx,%p),stub\n",
866 buf,pUnk,dwClsContext,flags,lpdwRegister
872 /******************************************************************************
873 * CoRevokeClassObject16 [COMPOBJ.6]
876 HRESULT WINAPI CoRevokeClassObject16(DWORD dwRegister /* token on class obj */)
878 FIXME("(0x%08lx),stub!\n", dwRegister);
884 * COM_GetRegisteredClassObject
886 * This internal method is used to scan the registered class list to
887 * find a class object.
890 * rclsid Class ID of the class to find.
891 * dwClsContext Class context to match.
892 * ppv [out] returns a pointer to the class object. Complying
893 * to normal COM usage, this method will increase the
894 * reference count on this object.
896 static HRESULT COM_GetRegisteredClassObject(
901 RegisteredClass* curClass;
909 * Iterate through the whole list and try to match the class ID.
911 curClass = firstRegisteredClass;
913 while (curClass != 0)
916 * Check if we have a match on the class ID.
918 if (IsEqualGUID32(&(curClass->classIdentifier), rclsid))
921 * Since we don't do out-of process or DCOM just right away, let's ignore the
926 * We have a match, return the pointer to the class object.
928 *ppUnk = curClass->classObject;
930 IUnknown_AddRef(curClass->classObject);
936 * Step to the next class in the list.
938 curClass = curClass->nextClass;
942 * If we get to here, we haven't found our class.
947 /******************************************************************************
948 * CoRegisterClassObject [OLE32.36]
950 * This method will register the class object for a given class ID.
952 * See the Windows documentation for more details.
954 HRESULT WINAPI CoRegisterClassObject(
957 DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
958 DWORD flags, /* [in] REGCLS flags indicating how connections are made */
962 RegisteredClass* newClass;
963 LPUNKNOWN foundObject;
967 WINE_StringFromCLSID(rclsid,buf);
969 TRACE("(%s,%p,0x%08lx,0x%08lx,%p)\n",
970 buf,pUnk,dwClsContext,flags,lpdwRegister);
973 * Perform a sanity check on the parameters
975 if ( (lpdwRegister==0) || (pUnk==0) )
981 * Initialize the cookie (out parameter)
986 * First, check if the class is already registered.
987 * If it is, this should cause an error.
989 hr = COM_GetRegisteredClassObject(rclsid, dwClsContext, &foundObject);
994 * The COM_GetRegisteredClassObject increased the reference count on the
995 * object so it has to be released.
997 IUnknown_Release(foundObject);
999 return CO_E_OBJISREG;
1003 * If it is not registered, we must create a new entry for this class and
1004 * append it to the registered class list.
1005 * We use the address of the chain node as the cookie since we are sure it's
1008 newClass = HeapAlloc(GetProcessHeap(), 0, sizeof(RegisteredClass));
1011 * Initialize the node.
1013 newClass->classIdentifier = *rclsid;
1014 newClass->runContext = dwClsContext;
1015 newClass->connectFlags = flags;
1016 newClass->dwCookie = (DWORD)newClass;
1017 newClass->nextClass = firstRegisteredClass;
1020 * Since we're making a copy of the object pointer, we have to increase its
1023 newClass->classObject = pUnk;
1024 IUnknown_AddRef(newClass->classObject);
1026 firstRegisteredClass = newClass;
1029 * Assign the out parameter (cookie)
1031 *lpdwRegister = newClass->dwCookie;
1034 * We're successful Yippee!
1039 /***********************************************************************
1040 * CoRevokeClassObject [OLE32.40]
1042 * This method will remove a class object from the class registry
1044 * See the Windows documentation for more details.
1046 HRESULT WINAPI CoRevokeClassObject(
1049 RegisteredClass** prevClassLink;
1050 RegisteredClass* curClass;
1052 TRACE("(%08lx)\n",dwRegister);
1055 * Iterate through the whole list and try to match the cookie.
1057 curClass = firstRegisteredClass;
1058 prevClassLink = &firstRegisteredClass;
1060 while (curClass != 0)
1063 * Check if we have a match on the cookie.
1065 if (curClass->dwCookie == dwRegister)
1068 * Remove the class from the chain.
1070 *prevClassLink = curClass->nextClass;
1073 * Release the reference to the class object.
1075 IUnknown_Release(curClass->classObject);
1078 * Free the memory used by the chain node.
1080 HeapFree(GetProcessHeap(), 0, curClass);
1086 * Step to the next class in the list.
1088 prevClassLink = &(curClass->nextClass);
1089 curClass = curClass->nextClass;
1093 * If we get to here, we haven't found our class.
1095 return E_INVALIDARG;
1098 /***********************************************************************
1099 * CoGetClassObject [COMPOBJ.7]
1101 HRESULT WINAPI CoGetClassObject(REFCLSID rclsid, DWORD dwClsContext,
1102 LPVOID pvReserved, REFIID iid, LPVOID *ppv)
1104 LPUNKNOWN regClassObject;
1105 HRESULT hres = E_UNEXPECTED;
1107 WCHAR dllName[MAX_PATH+1];
1108 DWORD dllNameLen = sizeof(dllName);
1110 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid,
1111 REFIID iid, LPVOID *ppv);
1112 DllGetClassObjectFunc DllGetClassObject;
1114 WINE_StringFromCLSID((LPCLSID)rclsid,xclsid);
1116 TRACE("\n\tCLSID:\t%s,\n\tIID:\t%s\n",
1117 debugstr_guid(rclsid),
1122 * First, try and see if we can't match the class ID with one of the
1123 * registered classes.
1125 if (S_OK == COM_GetRegisteredClassObject(rclsid, dwClsContext, ®ClassObject))
1128 * Get the required interface from the retrieved pointer.
1130 hres = IUnknown_QueryInterface(regClassObject, iid, ppv);
1133 * Since QI got another reference on the pointer, we want to release the
1134 * one we already have. If QI was unsuccessful, this will release the object. This
1135 * is good since we are not returning it in the "out" parameter.
1137 IUnknown_Release(regClassObject);
1142 /* out of process and remote servers not supported yet */
1143 if (((CLSCTX_LOCAL_SERVER|CLSCTX_REMOTE_SERVER) & dwClsContext)
1144 && !((CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER) & dwClsContext)){
1145 FIXME("CLSCTX_LOCAL_SERVER and CLSCTX_REMOTE_SERVER not supported!\n");
1146 return E_ACCESSDENIED;
1149 if ((CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER) & dwClsContext) {
1151 WCHAR valname[]={ 'I','n','p','r','o','c',
1152 'S','e','r','v','e','r','3','2',0};
1154 /* lookup CLSID in registry key HKCR/CLSID */
1155 hres = RegOpenKeyExA(HKEY_CLASSES_ROOT, "CLSID", 0,
1156 KEY_READ, &CLSIDkey);
1158 if (hres != ERROR_SUCCESS)
1159 return REGDB_E_READREGDB;
1160 hres = RegOpenKeyExA(CLSIDkey,xclsid,0,KEY_QUERY_VALUE,&key);
1161 if (hres != ERROR_SUCCESS) {
1162 RegCloseKey(CLSIDkey);
1163 return REGDB_E_CLASSNOTREG;
1165 memset(dllName,0,sizeof(dllName));
1166 hres = RegQueryValueW(key, valname, dllName, &dllNameLen);
1168 ERR("RegQueryValue of %s failed with hres %lx\n",debugstr_w(dllName),hres);
1169 return REGDB_E_CLASSNOTREG; /* FIXME: check retval */
1172 RegCloseKey(CLSIDkey);
1173 if (hres != ERROR_SUCCESS)
1174 return REGDB_E_READREGDB;
1175 TRACE("found InprocServer32 dll %s\n", debugstr_w(dllName));
1177 /* open dll, call DllGetClassObject */
1178 hLibrary = CoLoadLibrary(dllName, TRUE);
1179 if (hLibrary == 0) {
1180 FIXME("couldn't load InprocServer32 dll %s\n", debugstr_w(dllName));
1181 return E_ACCESSDENIED; /* or should this be CO_E_DLLNOTFOUND? */
1183 DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress(hLibrary, "DllGetClassObject");
1184 if (!DllGetClassObject) {
1185 /* not sure if this should be called here CoFreeLibrary(hLibrary);*/
1186 FIXME("couldn't find function DllGetClassObject in %s\n", debugstr_w(dllName));
1187 return E_ACCESSDENIED;
1191 * Ask the DLL for its class object. (there was a note here about class
1192 * factories but this is good.
1194 return DllGetClassObject(rclsid, iid, ppv);
1199 /***********************************************************************
1200 * CoResumeClassObjects
1202 * Resumes classobjects registered with REGCLS suspended
1204 HRESULT WINAPI CoResumeClassObjects(void)
1210 /***********************************************************************
1213 * This function supplies the CLSID associated with the given filename.
1215 HRESULT WINAPI GetClassFile(LPOLESTR filePathName,CLSID *pclsid)
1219 int nbElm=0,length=0,i=0;
1221 LPOLESTR *pathDec=0,absFile=0,progId=0;
1222 WCHAR extention[100]={0};
1226 /* if the file contain a storage object the return the CLSID writen by IStorage_SetClass method*/
1227 if((StgIsStorageFile(filePathName))==S_OK){
1229 res=StgOpenStorage(filePathName,NULL,STGM_READ | STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
1232 res=ReadClassStg(pstg,pclsid);
1234 IStorage_Release(pstg);
1238 /* if the file is not a storage object then attemps to match various bits in the file against a
1239 pattern in the registry. this case is not frequently used ! so I present only the psodocode for
1242 for(i=0;i<nFileTypes;i++)
1244 for(i=0;j<nPatternsForType;j++){
1249 pat=ReadPatternFromRegistry(i,j);
1250 hFile=CreateFileW(filePathName,,,,,,hFile);
1251 SetFilePosition(hFile,pat.offset);
1252 ReadFile(hFile,buf,pat.size,NULL,NULL);
1253 if (memcmp(buf&pat.mask,pat.pattern.pat.size)==0){
1255 *pclsid=ReadCLSIDFromRegistry(i);
1261 /* if the obove strategies fail then search for the extension key in the registry */
1263 /* get the last element (absolute file) in the path name */
1264 nbElm=FileMonikerImpl_DecomposePath(filePathName,&pathDec);
1265 absFile=pathDec[nbElm-1];
1267 /* failed if the path represente a directory and not an absolute file name*/
1268 if (lstrcmpW(absFile,(LPOLESTR)"\\"))
1269 return MK_E_INVALIDEXTENSION;
1271 /* get the extension of the file */
1272 length=lstrlenW(absFile);
1273 for(i=length-1; ( (i>=0) && (extention[i]=absFile[i]) );i--);
1275 /* get the progId associated to the extension */
1276 progId=CoTaskMemAlloc(sizeProgId);
1278 res=RegQueryValueW(HKEY_CLASSES_ROOT,extention,progId,&sizeProgId);
1280 if (res==ERROR_MORE_DATA){
1282 progId = CoTaskMemRealloc(progId,sizeProgId);
1283 res=RegQueryValueW(HKEY_CLASSES_ROOT,extention,progId,&sizeProgId);
1285 if (res==ERROR_SUCCESS)
1286 /* return the clsid associated to the progId */
1287 res= CLSIDFromProgID(progId,pclsid);
1289 for(i=0; pathDec[i]!=NULL;i++)
1290 CoTaskMemFree(pathDec[i]);
1291 CoTaskMemFree(pathDec);
1293 CoTaskMemFree(progId);
1295 if (res==ERROR_SUCCESS)
1298 return MK_E_INVALIDEXTENSION;
1300 /******************************************************************************
1301 * CoRegisterMessageFilter16 [COMPOBJ.27]
1303 HRESULT WINAPI CoRegisterMessageFilter16(
1304 LPMESSAGEFILTER lpMessageFilter,
1305 LPMESSAGEFILTER *lplpMessageFilter
1307 FIXME("(%p,%p),stub!\n",lpMessageFilter,lplpMessageFilter);
1311 /***********************************************************************
1312 * CoCreateInstance [COMPOBJ.13, OLE32.7]
1314 HRESULT WINAPI CoCreateInstance(
1316 LPUNKNOWN pUnkOuter,
1322 LPCLASSFACTORY lpclf = 0;
1331 * Initialize the "out" parameter
1336 * Get a class factory to construct the object we want.
1338 hres = CoGetClassObject(rclsid,
1348 * Create the object and don't forget to release the factory
1350 hres = IClassFactory_CreateInstance(lpclf, pUnkOuter, iid, ppv);
1351 IClassFactory_Release(lpclf);
1356 /***********************************************************************
1357 * CoCreateInstanceEx [OLE32.165]
1359 HRESULT WINAPI CoCreateInstanceEx(
1361 LPUNKNOWN pUnkOuter,
1363 COSERVERINFO* pServerInfo,
1367 IUnknown* pUnk = NULL;
1370 int successCount = 0;
1375 if ( (cmq==0) || (pResults==NULL))
1376 return E_INVALIDARG;
1378 if (pServerInfo!=NULL)
1379 FIXME("() non-NULL pServerInfo not supported!\n");
1382 * Initialize all the "out" parameters.
1384 for (index = 0; index < cmq; index++)
1386 pResults[index].pItf = NULL;
1387 pResults[index].hr = E_NOINTERFACE;
1391 * Get the object and get its IUnknown pointer.
1393 hr = CoCreateInstance(rclsid,
1403 * Then, query for all the interfaces requested.
1405 for (index = 0; index < cmq; index++)
1407 pResults[index].hr = IUnknown_QueryInterface(pUnk,
1408 pResults[index].pIID,
1409 (VOID**)&(pResults[index].pItf));
1411 if (pResults[index].hr == S_OK)
1416 * Release our temporary unknown pointer.
1418 IUnknown_Release(pUnk);
1420 if (successCount == 0)
1421 return E_NOINTERFACE;
1423 if (successCount!=cmq)
1424 return CO_S_NOTALLINTERFACES;
1429 /***********************************************************************
1430 * CoFreeLibrary [COMPOBJ.13]
1432 void WINAPI CoFreeLibrary(HINSTANCE hLibrary)
1434 OpenDll *ptr, *prev;
1437 /* lookup library in linked list */
1439 for (ptr = openDllList; ptr != NULL; ptr=ptr->next) {
1440 if (ptr->hLibrary == hLibrary) {
1447 /* shouldn't happen if user passed in a valid hLibrary */
1450 /* assert: ptr points to the library entry to free */
1452 /* free library and remove node from list */
1453 FreeLibrary(hLibrary);
1454 if (ptr == openDllList) {
1455 tmp = openDllList->next;
1456 HeapFree(GetProcessHeap(), 0, openDllList);
1460 HeapFree(GetProcessHeap(), 0, ptr);
1467 /***********************************************************************
1468 * CoFreeAllLibraries [COMPOBJ.12]
1470 void WINAPI CoFreeAllLibraries(void)
1474 for (ptr = openDllList; ptr != NULL; ) {
1476 CoFreeLibrary(ptr->hLibrary);
1483 /***********************************************************************
1484 * CoFreeUnusedLibraries [COMPOBJ.17]
1486 void WINAPI CoFreeUnusedLibraries(void)
1489 typedef HRESULT(*DllCanUnloadNowFunc)(void);
1490 DllCanUnloadNowFunc DllCanUnloadNow;
1492 for (ptr = openDllList; ptr != NULL; ) {
1493 DllCanUnloadNow = (DllCanUnloadNowFunc)
1494 GetProcAddress(ptr->hLibrary, "DllCanUnloadNow");
1496 if ( (DllCanUnloadNow != NULL) &&
1497 (DllCanUnloadNow() == S_OK) ) {
1499 CoFreeLibrary(ptr->hLibrary);
1507 /***********************************************************************
1508 * CoFileTimeNow [COMPOBJ.82, OLE32.10]
1510 * the current system time in lpFileTime
1512 HRESULT WINAPI CoFileTimeNow(
1513 FILETIME *lpFileTime /* [out] the current time */
1515 DOSFS_UnixTimeToFileTime(time(NULL), lpFileTime, 0);
1519 /***********************************************************************
1520 * CoTaskMemAlloc (OLE32.43)
1522 * pointer to newly allocated block
1524 LPVOID WINAPI CoTaskMemAlloc(
1525 ULONG size /* [in] size of memoryblock to be allocated */
1528 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1533 return IMalloc_Alloc(lpmalloc,size);
1535 /***********************************************************************
1536 * CoTaskMemFree (OLE32.44)
1538 VOID WINAPI CoTaskMemFree(
1539 LPVOID ptr /* [in] pointer to be freed */
1542 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1547 IMalloc_Free(lpmalloc, ptr);
1550 /***********************************************************************
1551 * CoTaskMemRealloc (OLE32.45)
1553 * pointer to newly allocated block
1555 LPVOID WINAPI CoTaskMemRealloc(
1557 ULONG size) /* [in] size of memoryblock to be allocated */
1560 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1565 return IMalloc_Realloc(lpmalloc, pvOld, size);
1568 /***********************************************************************
1569 * CoLoadLibrary (OLE32.30)
1571 HINSTANCE WINAPI CoLoadLibrary(LPOLESTR lpszLibName, BOOL bAutoFree)
1577 TRACE("CoLoadLibrary(%p, %d\n", debugstr_w(lpszLibName), bAutoFree);
1579 hLibrary = LoadLibraryExW(lpszLibName, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
1584 if (openDllList == NULL) {
1585 /* empty list -- add first node */
1586 openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
1587 openDllList->hLibrary=hLibrary;
1588 openDllList->next = NULL;
1590 /* search for this dll */
1592 for (ptr = openDllList; ptr->next != NULL; ptr=ptr->next) {
1593 if (ptr->hLibrary == hLibrary) {
1599 /* dll not found, add it */
1601 openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
1602 openDllList->hLibrary = hLibrary;
1603 openDllList->next = tmp;
1610 /***********************************************************************
1611 * CoInitializeWOW (OLE32.27)
1613 HRESULT WINAPI CoInitializeWOW(DWORD x,DWORD y) {
1614 FIXME("(0x%08lx,0x%08lx),stub!\n",x,y);
1618 /******************************************************************************
1619 * CoLockObjectExternal16 [COMPOBJ.63]
1621 HRESULT WINAPI CoLockObjectExternal16(
1622 LPUNKNOWN pUnk, /* [in] object to be locked */
1623 BOOL16 fLock, /* [in] do lock */
1624 BOOL16 fLastUnlockReleases /* [in] ? */
1626 FIXME("(%p,%d,%d),stub!\n",pUnk,fLock,fLastUnlockReleases);
1630 /******************************************************************************
1631 * CoLockObjectExternal [OLE32.31]
1633 HRESULT WINAPI CoLockObjectExternal(
1634 LPUNKNOWN pUnk, /* [in] object to be locked */
1635 BOOL fLock, /* [in] do lock */
1636 BOOL fLastUnlockReleases) /* [in] unlock all */
1642 * Increment the external lock coutner, COM_ExternalLockAddRef also
1643 * increment the object's internal lock counter.
1645 COM_ExternalLockAddRef( pUnk);
1650 * Decrement the external lock coutner, COM_ExternalLockRelease also
1651 * decrement the object's internal lock counter.
1653 COM_ExternalLockRelease( pUnk, fLastUnlockReleases);
1659 /***********************************************************************
1660 * CoGetState16 [COMPOBJ.115]
1662 HRESULT WINAPI CoGetState16(LPDWORD state)
1664 FIXME("(%p),stub!\n", state);
1668 /***********************************************************************
1669 * CoSetState [COM32.42]
1671 HRESULT WINAPI CoSetState(LPDWORD state)
1673 FIXME("(%p),stub!\n", state);
1674 if (state) *state = 0;
1677 /***********************************************************************
1678 * CoCreateFreeThreadedMarshaler [OLE32.5]
1680 HRESULT WINAPI CoCreateFreeThreadedMarshaler (LPUNKNOWN punkOuter, LPUNKNOWN* ppunkMarshal)
1682 FIXME ("(%p %p): stub\n", punkOuter, ppunkMarshal);
1688 /***********************************************************************
1689 * DllGetClassObject [OLE32.63]
1691 HRESULT WINAPI OLE32_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
1693 FIXME("\n\tCLSID:\t%s,\n\tIID:\t%s\n",debugstr_guid(rclsid),debugstr_guid(iid));
1695 return CLASS_E_CLASSNOTAVAILABLE;
1700 * COM_RevokeAllClasses
1702 * This method is called when the COM libraries are uninitialized to
1703 * release all the references to the class objects registered with
1706 static void COM_RevokeAllClasses()
1708 while (firstRegisteredClass!=0)
1710 CoRevokeClassObject(firstRegisteredClass->dwCookie);
1714 /****************************************************************************
1715 * COM External Lock methods implementation
1718 /****************************************************************************
1719 * Public - Method that increments the count for a IUnknown* in the linked
1720 * list. The item is inserted if not already in the list.
1722 static void COM_ExternalLockAddRef(
1725 COM_ExternalLock *externalLock = COM_ExternalLockFind(pUnk);
1728 * Add an external lock to the object. If it was already externally
1729 * locked, just increase the reference count. If it was not.
1730 * add the item to the list.
1732 if ( externalLock == EL_NOT_FOUND )
1733 COM_ExternalLockInsert(pUnk);
1735 externalLock->uRefCount++;
1738 * Add an internal lock to the object
1740 IUnknown_AddRef(pUnk);
1743 /****************************************************************************
1744 * Public - Method that decrements the count for a IUnknown* in the linked
1745 * list. The item is removed from the list if its count end up at zero or if
1748 static void COM_ExternalLockRelease(
1752 COM_ExternalLock *externalLock = COM_ExternalLockFind(pUnk);
1754 if ( externalLock != EL_NOT_FOUND )
1758 externalLock->uRefCount--; /* release external locks */
1759 IUnknown_Release(pUnk); /* release local locks as well */
1761 if ( bRelAll == FALSE )
1762 break; /* perform single release */
1764 } while ( externalLock->uRefCount > 0 );
1766 if ( externalLock->uRefCount == 0 ) /* get rid of the list entry */
1767 COM_ExternalLockDelete(externalLock);
1770 /****************************************************************************
1771 * Public - Method that frees the content of the list.
1773 static void COM_ExternalLockFreeList()
1775 COM_ExternalLock *head;
1777 head = elList.head; /* grab it by the head */
1778 while ( head != EL_END_OF_LIST )
1780 COM_ExternalLockDelete(head); /* get rid of the head stuff */
1782 head = elList.head; /* get the new head... */
1786 /****************************************************************************
1787 * Public - Method that dump the content of the list.
1789 void COM_ExternalLockDump()
1791 COM_ExternalLock *current = elList.head;
1793 DPRINTF("\nExternal lock list contains:\n");
1795 while ( current != EL_END_OF_LIST )
1797 DPRINTF( "\t%p with %lu references count.\n", current->pUnk, current->uRefCount);
1799 /* Skip to the next item */
1800 current = current->next;
1805 /****************************************************************************
1806 * Internal - Find a IUnknown* in the linked list
1808 static COM_ExternalLock* COM_ExternalLockFind(
1811 return COM_ExternalLockLocate(elList.head, pUnk);
1814 /****************************************************************************
1815 * Internal - Recursivity agent for IUnknownExternalLockList_Find
1817 static COM_ExternalLock* COM_ExternalLockLocate(
1818 COM_ExternalLock *element,
1821 if ( element == EL_END_OF_LIST )
1822 return EL_NOT_FOUND;
1824 else if ( element->pUnk == pUnk ) /* We found it */
1827 else /* Not the right guy, keep on looking */
1828 return COM_ExternalLockLocate( element->next, pUnk);
1831 /****************************************************************************
1832 * Internal - Insert a new IUnknown* to the linked list
1834 static BOOL COM_ExternalLockInsert(
1837 COM_ExternalLock *newLock = NULL;
1838 COM_ExternalLock *previousHead = NULL;
1841 * Allocate space for the new storage object
1843 newLock = HeapAlloc(GetProcessHeap(), 0, sizeof(COM_ExternalLock));
1847 if ( elList.head == EL_END_OF_LIST )
1849 elList.head = newLock; /* The list is empty */
1854 * insert does it at the head
1856 previousHead = elList.head;
1857 elList.head = newLock;
1861 * Set new list item data member
1863 newLock->pUnk = pUnk;
1864 newLock->uRefCount = 1;
1865 newLock->next = previousHead;
1873 /****************************************************************************
1874 * Internal - Method that removes an item from the linked list.
1876 static void COM_ExternalLockDelete(
1877 COM_ExternalLock *itemList)
1879 COM_ExternalLock *current = elList.head;
1881 if ( current == itemList )
1884 * this section handles the deletion of the first node
1886 elList.head = itemList->next;
1887 HeapFree( GetProcessHeap(), 0, itemList);
1893 if ( current->next == itemList ) /* We found the item to free */
1895 current->next = itemList->next; /* readjust the list pointers */
1897 HeapFree( GetProcessHeap(), 0, itemList);
1901 /* Skip to the next item */
1902 current = current->next;
1904 } while ( current != EL_END_OF_LIST );
1908 /***********************************************************************
1909 * COMPOBJ_DllEntryPoint [COMPOBJ.entry]
1911 * Initialization code for the COMPOBJ DLL
1915 BOOL WINAPI COMPOBJ_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst, WORD ds, WORD HeapSize, DWORD res1, WORD res2)
1917 TRACE("(%08lx, %04x, %04x, %04x, %08lx, %04x)\n", Reason, hInst, ds, HeapSize,
1921 case DLL_PROCESS_ATTACH:
1923 if(COMPOBJ_hInstance)
1925 ERR("compobj.dll instantiated twice!\n");
1927 * We should return FALSE here, but that will break
1928 * most apps that use CreateProcess because we do
1929 * not yet support seperate address-spaces.
1934 COMPOBJ_hInstance = hInst;
1937 case DLL_PROCESS_DETACH:
1938 if(!--COMPOBJ_Attach)
1939 COMPOBJ_hInstance = 0;