4 * Copyright 1995 Martin von Loewis
5 * Copyright 1998 Justin Bradford
6 * Copyright 1999 Francis Beaudet
7 * Copyright 1999 Sylvain St-Germain
14 #include <sys/types.h>
17 #ifdef HAVE_SYS_FILE_H
18 # include <sys/file.h>
20 #include <sys/ioctl.h>
21 #ifdef HAVE_SYS_SOCKET_H
22 # include <sys/socket.h>
24 #ifdef HAVE_SYS_SOCKIO_H
25 # include <sys/sockio.h>
30 #ifdef HAVE_NETINET_IN_H
31 # include <netinet/in.h>
38 #include "wine/winbase16.h"
43 #include "debugtools.h"
49 #include "wine/obj_base.h"
50 #include "wine/obj_misc.h"
51 #include "wine/obj_storage.h"
52 #include "wine/obj_clientserver.h"
57 DEFAULT_DEBUG_CHANNEL(ole)
58 /****************************************************************************
59 * COM External Lock structures and methods declaration
61 * This api provides a linked list to managed external references to
64 * The public interface consists of three calls:
65 * COM_ExternalLockAddRef
66 * COM_ExternalLockRelease
67 * COM_ExternalLockFreeList
70 #define EL_END_OF_LIST 0
71 #define EL_NOT_FOUND 0
74 * Declaration of the static structure that manage the
75 * external lock to COM objects.
77 typedef struct COM_ExternalLock COM_ExternalLock;
78 typedef struct COM_ExternalLockList COM_ExternalLockList;
80 struct COM_ExternalLock
82 IUnknown *pUnk; /* IUnknown referenced */
83 ULONG uRefCount; /* external lock counter to IUnknown object*/
84 COM_ExternalLock *next; /* Pointer to next element in list */
87 struct COM_ExternalLockList
89 COM_ExternalLock *head; /* head of list */
93 * Declaration and initialization of the static structure that manages
94 * the external lock to COM objects.
96 static COM_ExternalLockList elList = { EL_END_OF_LIST };
99 * Public Interface to the external lock list
101 static void COM_ExternalLockFreeList();
102 static void COM_ExternalLockAddRef(IUnknown *pUnk);
103 static void COM_ExternalLockRelease(IUnknown *pUnk, BOOL bRelAll);
104 void COM_ExternalLockDump(); /* testing purposes, not static to avoid warning */
107 * Private methods used to managed the linked list
109 static BOOL COM_ExternalLockInsert(
112 static void COM_ExternalLockDelete(
113 COM_ExternalLock *element);
115 static COM_ExternalLock* COM_ExternalLockFind(
118 static COM_ExternalLock* COM_ExternalLockLocate(
119 COM_ExternalLock *element,
122 /****************************************************************************
123 * This section defines variables internal to the COM module.
125 * TODO: Most of these things will have to be made thread-safe.
127 HINSTANCE16 COMPOBJ_hInstance = 0;
128 HINSTANCE COMPOBJ_hInstance32 = 0;
129 static int COMPOBJ_Attach = 0;
131 LPMALLOC16 currentMalloc16=NULL;
132 LPMALLOC currentMalloc32=NULL;
135 WORD Table_ETask[62];
138 * This lock count counts the number of times CoInitialize is called. It is
139 * decreased every time CoUninitialize is called. When it hits 0, the COM
140 * libraries are freed
142 static ULONG s_COMLockCount = 0;
145 * This linked list contains the list of registered class objects. These
146 * are mostly used to register the factories for out-of-proc servers of OLE
149 * TODO: Make this data structure aware of inter-process communication. This
150 * means that parts of this will be exported to the Wine Server.
152 typedef struct tagRegisteredClass
154 CLSID classIdentifier;
155 LPUNKNOWN classObject;
159 struct tagRegisteredClass* nextClass;
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 char *DllName; /* really only needed for debugging */
171 struct tagOpenDll *next;
174 static OpenDll *openDllList = NULL; /* linked list of open dlls */
176 /*****************************************************************************
177 * This section contains prototypes to internal methods for this
180 static HRESULT COM_GetRegisteredClassObject(REFCLSID rclsid,
184 static void COM_RevokeAllClasses();
187 /******************************************************************************
188 * CoBuildVersion [COMPOBJ.1]
191 * Current build version, hiword is majornumber, loword is minornumber
193 DWORD WINAPI CoBuildVersion(void)
195 TRACE("Returning version %d, build %d.\n", rmm, rup);
196 return (rmm<<16)+rup;
199 /******************************************************************************
200 * CoInitialize16 [COMPOBJ.2]
201 * Set the win16 IMalloc used for memory management
203 HRESULT WINAPI CoInitialize16(
204 LPVOID lpReserved /* [in] pointer to win16 malloc interface */
206 currentMalloc16 = (LPMALLOC16)lpReserved;
210 /******************************************************************************
211 * CoInitialize32 [OLE32.26]
213 * Initializes the COM libraries.
215 * See CoInitializeEx32
217 HRESULT WINAPI CoInitialize(
218 LPVOID lpReserved /* [in] pointer to win32 malloc interface
219 (obsolete, should be NULL) */
223 * Just delegate to the newer method.
225 return CoInitializeEx(lpReserved, COINIT_APARTMENTTHREADED);
228 /******************************************************************************
229 * CoInitializeEx32 [OLE32.163]
231 * Initializes the COM libraries. The behavior used to set the win32 IMalloc
232 * used for memory management is obsolete.
235 * S_OK if successful,
236 * S_FALSE if this function was called already.
237 * RPC_E_CHANGED_MODE if a previous call to CoInitialize specified another
241 * Only the single threaded model is supported. As a result RPC_E_CHANGED_MODE
244 * See the windows documentation for more details.
246 HRESULT WINAPI CoInitializeEx(
247 LPVOID lpReserved, /* [in] pointer to win32 malloc interface
248 (obsolete, should be NULL) */
249 DWORD dwCoInit /* [in] A value from COINIT specifies the threading model */
254 TRACE("(%p, %x)\n", lpReserved, (int)dwCoInit);
256 if (lpReserved!=NULL)
258 ERR("(%p, %x) - Bad parameter passed-in %p, must be an old Windows Application\n", lpReserved, (int)dwCoInit, lpReserved);
262 * Check for unsupported features.
264 if (dwCoInit!=COINIT_APARTMENTTHREADED)
266 FIXME(":(%p,%x): unsupported flag %x\n", lpReserved, (int)dwCoInit, (int)dwCoInit);
267 /* Hope for the best and continue anyway */
271 * Check the lock count. If this is the first time going through the initialize
272 * process, we have to initialize the libraries.
274 if (s_COMLockCount==0)
277 * Initialize the various COM libraries and data structures.
279 TRACE("() - Initializing the COM libraries\n");
281 RunningObjectTableImpl_Initialize();
289 * Crank-up that lock count.
296 /***********************************************************************
297 * CoUninitialize16 [COMPOBJ.3]
298 * Don't know what it does.
299 * 3-Nov-98 -- this was originally misspelled, I changed it to what I
300 * believe is the correct spelling
302 void WINAPI CoUninitialize16(void)
305 CoFreeAllLibraries();
308 /***********************************************************************
309 * CoUninitialize32 [OLE32.47]
311 * This method will release the COM libraries.
313 * See the windows documentation for more details.
315 void WINAPI CoUninitialize(void)
320 * Decrease the reference count.
325 * If we are back to 0 locks on the COM library, make sure we free
326 * all the associated data structures.
328 if (s_COMLockCount==0)
331 * Release the various COM libraries and data structures.
333 TRACE("() - Releasing the COM libraries\n");
335 RunningObjectTableImpl_UnInitialize();
337 * Release the references to the registered class objects.
339 COM_RevokeAllClasses();
342 * This will free the loaded COM Dlls.
344 CoFreeAllLibraries();
347 * This will free list of external references to COM objects.
349 COM_ExternalLockFreeList();
353 /***********************************************************************
354 * CoGetMalloc16 [COMPOBJ.4]
356 * The current win16 IMalloc
358 HRESULT WINAPI CoGetMalloc16(
359 DWORD dwMemContext, /* [in] unknown */
360 LPMALLOC16 * lpMalloc /* [out] current win16 malloc interface */
363 currentMalloc16 = IMalloc16_Constructor();
364 *lpMalloc = currentMalloc16;
368 /******************************************************************************
369 * CoGetMalloc32 [OLE32.20]
372 * The current win32 IMalloc
374 HRESULT WINAPI CoGetMalloc(
375 DWORD dwMemContext, /* [in] unknown */
376 LPMALLOC *lpMalloc /* [out] current win32 malloc interface */
379 currentMalloc32 = IMalloc_Constructor();
380 *lpMalloc = currentMalloc32;
384 /***********************************************************************
385 * CoCreateStandardMalloc16 [COMPOBJ.71]
387 HRESULT WINAPI CoCreateStandardMalloc16(DWORD dwMemContext,
388 LPMALLOC16 *lpMalloc)
390 /* FIXME: docu says we shouldn't return the same allocator as in
392 *lpMalloc = IMalloc16_Constructor();
396 /******************************************************************************
397 * CoDisconnectObject [COMPOBJ.15]
399 HRESULT WINAPI CoDisconnectObject( LPUNKNOWN lpUnk, DWORD reserved )
401 TRACE("%p %lx\n",lpUnk,reserved);
405 /***********************************************************************
406 * IsEqualGUID16 [COMPOBJ.18]
408 * Compares two Unique Identifiers.
413 BOOL16 WINAPI IsEqualGUID16(
414 GUID* g1, /* [in] unique id 1 */
417 return !memcmp( g1, g2, sizeof(GUID) );
420 /***********************************************************************
421 * IsEqualGUID32 [OLE32.76]
423 * Compares two Unique Identifiers.
428 BOOL WINAPI IsEqualGUID32(
429 REFGUID rguid1, /* [in] unique id 1 */
430 REFGUID rguid2 /* [in] unique id 2 */
433 return !memcmp(rguid1,rguid2,sizeof(GUID));
436 /******************************************************************************
437 * CLSIDFromString16 [COMPOBJ.20]
438 * Converts a unique identifier from it's string representation into
441 * Class id: DWORD-WORD-WORD-BYTES[2]-BYTES[6]
446 HRESULT WINAPI CLSIDFromString16(
447 LPCOLESTR16 idstr, /* [in] string representation of guid */
448 CLSID *id /* [out] GUID converted from string */
450 BYTE *s = (BYTE *) idstr;
455 TRACE("%s -> %p\n", idstr, id);
457 /* quick lookup table */
458 memset(table, 0, 256);
460 for (i = 0; i < 10; i++) {
463 for (i = 0; i < 6; i++) {
464 table['A' + i] = i+10;
465 table['a' + i] = i+10;
468 /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
470 if (strlen(idstr) != 38)
471 return OLE_ERROR_OBJECT;
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]
515 * Creates a 128bit GUID.
516 * Implemented according the DCE specification for UUID generation.
517 * Code is based upon uuid library in e2fsprogs by Theodore Ts'o.
518 * Copyright (C) 1996, 1997 Theodore Ts'o.
522 * S_OK if successful.
524 HRESULT WINAPI CoCreateGuid(
525 GUID *pguid /* [out] points to the GUID to initialize */
527 static char has_init = 0;
529 static int adjustment = 0;
530 static struct timeval last = {0, 0};
531 static UINT16 clock_seq;
533 unsigned long long clock_reg;
534 UINT clock_high, clock_low;
535 UINT16 temp_clock_seq, temp_clock_mid, temp_clock_hi_and_version;
538 struct ifreq ifr, *ifrp;
544 /* Have we already tried to get the MAC address? */
547 /* BSD 4.4 defines the size of an ifreq to be
548 * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
549 * However, under earlier systems, sa_len isn't present, so
550 * the size is just sizeof(struct ifreq)
554 # define max(a,b) ((a) > (b) ? (a) : (b))
556 # define ifreq_size(i) max(sizeof(struct ifreq),\
557 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
559 # define ifreq_size(i) sizeof(struct ifreq)
560 # endif /* HAVE_SA_LEN */
562 sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
564 /* if we can't open a socket, just use random numbers */
565 /* set the multicast bit to prevent conflicts with real cards */
566 a[0] = (rand() & 0xff) | 0x80;
567 a[1] = rand() & 0xff;
568 a[2] = rand() & 0xff;
569 a[3] = rand() & 0xff;
570 a[4] = rand() & 0xff;
571 a[5] = rand() & 0xff;
573 memset(buf, 0, sizeof(buf));
574 ifc.ifc_len = sizeof(buf);
576 /* get the ifconf interface */
577 if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) {
579 /* no ifconf, so just use random numbers */
580 /* set the multicast bit to prevent conflicts with real cards */
581 a[0] = (rand() & 0xff) | 0x80;
582 a[1] = rand() & 0xff;
583 a[2] = rand() & 0xff;
584 a[3] = rand() & 0xff;
585 a[4] = rand() & 0xff;
586 a[5] = rand() & 0xff;
588 /* loop through the interfaces, looking for a valid one */
590 for (i = 0; i < n; i+= ifreq_size(*ifr) ) {
591 ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i);
592 strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
593 /* try to get the address for this interface */
594 # ifdef SIOCGIFHWADDR
595 if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
597 memcpy(a, (unsigned char *)&ifr.ifr_hwaddr.sa_data, 6);
600 if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
602 memcpy(a, (unsigned char *) ifr.ifr_enaddr, 6);
604 /* XXX we don't have a way of getting the hardware address */
608 # endif /* SIOCGENADDR */
609 # endif /* SIOCGIFHWADDR */
610 /* make sure it's not blank */
611 if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5])
616 /* if we didn't find a valid address, make a random one */
617 /* once again, set multicast bit to avoid conflicts */
618 a[0] = (rand() & 0xff) | 0x80;
619 a[1] = rand() & 0xff;
620 a[2] = rand() & 0xff;
621 a[3] = rand() & 0xff;
622 a[4] = rand() & 0xff;
623 a[5] = rand() & 0xff;
630 /* no networking info, so generate a random address */
631 a[0] = (rand() & 0xff) | 0x80;
632 a[1] = rand() & 0xff;
633 a[2] = rand() & 0xff;
634 a[3] = rand() & 0xff;
635 a[4] = rand() & 0xff;
636 a[5] = rand() & 0xff;
637 #endif /* HAVE_NET_IF_H */
641 /* generate time element of GUID */
643 /* Assume that the gettimeofday() has microsecond granularity */
644 #define MAX_ADJUSTMENT 10
647 gettimeofday(&tv, 0);
648 if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
649 clock_seq = ((rand() & 0xff) << 8) + (rand() & 0xff);
654 if ((tv.tv_sec < last.tv_sec) ||
655 ((tv.tv_sec == last.tv_sec) &&
656 (tv.tv_usec < last.tv_usec))) {
657 clock_seq = (clock_seq+1) & 0x1FFF;
659 } else if ((tv.tv_sec == last.tv_sec) &&
660 (tv.tv_usec == last.tv_usec)) {
661 if (adjustment >= MAX_ADJUSTMENT)
667 clock_reg = tv.tv_usec*10 + adjustment;
668 clock_reg += ((unsigned long long) tv.tv_sec)*10000000;
669 clock_reg += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
671 clock_high = clock_reg >> 32;
672 clock_low = clock_reg;
673 temp_clock_seq = clock_seq | 0x8000;
674 temp_clock_mid = (UINT16)clock_high;
675 temp_clock_hi_and_version = (clock_high >> 16) | 0x1000;
677 /* pack the information into the GUID structure */
679 ((unsigned char*)&pguid->Data1)[3] = (unsigned char)clock_low;
681 ((unsigned char*)&pguid->Data1)[2] = (unsigned char)clock_low;
683 ((unsigned char*)&pguid->Data1)[1] = (unsigned char)clock_low;
685 ((unsigned char*)&pguid->Data1)[0] = (unsigned char)clock_low;
687 ((unsigned char*)&pguid->Data2)[1] = (unsigned char)temp_clock_mid;
688 temp_clock_mid >>= 8;
689 ((unsigned char*)&pguid->Data2)[0] = (unsigned char)temp_clock_mid;
691 ((unsigned char*)&pguid->Data3)[1] = (unsigned char)temp_clock_hi_and_version;
692 temp_clock_hi_and_version >>= 8;
693 ((unsigned char*)&pguid->Data3)[0] = (unsigned char)temp_clock_hi_and_version;
695 ((unsigned char*)pguid->Data4)[1] = (unsigned char)temp_clock_seq;
696 temp_clock_seq >>= 8;
697 ((unsigned char*)pguid->Data4)[0] = (unsigned char)temp_clock_seq;
699 ((unsigned char*)pguid->Data4)[2] = a[0];
700 ((unsigned char*)pguid->Data4)[3] = a[1];
701 ((unsigned char*)pguid->Data4)[4] = a[2];
702 ((unsigned char*)pguid->Data4)[5] = a[3];
703 ((unsigned char*)pguid->Data4)[6] = a[4];
704 ((unsigned char*)pguid->Data4)[7] = a[5];
711 /******************************************************************************
712 * CLSIDFromString32 [OLE32.3]
713 * Converts a unique identifier from it's string representation into
718 HRESULT WINAPI CLSIDFromString(
719 LPCOLESTR idstr, /* [in] string representation of GUID */
720 CLSID *id /* [out] GUID represented by above string */
722 LPOLESTR16 xid = HEAP_strdupWtoA(GetProcessHeap(),0,idstr);
723 OLESTATUS ret = CLSIDFromString16(xid,id);
725 HeapFree(GetProcessHeap(),0,xid);
729 /******************************************************************************
730 * WINE_StringFromCLSID [Internal]
731 * Converts a GUID into the respective string representation.
736 * the string representation and OLESTATUS
738 HRESULT WINE_StringFromCLSID(
739 const CLSID *id, /* [in] GUID to be converted */
740 LPSTR idstr /* [out] pointer to buffer to contain converted guid */
742 static const char *hex = "0123456789ABCDEF";
747 { ERR("called with id=Null\n");
752 sprintf(idstr, "{%08lX-%04X-%04X-%02X%02X-",
753 id->Data1, id->Data2, id->Data3,
754 id->Data4[0], id->Data4[1]);
758 for (i = 2; i < 8; i++) {
759 *s++ = hex[id->Data4[i]>>4];
760 *s++ = hex[id->Data4[i] & 0xf];
766 TRACE("%p->%s\n", id, idstr);
771 /******************************************************************************
772 * StringFromCLSID16 [COMPOBJ.19]
773 * Converts a GUID into the respective string representation.
774 * The target string is allocated using the OLE IMalloc.
776 * the string representation and OLESTATUS
778 HRESULT WINAPI StringFromCLSID16(
779 REFCLSID id, /* [in] the GUID to be converted */
780 LPOLESTR16 *idstr /* [out] a pointer to a to-be-allocated segmented pointer pointing to the resulting string */
787 ret = CoGetMalloc16(0,&mllc);
790 args[0] = (DWORD)mllc;
793 /* No need for a Callback entry, we have WOWCallback16Ex which does
794 * everything we need.
796 if (!WOWCallback16Ex(
797 (DWORD)((ICOM_VTABLE(IMalloc16)*)PTR_SEG_TO_LIN(
798 ICOM_VTBL(((LPMALLOC16)PTR_SEG_TO_LIN(mllc))))
805 WARN("CallTo16 IMalloc16 failed\n");
808 return WINE_StringFromCLSID(id,PTR_SEG_TO_LIN(*idstr));
811 /******************************************************************************
812 * StringFromCLSID32 [OLE32.151]
813 * Converts a GUID into the respective string representation.
814 * The target string is allocated using the OLE IMalloc.
816 * the string representation and OLESTATUS
818 HRESULT WINAPI StringFromCLSID(
819 REFCLSID id, /* [in] the GUID to be converted */
820 LPOLESTR *idstr /* [out] a pointer to a to-be-allocated pointer pointing to the resulting string */
826 if ((ret=CoGetMalloc(0,&mllc)))
829 ret=WINE_StringFromCLSID(id,buf);
831 *idstr = IMalloc_Alloc(mllc,strlen(buf)*2+2);
832 lstrcpyAtoW(*idstr,buf);
837 /******************************************************************************
838 * StringFromGUID2 [COMPOBJ.76] [OLE32.152]
840 * Converts a global unique identifier into a string of an API-
841 * specified fixed format. (The usual {.....} stuff.)
844 * The (UNICODE) string representation of the GUID in 'str'
845 * The length of the resulting string, 0 if there was any problem.
848 StringFromGUID2(REFGUID id, LPOLESTR str, INT cmax)
852 if (WINE_StringFromCLSID(id,xguid))
854 if (strlen(xguid)>=cmax)
856 lstrcpyAtoW(str,xguid);
857 return strlen(xguid);
860 /******************************************************************************
861 * ProgIDFromCLSID [OLE32.133]
862 * Converts a class id into the respective Program ID. (By using a registry lookup)
863 * RETURNS S_OK on success
864 * riid associated with the progid
867 HRESULT WINAPI ProgIDFromCLSID(
868 REFCLSID clsid, /* [in] class id as found in registry */
869 LPOLESTR *lplpszProgID/* [out] associated Prog ID */
872 char strCLSID[50], *buf, *buf2;
878 WINE_StringFromCLSID(clsid, strCLSID);
880 buf = HeapAlloc(GetProcessHeap(), 0, strlen(strCLSID)+14);
881 sprintf(buf,"CLSID\\%s\\ProgID", strCLSID);
882 if (RegOpenKeyA(HKEY_CLASSES_ROOT, buf, &xhkey))
883 ret = REGDB_E_CLASSNOTREG;
885 HeapFree(GetProcessHeap(), 0, buf);
889 buf2 = HeapAlloc(GetProcessHeap(), 0, 255);
891 if (RegQueryValueA(xhkey, NULL, buf2, &buf2len))
892 ret = REGDB_E_CLASSNOTREG;
896 if (CoGetMalloc(0,&mllc))
900 *lplpszProgID = IMalloc_Alloc(mllc, (buf2len+1)*2);
901 lstrcpyAtoW(*lplpszProgID, buf2);
904 HeapFree(GetProcessHeap(), 0, buf2);
911 /******************************************************************************
912 * CLSIDFromProgID16 [COMPOBJ.61]
913 * Converts a program id into the respective GUID. (By using a registry lookup)
915 * riid associated with the progid
917 HRESULT WINAPI CLSIDFromProgID16(
918 LPCOLESTR16 progid, /* [in] program id as found in registry */
919 LPCLSID riid /* [out] associated CLSID */
926 buf = HeapAlloc(GetProcessHeap(),0,strlen(progid)+8);
927 sprintf(buf,"%s\\CLSID",progid);
928 if ((err=RegOpenKeyA(HKEY_CLASSES_ROOT,buf,&xhkey))) {
929 HeapFree(GetProcessHeap(),0,buf);
930 return OLE_ERROR_GENERIC;
932 HeapFree(GetProcessHeap(),0,buf);
933 buf2len = sizeof(buf2);
934 if ((err=RegQueryValueA(xhkey,NULL,buf2,&buf2len))) {
936 return OLE_ERROR_GENERIC;
939 return CLSIDFromString16(buf2,riid);
942 /******************************************************************************
943 * CLSIDFromProgID32 [OLE32.2]
944 * Converts a program id into the respective GUID. (By using a registry lookup)
946 * riid associated with the progid
948 HRESULT WINAPI CLSIDFromProgID(
949 LPCOLESTR progid, /* [in] program id as found in registry */
950 LPCLSID riid /* [out] associated CLSID */
952 LPOLESTR16 pid = HEAP_strdupWtoA(GetProcessHeap(),0,progid);
953 OLESTATUS ret = CLSIDFromProgID16(pid,riid);
955 HeapFree(GetProcessHeap(),0,pid);
959 /************************************************************************************************
962 * This function write a CLSID on stream
964 HRESULT WINAPI WriteClassStm(IStream *pStm,REFCLSID rclsid)
966 TRACE("(%p,%p)\n",pStm,rclsid);
971 return IStream_Write(pStm,rclsid,sizeof(CLSID),NULL);
974 /************************************************************************************************
977 * This function read a CLSID from a stream
979 HRESULT WINAPI ReadClassStm(IStream *pStm,REFCLSID rclsid)
984 TRACE("(%p,%p)\n",pStm,rclsid);
989 res = IStream_Read(pStm,(void*)rclsid,sizeof(CLSID),&nbByte);
994 if (nbByte != sizeof(CLSID))
1000 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
1001 /***********************************************************************
1002 * LookupETask (COMPOBJ.94)
1004 OLESTATUS WINAPI LookupETask16(HTASK16 *hTask,LPVOID p) {
1005 FIXME("(%p,%p),stub!\n",hTask,p);
1006 if ((*hTask = GetCurrentTask()) == hETask) {
1007 memcpy(p, Table_ETask, sizeof(Table_ETask));
1012 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
1013 /***********************************************************************
1014 * SetETask (COMPOBJ.95)
1016 OLESTATUS WINAPI SetETask16(HTASK16 hTask, LPVOID p) {
1017 FIXME("(%04x,%p),stub!\n",hTask,p);
1022 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
1023 /***********************************************************************
1024 * CallObjectInWOW (COMPOBJ.201)
1026 OLESTATUS WINAPI CallObjectInWOW(LPVOID p1,LPVOID p2) {
1027 FIXME("(%p,%p),stub!\n",p1,p2);
1031 /******************************************************************************
1032 * CoRegisterClassObject16 [COMPOBJ.5]
1034 * Don't know where it registers it ...
1036 HRESULT WINAPI CoRegisterClassObject16(
1039 DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
1040 DWORD flags, /* [in] REGCLS flags indicating how connections are made */
1041 LPDWORD lpdwRegister
1045 WINE_StringFromCLSID(rclsid,buf);
1047 FIXME("(%s,%p,0x%08lx,0x%08lx,%p),stub\n",
1048 buf,pUnk,dwClsContext,flags,lpdwRegister
1054 /******************************************************************************
1055 * CoRevokeClassObject16 [COMPOBJ.6]
1058 HRESULT WINAPI CoRevokeClassObject16(DWORD dwRegister /* token on class obj */)
1060 FIXME("(0x%08lx),stub!\n", dwRegister);
1066 * COM_GetRegisteredClassObject
1068 * This internal method is used to scan the registered class list to
1069 * find a class object.
1072 * rclsid Class ID of the class to find.
1073 * dwClsContext Class context to match.
1074 * ppv [out] returns a pointer to the class object. Complying
1075 * to normal COM usage, this method will increase the
1076 * reference count on this object.
1078 static HRESULT COM_GetRegisteredClassObject(
1083 RegisteredClass* curClass;
1091 * Iterate through the whole list and try to match the class ID.
1093 curClass = firstRegisteredClass;
1095 while (curClass != 0)
1098 * Check if we have a match on the class ID.
1100 if (IsEqualGUID32(&(curClass->classIdentifier), rclsid))
1103 * Since we don't do out-of process or DCOM just right away, let's ignore the
1108 * We have a match, return the pointer to the class object.
1110 *ppUnk = curClass->classObject;
1112 IUnknown_AddRef(curClass->classObject);
1118 * Step to the next class in the list.
1120 curClass = curClass->nextClass;
1124 * If we get to here, we haven't found our class.
1129 /******************************************************************************
1130 * CoRegisterClassObject32 [OLE32.36]
1132 * This method will register the class object for a given class ID.
1134 * See the Windows documentation for more details.
1136 HRESULT WINAPI CoRegisterClassObject(
1139 DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
1140 DWORD flags, /* [in] REGCLS flags indicating how connections are made */
1141 LPDWORD lpdwRegister
1144 RegisteredClass* newClass;
1145 LPUNKNOWN foundObject;
1149 WINE_StringFromCLSID(rclsid,buf);
1151 TRACE("(%s,%p,0x%08lx,0x%08lx,%p)\n",
1152 buf,pUnk,dwClsContext,flags,lpdwRegister);
1155 * Perform a sanity check on the parameters
1157 if ( (lpdwRegister==0) || (pUnk==0) )
1159 return E_INVALIDARG;
1163 * Initialize the cookie (out parameter)
1168 * First, check if the class is already registered.
1169 * If it is, this should cause an error.
1171 hr = COM_GetRegisteredClassObject(rclsid, dwClsContext, &foundObject);
1176 * The COM_GetRegisteredClassObject increased the reference count on the
1177 * object so it has to be released.
1179 IUnknown_Release(foundObject);
1181 return CO_E_OBJISREG;
1185 * If it is not registered, we must create a new entry for this class and
1186 * append it to the registered class list.
1187 * We use the address of the chain node as the cookie since we are sure it's
1190 newClass = HeapAlloc(GetProcessHeap(), 0, sizeof(RegisteredClass));
1193 * Initialize the node.
1195 newClass->classIdentifier = *rclsid;
1196 newClass->runContext = dwClsContext;
1197 newClass->connectFlags = flags;
1198 newClass->dwCookie = (DWORD)newClass;
1199 newClass->nextClass = firstRegisteredClass;
1202 * Since we're making a copy of the object pointer, we have to increase it's
1205 newClass->classObject = pUnk;
1206 IUnknown_AddRef(newClass->classObject);
1208 firstRegisteredClass = newClass;
1211 * Assign the out parameter (cookie)
1213 *lpdwRegister = newClass->dwCookie;
1216 * We're successful Yippee!
1221 /***********************************************************************
1222 * CoRevokeClassObject32 [OLE32.40]
1224 * This method will remove a class object from the class registry
1226 * See the Windows documentation for more details.
1228 HRESULT WINAPI CoRevokeClassObject(
1231 RegisteredClass** prevClassLink;
1232 RegisteredClass* curClass;
1234 TRACE("(%08lx)\n",dwRegister);
1237 * Iterate through the whole list and try to match the cookie.
1239 curClass = firstRegisteredClass;
1240 prevClassLink = &firstRegisteredClass;
1242 while (curClass != 0)
1245 * Check if we have a match on the cookie.
1247 if (curClass->dwCookie == dwRegister)
1250 * Remove the class from the chain.
1252 *prevClassLink = curClass->nextClass;
1255 * Release the reference to the class object.
1257 IUnknown_Release(curClass->classObject);
1260 * Free the memory used by the chain node.
1262 HeapFree(GetProcessHeap(), 0, curClass);
1268 * Step to the next class in the list.
1270 prevClassLink = &(curClass->nextClass);
1271 curClass = curClass->nextClass;
1275 * If we get to here, we haven't found our class.
1277 return E_INVALIDARG;
1280 /***********************************************************************
1281 * CoGetClassObject [COMPOBJ.7]
1283 HRESULT WINAPI CoGetClassObject(REFCLSID rclsid, DWORD dwClsContext,
1284 LPVOID pvReserved, REFIID iid, LPVOID *ppv)
1286 LPUNKNOWN regClassObject;
1287 char xclsid[50],xiid[50];
1288 HRESULT hres = E_UNEXPECTED;
1290 char dllName[MAX_PATH+1];
1291 DWORD dllNameLen = sizeof(dllName);
1293 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid,
1294 REFIID iid, LPVOID *ppv);
1295 DllGetClassObjectFunc DllGetClassObject;
1297 WINE_StringFromCLSID((LPCLSID)rclsid,xclsid);
1298 WINE_StringFromCLSID((LPCLSID)iid,xiid);
1299 TRACE("\n\tCLSID:\t%s,\n\tIID:\t%s\n",xclsid,xiid);
1302 * First, try and see if we can't match the class ID with one of the
1303 * registered classes.
1305 if (S_OK == COM_GetRegisteredClassObject(rclsid, dwClsContext, ®ClassObject))
1308 * Get the required interface from the retrieved pointer.
1310 hres = IUnknown_QueryInterface(regClassObject, iid, ppv);
1313 * Since QI got another reference on the pointer, we want to release the
1314 * one we already have. If QI was unsuccessful, this will release the object. This
1315 * is good since we are not returning it in the "out" parameter.
1317 IUnknown_Release(regClassObject);
1322 /* out of process and remote servers not supported yet */
1323 if (((CLSCTX_LOCAL_SERVER|CLSCTX_REMOTE_SERVER) & dwClsContext)
1324 && !((CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER) & dwClsContext)){
1325 FIXME("CLSCTX_LOCAL_SERVER and CLSCTX_REMOTE_SERVER not supported!\n");
1326 return E_ACCESSDENIED;
1329 if ((CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER) & dwClsContext) {
1332 /* lookup CLSID in registry key HKCR/CLSID */
1333 hres = RegOpenKeyExA(HKEY_CLASSES_ROOT, "CLSID", 0,
1334 KEY_READ, &CLSIDkey);
1336 if (hres != ERROR_SUCCESS)
1337 return REGDB_E_READREGDB;
1338 hres = RegOpenKeyExA(CLSIDkey,xclsid,0,KEY_QUERY_VALUE,&key);
1339 if (hres != ERROR_SUCCESS) {
1340 RegCloseKey(CLSIDkey);
1341 return REGDB_E_CLASSNOTREG;
1343 hres = RegQueryValueA(key, "InprocServer32", dllName, &dllNameLen);
1345 RegCloseKey(CLSIDkey);
1346 if (hres != ERROR_SUCCESS)
1347 return REGDB_E_READREGDB;
1348 TRACE("found InprocServer32 dll %s\n", dllName);
1350 /* open dll, call DllGetClassFactory */
1351 hLibrary = CoLoadLibrary(dllName, TRUE);
1352 if (hLibrary == 0) {
1353 TRACE("couldn't load InprocServer32 dll %s\n", dllName);
1354 return E_ACCESSDENIED; /* or should this be CO_E_DLLNOTFOUND? */
1356 DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress(hLibrary, "DllGetClassObject");
1357 if (!DllGetClassObject) {
1358 /* not sure if this should be called here CoFreeLibrary(hLibrary);*/
1359 TRACE("couldn't find function DllGetClassObject in %s\n", dllName);
1360 return E_ACCESSDENIED;
1364 * Ask the DLL for it's class object. (there was a note here about class
1365 * factories but this is good.
1367 return DllGetClassObject(rclsid, iid, ppv);
1372 /****************************************************************************************
1375 * This function supplies the CLSID associated with the given filename.
1377 HRESULT WINAPI GetClassFile(LPOLESTR filePathName,CLSID *pclsid)
1381 int nbElm=0,length=0,i=0;
1383 LPOLESTR *pathDec=0,absFile=0,progId=0;
1384 WCHAR extention[100]={0};
1388 /* if the file contain a storage object the return the CLSID writen by IStorage_SetClass method*/
1389 if((StgIsStorageFile(filePathName))==S_OK){
1391 res=StgOpenStorage(filePathName,NULL,STGM_READ | STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
1394 res=ReadClassStg(pstg,pclsid);
1396 IStorage_Release(pstg);
1400 /* if the file is not a storage object then attemps to match various bits in the file against a
1401 pattern in the registry. this case is not frequently used ! so I present only the psodocode for
1404 for(i=0;i<nFileTypes;i++)
1406 for(i=0;j<nPatternsForType;j++){
1411 pat=ReadPatternFromRegistry(i,j);
1412 hFile=CreateFileW(filePathName,,,,,,hFile);
1413 SetFilePosition(hFile,pat.offset);
1414 ReadFile(hFile,buf,pat.size,NULL,NULL);
1415 if (memcmp(buf&pat.mask,pat.pattern.pat.size)==0){
1417 *pclsid=ReadCLSIDFromRegistry(i);
1423 /* if the obove strategies fail then search for the extension key in the registry */
1425 /* get the last element (absolute file) in the path name */
1426 nbElm=FileMonikerImpl_DecomposePath(filePathName,&pathDec);
1427 absFile=pathDec[nbElm-1];
1429 /* failed if the path represente a directory and not an absolute file name*/
1430 if (lstrcmpW(absFile,(LPOLESTR)"\\"))
1431 return MK_E_INVALIDEXTENSION;
1433 /* get the extension of the file */
1434 length=lstrlenW(absFile);
1435 for(i=length-1; ( (i>=0) && (extention[i]=absFile[i]) );i--);
1437 /* get the progId associated to the extension */
1438 progId=CoTaskMemAlloc(sizeProgId);
1440 res=RegQueryValueW(HKEY_CLASSES_ROOT,extention,progId,&sizeProgId);
1442 if (res==ERROR_MORE_DATA){
1444 CoTaskMemRealloc(progId,sizeProgId);
1446 res=RegQueryValueW(HKEY_CLASSES_ROOT,extention,progId,&sizeProgId);
1448 if (res==ERROR_SUCCESS)
1449 /* return the clsid associated to the progId */
1450 res= CLSIDFromProgID(progId,pclsid);
1452 for(i=0; pathDec[i]!=NULL;i++)
1453 CoTaskMemFree(pathDec[i]);
1454 CoTaskMemFree(pathDec);
1456 CoTaskMemFree(progId);
1458 if (res==ERROR_SUCCESS)
1461 return MK_E_INVALIDEXTENSION;
1463 /******************************************************************************
1464 * CoRegisterMessageFilter16 [COMPOBJ.27]
1466 HRESULT WINAPI CoRegisterMessageFilter16(
1467 LPMESSAGEFILTER lpMessageFilter,
1468 LPMESSAGEFILTER *lplpMessageFilter
1470 FIXME("(%p,%p),stub!\n",lpMessageFilter,lplpMessageFilter);
1474 /***********************************************************************
1475 * CoCreateInstance [COMPOBJ.13, OLE32.7]
1477 HRESULT WINAPI CoCreateInstance(
1479 LPUNKNOWN pUnkOuter,
1485 LPCLASSFACTORY lpclf = 0;
1494 * Initialize the "out" parameter
1499 * Get a class factory to construct the object we want.
1501 hres = CoGetClassObject(rclsid,
1511 * Create the object and don't forget to release the factory
1513 hres = IClassFactory_CreateInstance(lpclf, pUnkOuter, iid, ppv);
1514 IClassFactory_Release(lpclf);
1519 /***********************************************************************
1520 * CoCreateInstanceEx [OLE32.165]
1522 HRESULT WINAPI CoCreateInstanceEx(
1524 LPUNKNOWN pUnkOuter,
1526 COSERVERINFO* pServerInfo,
1530 IUnknown* pUnk = NULL;
1533 int successCount = 0;
1538 if ( (cmq==0) || (pResults==NULL))
1539 return E_INVALIDARG;
1541 if (pServerInfo!=NULL)
1542 FIXME("() non-NULL pServerInfo not supported!\n");
1545 * Initialize all the "out" parameters.
1547 for (index = 0; index < cmq; index++)
1549 pResults[index].pItf = NULL;
1550 pResults[index].hr = E_NOINTERFACE;
1554 * Get the object and get it's IUnknown pointer.
1556 hr = CoCreateInstance(rclsid,
1566 * Then, query for all the interfaces requested.
1568 for (index = 0; index < cmq; index++)
1570 pResults[index].hr = IUnknown_QueryInterface(pUnk,
1571 pResults[index].pIID,
1572 (VOID**)&(pResults[index].pItf));
1574 if (pResults[index].hr == S_OK)
1579 * Release our temporary unknown pointer.
1581 IUnknown_Release(pUnk);
1583 if (successCount == 0)
1584 return E_NOINTERFACE;
1586 if (successCount!=cmq)
1587 return CO_S_NOTALLINTERFACES;
1592 /***********************************************************************
1593 * CoFreeLibrary [COMPOBJ.13]
1595 void WINAPI CoFreeLibrary(HINSTANCE hLibrary)
1597 OpenDll *ptr, *prev;
1600 /* lookup library in linked list */
1602 for (ptr = openDllList; ptr != NULL; ptr=ptr->next) {
1603 if (ptr->hLibrary == hLibrary) {
1610 /* shouldn't happen if user passed in a valid hLibrary */
1613 /* assert: ptr points to the library entry to free */
1615 /* free library and remove node from list */
1616 FreeLibrary(hLibrary);
1617 if (ptr == openDllList) {
1618 tmp = openDllList->next;
1619 HeapFree(GetProcessHeap(), 0, openDllList->DllName);
1620 HeapFree(GetProcessHeap(), 0, openDllList);
1624 HeapFree(GetProcessHeap(), 0, ptr->DllName);
1625 HeapFree(GetProcessHeap(), 0, ptr);
1632 /***********************************************************************
1633 * CoFreeAllLibraries [COMPOBJ.12]
1635 void WINAPI CoFreeAllLibraries(void)
1639 for (ptr = openDllList; ptr != NULL; ) {
1641 CoFreeLibrary(ptr->hLibrary);
1648 /***********************************************************************
1649 * CoFreeUnusedLibraries [COMPOBJ.17]
1651 void WINAPI CoFreeUnusedLibraries(void)
1654 typedef HRESULT(*DllCanUnloadNowFunc)(void);
1655 DllCanUnloadNowFunc DllCanUnloadNow;
1657 for (ptr = openDllList; ptr != NULL; ) {
1658 DllCanUnloadNow = (DllCanUnloadNowFunc)
1659 GetProcAddress(ptr->hLibrary, "DllCanUnloadNow");
1661 if ( (DllCanUnloadNow != NULL) &&
1662 (DllCanUnloadNow() == S_OK) ) {
1664 CoFreeLibrary(ptr->hLibrary);
1672 /***********************************************************************
1673 * CoFileTimeNow [COMPOBJ.82, OLE32.10]
1675 * the current system time in lpFileTime
1677 HRESULT WINAPI CoFileTimeNow(
1678 FILETIME *lpFileTime /* [out] the current time */
1680 DOSFS_UnixTimeToFileTime(time(NULL), lpFileTime, 0);
1684 /***********************************************************************
1685 * CoTaskMemAlloc (OLE32.43)
1687 * pointer to newly allocated block
1689 LPVOID WINAPI CoTaskMemAlloc(
1690 ULONG size /* [in] size of memoryblock to be allocated */
1693 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1698 return IMalloc_Alloc(lpmalloc,size);
1700 /***********************************************************************
1701 * CoTaskMemFree (OLE32.44)
1703 VOID WINAPI CoTaskMemFree(
1704 LPVOID ptr /* [in] pointer to be freed */
1707 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1712 IMalloc_Free(lpmalloc, ptr);
1715 /***********************************************************************
1716 * CoTaskMemRealloc (OLE32.45)
1718 * pointer to newly allocated block
1720 LPVOID WINAPI CoTaskMemRealloc(
1722 ULONG size) /* [in] size of memoryblock to be allocated */
1725 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1730 return IMalloc_Realloc(lpmalloc, pvOld, size);
1733 /***********************************************************************
1734 * CoLoadLibrary (OLE32.30)
1736 HINSTANCE WINAPI CoLoadLibrary(LPOLESTR16 lpszLibName, BOOL bAutoFree)
1742 TRACE("CoLoadLibrary(%p, %d\n", lpszLibName, bAutoFree);
1744 hLibrary = LoadLibraryExA(lpszLibName, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
1749 if (openDllList == NULL) {
1750 /* empty list -- add first node */
1751 openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
1752 openDllList->DllName = HEAP_strdupA(GetProcessHeap(), 0, lpszLibName);
1753 openDllList->hLibrary = hLibrary;
1754 openDllList->next = NULL;
1756 /* search for this dll */
1758 for (ptr = openDllList; ptr->next != NULL; ptr=ptr->next) {
1759 if (ptr->hLibrary == hLibrary) {
1765 /* dll not found, add it */
1767 openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
1768 openDllList->DllName = HEAP_strdupA(GetProcessHeap(), 0, lpszLibName);
1769 openDllList->hLibrary = hLibrary;
1770 openDllList->next = tmp;
1777 /***********************************************************************
1778 * CoInitializeWOW (OLE32.27)
1780 HRESULT WINAPI CoInitializeWOW(DWORD x,DWORD y) {
1781 FIXME("(0x%08lx,0x%08lx),stub!\n",x,y);
1785 /******************************************************************************
1786 * CoLockObjectExternal16 [COMPOBJ.63]
1788 HRESULT WINAPI CoLockObjectExternal16(
1789 LPUNKNOWN pUnk, /* [in] object to be locked */
1790 BOOL16 fLock, /* [in] do lock */
1791 BOOL16 fLastUnlockReleases /* [in] ? */
1793 FIXME("(%p,%d,%d),stub!\n",pUnk,fLock,fLastUnlockReleases);
1797 /******************************************************************************
1798 * CoLockObjectExternal32 [OLE32.31]
1800 HRESULT WINAPI CoLockObjectExternal(
1801 LPUNKNOWN pUnk, /* [in] object to be locked */
1802 BOOL fLock, /* [in] do lock */
1803 BOOL fLastUnlockReleases) /* [in] unlock all */
1809 * Increment the external lock coutner, COM_ExternalLockAddRef also
1810 * increment the object's internal lock counter.
1812 COM_ExternalLockAddRef( pUnk);
1817 * Decrement the external lock coutner, COM_ExternalLockRelease also
1818 * decrement the object's internal lock counter.
1820 COM_ExternalLockRelease( pUnk, fLastUnlockReleases);
1826 /***********************************************************************
1827 * CoGetState16 [COMPOBJ.115]
1829 HRESULT WINAPI CoGetState16(LPDWORD state)
1831 FIXME("(%p),stub!\n", state);
1835 /***********************************************************************
1836 * CoSetState32 [COM32.42]
1838 HRESULT WINAPI CoSetState(LPDWORD state)
1840 FIXME("(%p),stub!\n", state);
1841 if (state) *state = 0;
1846 /***********************************************************************
1847 * DllGetClassObject [OLE32.63]
1849 HRESULT WINAPI OLE32_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
1851 char xclsid[50],xiid[50];
1852 WINE_StringFromCLSID((LPCLSID)rclsid,xclsid);
1853 WINE_StringFromCLSID((LPCLSID)iid,xiid);
1854 FIXME("\n\tCLSID:\t%s,\n\tIID:\t%s\n",xclsid,xiid);
1858 return CLASS_E_CLASSNOTAVAILABLE;
1863 * COM_RevokeAllClasses
1865 * This method is called when the COM libraries are uninitialized to
1866 * release all the references to the class objects registered with
1869 static void COM_RevokeAllClasses()
1871 while (firstRegisteredClass!=0)
1873 CoRevokeClassObject(firstRegisteredClass->dwCookie);
1877 /****************************************************************************
1878 * COM External Lock methods implementation
1881 /****************************************************************************
1882 * Public - Method that increments the count for a IUnknown* in the linked
1883 * list. The item is inserted if not already in the list.
1885 static void COM_ExternalLockAddRef(
1888 COM_ExternalLock *externalLock = COM_ExternalLockFind(pUnk);
1891 * Add an external lock to the object. If it was already externally
1892 * locked, just increase the reference count. If it was not.
1893 * add the item to the list.
1895 if ( externalLock == EL_NOT_FOUND )
1896 COM_ExternalLockInsert(pUnk);
1898 externalLock->uRefCount++;
1901 * Add an internal lock to the object
1903 IUnknown_AddRef(pUnk);
1906 /****************************************************************************
1907 * Public - Method that decrements the count for a IUnknown* in the linked
1908 * list. The item is removed from the list if its count end up at zero or if
1911 static void COM_ExternalLockRelease(
1915 COM_ExternalLock *externalLock = COM_ExternalLockFind(pUnk);
1917 if ( externalLock != EL_NOT_FOUND )
1921 externalLock->uRefCount--; /* release external locks */
1922 IUnknown_Release(pUnk); /* release local locks as well */
1924 if ( bRelAll == FALSE )
1925 break; /* perform single release */
1927 } while ( externalLock->uRefCount > 0 );
1929 if ( externalLock->uRefCount == 0 ) /* get rid of the list entry */
1930 COM_ExternalLockDelete(externalLock);
1933 /****************************************************************************
1934 * Public - Method that frees the content of the list.
1936 static void COM_ExternalLockFreeList()
1938 COM_ExternalLock *head;
1940 head = elList.head; /* grab it by the head */
1941 while ( head != EL_END_OF_LIST )
1943 COM_ExternalLockDelete(head); /* get rid of the head stuff */
1945 head = elList.head; /* get the new head... */
1949 /****************************************************************************
1950 * Public - Method that dump the content of the list.
1952 void COM_ExternalLockDump()
1954 COM_ExternalLock *current = elList.head;
1956 printf("\nExternal lock list contains:\n");
1958 while ( current != EL_END_OF_LIST )
1960 printf( "\t%p with %lu references count.\n",
1962 current->uRefCount);
1964 /* Skip to the next item */
1965 current = current->next;
1970 /****************************************************************************
1971 * Internal - Find a IUnknown* in the linked list
1973 static COM_ExternalLock* COM_ExternalLockFind(
1976 return COM_ExternalLockLocate(elList.head, pUnk);
1979 /****************************************************************************
1980 * Internal - Recursivity agent for IUnknownExternalLockList_Find
1982 static COM_ExternalLock* COM_ExternalLockLocate(
1983 COM_ExternalLock *element,
1986 if ( element == EL_END_OF_LIST )
1987 return EL_NOT_FOUND;
1989 else if ( element->pUnk == pUnk ) /* We found it */
1992 else /* Not the right guy, keep on looking */
1993 return COM_ExternalLockLocate( element->next, pUnk);
1996 /****************************************************************************
1997 * Internal - Insert a new IUnknown* to the linked list
1999 static BOOL COM_ExternalLockInsert(
2002 COM_ExternalLock *newLock = NULL;
2003 COM_ExternalLock *previousHead = NULL;
2006 * Allocate space for the new storage object
2008 newLock = HeapAlloc(GetProcessHeap(), 0, sizeof(COM_ExternalLock));
2012 if ( elList.head == EL_END_OF_LIST )
2014 elList.head = newLock; /* The list is empty */
2019 * insert does it at the head
2021 previousHead = elList.head;
2022 elList.head = newLock;
2026 * Set new list item data member
2028 newLock->pUnk = pUnk;
2029 newLock->uRefCount = 1;
2030 newLock->next = previousHead;
2038 /****************************************************************************
2039 * Internal - Method that removes an item from the linked list.
2041 static void COM_ExternalLockDelete(
2042 COM_ExternalLock *itemList)
2044 COM_ExternalLock *current = elList.head;
2046 if ( current == itemList )
2049 * this section handles the deletion of the first node
2051 elList.head = itemList->next;
2052 HeapFree( GetProcessHeap(), 0, itemList);
2058 if ( current->next == itemList ) /* We found the item to free */
2060 current->next = itemList->next; /* readjust the list pointers */
2062 HeapFree( GetProcessHeap(), 0, itemList);
2066 /* Skip to the next item */
2067 current = current->next;
2069 } while ( current != EL_END_OF_LIST );
2073 /***********************************************************************
2074 * COMPOBJ_DllEntryPoint [COMPOBJ.entry]
2076 * Initialization code for the COMPOBJ DLL
2080 BOOL WINAPI COMPOBJ_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst, WORD ds, WORD HeapSize, DWORD res1, WORD res2)
2082 TRACE("(%08lx, %04x, %04x, %04x, %08lx, %04x)\n", Reason, hInst, ds, HeapSize,
2086 case DLL_PROCESS_ATTACH:
2088 if(COMPOBJ_hInstance)
2090 ERR("compobj.dll instantiated twice!\n");
2092 * We should return FALSE here, but that will break
2093 * most apps that use CreateProcess because we do
2094 * not yet support seperate address-spaces.
2099 COMPOBJ_hInstance = hInst;
2102 case DLL_PROCESS_DETACH:
2103 if(!--COMPOBJ_Attach)
2104 COMPOBJ_hInstance = 0;