4 * Copyright 1995 Martin von Loewis
5 * Copyright 1998 Justin Bradford
6 * Copyright 1999 Francis Beaudet
7 * Copyright 1999 Sylvain St-Germain
15 #include <sys/types.h>
18 #ifdef HAVE_SYS_FILE_H
19 # include <sys/file.h>
21 #include <sys/ioctl.h>
22 #include <sys/socket.h>
23 #ifdef HAVE_SYS_SOCKIO_H
24 # include <sys/sockio.h>
29 #ifdef HAVE_NETINET_IN_H
30 # include <netinet/in.h>
37 #include "wine/winbase16.h"
38 #include "wine/winestring.h"
48 #include "wine/obj_base.h"
49 #include "wine/obj_misc.h"
50 #include "wine/obj_clientserver.h"
55 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
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(
110 static void COM_ExternalLockDelete(
111 COM_ExternalLock
*element
);
113 static COM_ExternalLock
* COM_ExternalLockFind(
116 static COM_ExternalLock
* COM_ExternalLockLocate(
117 COM_ExternalLock
*element
,
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 LPMALLOC16 currentMalloc16
=NULL
;
126 LPMALLOC currentMalloc32
=NULL
;
129 WORD Table_ETask
[62];
132 * This lock count counts the number of times CoInitialize is called. It is
133 * decreased every time CoUninitialize is called. When it hits 0, the COM
134 * libraries are freed
136 static ULONG s_COMLockCount
= 0;
139 * This linked list contains the list of registered class objects. These
140 * are mostly used to register the factories for out-of-proc servers of OLE
143 * TODO: Make this data structure aware of inter-process communication. This
144 * means that parts of this will be exported to the Wine Server.
146 typedef struct tagRegisteredClass
148 CLSID classIdentifier
;
149 LPUNKNOWN classObject
;
153 struct tagRegisteredClass
* nextClass
;
156 static RegisteredClass
* firstRegisteredClass
= NULL
;
158 /* this open DLL table belongs in a per process table, but my guess is that
159 * it shouldn't live in the kernel, so I'll put them out here in DLL
160 * space assuming that there is one OLE32 per process.
162 typedef struct tagOpenDll
{
163 char *DllName
; /* really only needed for debugging */
165 struct tagOpenDll
*next
;
168 static OpenDll
*openDllList
= NULL
; /* linked list of open dlls */
170 /*****************************************************************************
171 * This section contains prototypes to internal methods for this
174 static HRESULT
COM_GetRegisteredClassObject(REFCLSID rclsid
,
178 static void COM_RevokeAllClasses();
181 /******************************************************************************
182 * CoBuildVersion [COMPOBJ.1]
185 * Current built version, hiword is majornumber, loword is minornumber
187 DWORD WINAPI
CoBuildVersion(void)
189 TRACE(ole
,"(void)\n");
190 return (rmm
<<16)+rup
;
193 /******************************************************************************
194 * CoInitialize16 [COMPOBJ.2]
195 * Set the win16 IMalloc used for memory management
197 HRESULT WINAPI
CoInitialize16(
198 LPVOID lpReserved
/* [in] pointer to win16 malloc interface */
200 currentMalloc16
= (LPMALLOC16
)lpReserved
;
204 /******************************************************************************
205 * CoInitialize32 [OLE32.26]
207 * Initializes the COM libraries.
209 * See CoInitializeEx32
211 HRESULT WINAPI
CoInitialize(
212 LPVOID lpReserved
/* [in] pointer to win32 malloc interface
213 (obsolete, should be NULL) */
217 * Just delegate to the newer method.
219 return CoInitializeEx(lpReserved
, COINIT_APARTMENTTHREADED
);
222 /******************************************************************************
223 * CoInitializeEx32 [OLE32.163]
225 * Initializes the COM libraries. The behavior used to set the win32 IMalloc
226 * used for memory management is obsolete.
229 * S_OK if successful,
230 * S_FALSE if this function was called already.
231 * RPC_E_CHANGED_MODE if a previous call to CoInitialize specified another
235 * Only the single threaded model is supported. As a result RPC_E_CHANGED_MODE
238 * See the windows documentation for more details.
240 HRESULT WINAPI
CoInitializeEx(
241 LPVOID lpReserved
, /* [in] pointer to win32 malloc interface
242 (obsolete, should be NULL) */
243 DWORD dwCoInit
/* [in] A value from COINIT specifies the threading model */
248 TRACE(ole
, "(%p, %x)\n", lpReserved
, (int)dwCoInit
);
250 if (lpReserved
!=NULL
)
252 ERR(ole
,"(%p, %x) - Bad parameter passed-in %p, must be an old Windows Application\n", lpReserved
, (int)dwCoInit
, lpReserved
);
256 * Check for unsupported features.
258 if (dwCoInit
!=COINIT_APARTMENTTHREADED
)
260 FIXME(ole
, ":(%p,%x): unsupported flag %x\n", lpReserved
, (int)dwCoInit
, (int)dwCoInit
);
261 /* Hope for the best and continue anyway */
265 * Check the lock count. If this is the first time going through the initialize
266 * process, we have to initialize the libraries.
268 if (s_COMLockCount
==0)
271 * Initialize the various COM libraries and data structures.
273 TRACE(ole
, "() - Initializing the COM libraries\n");
275 RunningObjectTableImpl_Initialize();
283 * Crank-up that lock count.
290 /***********************************************************************
291 * CoUninitialize16 [COMPOBJ.3]
292 * Don't know what it does.
293 * 3-Nov-98 -- this was originally misspelled, I changed it to what I
294 * believe is the correct spelling
296 void WINAPI
CoUninitialize16(void)
299 CoFreeAllLibraries();
302 /***********************************************************************
303 * CoUninitialize32 [OLE32.47]
305 * This method will release the COM libraries.
307 * See the windows documentation for more details.
309 void WINAPI
CoUninitialize(void)
314 * Decrease the reference count.
319 * If we are back to 0 locks on the COM library, make sure we free
320 * all the associated data structures.
322 if (s_COMLockCount
==0)
325 * Release the various COM libraries and data structures.
327 TRACE(ole
, "() - Releasing the COM libraries\n");
329 RunningObjectTableImpl_UnInitialize();
331 * Release the references to the registered class objects.
333 COM_RevokeAllClasses();
336 * This will free the loaded COM Dlls.
338 CoFreeAllLibraries();
341 * This will free list of external references to COM objects.
343 COM_ExternalLockFreeList();
347 /***********************************************************************
348 * CoGetMalloc16 [COMPOBJ.4]
350 * The current win16 IMalloc
352 HRESULT WINAPI
CoGetMalloc16(
353 DWORD dwMemContext
, /* [in] unknown */
354 LPMALLOC16
* lpMalloc
/* [out] current win16 malloc interface */
357 currentMalloc16
= IMalloc16_Constructor();
358 *lpMalloc
= currentMalloc16
;
362 /******************************************************************************
363 * CoGetMalloc32 [OLE32.20]
366 * The current win32 IMalloc
368 HRESULT WINAPI
CoGetMalloc(
369 DWORD dwMemContext
, /* [in] unknown */
370 LPMALLOC
*lpMalloc
/* [out] current win32 malloc interface */
373 currentMalloc32
= IMalloc_Constructor();
374 *lpMalloc
= currentMalloc32
;
378 /***********************************************************************
379 * CoCreateStandardMalloc16 [COMPOBJ.71]
381 HRESULT WINAPI
CoCreateStandardMalloc16(DWORD dwMemContext
,
382 LPMALLOC16
*lpMalloc
)
384 /* FIXME: docu says we shouldn't return the same allocator as in
386 *lpMalloc
= IMalloc16_Constructor();
390 /******************************************************************************
391 * CoDisconnectObject [COMPOBJ.15]
393 HRESULT WINAPI
CoDisconnectObject( LPUNKNOWN lpUnk
, DWORD reserved
)
395 TRACE(ole
,"%p %lx\n",lpUnk
,reserved
);
399 /***********************************************************************
400 * IsEqualGUID16 [COMPOBJ.18]
402 * Compares two Unique Identifiers.
407 BOOL16 WINAPI
IsEqualGUID16(
408 GUID
* g1
, /* [in] unique id 1 */
411 return !memcmp( g1
, g2
, sizeof(GUID
) );
414 /***********************************************************************
415 * IsEqualGUID32 [OLE32.76]
417 * Compares two Unique Identifiers.
422 BOOL WINAPI
IsEqualGUID32(
423 REFGUID rguid1
, /* [in] unique id 1 */
424 REFGUID rguid2
/* [in] unique id 2 */
427 return !memcmp(rguid1
,rguid2
,sizeof(GUID
));
430 /******************************************************************************
431 * CLSIDFromString16 [COMPOBJ.20]
432 * Converts a unique identifier from it's string representation into
435 * Class id: DWORD-WORD-WORD-BYTES[2]-BYTES[6]
440 HRESULT WINAPI
CLSIDFromString16(
441 LPCOLESTR16 idstr
, /* [in] string representation of guid */
442 CLSID
*id
/* [out] GUID converted from string */
444 BYTE
*s
= (BYTE
*) idstr
;
449 TRACE(ole
,"%s -> %p\n", idstr
, id
);
451 /* quick lookup table */
452 memset(table
, 0, 256);
454 for (i
= 0; i
< 10; i
++) {
457 for (i
= 0; i
< 6; i
++) {
458 table
['A' + i
] = i
+10;
459 table
['a' + i
] = i
+10;
462 /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
464 if (strlen(idstr
) != 38)
465 return OLE_ERROR_OBJECT
;
469 s
++; /* skip leading brace */
470 for (i
= 0; i
< 4; i
++) {
471 p
[3 - i
] = table
[*s
]<<4 | table
[*(s
+1)];
477 for (i
= 0; i
< 2; i
++) {
478 p
[1-i
] = table
[*s
]<<4 | table
[*(s
+1)];
484 for (i
= 0; i
< 2; i
++) {
485 p
[1-i
] = table
[*s
]<<4 | table
[*(s
+1)];
491 /* these are just sequential bytes */
492 for (i
= 0; i
< 2; i
++) {
493 *p
++ = table
[*s
]<<4 | table
[*(s
+1)];
498 for (i
= 0; i
< 6; i
++) {
499 *p
++ = table
[*s
]<<4 | table
[*(s
+1)];
506 /******************************************************************************
507 * CoCreateGuid[OLE32.6]
509 * Creates a 128bit GUID.
510 * Implemented according the DCE specification for UUID generation.
511 * Code is based upon uuid library in e2fsprogs by Theodore Ts'o.
512 * Copyright (C) 1996, 1997 Theodore Ts'o.
516 * S_OK if successful.
518 HRESULT WINAPI
CoCreateGuid(
519 GUID
*pguid
/* [out] points to the GUID to initialize */
521 static char has_init
= 0;
523 static int adjustment
= 0;
524 static struct timeval last
= {0, 0};
525 static UINT16 clock_seq
;
527 unsigned long long clock_reg
;
528 UINT clock_high
, clock_low
;
529 UINT16 temp_clock_seq
, temp_clock_mid
, temp_clock_hi_and_version
;
532 struct ifreq ifr
, *ifrp
;
538 /* Have we already tried to get the MAC address? */
541 /* BSD 4.4 defines the size of an ifreq to be
542 * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
543 * However, under earlier systems, sa_len isn't present, so
544 * the size is just sizeof(struct ifreq)
548 # define max(a,b) ((a) > (b) ? (a) : (b))
550 # define ifreq_size(i) max(sizeof(struct ifreq),\
551 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
553 # define ifreq_size(i) sizeof(struct ifreq)
554 # endif /* HAVE_SA_LEN */
556 sd
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_IP
);
558 /* if we can't open a socket, just use random numbers */
559 /* set the multicast bit to prevent conflicts with real cards */
560 a
[0] = (rand() & 0xff) | 0x80;
561 a
[1] = rand() & 0xff;
562 a
[2] = rand() & 0xff;
563 a
[3] = rand() & 0xff;
564 a
[4] = rand() & 0xff;
565 a
[5] = rand() & 0xff;
567 memset(buf
, 0, sizeof(buf
));
568 ifc
.ifc_len
= sizeof(buf
);
570 /* get the ifconf interface */
571 if (ioctl (sd
, SIOCGIFCONF
, (char *)&ifc
) < 0) {
573 /* no ifconf, so just use random numbers */
574 /* set the multicast bit to prevent conflicts with real cards */
575 a
[0] = (rand() & 0xff) | 0x80;
576 a
[1] = rand() & 0xff;
577 a
[2] = rand() & 0xff;
578 a
[3] = rand() & 0xff;
579 a
[4] = rand() & 0xff;
580 a
[5] = rand() & 0xff;
582 /* loop through the interfaces, looking for a valid one */
584 for (i
= 0; i
< n
; i
+= ifreq_size(*ifr
) ) {
585 ifrp
= (struct ifreq
*)((char *) ifc
.ifc_buf
+i
);
586 strncpy(ifr
.ifr_name
, ifrp
->ifr_name
, IFNAMSIZ
);
587 /* try to get the address for this interface */
588 # ifdef SIOCGIFHWADDR
589 if (ioctl(sd
, SIOCGIFHWADDR
, &ifr
) < 0)
591 memcpy(a
, (unsigned char *)&ifr
.ifr_hwaddr
.sa_data
, 6);
594 if (ioctl(sd
, SIOCGENADDR
, &ifr
) < 0)
596 memcpy(a
, (unsigned char *) ifr
.ifr_enaddr
, 6);
598 /* XXX we don't have a way of getting the hardware address */
602 # endif /* SIOCGENADDR */
603 # endif /* SIOCGIFHWADDR */
604 /* make sure it's not blank */
605 if (!a
[0] && !a
[1] && !a
[2] && !a
[3] && !a
[4] && !a
[5])
610 /* if we didn't find a valid address, make a random one */
611 /* once again, set multicast bit to avoid conflicts */
612 a
[0] = (rand() & 0xff) | 0x80;
613 a
[1] = rand() & 0xff;
614 a
[2] = rand() & 0xff;
615 a
[3] = rand() & 0xff;
616 a
[4] = rand() & 0xff;
617 a
[5] = rand() & 0xff;
624 /* no networking info, so generate a random address */
625 a
[0] = (rand() & 0xff) | 0x80;
626 a
[1] = rand() & 0xff;
627 a
[2] = rand() & 0xff;
628 a
[3] = rand() & 0xff;
629 a
[4] = rand() & 0xff;
630 a
[5] = rand() & 0xff;
631 #endif /* HAVE_NET_IF_H */
635 /* generate time element of GUID */
637 /* Assume that the gettimeofday() has microsecond granularity */
638 #define MAX_ADJUSTMENT 10
641 gettimeofday(&tv
, 0);
642 if ((last
.tv_sec
== 0) && (last
.tv_usec
== 0)) {
643 clock_seq
= ((rand() & 0xff) << 8) + (rand() & 0xff);
648 if ((tv
.tv_sec
< last
.tv_sec
) ||
649 ((tv
.tv_sec
== last
.tv_sec
) &&
650 (tv
.tv_usec
< last
.tv_usec
))) {
651 clock_seq
= (clock_seq
+1) & 0x1FFF;
653 } else if ((tv
.tv_sec
== last
.tv_sec
) &&
654 (tv
.tv_usec
== last
.tv_usec
)) {
655 if (adjustment
>= MAX_ADJUSTMENT
)
661 clock_reg
= tv
.tv_usec
*10 + adjustment
;
662 clock_reg
+= ((unsigned long long) tv
.tv_sec
)*10000000;
663 clock_reg
+= (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
665 clock_high
= clock_reg
>> 32;
666 clock_low
= clock_reg
;
667 temp_clock_seq
= clock_seq
| 0x8000;
668 temp_clock_mid
= (UINT16
)clock_high
;
669 temp_clock_hi_and_version
= (clock_high
>> 16) | 0x1000;
671 /* pack the information into the GUID structure */
673 ((unsigned char*)&pguid
->Data1
)[3] = (unsigned char)clock_low
;
675 ((unsigned char*)&pguid
->Data1
)[2] = (unsigned char)clock_low
;
677 ((unsigned char*)&pguid
->Data1
)[1] = (unsigned char)clock_low
;
679 ((unsigned char*)&pguid
->Data1
)[0] = (unsigned char)clock_low
;
681 ((unsigned char*)&pguid
->Data2
)[1] = (unsigned char)temp_clock_mid
;
682 temp_clock_mid
>>= 8;
683 ((unsigned char*)&pguid
->Data2
)[0] = (unsigned char)temp_clock_mid
;
685 ((unsigned char*)&pguid
->Data3
)[1] = (unsigned char)temp_clock_hi_and_version
;
686 temp_clock_hi_and_version
>>= 8;
687 ((unsigned char*)&pguid
->Data3
)[0] = (unsigned char)temp_clock_hi_and_version
;
689 ((unsigned char*)pguid
->Data4
)[1] = (unsigned char)temp_clock_seq
;
690 temp_clock_seq
>>= 8;
691 ((unsigned char*)pguid
->Data4
)[0] = (unsigned char)temp_clock_seq
;
693 ((unsigned char*)pguid
->Data4
)[2] = a
[0];
694 ((unsigned char*)pguid
->Data4
)[3] = a
[1];
695 ((unsigned char*)pguid
->Data4
)[4] = a
[2];
696 ((unsigned char*)pguid
->Data4
)[5] = a
[3];
697 ((unsigned char*)pguid
->Data4
)[6] = a
[4];
698 ((unsigned char*)pguid
->Data4
)[7] = a
[5];
700 TRACE(ole
, "%p", pguid
);
705 /******************************************************************************
706 * CLSIDFromString32 [OLE32.3]
707 * Converts a unique identifier from it's string representation into
712 HRESULT WINAPI
CLSIDFromString(
713 LPCOLESTR idstr
, /* [in] string representation of GUID */
714 CLSID
*id
/* [out] GUID represented by above string */
716 LPOLESTR16 xid
= HEAP_strdupWtoA(GetProcessHeap(),0,idstr
);
717 OLESTATUS ret
= CLSIDFromString16(xid
,id
);
719 HeapFree(GetProcessHeap(),0,xid
);
723 /******************************************************************************
724 * WINE_StringFromCLSID [Internal]
725 * Converts a GUID into the respective string representation.
730 * the string representation and OLESTATUS
732 HRESULT
WINE_StringFromCLSID(
733 const CLSID
*id
, /* [in] GUID to be converted */
734 LPSTR idstr
/* [out] pointer to buffer to contain converted guid */
736 static const char *hex
= "0123456789ABCDEF";
741 { ERR(ole
,"called with id=Null\n");
746 sprintf(idstr
, "{%08lX-%04X-%04X-%02X%02X-",
747 id
->Data1
, id
->Data2
, id
->Data3
,
748 id
->Data4
[0], id
->Data4
[1]);
752 for (i
= 2; i
< 8; i
++) {
753 *s
++ = hex
[id
->Data4
[i
]>>4];
754 *s
++ = hex
[id
->Data4
[i
] & 0xf];
760 TRACE(ole
,"%p->%s\n", id
, idstr
);
765 /******************************************************************************
766 * StringFromCLSID16 [COMPOBJ.19]
767 * Converts a GUID into the respective string representation.
768 * The target string is allocated using the OLE IMalloc.
770 * the string representation and OLESTATUS
772 HRESULT WINAPI
StringFromCLSID16(
773 REFCLSID id
, /* [in] the GUID to be converted */
774 LPOLESTR16
*idstr
/* [out] a pointer to a to-be-allocated segmented pointer pointing to the resulting string */
781 ret
= CoGetMalloc16(0,&mllc
);
784 args
[0] = (DWORD
)mllc
;
787 /* No need for a Callback entry, we have WOWCallback16Ex which does
788 * everything we need.
790 if (!WOWCallback16Ex(
791 (FARPROC16
)((ICOM_VTABLE(IMalloc16
)*)PTR_SEG_TO_LIN(
792 ((LPMALLOC16
)PTR_SEG_TO_LIN(mllc
))->lpvtbl
)
799 WARN(ole
,"CallTo16 IMalloc16 failed\n");
802 return WINE_StringFromCLSID(id
,PTR_SEG_TO_LIN(*idstr
));
805 /******************************************************************************
806 * StringFromCLSID32 [OLE32.151]
807 * Converts a GUID into the respective string representation.
808 * The target string is allocated using the OLE IMalloc.
810 * the string representation and OLESTATUS
812 HRESULT WINAPI
StringFromCLSID(
813 REFCLSID id
, /* [in] the GUID to be converted */
814 LPOLESTR
*idstr
/* [out] a pointer to a to-be-allocated pointer pointing to the resulting string */
820 if ((ret
=CoGetMalloc(0,&mllc
)))
823 ret
=WINE_StringFromCLSID(id
,buf
);
825 *idstr
= mllc
->lpvtbl
->fnAlloc(mllc
,strlen(buf
)*2+2);
826 lstrcpyAtoW(*idstr
,buf
);
831 /******************************************************************************
832 * StringFromGUID2 [COMPOBJ.76] [OLE32.152]
834 * Converts a global unique identifier into a string of an API-
835 * specified fixed format. (The usual {.....} stuff.)
838 * The (UNICODE) string representation of the GUID in 'str'
839 * The length of the resulting string, 0 if there was any problem.
842 StringFromGUID2(REFGUID id
, LPOLESTR str
, INT cmax
)
846 if (WINE_StringFromCLSID(id
,xguid
))
848 if (strlen(xguid
)>=cmax
)
850 lstrcpyAtoW(str
,xguid
);
851 return strlen(xguid
);
854 /******************************************************************************
855 * CLSIDFromProgID16 [COMPOBJ.61]
856 * Converts a program id into the respective GUID. (By using a registry lookup)
858 * riid associated with the progid
860 HRESULT WINAPI
CLSIDFromProgID16(
861 LPCOLESTR16 progid
, /* [in] program id as found in registry */
862 LPCLSID riid
/* [out] associated CLSID */
869 buf
= HeapAlloc(GetProcessHeap(),0,strlen(progid
)+8);
870 sprintf(buf
,"%s\\CLSID",progid
);
871 if ((err
=RegOpenKeyA(HKEY_CLASSES_ROOT
,buf
,&xhkey
))) {
872 HeapFree(GetProcessHeap(),0,buf
);
873 return OLE_ERROR_GENERIC
;
875 HeapFree(GetProcessHeap(),0,buf
);
876 buf2len
= sizeof(buf2
);
877 if ((err
=RegQueryValueA(xhkey
,NULL
,buf2
,&buf2len
))) {
879 return OLE_ERROR_GENERIC
;
882 return CLSIDFromString16(buf2
,riid
);
885 /******************************************************************************
886 * CLSIDFromProgID32 [OLE32.2]
887 * Converts a program id into the respective GUID. (By using a registry lookup)
889 * riid associated with the progid
891 HRESULT WINAPI
CLSIDFromProgID(
892 LPCOLESTR progid
, /* [in] program id as found in registry */
893 LPCLSID riid
/* [out] associated CLSID */
895 LPOLESTR16 pid
= HEAP_strdupWtoA(GetProcessHeap(),0,progid
);
896 OLESTATUS ret
= CLSIDFromProgID16(pid
,riid
);
898 HeapFree(GetProcessHeap(),0,pid
);
902 /************************************************************************************************
905 * This function write a CLSID on stream
907 HRESULT WINAPI
WriteClassStm(IStream
*pStm
,REFCLSID rclsid
)
909 TRACE(ole
,"(%p,%p)\n",pStm
,rclsid
);
914 return IStream_Write(pStm
,rclsid
,sizeof(CLSID
),NULL
);
917 /************************************************************************************************
920 * This function read a CLSID from a stream
922 HRESULT WINAPI
ReadClassStm(IStream
*pStm
,REFCLSID rclsid
)
927 TRACE(ole
,"(%p,%p)\n",pStm
,rclsid
);
932 res
= IStream_Read(pStm
,(void*)rclsid
,sizeof(CLSID
),&nbByte
);
937 if (nbByte
!= sizeof(CLSID
))
943 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
944 /***********************************************************************
945 * LookupETask (COMPOBJ.94)
947 OLESTATUS WINAPI
LookupETask16(HTASK16
*hTask
,LPVOID p
) {
948 FIXME(ole
,"(%p,%p),stub!\n",hTask
,p
);
949 if ((*hTask
= GetCurrentTask()) == hETask
) {
950 memcpy(p
, Table_ETask
, sizeof(Table_ETask
));
955 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
956 /***********************************************************************
957 * SetETask (COMPOBJ.95)
959 OLESTATUS WINAPI
SetETask16(HTASK16 hTask
, LPVOID p
) {
960 FIXME(ole
,"(%04x,%p),stub!\n",hTask
,p
);
965 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
966 /***********************************************************************
967 * CallObjectInWOW (COMPOBJ.201)
969 OLESTATUS WINAPI
CallObjectInWOW(LPVOID p1
,LPVOID p2
) {
970 FIXME(ole
,"(%p,%p),stub!\n",p1
,p2
);
974 /******************************************************************************
975 * CoRegisterClassObject16 [COMPOBJ.5]
977 * Don't know where it registers it ...
979 HRESULT WINAPI
CoRegisterClassObject16(
982 DWORD dwClsContext
, /* [in] CLSCTX flags indicating the context in which to run the executable */
983 DWORD flags
, /* [in] REGCLS flags indicating how connections are made */
988 WINE_StringFromCLSID(rclsid
,buf
);
990 FIXME(ole
,"(%s,%p,0x%08lx,0x%08lx,%p),stub\n",
991 buf
,pUnk
,dwClsContext
,flags
,lpdwRegister
997 * COM_GetRegisteredClassObject
999 * This internal method is used to scan the registered class list to
1000 * find a class object.
1003 * rclsid Class ID of the class to find.
1004 * dwClsContext Class context to match.
1005 * ppv [out] returns a pointer to the class object. Complying
1006 * to normal COM usage, this method will increase the
1007 * reference count on this object.
1009 static HRESULT
COM_GetRegisteredClassObject(
1014 RegisteredClass
* curClass
;
1022 * Iterate through the whole list and try to match the class ID.
1024 curClass
= firstRegisteredClass
;
1026 while (curClass
!= 0)
1029 * Check if we have a match on the class ID.
1031 if (IsEqualGUID32(&(curClass
->classIdentifier
), rclsid
))
1034 * Since we don't do out-of process or DCOM just right away, let's ignore the
1039 * We have a match, return the pointer to the class object.
1041 *ppUnk
= curClass
->classObject
;
1043 IUnknown_AddRef(curClass
->classObject
);
1049 * Step to the next class in the list.
1051 curClass
= curClass
->nextClass
;
1055 * If we get to here, we haven't found our class.
1060 /******************************************************************************
1061 * CoRegisterClassObject32 [OLE32.36]
1063 * This method will register the class object for a given class ID.
1065 * See the Windows documentation for more details.
1067 HRESULT WINAPI
CoRegisterClassObject(
1070 DWORD dwClsContext
, /* [in] CLSCTX flags indicating the context in which to run the executable */
1071 DWORD flags
, /* [in] REGCLS flags indicating how connections are made */
1072 LPDWORD lpdwRegister
1075 RegisteredClass
* newClass
;
1076 LPUNKNOWN foundObject
;
1080 WINE_StringFromCLSID(rclsid
,buf
);
1082 TRACE(ole
,"(%s,%p,0x%08lx,0x%08lx,%p)\n",
1083 buf
,pUnk
,dwClsContext
,flags
,lpdwRegister
);
1086 * Perform a sanity check on the parameters
1088 if ( (lpdwRegister
==0) || (pUnk
==0) )
1090 return E_INVALIDARG
;
1094 * Initialize the cookie (out parameter)
1099 * First, check if the class is already registered.
1100 * If it is, this should cause an error.
1102 hr
= COM_GetRegisteredClassObject(rclsid
, dwClsContext
, &foundObject
);
1107 * The COM_GetRegisteredClassObject increased the reference count on the
1108 * object so it has to be released.
1110 IUnknown_Release(foundObject
);
1112 return CO_E_OBJISREG
;
1116 * If it is not registered, we must create a new entry for this class and
1117 * append it to the registered class list.
1118 * We use the address of the chain node as the cookie since we are sure it's
1121 newClass
= HeapAlloc(GetProcessHeap(), 0, sizeof(RegisteredClass
));
1124 * Initialize the node.
1126 newClass
->classIdentifier
= *rclsid
;
1127 newClass
->runContext
= dwClsContext
;
1128 newClass
->connectFlags
= flags
;
1129 newClass
->dwCookie
= (DWORD
)newClass
;
1130 newClass
->nextClass
= firstRegisteredClass
;
1133 * Since we're making a copy of the object pointer, we have to increase it's
1136 newClass
->classObject
= pUnk
;
1137 IUnknown_AddRef(newClass
->classObject
);
1139 firstRegisteredClass
= newClass
;
1142 * Assign the out parameter (cookie)
1144 *lpdwRegister
= newClass
->dwCookie
;
1147 * We're successfyl Yippee!
1152 /***********************************************************************
1153 * CoRevokeClassObject32 [OLE32.40]
1155 * This method will remove a class object from the class registry
1157 * See the Windows documentation for more details.
1159 HRESULT WINAPI
CoRevokeClassObject(
1162 RegisteredClass
** prevClassLink
;
1163 RegisteredClass
* curClass
;
1165 TRACE(ole
,"(%08lx)\n",dwRegister
);
1168 * Iterate through the whole list and try to match the cookie.
1170 curClass
= firstRegisteredClass
;
1171 prevClassLink
= &firstRegisteredClass
;
1173 while (curClass
!= 0)
1176 * Check if we have a match on the cookie.
1178 if (curClass
->dwCookie
== dwRegister
)
1181 * Remove the class from the chain.
1183 *prevClassLink
= curClass
->nextClass
;
1186 * Release the reference to the class object.
1188 IUnknown_Release(curClass
->classObject
);
1191 * Free the memory used by the chain node.
1193 HeapFree(GetProcessHeap(), 0, curClass
);
1199 * Step to the next class in the list.
1201 prevClassLink
= &(curClass
->nextClass
);
1202 curClass
= curClass
->nextClass
;
1206 * If we get to here, we haven't found our class.
1208 return E_INVALIDARG
;
1211 /***********************************************************************
1212 * CoGetClassObject [COMPOBJ.7]
1214 HRESULT WINAPI
CoGetClassObject(REFCLSID rclsid
, DWORD dwClsContext
,
1215 LPVOID pvReserved
, REFIID iid
, LPVOID
*ppv
)
1217 LPUNKNOWN regClassObject
;
1218 char xclsid
[50],xiid
[50];
1219 HRESULT hres
= E_UNEXPECTED
;
1221 char dllName
[MAX_PATH
+1];
1222 DWORD dllNameLen
= sizeof(dllName
);
1224 typedef HRESULT (CALLBACK
*DllGetClassObjectFunc
)(REFCLSID clsid
,
1225 REFIID iid
, LPVOID
*ppv
);
1226 DllGetClassObjectFunc DllGetClassObject
;
1228 WINE_StringFromCLSID((LPCLSID
)rclsid
,xclsid
);
1229 WINE_StringFromCLSID((LPCLSID
)iid
,xiid
);
1230 TRACE(ole
,"\n\tCLSID:\t%s,\n\tIID:\t%s\n",xclsid
,xiid
);
1233 * First, try and see if we can't match the class ID with one of the
1234 * registered classes.
1236 if (S_OK
== COM_GetRegisteredClassObject(rclsid
, dwClsContext
, ®ClassObject
))
1239 * Get the required interface from the retrieved pointer.
1241 hres
= IUnknown_QueryInterface(regClassObject
, iid
, ppv
);
1244 * Since QI got another reference on the pointer, we want to release the
1245 * one we already have. If QI was unsuccessful, this will release the object. This
1246 * is good since we are not returning it in the "out" parameter.
1248 IUnknown_Release(regClassObject
);
1253 /* out of process and remote servers not supported yet */
1254 if ((CLSCTX_LOCAL_SERVER
|CLSCTX_REMOTE_SERVER
) & dwClsContext
) {
1255 FIXME(ole
, "CLSCTX_LOCAL_SERVER and CLSCTX_REMOTE_SERVER not supported!\n");
1256 return E_ACCESSDENIED
;
1259 if ((CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
) & dwClsContext
) {
1262 /* lookup CLSID in registry key HKCR/CLSID */
1263 hres
= RegOpenKeyExA(HKEY_CLASSES_ROOT
, "CLSID", 0,
1264 KEY_READ
, &CLSIDkey
);
1266 if (hres
!= ERROR_SUCCESS
)
1267 return REGDB_E_READREGDB
;
1268 hres
= RegOpenKeyExA(CLSIDkey
,xclsid
,0,KEY_QUERY_VALUE
,&key
);
1269 if (hres
!= ERROR_SUCCESS
) {
1270 RegCloseKey(CLSIDkey
);
1271 return REGDB_E_CLASSNOTREG
;
1273 hres
= RegQueryValueA(key
, "InprocServer32", dllName
, &dllNameLen
);
1275 RegCloseKey(CLSIDkey
);
1276 if (hres
!= ERROR_SUCCESS
)
1277 return REGDB_E_READREGDB
;
1278 TRACE(ole
,"found InprocServer32 dll %s\n", dllName
);
1280 /* open dll, call DllGetClassFactory */
1281 hLibrary
= CoLoadLibrary(dllName
, TRUE
);
1282 if (hLibrary
== 0) {
1283 TRACE(ole
,"couldn't load InprocServer32 dll %s\n", dllName
);
1284 return E_ACCESSDENIED
; /* or should this be CO_E_DLLNOTFOUND? */
1286 DllGetClassObject
= (DllGetClassObjectFunc
)GetProcAddress(hLibrary
, "DllGetClassObject");
1287 if (!DllGetClassObject
) {
1288 /* not sure if this should be called here CoFreeLibrary(hLibrary);*/
1289 TRACE(ole
,"couldn't find function DllGetClassObject in %s\n", dllName
);
1290 return E_ACCESSDENIED
;
1294 * Ask the DLL for it's class object. (there was a note here about class
1295 * factories but this is good.
1297 return DllGetClassObject(rclsid
, iid
, ppv
);
1302 /****************************************************************************************
1305 * This function supplies the CLSID associated with the given filename.
1307 HRESULT WINAPI
GetClassFile(LPOLESTR filePathName
,CLSID
*pclsid
)
1311 int nbElm
=0,length
=0,i
=0;
1313 LPOLESTR
*pathDec
=0,absFile
=0,progId
=0;
1314 WCHAR extention
[100]={0};
1318 /* if the file contain a storage object the return the CLSID writen by IStorage_SetClass method*/
1319 if((StgIsStorageFile(filePathName
))==S_OK
){
1321 res
=StgOpenStorage(filePathName
,NULL
,STGM_READ
| STGM_SHARE_DENY_WRITE
,NULL
,0,&pstg
);
1324 res
=ReadClassStg(pstg
,pclsid
);
1326 IStorage_Release(pstg
);
1330 /* if the file is not a storage object then attemps to match various bits in the file against a
1331 pattern in the registry. this case is not frequently used ! so I present only the psodocode for
1334 for(i=0;i<nFileTypes;i++)
1336 for(i=0;j<nPatternsForType;j++){
1341 pat=ReadPatternFromRegistry(i,j);
1342 hFile=CreateFileW(filePathName,,,,,,hFile);
1343 SetFilePosition(hFile,pat.offset);
1344 ReadFile(hFile,buf,pat.size,NULL,NULL);
1345 if (memcmp(buf&pat.mask,pat.pattern.pat.size)==0){
1347 *pclsid=ReadCLSIDFromRegistry(i);
1353 /* if the obove strategies fail then search for the extension key in the registry */
1355 /* get the last element (absolute file) in the path name */
1356 nbElm
=FileMonikerImpl_DecomposePath(filePathName
,&pathDec
);
1357 absFile
=pathDec
[nbElm
-1];
1359 /* failed if the path represente a directory and not an absolute file name*/
1360 if (lstrcmpW(absFile
,(LPOLESTR
)"\\"))
1361 return MK_E_INVALIDEXTENSION
;
1363 /* get the extension of the file */
1364 length
=lstrlenW(absFile
);
1365 for(i
=length
-1; ( (i
>=0) && (extention
[i
]=absFile
[i
]) );i
--);
1367 /* get the progId associated to the extension */
1368 progId
=CoTaskMemAlloc(sizeProgId
);
1370 res
=RegQueryValueW(HKEY_CLASSES_ROOT
,extention
,progId
,&sizeProgId
);
1372 if (res
==ERROR_MORE_DATA
){
1374 CoTaskMemRealloc(progId
,sizeProgId
);
1376 res
=RegQueryValueW(HKEY_CLASSES_ROOT
,extention
,progId
,&sizeProgId
);
1378 if (res
==ERROR_SUCCESS
)
1379 /* return the clsid associated to the progId */
1380 res
= CLSIDFromProgID(progId
,pclsid
);
1382 for(i
=0; pathDec
[i
]!=NULL
;i
++)
1383 CoTaskMemFree(pathDec
[i
]);
1384 CoTaskMemFree(pathDec
);
1386 CoTaskMemFree(progId
);
1388 if (res
==ERROR_SUCCESS
)
1391 return MK_E_INVALIDEXTENSION
;
1393 /******************************************************************************
1394 * CoRegisterMessageFilter16 [COMPOBJ.27]
1396 HRESULT WINAPI
CoRegisterMessageFilter16(
1397 LPMESSAGEFILTER lpMessageFilter
,
1398 LPMESSAGEFILTER
*lplpMessageFilter
1400 FIXME(ole
,"(%p,%p),stub!\n",lpMessageFilter
,lplpMessageFilter
);
1404 /***********************************************************************
1405 * CoCreateInstance [COMPOBJ.13, OLE32.7]
1407 HRESULT WINAPI
CoCreateInstance(
1409 LPUNKNOWN pUnkOuter
,
1415 LPCLASSFACTORY lpclf
= 0;
1424 * Initialize the "out" parameter
1429 * Get a class factory to construct the object we want.
1431 hres
= CoGetClassObject(rclsid
,
1441 * Create the object and don't forget to release the factory
1443 hres
= IClassFactory_CreateInstance(lpclf
, pUnkOuter
, iid
, ppv
);
1444 IClassFactory_Release(lpclf
);
1449 /***********************************************************************
1450 * CoCreateInstanceEx [OLE32.165]
1452 HRESULT WINAPI
CoCreateInstanceEx(
1454 LPUNKNOWN pUnkOuter
,
1456 COSERVERINFO
* pServerInfo
,
1460 IUnknown
* pUnk
= NULL
;
1463 int successCount
= 0;
1468 if ( (cmq
==0) || (pResults
==NULL
))
1469 return E_INVALIDARG
;
1471 if (pServerInfo
!=NULL
)
1472 FIXME(ole
, "() non-NULL pServerInfo not supported!\n");
1475 * Initialize all the "out" parameters.
1477 for (index
= 0; index
< cmq
; index
++)
1479 pResults
[index
].pItf
= NULL
;
1480 pResults
[index
].hr
= E_NOINTERFACE
;
1484 * Get the object and get it's IUnknown pointer.
1486 hr
= CoCreateInstance(rclsid
,
1496 * Then, query for all the interfaces requested.
1498 for (index
= 0; index
< cmq
; index
++)
1500 pResults
[index
].hr
= IUnknown_QueryInterface(pUnk
,
1501 pResults
[index
].pIID
,
1502 (VOID
**)&(pResults
[index
].pItf
));
1504 if (pResults
[index
].hr
== S_OK
)
1509 * Release our temporary unknown pointer.
1511 IUnknown_Release(pUnk
);
1513 if (successCount
== 0)
1514 return E_NOINTERFACE
;
1516 if (successCount
!=cmq
)
1517 return CO_S_NOTALLINTERFACES
;
1522 /***********************************************************************
1523 * CoFreeLibrary [COMPOBJ.13]
1525 void WINAPI
CoFreeLibrary(HINSTANCE hLibrary
)
1527 OpenDll
*ptr
, *prev
;
1530 /* lookup library in linked list */
1532 for (ptr
= openDllList
; ptr
!= NULL
; ptr
=ptr
->next
) {
1533 if (ptr
->hLibrary
== hLibrary
) {
1540 /* shouldn't happen if user passed in a valid hLibrary */
1543 /* assert: ptr points to the library entry to free */
1545 /* free library and remove node from list */
1546 FreeLibrary(hLibrary
);
1547 if (ptr
== openDllList
) {
1548 tmp
= openDllList
->next
;
1549 HeapFree(GetProcessHeap(), 0, openDllList
->DllName
);
1550 HeapFree(GetProcessHeap(), 0, openDllList
);
1554 HeapFree(GetProcessHeap(), 0, ptr
->DllName
);
1555 HeapFree(GetProcessHeap(), 0, ptr
);
1562 /***********************************************************************
1563 * CoFreeAllLibraries [COMPOBJ.12]
1565 void WINAPI
CoFreeAllLibraries(void)
1569 for (ptr
= openDllList
; ptr
!= NULL
; ) {
1571 CoFreeLibrary(ptr
->hLibrary
);
1578 /***********************************************************************
1579 * CoFreeUnusedLibraries [COMPOBJ.17]
1581 void WINAPI
CoFreeUnusedLibraries(void)
1584 typedef HRESULT(*DllCanUnloadNowFunc
)(void);
1585 DllCanUnloadNowFunc DllCanUnloadNow
;
1587 for (ptr
= openDllList
; ptr
!= NULL
; ) {
1588 DllCanUnloadNow
= (DllCanUnloadNowFunc
)
1589 GetProcAddress(ptr
->hLibrary
, "DllCanUnloadNow");
1591 if ( (DllCanUnloadNow
!= NULL
) &&
1592 (DllCanUnloadNow() == S_OK
) ) {
1594 CoFreeLibrary(ptr
->hLibrary
);
1602 /***********************************************************************
1603 * CoFileTimeNow [COMPOBJ.82, OLE32.10]
1605 * the current system time in lpFileTime
1607 HRESULT WINAPI
CoFileTimeNow(
1608 FILETIME
*lpFileTime
/* [out] the current time */
1610 DOSFS_UnixTimeToFileTime(time(NULL
), lpFileTime
, 0);
1614 /***********************************************************************
1615 * CoTaskMemAlloc (OLE32.43)
1617 * pointer to newly allocated block
1619 LPVOID WINAPI
CoTaskMemAlloc(
1620 ULONG size
/* [in] size of memoryblock to be allocated */
1623 HRESULT ret
= CoGetMalloc(0,&lpmalloc
);
1628 return IMalloc_Alloc(lpmalloc
,size
);
1630 /***********************************************************************
1631 * CoTaskMemFree (OLE32.44)
1633 VOID WINAPI
CoTaskMemFree(
1634 LPVOID ptr
/* [in] pointer to be freed */
1637 HRESULT ret
= CoGetMalloc(0,&lpmalloc
);
1642 IMalloc_Free(lpmalloc
, ptr
);
1645 /***********************************************************************
1646 * CoTaskMemRealloc (OLE32.45)
1648 * pointer to newly allocated block
1650 LPVOID WINAPI
CoTaskMemRealloc(
1652 ULONG size
) /* [in] size of memoryblock to be allocated */
1655 HRESULT ret
= CoGetMalloc(0,&lpmalloc
);
1660 return IMalloc_Realloc(lpmalloc
, pvOld
, size
);
1663 /***********************************************************************
1664 * CoLoadLibrary (OLE32.30)
1666 HINSTANCE WINAPI
CoLoadLibrary(LPOLESTR16 lpszLibName
, BOOL bAutoFree
)
1672 TRACE(ole
,"CoLoadLibrary(%p, %d\n", lpszLibName
, bAutoFree
);
1674 hLibrary
= LoadLibraryA(lpszLibName
);
1679 if (openDllList
== NULL
) {
1680 /* empty list -- add first node */
1681 openDllList
= (OpenDll
*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll
));
1682 openDllList
->DllName
= HEAP_strdupA(GetProcessHeap(), 0, lpszLibName
);
1683 openDllList
->hLibrary
= hLibrary
;
1684 openDllList
->next
= NULL
;
1686 /* search for this dll */
1688 for (ptr
= openDllList
; ptr
->next
!= NULL
; ptr
=ptr
->next
) {
1689 if (ptr
->hLibrary
== hLibrary
) {
1695 /* dll not found, add it */
1697 openDllList
= (OpenDll
*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll
));
1698 openDllList
->DllName
= HEAP_strdupA(GetProcessHeap(), 0, lpszLibName
);
1699 openDllList
->hLibrary
= hLibrary
;
1700 openDllList
->next
= tmp
;
1707 /***********************************************************************
1708 * CoInitializeWOW (OLE32.27)
1710 HRESULT WINAPI
CoInitializeWOW(DWORD x
,DWORD y
) {
1711 FIXME(ole
,"(0x%08lx,0x%08lx),stub!\n",x
,y
);
1715 /******************************************************************************
1716 * CoLockObjectExternal16 [COMPOBJ.63]
1718 HRESULT WINAPI
CoLockObjectExternal16(
1719 LPUNKNOWN pUnk
, /* [in] object to be locked */
1720 BOOL16 fLock
, /* [in] do lock */
1721 BOOL16 fLastUnlockReleases
/* [in] ? */
1723 FIXME(ole
,"(%p,%d,%d),stub!\n",pUnk
,fLock
,fLastUnlockReleases
);
1727 /******************************************************************************
1728 * CoLockObjectExternal32 [OLE32.31]
1730 HRESULT WINAPI
CoLockObjectExternal(
1731 LPUNKNOWN pUnk
, /* [in] object to be locked */
1732 BOOL fLock
, /* [in] do lock */
1733 BOOL fLastUnlockReleases
) /* [in] unlock all */
1739 * Increment the external lock coutner, COM_ExternalLockAddRef also
1740 * increment the object's internal lock counter.
1742 COM_ExternalLockAddRef( pUnk
);
1747 * Decrement the external lock coutner, COM_ExternalLockRelease also
1748 * decrement the object's internal lock counter.
1750 COM_ExternalLockRelease( pUnk
, fLastUnlockReleases
);
1756 /***********************************************************************
1757 * CoGetState16 [COMPOBJ.115]
1759 HRESULT WINAPI
CoGetState16(LPDWORD state
)
1761 FIXME(ole
, "(%p),stub!\n", state
);
1765 /***********************************************************************
1766 * CoSetState32 [COM32.42]
1768 HRESULT WINAPI
CoSetState(LPDWORD state
)
1770 FIXME(ole
, "(%p),stub!\n", state
);
1771 if (state
) *state
= 0;
1776 * COM_RevokeAllClasses
1778 * This method is called when the COM libraries are uninitialized to
1779 * release all the references to the class objects registered with
1782 static void COM_RevokeAllClasses()
1784 while (firstRegisteredClass
!=0)
1786 CoRevokeClassObject(firstRegisteredClass
->dwCookie
);
1790 /****************************************************************************
1791 * COM External Lock methods implementation
1794 /****************************************************************************
1795 * Public - Method that increments the count for a IUnknown* in the linked
1796 * list. The item is inserted if not already in the list.
1798 static void COM_ExternalLockAddRef(
1801 COM_ExternalLock
*externalLock
= COM_ExternalLockFind(pUnk
);
1804 * Add an external lock to the object. If it was already externally
1805 * locked, just increase the reference count. If it was not.
1806 * add the item to the list.
1808 if ( externalLock
== EL_NOT_FOUND
)
1809 COM_ExternalLockInsert(pUnk
);
1811 externalLock
->uRefCount
++;
1814 * Add an internal lock to the object
1816 IUnknown_AddRef(pUnk
);
1819 /****************************************************************************
1820 * Public - Method that decrements the count for a IUnknown* in the linked
1821 * list. The item is removed from the list if its count end up at zero or if
1824 static void COM_ExternalLockRelease(
1828 COM_ExternalLock
*externalLock
= COM_ExternalLockFind(pUnk
);
1830 if ( externalLock
!= EL_NOT_FOUND
)
1834 externalLock
->uRefCount
--; /* release external locks */
1835 IUnknown_Release(pUnk
); /* release local locks as well */
1837 if ( bRelAll
== FALSE
)
1838 break; /* perform single release */
1840 } while ( externalLock
->uRefCount
> 0 );
1842 if ( externalLock
->uRefCount
== 0 ) /* get rid of the list entry */
1843 COM_ExternalLockDelete(externalLock
);
1846 /****************************************************************************
1847 * Public - Method that frees the content of the list.
1849 static void COM_ExternalLockFreeList()
1851 COM_ExternalLock
*head
;
1853 head
= elList
.head
; /* grab it by the head */
1854 while ( head
!= EL_END_OF_LIST
)
1856 COM_ExternalLockDelete(head
); /* get rid of the head stuff */
1858 head
= elList
.head
; /* get the new head... */
1862 /****************************************************************************
1863 * Public - Method that dump the content of the list.
1865 void COM_ExternalLockDump()
1867 COM_ExternalLock
*current
= elList
.head
;
1869 printf("\nExternal lock list contains:\n");
1871 while ( current
!= EL_END_OF_LIST
)
1873 printf( "\t%p with %lu references count.\n",
1875 current
->uRefCount
);
1877 /* Skip to the next item */
1878 current
= current
->next
;
1883 /****************************************************************************
1884 * Internal - Find a IUnknown* in the linked list
1886 static COM_ExternalLock
* COM_ExternalLockFind(
1889 return COM_ExternalLockLocate(elList
.head
, pUnk
);
1892 /****************************************************************************
1893 * Internal - Recursivity agent for IUnknownExternalLockList_Find
1895 static COM_ExternalLock
* COM_ExternalLockLocate(
1896 COM_ExternalLock
*element
,
1899 if ( element
== EL_END_OF_LIST
)
1900 return EL_NOT_FOUND
;
1902 else if ( element
->pUnk
== pUnk
) /* We found it */
1905 else /* Not the right guy, keep on looking */
1906 return COM_ExternalLockLocate( element
->next
, pUnk
);
1909 /****************************************************************************
1910 * Internal - Insert a new IUnknown* to the linked list
1912 static BOOL
COM_ExternalLockInsert(
1915 COM_ExternalLock
*newLock
= NULL
;
1916 COM_ExternalLock
*previousHead
= NULL
;
1919 * Allocate space for the new storage object
1921 newLock
= HeapAlloc(GetProcessHeap(), 0, sizeof(COM_ExternalLock
));
1925 if ( elList
.head
== EL_END_OF_LIST
)
1927 elList
.head
= newLock
; /* The list is empty */
1932 * insert does it at the head
1934 previousHead
= elList
.head
;
1935 elList
.head
= newLock
;
1939 * Set new list item data member
1941 newLock
->pUnk
= pUnk
;
1942 newLock
->uRefCount
= 1;
1943 newLock
->next
= previousHead
;
1951 /****************************************************************************
1952 * Internal - Method that removes an item from the linked list.
1954 static void COM_ExternalLockDelete(
1955 COM_ExternalLock
*itemList
)
1957 COM_ExternalLock
*current
= elList
.head
;
1959 if ( current
== itemList
)
1962 * this section handles the deletion of the first node
1964 elList
.head
= itemList
->next
;
1965 HeapFree( GetProcessHeap(), 0, itemList
);
1971 if ( current
->next
== itemList
) /* We found the item to free */
1973 current
->next
= itemList
->next
; /* readjust the list pointers */
1975 HeapFree( GetProcessHeap(), 0, itemList
);
1979 /* Skip to the next item */
1980 current
= current
->next
;
1982 } while ( current
!= EL_END_OF_LIST
);