Changed the button state to "up" as soon as it is known that the
[wine/multimedia.git] / ole / compobj.c
blob30a0c47bd1fe8fdfc48868fbef6ab2b393ebd2ea
1 /*
2 * COMPOBJ library
4 * Copyright 1995 Martin von Loewis
5 * Copyright 1998 Justin Bradford
6 * Copyright 1999 Francis Beaudet
7 * Copyright 1999 Sylvain St-Germain
8 */
10 #include "config.h"
11 #ifdef HAVE_UNISTD_H
12 # include <unistd.h>
13 #endif
14 #include <fcntl.h>
15 #include <sys/types.h>
16 #include <sys/time.h>
17 #include <sys/stat.h>
18 #ifdef HAVE_SYS_FILE_H
19 # include <sys/file.h>
20 #endif
21 #include <sys/ioctl.h>
22 #include <sys/socket.h>
23 #ifdef HAVE_SYS_SOCKIO_H
24 # include <sys/sockio.h>
25 #endif
26 #ifdef HAVE_NET_IF_H
27 # include <net/if.h>
28 #endif
29 #ifdef HAVE_NETINET_IN_H
30 # include <netinet/in.h>
31 #endif
32 #include <stdlib.h>
33 #include <string.h>
34 #include <time.h>
35 #include <assert.h>
36 #include "windef.h"
37 #include "wine/winbase16.h"
38 #include "wine/winestring.h"
39 #include "winerror.h"
40 #include "ole.h"
41 #include "ole2ver.h"
42 #include "debug.h"
43 #include "file.h"
44 #include "heap.h"
45 #include "ldt.h"
46 #include "winreg.h"
48 #include "wine/obj_base.h"
49 #include "wine/obj_misc.h"
50 #include "wine/obj_clientserver.h"
52 #include "ifs.h"
54 /****************************************************************************
55 * COM External Lock structures and methods declaration
57 * This api provides a linked list to managed external references to
58 * COM objects.
60 * The public interface consists of three calls:
61 * COM_ExternalLockAddRef
62 * COM_ExternalLockRelease
63 * COM_ExternalLockFreeList
66 #define EL_END_OF_LIST 0
67 #define EL_NOT_FOUND 0
70 * Declaration of the static structure that manage the
71 * external lock to COM objects.
73 typedef struct COM_ExternalLock COM_ExternalLock;
74 typedef struct COM_ExternalLockList COM_ExternalLockList;
76 struct COM_ExternalLock
78 IUnknown *pUnk; /* IUnknown referenced */
79 ULONG uRefCount; /* external lock counter to IUnknown object*/
80 COM_ExternalLock *next; /* Pointer to next element in list */
83 struct COM_ExternalLockList
85 COM_ExternalLock *head; /* head of list */
89 * Declaration and initialization of the static structure that manages
90 * the external lock to COM objects.
92 static COM_ExternalLockList elList = { EL_END_OF_LIST };
95 * Public Interface to the external lock list
97 static void COM_ExternalLockFreeList();
98 static void COM_ExternalLockAddRef(IUnknown *pUnk);
99 static void COM_ExternalLockRelease(IUnknown *pUnk, BOOL bRelAll);
100 void COM_ExternalLockDump(); /* testing purposes, not static to avoid warning */
103 * Private methods used to managed the linked list
105 static BOOL COM_ExternalLockInsert(
106 IUnknown *pUnk);
108 static void COM_ExternalLockDelete(
109 COM_ExternalLock *element);
111 static COM_ExternalLock* COM_ExternalLockFind(
112 IUnknown *pUnk);
114 static COM_ExternalLock* COM_ExternalLockLocate(
115 COM_ExternalLock *element,
116 IUnknown *pUnk);
118 /****************************************************************************
119 * This section defines variables internal to the COM module.
121 * TODO: Most of these things will have to be made thread-safe.
123 LPMALLOC16 currentMalloc16=NULL;
124 LPMALLOC currentMalloc32=NULL;
126 HTASK16 hETask = 0;
127 WORD Table_ETask[62];
130 * This lock count counts the number of times CoInitialize is called. It is
131 * decreased every time CoUninitialize is called. When it hits 0, the COM
132 * libraries are freed
134 static ULONG s_COMLockCount = 0;
137 * This linked list contains the list of registered class objects. These
138 * are mostly used to register the factories for out-of-proc servers of OLE
139 * objects.
141 * TODO: Make this data structure aware of inter-process communication. This
142 * means that parts of this will be exported to the Wine Server.
144 typedef struct tagRegisteredClass
146 CLSID classIdentifier;
147 LPUNKNOWN classObject;
148 DWORD runContext;
149 DWORD connectFlags;
150 DWORD dwCookie;
151 struct tagRegisteredClass* nextClass;
152 } RegisteredClass;
154 static RegisteredClass* firstRegisteredClass = NULL;
156 /* this open DLL table belongs in a per process table, but my guess is that
157 * it shouldn't live in the kernel, so I'll put them out here in DLL
158 * space assuming that there is one OLE32 per process.
160 typedef struct tagOpenDll {
161 char *DllName; /* really only needed for debugging */
162 HINSTANCE hLibrary;
163 struct tagOpenDll *next;
164 } OpenDll;
166 static OpenDll *openDllList = NULL; /* linked list of open dlls */
168 /*****************************************************************************
169 * This section contains prototypes to internal methods for this
170 * module
172 static HRESULT COM_GetRegisteredClassObject(REFCLSID rclsid,
173 DWORD dwClsContext,
174 LPUNKNOWN* ppUnk);
176 static void COM_RevokeAllClasses();
179 /******************************************************************************
180 * CoBuildVersion [COMPOBJ.1]
182 * RETURNS
183 * Current built version, hiword is majornumber, loword is minornumber
185 DWORD WINAPI CoBuildVersion(void)
187 TRACE(ole,"(void)\n");
188 return (rmm<<16)+rup;
191 /******************************************************************************
192 * CoInitialize16 [COMPOBJ.2]
193 * Set the win16 IMalloc used for memory management
195 HRESULT WINAPI CoInitialize16(
196 LPVOID lpReserved /* [in] pointer to win16 malloc interface */
198 currentMalloc16 = (LPMALLOC16)lpReserved;
199 return S_OK;
202 /******************************************************************************
203 * CoInitialize32 [OLE32.26]
205 * Initializes the COM libraries.
207 * See CoInitializeEx32
209 HRESULT WINAPI CoInitialize(
210 LPVOID lpReserved /* [in] pointer to win32 malloc interface
211 (obsolete, should be NULL) */
215 * Just delegate to the newer method.
217 return CoInitializeEx(lpReserved, COINIT_APARTMENTTHREADED);
220 /******************************************************************************
221 * CoInitializeEx32 [OLE32.163]
223 * Initializes the COM libraries. The behavior used to set the win32 IMalloc
224 * used for memory management is obsolete.
226 * RETURNS
227 * S_OK if successful,
228 * S_FALSE if this function was called already.
229 * RPC_E_CHANGED_MODE if a previous call to CoInitialize specified another
230 * threading model.
232 * BUGS
233 * Only the single threaded model is supported. As a result RPC_E_CHANGED_MODE
234 * is never returned.
236 * See the windows documentation for more details.
238 HRESULT WINAPI CoInitializeEx(
239 LPVOID lpReserved, /* [in] pointer to win32 malloc interface
240 (obsolete, should be NULL) */
241 DWORD dwCoInit /* [in] A value from COINIT specifies the threading model */
244 HRESULT hr;
246 TRACE(ole, "(%p, %x)\n", lpReserved, (int)dwCoInit);
248 if (lpReserved!=NULL)
250 ERR(ole,"(%p, %x) - Bad parameter passed-in %p, must be an old Windows Application\n", lpReserved, (int)dwCoInit, lpReserved);
254 * Check for unsupported features.
256 if (dwCoInit!=COINIT_APARTMENTTHREADED)
258 FIXME(ole, ":(%p,%x): unsupported flag %x\n", lpReserved, (int)dwCoInit, (int)dwCoInit);
259 /* Hope for the best and continue anyway */
263 * Check the lock count. If this is the first time going through the initialize
264 * process, we have to initialize the libraries.
266 if (s_COMLockCount==0)
269 * Initialize the various COM libraries and data structures.
271 TRACE(ole, "() - Initializing the COM libraries\n");
273 hr = S_OK;
275 else
276 hr = S_FALSE;
279 * Crank-up that lock count.
281 s_COMLockCount++;
283 return hr;
286 /***********************************************************************
287 * CoUninitialize16 [COMPOBJ.3]
288 * Don't know what it does.
289 * 3-Nov-98 -- this was originally misspelled, I changed it to what I
290 * believe is the correct spelling
292 void WINAPI CoUninitialize16(void)
294 TRACE(ole,"()\n");
295 CoFreeAllLibraries();
298 /***********************************************************************
299 * CoUninitialize32 [OLE32.47]
301 * This method will release the COM libraries.
303 * See the windows documentation for more details.
305 void WINAPI CoUninitialize(void)
307 TRACE(ole,"()\n");
310 * Decrease the reference count.
312 s_COMLockCount--;
315 * If we are back to 0 locks on the COM library, make sure we free
316 * all the associated data structures.
318 if (s_COMLockCount==0)
321 * Release the various COM libraries and data structures.
323 TRACE(ole, "() - Releasing the COM libraries\n");
326 * Release the references to the registered class objects.
328 COM_RevokeAllClasses();
331 * This will free the loaded COM Dlls.
333 CoFreeAllLibraries();
336 * This will free list of external references to COM objects.
338 COM_ExternalLockFreeList();
342 /***********************************************************************
343 * CoGetMalloc16 [COMPOBJ.4]
344 * RETURNS
345 * The current win16 IMalloc
347 HRESULT WINAPI CoGetMalloc16(
348 DWORD dwMemContext, /* [in] unknown */
349 LPMALLOC16 * lpMalloc /* [out] current win16 malloc interface */
351 if(!currentMalloc16)
352 currentMalloc16 = IMalloc16_Constructor();
353 *lpMalloc = currentMalloc16;
354 return S_OK;
357 /******************************************************************************
358 * CoGetMalloc32 [OLE32.20]
360 * RETURNS
361 * The current win32 IMalloc
363 HRESULT WINAPI CoGetMalloc(
364 DWORD dwMemContext, /* [in] unknown */
365 LPMALLOC *lpMalloc /* [out] current win32 malloc interface */
367 if(!currentMalloc32)
368 currentMalloc32 = IMalloc_Constructor();
369 *lpMalloc = currentMalloc32;
370 return S_OK;
373 /***********************************************************************
374 * CoCreateStandardMalloc16 [COMPOBJ.71]
376 HRESULT WINAPI CoCreateStandardMalloc16(DWORD dwMemContext,
377 LPMALLOC16 *lpMalloc)
379 /* FIXME: docu says we shouldn't return the same allocator as in
380 * CoGetMalloc16 */
381 *lpMalloc = IMalloc16_Constructor();
382 return S_OK;
385 /******************************************************************************
386 * CoDisconnectObject [COMPOBJ.15]
388 HRESULT WINAPI CoDisconnectObject( LPUNKNOWN lpUnk, DWORD reserved )
390 TRACE(ole,"%p %lx\n",lpUnk,reserved);
391 return S_OK;
394 /***********************************************************************
395 * IsEqualGUID16 [COMPOBJ.18]
397 * Compares two Unique Identifiers.
399 * RETURNS
400 * TRUE if equal
402 BOOL16 WINAPI IsEqualGUID16(
403 GUID* g1, /* [in] unique id 1 */
404 GUID* g2 /**/
406 return !memcmp( g1, g2, sizeof(GUID) );
409 /***********************************************************************
410 * IsEqualGUID32 [OLE32.76]
412 * Compares two Unique Identifiers.
414 * RETURNS
415 * TRUE if equal
417 BOOL WINAPI IsEqualGUID32(
418 REFGUID rguid1, /* [in] unique id 1 */
419 REFGUID rguid2 /* [in] unique id 2 */
422 return !memcmp(rguid1,rguid2,sizeof(GUID));
425 /******************************************************************************
426 * CLSIDFromString16 [COMPOBJ.20]
427 * Converts a unique identifier from it's string representation into
428 * the GUID struct.
430 * Class id: DWORD-WORD-WORD-BYTES[2]-BYTES[6]
432 * RETURNS
433 * the converted GUID
435 HRESULT WINAPI CLSIDFromString16(
436 LPCOLESTR16 idstr, /* [in] string representation of guid */
437 CLSID *id /* [out] GUID converted from string */
439 BYTE *s = (BYTE *) idstr;
440 BYTE *p;
441 int i;
442 BYTE table[256];
444 TRACE(ole,"%s -> %p\n", idstr, id);
446 /* quick lookup table */
447 memset(table, 0, 256);
449 for (i = 0; i < 10; i++) {
450 table['0' + i] = i;
452 for (i = 0; i < 6; i++) {
453 table['A' + i] = i+10;
454 table['a' + i] = i+10;
457 /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
459 if (strlen(idstr) != 38)
460 return OLE_ERROR_OBJECT;
462 p = (BYTE *) id;
464 s++; /* skip leading brace */
465 for (i = 0; i < 4; i++) {
466 p[3 - i] = table[*s]<<4 | table[*(s+1)];
467 s += 2;
469 p += 4;
470 s++; /* skip - */
472 for (i = 0; i < 2; i++) {
473 p[1-i] = table[*s]<<4 | table[*(s+1)];
474 s += 2;
476 p += 2;
477 s++; /* skip - */
479 for (i = 0; i < 2; i++) {
480 p[1-i] = table[*s]<<4 | table[*(s+1)];
481 s += 2;
483 p += 2;
484 s++; /* skip - */
486 /* these are just sequential bytes */
487 for (i = 0; i < 2; i++) {
488 *p++ = table[*s]<<4 | table[*(s+1)];
489 s += 2;
491 s++; /* skip - */
493 for (i = 0; i < 6; i++) {
494 *p++ = table[*s]<<4 | table[*(s+1)];
495 s += 2;
498 return S_OK;
501 /******************************************************************************
502 * CoCreateGuid[OLE32.6]
504 * Creates a 128bit GUID.
505 * Implemented according the DCE specification for UUID generation.
506 * Code is based upon uuid library in e2fsprogs by Theodore Ts'o.
507 * Copyright (C) 1996, 1997 Theodore Ts'o.
509 * RETURNS
511 * S_OK if successful.
513 HRESULT WINAPI CoCreateGuid(
514 GUID *pguid /* [out] points to the GUID to initialize */
516 static char has_init = 0;
517 unsigned char a[6];
518 static int adjustment = 0;
519 static struct timeval last = {0, 0};
520 static UINT16 clock_seq;
521 struct timeval tv;
522 unsigned long long clock_reg;
523 UINT clock_high, clock_low;
524 UINT16 temp_clock_seq, temp_clock_mid, temp_clock_hi_and_version;
525 #ifdef HAVE_NET_IF_H
526 int sd;
527 struct ifreq ifr, *ifrp;
528 struct ifconf ifc;
529 char buf[1024];
530 int n, i;
531 #endif
533 /* Have we already tried to get the MAC address? */
534 if (!has_init) {
535 #ifdef HAVE_NET_IF_H
536 /* BSD 4.4 defines the size of an ifreq to be
537 * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
538 * However, under earlier systems, sa_len isn't present, so
539 * the size is just sizeof(struct ifreq)
541 # ifdef HAVE_SA_LEN
542 # ifndef max
543 # define max(a,b) ((a) > (b) ? (a) : (b))
544 # endif
545 # define ifreq_size(i) max(sizeof(struct ifreq),\
546 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
547 # else
548 # define ifreq_size(i) sizeof(struct ifreq)
549 # endif /* HAVE_SA_LEN */
551 sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
552 if (sd < 0) {
553 /* if we can't open a socket, just use random numbers */
554 /* set the multicast bit to prevent conflicts with real cards */
555 a[0] = (rand() & 0xff) | 0x80;
556 a[1] = rand() & 0xff;
557 a[2] = rand() & 0xff;
558 a[3] = rand() & 0xff;
559 a[4] = rand() & 0xff;
560 a[5] = rand() & 0xff;
561 } else {
562 memset(buf, 0, sizeof(buf));
563 ifc.ifc_len = sizeof(buf);
564 ifc.ifc_buf = buf;
565 /* get the ifconf interface */
566 if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) {
567 close(sd);
568 /* no ifconf, so just use random numbers */
569 /* set the multicast bit to prevent conflicts with real cards */
570 a[0] = (rand() & 0xff) | 0x80;
571 a[1] = rand() & 0xff;
572 a[2] = rand() & 0xff;
573 a[3] = rand() & 0xff;
574 a[4] = rand() & 0xff;
575 a[5] = rand() & 0xff;
576 } else {
577 /* loop through the interfaces, looking for a valid one */
578 n = ifc.ifc_len;
579 for (i = 0; i < n; i+= ifreq_size(*ifr) ) {
580 ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i);
581 strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
582 /* try to get the address for this interface */
583 # ifdef SIOCGIFHWADDR
584 if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
585 continue;
586 memcpy(a, (unsigned char *)&ifr.ifr_hwaddr.sa_data, 6);
587 # else
588 # ifdef SIOCGENADDR
589 if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
590 continue;
591 memcpy(a, (unsigned char *) ifr.ifr_enaddr, 6);
592 # else
593 /* XXX we don't have a way of getting the hardware address */
594 close(sd);
595 a[0] = 0;
596 break;
597 # endif /* SIOCGENADDR */
598 # endif /* SIOCGIFHWADDR */
599 /* make sure it's not blank */
600 if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5])
601 continue;
603 goto valid_address;
605 /* if we didn't find a valid address, make a random one */
606 /* once again, set multicast bit to avoid conflicts */
607 a[0] = (rand() & 0xff) | 0x80;
608 a[1] = rand() & 0xff;
609 a[2] = rand() & 0xff;
610 a[3] = rand() & 0xff;
611 a[4] = rand() & 0xff;
612 a[5] = rand() & 0xff;
614 valid_address:
615 close(sd);
618 #else
619 /* no networking info, so generate a random address */
620 a[0] = (rand() & 0xff) | 0x80;
621 a[1] = rand() & 0xff;
622 a[2] = rand() & 0xff;
623 a[3] = rand() & 0xff;
624 a[4] = rand() & 0xff;
625 a[5] = rand() & 0xff;
626 #endif /* HAVE_NET_IF_H */
627 has_init = 1;
630 /* generate time element of GUID */
632 /* Assume that the gettimeofday() has microsecond granularity */
633 #define MAX_ADJUSTMENT 10
635 try_again:
636 gettimeofday(&tv, 0);
637 if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
638 clock_seq = ((rand() & 0xff) << 8) + (rand() & 0xff);
639 clock_seq &= 0x1FFF;
640 last = tv;
641 last.tv_sec--;
643 if ((tv.tv_sec < last.tv_sec) ||
644 ((tv.tv_sec == last.tv_sec) &&
645 (tv.tv_usec < last.tv_usec))) {
646 clock_seq = (clock_seq+1) & 0x1FFF;
647 adjustment = 0;
648 } else if ((tv.tv_sec == last.tv_sec) &&
649 (tv.tv_usec == last.tv_usec)) {
650 if (adjustment >= MAX_ADJUSTMENT)
651 goto try_again;
652 adjustment++;
653 } else
654 adjustment = 0;
656 clock_reg = tv.tv_usec*10 + adjustment;
657 clock_reg += ((unsigned long long) tv.tv_sec)*10000000;
658 clock_reg += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
660 clock_high = clock_reg >> 32;
661 clock_low = clock_reg;
662 temp_clock_seq = clock_seq | 0x8000;
663 temp_clock_mid = (UINT16)clock_high;
664 temp_clock_hi_and_version = (clock_high >> 16) | 0x1000;
666 /* pack the information into the GUID structure */
668 ((unsigned char*)&pguid->Data1)[3] = (unsigned char)clock_low;
669 clock_low >>= 8;
670 ((unsigned char*)&pguid->Data1)[2] = (unsigned char)clock_low;
671 clock_low >>= 8;
672 ((unsigned char*)&pguid->Data1)[1] = (unsigned char)clock_low;
673 clock_low >>= 8;
674 ((unsigned char*)&pguid->Data1)[0] = (unsigned char)clock_low;
676 ((unsigned char*)&pguid->Data2)[1] = (unsigned char)temp_clock_mid;
677 temp_clock_mid >>= 8;
678 ((unsigned char*)&pguid->Data2)[0] = (unsigned char)temp_clock_mid;
680 ((unsigned char*)&pguid->Data3)[1] = (unsigned char)temp_clock_hi_and_version;
681 temp_clock_hi_and_version >>= 8;
682 ((unsigned char*)&pguid->Data3)[0] = (unsigned char)temp_clock_hi_and_version;
684 ((unsigned char*)pguid->Data4)[1] = (unsigned char)temp_clock_seq;
685 temp_clock_seq >>= 8;
686 ((unsigned char*)pguid->Data4)[0] = (unsigned char)temp_clock_seq;
688 ((unsigned char*)pguid->Data4)[2] = a[0];
689 ((unsigned char*)pguid->Data4)[3] = a[1];
690 ((unsigned char*)pguid->Data4)[4] = a[2];
691 ((unsigned char*)pguid->Data4)[5] = a[3];
692 ((unsigned char*)pguid->Data4)[6] = a[4];
693 ((unsigned char*)pguid->Data4)[7] = a[5];
695 TRACE(ole, "%p", pguid);
697 return S_OK;
700 /******************************************************************************
701 * CLSIDFromString32 [OLE32.3]
702 * Converts a unique identifier from it's string representation into
703 * the GUID struct.
704 * RETURNS
705 * the converted GUID
707 HRESULT WINAPI CLSIDFromString(
708 LPCOLESTR idstr, /* [in] string representation of GUID */
709 CLSID *id /* [out] GUID represented by above string */
711 LPOLESTR16 xid = HEAP_strdupWtoA(GetProcessHeap(),0,idstr);
712 OLESTATUS ret = CLSIDFromString16(xid,id);
714 HeapFree(GetProcessHeap(),0,xid);
715 return ret;
718 /******************************************************************************
719 * WINE_StringFromCLSID [Internal]
720 * Converts a GUID into the respective string representation.
722 * NOTES
724 * RETURNS
725 * the string representation and OLESTATUS
727 HRESULT WINE_StringFromCLSID(
728 const CLSID *id, /* [in] GUID to be converted */
729 LPSTR idstr /* [out] pointer to buffer to contain converted guid */
731 static const char *hex = "0123456789ABCDEF";
732 char *s;
733 int i;
735 if (!id)
736 { ERR(ole,"called with id=Null\n");
737 *idstr = 0x00;
738 return E_FAIL;
741 sprintf(idstr, "{%08lX-%04X-%04X-%02x%02X-",
742 id->Data1, id->Data2, id->Data3,
743 id->Data4[0], id->Data4[1]);
744 s = &idstr[25];
746 /* 6 hex bytes */
747 for (i = 2; i < 8; i++) {
748 *s++ = hex[id->Data4[i]>>4];
749 *s++ = hex[id->Data4[i] & 0xf];
752 *s++ = '}';
753 *s++ = '\0';
755 TRACE(ole,"%p->%s\n", id, idstr);
757 return OLE_OK;
760 /******************************************************************************
761 * StringFromCLSID16 [COMPOBJ.19]
762 * Converts a GUID into the respective string representation.
763 * The target string is allocated using the OLE IMalloc.
764 * RETURNS
765 * the string representation and OLESTATUS
767 HRESULT WINAPI StringFromCLSID16(
768 REFCLSID id, /* [in] the GUID to be converted */
769 LPOLESTR16 *idstr /* [out] a pointer to a to-be-allocated segmented pointer pointing to the resulting string */
772 LPMALLOC16 mllc;
773 OLESTATUS ret;
774 DWORD args[2];
776 ret = CoGetMalloc16(0,&mllc);
777 if (ret) return ret;
779 args[0] = (DWORD)mllc;
780 args[1] = 40;
782 /* No need for a Callback entry, we have WOWCallback16Ex which does
783 * everything we need.
785 if (!WOWCallback16Ex(
786 (FARPROC16)((ICOM_VTABLE(IMalloc16)*)PTR_SEG_TO_LIN(
787 ((LPMALLOC16)PTR_SEG_TO_LIN(mllc))->lpvtbl)
788 )->fnAlloc,
789 WCB16_CDECL,
791 (LPVOID)args,
792 (LPDWORD)idstr
793 )) {
794 WARN(ole,"CallTo16 IMalloc16 failed\n");
795 return E_FAIL;
797 return WINE_StringFromCLSID(id,PTR_SEG_TO_LIN(*idstr));
800 /******************************************************************************
801 * StringFromCLSID32 [OLE32.151]
802 * Converts a GUID into the respective string representation.
803 * The target string is allocated using the OLE IMalloc.
804 * RETURNS
805 * the string representation and OLESTATUS
807 HRESULT WINAPI StringFromCLSID(
808 REFCLSID id, /* [in] the GUID to be converted */
809 LPOLESTR *idstr /* [out] a pointer to a to-be-allocated pointer pointing to the resulting string */
811 char buf[80];
812 OLESTATUS ret;
813 LPMALLOC mllc;
815 if ((ret=CoGetMalloc(0,&mllc)))
816 return ret;
818 ret=WINE_StringFromCLSID(id,buf);
819 if (!ret) {
820 *idstr = mllc->lpvtbl->fnAlloc(mllc,strlen(buf)*2+2);
821 lstrcpyAtoW(*idstr,buf);
823 return ret;
826 /******************************************************************************
827 * StringFromGUID2 [COMPOBJ.76] [OLE32.152]
829 * Converts a global unique identifier into a string of an API-
830 * specified fixed format. (The usual {.....} stuff.)
832 * RETURNS
833 * The (UNICODE) string representation of the GUID in 'str'
834 * The length of the resulting string, 0 if there was any problem.
836 INT WINAPI
837 StringFromGUID2(REFGUID id, LPOLESTR str, INT cmax)
839 char xguid[80];
841 if (WINE_StringFromCLSID(id,xguid))
842 return 0;
843 if (strlen(xguid)>=cmax)
844 return 0;
845 lstrcpyAtoW(str,xguid);
846 return strlen(xguid);
849 /******************************************************************************
850 * CLSIDFromProgID16 [COMPOBJ.61]
851 * Converts a program id into the respective GUID. (By using a registry lookup)
852 * RETURNS
853 * riid associated with the progid
855 HRESULT WINAPI CLSIDFromProgID16(
856 LPCOLESTR16 progid, /* [in] program id as found in registry */
857 LPCLSID riid /* [out] associated CLSID */
859 char *buf,buf2[80];
860 DWORD buf2len;
861 HRESULT err;
862 HKEY xhkey;
864 buf = HeapAlloc(GetProcessHeap(),0,strlen(progid)+8);
865 sprintf(buf,"%s\\CLSID",progid);
866 if ((err=RegOpenKeyA(HKEY_CLASSES_ROOT,buf,&xhkey))) {
867 HeapFree(GetProcessHeap(),0,buf);
868 return OLE_ERROR_GENERIC;
870 HeapFree(GetProcessHeap(),0,buf);
871 buf2len = sizeof(buf2);
872 if ((err=RegQueryValueA(xhkey,NULL,buf2,&buf2len))) {
873 RegCloseKey(xhkey);
874 return OLE_ERROR_GENERIC;
876 RegCloseKey(xhkey);
877 return CLSIDFromString16(buf2,riid);
880 /******************************************************************************
881 * CLSIDFromProgID32 [OLE32.2]
882 * Converts a program id into the respective GUID. (By using a registry lookup)
883 * RETURNS
884 * riid associated with the progid
886 HRESULT WINAPI CLSIDFromProgID(
887 LPCOLESTR progid, /* [in] program id as found in registry */
888 LPCLSID riid /* [out] associated CLSID */
890 LPOLESTR16 pid = HEAP_strdupWtoA(GetProcessHeap(),0,progid);
891 OLESTATUS ret = CLSIDFromProgID16(pid,riid);
893 HeapFree(GetProcessHeap(),0,pid);
894 return ret;
897 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
898 /***********************************************************************
899 * LookupETask (COMPOBJ.94)
901 OLESTATUS WINAPI LookupETask16(HTASK16 *hTask,LPVOID p) {
902 FIXME(ole,"(%p,%p),stub!\n",hTask,p);
903 if ((*hTask = GetCurrentTask()) == hETask) {
904 memcpy(p, Table_ETask, sizeof(Table_ETask));
906 return 0;
909 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
910 /***********************************************************************
911 * SetETask (COMPOBJ.95)
913 OLESTATUS WINAPI SetETask16(HTASK16 hTask, LPVOID p) {
914 FIXME(ole,"(%04x,%p),stub!\n",hTask,p);
915 hETask = hTask;
916 return 0;
919 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
920 /***********************************************************************
921 * CallObjectInWOW (COMPOBJ.201)
923 OLESTATUS WINAPI CallObjectInWOW(LPVOID p1,LPVOID p2) {
924 FIXME(ole,"(%p,%p),stub!\n",p1,p2);
925 return 0;
928 /******************************************************************************
929 * CoRegisterClassObject16 [COMPOBJ.5]
931 * Don't know where it registers it ...
933 HRESULT WINAPI CoRegisterClassObject16(
934 REFCLSID rclsid,
935 LPUNKNOWN pUnk,
936 DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
937 DWORD flags, /* [in] REGCLS flags indicating how connections are made */
938 LPDWORD lpdwRegister
940 char buf[80];
942 WINE_StringFromCLSID(rclsid,buf);
944 FIXME(ole,"(%s,%p,0x%08lx,0x%08lx,%p),stub\n",
945 buf,pUnk,dwClsContext,flags,lpdwRegister
947 return 0;
950 /***
951 * COM_GetRegisteredClassObject
953 * This internal method is used to scan the registered class list to
954 * find a class object.
956 * Params:
957 * rclsid Class ID of the class to find.
958 * dwClsContext Class context to match.
959 * ppv [out] returns a pointer to the class object. Complying
960 * to normal COM usage, this method will increase the
961 * reference count on this object.
963 static HRESULT COM_GetRegisteredClassObject(
964 REFCLSID rclsid,
965 DWORD dwClsContext,
966 LPUNKNOWN* ppUnk)
968 RegisteredClass* curClass;
971 * Sanity check
973 assert(ppUnk!=0);
976 * Iterate through the whole list and try to match the class ID.
978 curClass = firstRegisteredClass;
980 while (curClass != 0)
983 * Check if we have a match on the class ID.
985 if (IsEqualGUID32(&(curClass->classIdentifier), rclsid))
988 * Since we don't do out-of process or DCOM just right away, let's ignore the
989 * class context.
993 * We have a match, return the pointer to the class object.
995 *ppUnk = curClass->classObject;
997 IUnknown_AddRef(curClass->classObject);
999 return S_OK;
1003 * Step to the next class in the list.
1005 curClass = curClass->nextClass;
1009 * If we get to here, we haven't found our class.
1011 return S_FALSE;
1014 /******************************************************************************
1015 * CoRegisterClassObject32 [OLE32.36]
1017 * This method will register the class object for a given class ID.
1019 * See the Windows documentation for more details.
1021 HRESULT WINAPI CoRegisterClassObject(
1022 REFCLSID rclsid,
1023 LPUNKNOWN pUnk,
1024 DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
1025 DWORD flags, /* [in] REGCLS flags indicating how connections are made */
1026 LPDWORD lpdwRegister
1029 RegisteredClass* newClass;
1030 LPUNKNOWN foundObject;
1031 HRESULT hr;
1032 char buf[80];
1034 WINE_StringFromCLSID(rclsid,buf);
1036 TRACE(ole,"(%s,%p,0x%08lx,0x%08lx,%p)\n",
1037 buf,pUnk,dwClsContext,flags,lpdwRegister);
1040 * Perform a sanity check on the parameters
1042 if ( (lpdwRegister==0) || (pUnk==0) )
1044 return E_INVALIDARG;
1048 * Initialize the cookie (out parameter)
1050 *lpdwRegister = 0;
1053 * First, check if the class is already registered.
1054 * If it is, this should cause an error.
1056 hr = COM_GetRegisteredClassObject(rclsid, dwClsContext, &foundObject);
1058 if (hr == S_OK)
1061 * The COM_GetRegisteredClassObject increased the reference count on the
1062 * object so it has to be released.
1064 IUnknown_Release(foundObject);
1066 return CO_E_OBJISREG;
1070 * If it is not registered, we must create a new entry for this class and
1071 * append it to the registered class list.
1072 * We use the address of the chain node as the cookie since we are sure it's
1073 * unique.
1075 newClass = HeapAlloc(GetProcessHeap(), 0, sizeof(RegisteredClass));
1078 * Initialize the node.
1080 newClass->classIdentifier = *rclsid;
1081 newClass->runContext = dwClsContext;
1082 newClass->connectFlags = flags;
1083 newClass->dwCookie = (DWORD)newClass;
1084 newClass->nextClass = firstRegisteredClass;
1087 * Since we're making a copy of the object pointer, we have to increase it's
1088 * reference count.
1090 newClass->classObject = pUnk;
1091 IUnknown_AddRef(newClass->classObject);
1093 firstRegisteredClass = newClass;
1096 * We're successfyl Yippee!
1098 return S_OK;
1101 /***********************************************************************
1102 * CoRevokeClassObject32 [OLE32.40]
1104 * This method will remove a class object from the class registry
1106 * See the Windows documentation for more details.
1108 HRESULT WINAPI CoRevokeClassObject(
1109 DWORD dwRegister)
1111 RegisteredClass** prevClassLink;
1112 RegisteredClass* curClass;
1114 TRACE(ole,"(%08lx)\n",dwRegister);
1117 * Iterate through the whole list and try to match the cookie.
1119 curClass = firstRegisteredClass;
1120 prevClassLink = &firstRegisteredClass;
1122 while (curClass != 0)
1125 * Check if we have a match on the cookie.
1127 if (curClass->dwCookie == dwRegister)
1130 * Remove the class from the chain.
1132 *prevClassLink = curClass->nextClass;
1135 * Release the reference to the class object.
1137 IUnknown_Release(curClass->classObject);
1140 * Free the memory used by the chain node.
1142 HeapFree(GetProcessHeap(), 0, curClass);
1144 return S_OK;
1148 * Step to the next class in the list.
1150 prevClassLink = &(curClass->nextClass);
1151 curClass = curClass->nextClass;
1155 * If we get to here, we haven't found our class.
1157 return E_INVALIDARG;
1160 /***********************************************************************
1161 * CoGetClassObject [COMPOBJ.7]
1163 HRESULT WINAPI CoGetClassObject(REFCLSID rclsid, DWORD dwClsContext,
1164 LPVOID pvReserved, REFIID iid, LPVOID *ppv)
1166 LPUNKNOWN regClassObject;
1167 char xclsid[50],xiid[50];
1168 HRESULT hres = E_UNEXPECTED;
1170 char dllName[MAX_PATH+1];
1171 DWORD dllNameLen = sizeof(dllName);
1172 HINSTANCE hLibrary;
1173 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid,
1174 REFIID iid, LPVOID *ppv);
1175 DllGetClassObjectFunc DllGetClassObject;
1177 WINE_StringFromCLSID((LPCLSID)rclsid,xclsid);
1178 WINE_StringFromCLSID((LPCLSID)iid,xiid);
1179 TRACE(ole,"\n\tCLSID:\t%s,\n\tIID:\t%s\n",xclsid,xiid);
1182 * First, try and see if we can't match the class ID with one of the
1183 * registered classes.
1185 if (S_OK == COM_GetRegisteredClassObject(rclsid, dwClsContext, &regClassObject))
1188 * Get the required interface from the retrieved pointer.
1190 hres = IUnknown_QueryInterface(regClassObject, iid, ppv);
1193 * Since QI got another reference on the pointer, we want to release the
1194 * one we already have. If QI was unsuccessful, this will release the object. This
1195 * is good since we are not returning it in the "out" parameter.
1197 IUnknown_Release(regClassObject);
1199 return hres;
1202 /* out of process and remote servers not supported yet */
1203 if ((CLSCTX_LOCAL_SERVER|CLSCTX_REMOTE_SERVER) & dwClsContext) {
1204 FIXME(ole, "CLSCTX_LOCAL_SERVER and CLSCTX_REMOTE_SERVER not supported!\n");
1205 return E_ACCESSDENIED;
1208 if ((CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER) & dwClsContext) {
1209 HKEY CLSIDkey,key;
1211 /* lookup CLSID in registry key HKCR/CLSID */
1212 hres = RegOpenKeyExA(HKEY_CLASSES_ROOT, "CLSID", 0,
1213 KEY_READ, &CLSIDkey);
1215 if (hres != ERROR_SUCCESS)
1216 return REGDB_E_READREGDB;
1217 hres = RegOpenKeyExA(CLSIDkey,xclsid,0,KEY_QUERY_VALUE,&key);
1218 if (hres != ERROR_SUCCESS) {
1219 RegCloseKey(CLSIDkey);
1220 return REGDB_E_CLASSNOTREG;
1222 hres = RegQueryValueA(key, "InprocServer32", dllName, &dllNameLen);
1223 RegCloseKey(key);
1224 RegCloseKey(CLSIDkey);
1225 if (hres != ERROR_SUCCESS)
1226 return REGDB_E_READREGDB;
1227 TRACE(ole,"found InprocServer32 dll %s\n", dllName);
1229 /* open dll, call DllGetClassFactory */
1230 hLibrary = CoLoadLibrary(dllName, TRUE);
1231 if (hLibrary == 0) {
1232 TRACE(ole,"couldn't load InprocServer32 dll %s\n", dllName);
1233 return E_ACCESSDENIED; /* or should this be CO_E_DLLNOTFOUND? */
1235 DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress(hLibrary, "DllGetClassObject");
1236 if (!DllGetClassObject) {
1237 /* not sure if this should be called here CoFreeLibrary(hLibrary);*/
1238 TRACE(ole,"couldn't find function DllGetClassObject in %s\n", dllName);
1239 return E_ACCESSDENIED;
1243 * Ask the DLL for it's class object. (there was a note here about class
1244 * factories but this is good.
1246 return DllGetClassObject(rclsid, iid, ppv);
1248 return hres;
1251 /******************************************************************************
1252 * CoRegisterMessageFilter16 [COMPOBJ.27]
1254 HRESULT WINAPI CoRegisterMessageFilter16(
1255 LPMESSAGEFILTER lpMessageFilter,
1256 LPMESSAGEFILTER *lplpMessageFilter
1258 FIXME(ole,"(%p,%p),stub!\n",lpMessageFilter,lplpMessageFilter);
1259 return 0;
1262 /***********************************************************************
1263 * CoCreateInstance [COMPOBJ.13, OLE32.7]
1265 HRESULT WINAPI CoCreateInstance(
1266 REFCLSID rclsid,
1267 LPUNKNOWN pUnkOuter,
1268 DWORD dwClsContext,
1269 REFIID iid,
1270 LPVOID *ppv)
1272 HRESULT hres;
1273 LPCLASSFACTORY lpclf = 0;
1276 * Sanity check
1278 if (ppv==0)
1279 return E_POINTER;
1282 * Initialize the "out" parameter
1284 *ppv = 0;
1287 * Get a class factory to construct the object we want.
1289 hres = CoGetClassObject(rclsid,
1290 dwClsContext,
1291 NULL,
1292 &IID_IClassFactory,
1293 (LPVOID)&lpclf);
1295 if (FAILED(hres))
1296 return hres;
1299 * Create the object and don't forget to release the factory
1301 hres = IClassFactory_CreateInstance(lpclf, pUnkOuter, iid, ppv);
1302 IClassFactory_Release(lpclf);
1304 return hres;
1309 /***********************************************************************
1310 * CoFreeLibrary [COMPOBJ.13]
1312 void WINAPI CoFreeLibrary(HINSTANCE hLibrary)
1314 OpenDll *ptr, *prev;
1315 OpenDll *tmp;
1317 /* lookup library in linked list */
1318 prev = NULL;
1319 for (ptr = openDllList; ptr != NULL; ptr=ptr->next) {
1320 if (ptr->hLibrary == hLibrary) {
1321 break;
1323 prev = ptr;
1326 if (ptr == NULL) {
1327 /* shouldn't happen if user passed in a valid hLibrary */
1328 return;
1330 /* assert: ptr points to the library entry to free */
1332 /* free library and remove node from list */
1333 FreeLibrary(hLibrary);
1334 if (ptr == openDllList) {
1335 tmp = openDllList->next;
1336 HeapFree(GetProcessHeap(), 0, openDllList->DllName);
1337 HeapFree(GetProcessHeap(), 0, openDllList);
1338 openDllList = tmp;
1339 } else {
1340 tmp = ptr->next;
1341 HeapFree(GetProcessHeap(), 0, ptr->DllName);
1342 HeapFree(GetProcessHeap(), 0, ptr);
1343 prev->next = tmp;
1349 /***********************************************************************
1350 * CoFreeAllLibraries [COMPOBJ.12]
1352 void WINAPI CoFreeAllLibraries(void)
1354 OpenDll *ptr, *tmp;
1356 for (ptr = openDllList; ptr != NULL; ) {
1357 tmp=ptr->next;
1358 CoFreeLibrary(ptr->hLibrary);
1359 ptr = tmp;
1365 /***********************************************************************
1366 * CoFreeUnusedLibraries [COMPOBJ.17]
1368 void WINAPI CoFreeUnusedLibraries(void)
1370 OpenDll *ptr, *tmp;
1371 typedef HRESULT(*DllCanUnloadNowFunc)(void);
1372 DllCanUnloadNowFunc DllCanUnloadNow;
1374 for (ptr = openDllList; ptr != NULL; ) {
1375 DllCanUnloadNow = (DllCanUnloadNowFunc)
1376 GetProcAddress(ptr->hLibrary, "DllCanUnloadNow");
1378 if (DllCanUnloadNow() == S_OK) {
1379 tmp=ptr->next;
1380 CoFreeLibrary(ptr->hLibrary);
1381 ptr = tmp;
1382 } else {
1383 ptr=ptr->next;
1388 /***********************************************************************
1389 * CoFileTimeNow [COMPOBJ.82, OLE32.10]
1390 * RETURNS
1391 * the current system time in lpFileTime
1393 HRESULT WINAPI CoFileTimeNow(
1394 FILETIME *lpFileTime /* [out] the current time */
1396 DOSFS_UnixTimeToFileTime(time(NULL), lpFileTime, 0);
1397 return S_OK;
1400 /***********************************************************************
1401 * CoTaskMemAlloc (OLE32.43)
1402 * RETURNS
1403 * pointer to newly allocated block
1405 LPVOID WINAPI CoTaskMemAlloc(
1406 ULONG size /* [in] size of memoryblock to be allocated */
1408 LPMALLOC lpmalloc;
1409 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1411 if (FAILED(ret))
1412 return NULL;
1414 return IMalloc_Alloc(lpmalloc,size);
1417 /***********************************************************************
1418 * CoTaskMemFree (OLE32.44)
1420 VOID WINAPI CoTaskMemFree(
1421 LPVOID ptr /* [in] pointer to be freed */
1423 LPMALLOC lpmalloc;
1424 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1426 if (FAILED(ret))
1427 return;
1429 IMalloc_Free(lpmalloc, ptr);
1432 /***********************************************************************
1433 * CoTaskMemRealloc (OLE32.45)
1434 * RETURNS
1435 * pointer to newly allocated block
1437 LPVOID WINAPI CoTaskMemRealloc(
1438 LPVOID pvOld,
1439 ULONG size) /* [in] size of memoryblock to be allocated */
1441 LPMALLOC lpmalloc;
1442 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1444 if (FAILED(ret))
1445 return NULL;
1447 return IMalloc_Realloc(lpmalloc, pvOld, size);
1450 /***********************************************************************
1451 * CoLoadLibrary (OLE32.30)
1453 HINSTANCE WINAPI CoLoadLibrary(LPOLESTR16 lpszLibName, BOOL bAutoFree)
1455 HINSTANCE hLibrary;
1456 OpenDll *ptr;
1457 OpenDll *tmp;
1459 TRACE(ole,"CoLoadLibrary(%p, %d\n", lpszLibName, bAutoFree);
1461 hLibrary = LoadLibraryA(lpszLibName);
1463 if (!bAutoFree)
1464 return hLibrary;
1466 if (openDllList == NULL) {
1467 /* empty list -- add first node */
1468 openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
1469 openDllList->DllName = HEAP_strdupA(GetProcessHeap(), 0, lpszLibName);
1470 openDllList->hLibrary = hLibrary;
1471 openDllList->next = NULL;
1472 } else {
1473 /* search for this dll */
1474 int found = FALSE;
1475 for (ptr = openDllList; ptr->next != NULL; ptr=ptr->next) {
1476 if (ptr->hLibrary == hLibrary) {
1477 found = TRUE;
1478 break;
1481 if (!found) {
1482 /* dll not found, add it */
1483 tmp = openDllList;
1484 openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
1485 openDllList->DllName = HEAP_strdupA(GetProcessHeap(), 0, lpszLibName);
1486 openDllList->hLibrary = hLibrary;
1487 openDllList->next = tmp;
1491 return hLibrary;
1494 /***********************************************************************
1495 * CoInitializeWOW (OLE32.27)
1497 HRESULT WINAPI CoInitializeWOW(DWORD x,DWORD y) {
1498 FIXME(ole,"(0x%08lx,0x%08lx),stub!\n",x,y);
1499 return 0;
1502 /******************************************************************************
1503 * CoLockObjectExternal16 [COMPOBJ.63]
1505 HRESULT WINAPI CoLockObjectExternal16(
1506 LPUNKNOWN pUnk, /* [in] object to be locked */
1507 BOOL16 fLock, /* [in] do lock */
1508 BOOL16 fLastUnlockReleases /* [in] ? */
1510 FIXME(ole,"(%p,%d,%d),stub!\n",pUnk,fLock,fLastUnlockReleases);
1511 return S_OK;
1514 /******************************************************************************
1515 * CoLockObjectExternal32 [OLE32.31]
1517 HRESULT WINAPI CoLockObjectExternal(
1518 LPUNKNOWN pUnk, /* [in] object to be locked */
1519 BOOL fLock, /* [in] do lock */
1520 BOOL fLastUnlockReleases) /* [in] unlock all */
1523 if (fLock)
1526 * Increment the external lock coutner, COM_ExternalLockAddRef also
1527 * increment the object's internal lock counter.
1529 COM_ExternalLockAddRef( pUnk);
1531 else
1534 * Decrement the external lock coutner, COM_ExternalLockRelease also
1535 * decrement the object's internal lock counter.
1537 COM_ExternalLockRelease( pUnk, fLastUnlockReleases);
1540 return S_OK;
1543 /***********************************************************************
1544 * CoGetState16 [COMPOBJ.115]
1546 HRESULT WINAPI CoGetState16(LPDWORD state)
1548 FIXME(ole, "(%p),stub!\n", state);
1549 *state = 0;
1550 return S_OK;
1552 /***********************************************************************
1553 * CoSetState32 [COM32.42]
1555 HRESULT WINAPI CoSetState(LPDWORD state)
1557 FIXME(ole, "(%p),stub!\n", state);
1558 *state = 0;
1559 return S_OK;
1562 /***
1563 * COM_RevokeAllClasses
1565 * This method is called when the COM libraries are uninitialized to
1566 * release all the references to the class objects registered with
1567 * the library
1569 static void COM_RevokeAllClasses()
1571 while (firstRegisteredClass!=0)
1573 CoRevokeClassObject(firstRegisteredClass->dwCookie);
1577 /****************************************************************************
1578 * COM External Lock methods implementation
1581 /****************************************************************************
1582 * Public - Method that increments the count for a IUnknown* in the linked
1583 * list. The item is inserted if not already in the list.
1585 static void COM_ExternalLockAddRef(
1586 IUnknown *pUnk)
1588 COM_ExternalLock *externalLock = COM_ExternalLockFind(pUnk);
1591 * Add an external lock to the object. If it was already externally
1592 * locked, just increase the reference count. If it was not.
1593 * add the item to the list.
1595 if ( externalLock == EL_NOT_FOUND )
1596 COM_ExternalLockInsert(pUnk);
1597 else
1598 externalLock->uRefCount++;
1601 * Add an internal lock to the object
1603 IUnknown_AddRef(pUnk);
1606 /****************************************************************************
1607 * Public - Method that decrements the count for a IUnknown* in the linked
1608 * list. The item is removed from the list if its count end up at zero or if
1609 * bRelAll is TRUE.
1611 static void COM_ExternalLockRelease(
1612 IUnknown *pUnk,
1613 BOOL bRelAll)
1615 COM_ExternalLock *externalLock = COM_ExternalLockFind(pUnk);
1617 if ( externalLock != EL_NOT_FOUND )
1621 externalLock->uRefCount--; /* release external locks */
1622 IUnknown_Release(pUnk); /* release local locks as well */
1624 if ( bRelAll == FALSE )
1625 break; /* perform single release */
1627 } while ( externalLock->uRefCount > 0 );
1629 if ( externalLock->uRefCount == 0 ) /* get rid of the list entry */
1630 COM_ExternalLockDelete(externalLock);
1633 /****************************************************************************
1634 * Public - Method that frees the content of the list.
1636 static void COM_ExternalLockFreeList()
1638 COM_ExternalLock *head;
1640 head = elList.head; /* grab it by the head */
1641 while ( head != EL_END_OF_LIST )
1643 COM_ExternalLockDelete(head); /* get rid of the head stuff */
1645 head = elList.head; /* get the new head... */
1649 /****************************************************************************
1650 * Public - Method that dump the content of the list.
1652 void COM_ExternalLockDump()
1654 COM_ExternalLock *current = elList.head;
1656 printf("\nExternal lock list contains:\n");
1658 while ( current != EL_END_OF_LIST )
1660 printf( "\t%p with %lu references count.\n",
1661 current->pUnk,
1662 current->uRefCount);
1664 /* Skip to the next item */
1665 current = current->next;
1670 /****************************************************************************
1671 * Internal - Find a IUnknown* in the linked list
1673 static COM_ExternalLock* COM_ExternalLockFind(
1674 IUnknown *pUnk)
1676 return COM_ExternalLockLocate(elList.head, pUnk);
1679 /****************************************************************************
1680 * Internal - Recursivity agent for IUnknownExternalLockList_Find
1682 static COM_ExternalLock* COM_ExternalLockLocate(
1683 COM_ExternalLock *element,
1684 IUnknown *pUnk)
1686 if ( element == EL_END_OF_LIST )
1687 return EL_NOT_FOUND;
1689 else if ( element->pUnk == pUnk ) /* We found it */
1690 return element;
1692 else /* Not the right guy, keep on looking */
1693 return COM_ExternalLockLocate( element->next, pUnk);
1696 /****************************************************************************
1697 * Internal - Insert a new IUnknown* to the linked list
1699 static BOOL COM_ExternalLockInsert(
1700 IUnknown *pUnk)
1702 COM_ExternalLock *newLock = NULL;
1703 COM_ExternalLock *previousHead = NULL;
1706 * Allocate space for the new storage object
1708 newLock = HeapAlloc(GetProcessHeap(), 0, sizeof(COM_ExternalLock));
1710 if (newLock!=NULL)
1712 if ( elList.head == EL_END_OF_LIST )
1714 elList.head = newLock; /* The list is empty */
1716 else
1719 * insert does it at the head
1721 previousHead = elList.head;
1722 elList.head = newLock;
1726 * Set new list item data member
1728 newLock->pUnk = pUnk;
1729 newLock->uRefCount = 1;
1730 newLock->next = previousHead;
1732 return TRUE;
1734 else
1735 return FALSE;
1738 /****************************************************************************
1739 * Internal - Method that removes an item from the linked list.
1741 static void COM_ExternalLockDelete(
1742 COM_ExternalLock *itemList)
1744 COM_ExternalLock *current = elList.head;
1746 if ( current == itemList )
1749 * this section handles the deletion of the first node
1751 elList.head = itemList->next;
1752 HeapFree( GetProcessHeap(), 0, itemList);
1754 else
1758 if ( current->next == itemList ) /* We found the item to free */
1760 current->next = itemList->next; /* readjust the list pointers */
1762 HeapFree( GetProcessHeap(), 0, itemList);
1763 break;
1766 /* Skip to the next item */
1767 current = current->next;
1769 } while ( current != EL_END_OF_LIST );