Implemented SHCreateShellFolderViewEx.
[wine.git] / ole / compobj.c
blobe51179fa42c15d22d8ab85080471b83ca59b24c1
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 "debugtools.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"
53 #include "compobj.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
60 * COM objects.
62 * The public interface consists of three calls:
63 * COM_ExternalLockAddRef
64 * COM_ExternalLockRelease
65 * COM_ExternalLockFreeList
68 #define EL_END_OF_LIST 0
69 #define EL_NOT_FOUND 0
72 * Declaration of the static structure that manage the
73 * external lock to COM objects.
75 typedef struct COM_ExternalLock COM_ExternalLock;
76 typedef struct COM_ExternalLockList COM_ExternalLockList;
78 struct COM_ExternalLock
80 IUnknown *pUnk; /* IUnknown referenced */
81 ULONG uRefCount; /* external lock counter to IUnknown object*/
82 COM_ExternalLock *next; /* Pointer to next element in list */
85 struct COM_ExternalLockList
87 COM_ExternalLock *head; /* head of list */
91 * Declaration and initialization of the static structure that manages
92 * the external lock to COM objects.
94 static COM_ExternalLockList elList = { EL_END_OF_LIST };
97 * Public Interface to the external lock list
99 static void COM_ExternalLockFreeList();
100 static void COM_ExternalLockAddRef(IUnknown *pUnk);
101 static void COM_ExternalLockRelease(IUnknown *pUnk, BOOL bRelAll);
102 void COM_ExternalLockDump(); /* testing purposes, not static to avoid warning */
105 * Private methods used to managed the linked list
107 static BOOL COM_ExternalLockInsert(
108 IUnknown *pUnk);
110 static void COM_ExternalLockDelete(
111 COM_ExternalLock *element);
113 static COM_ExternalLock* COM_ExternalLockFind(
114 IUnknown *pUnk);
116 static COM_ExternalLock* COM_ExternalLockLocate(
117 COM_ExternalLock *element,
118 IUnknown *pUnk);
120 /****************************************************************************
121 * This section defines variables internal to the COM module.
123 * TODO: Most of these things will have to be made thread-safe.
125 HINSTANCE16 COMPOBJ_hInstance = 0;
126 HINSTANCE COMPOBJ_hInstance32 = 0;
127 static int COMPOBJ_Attach = 0;
129 LPMALLOC16 currentMalloc16=NULL;
130 LPMALLOC currentMalloc32=NULL;
132 HTASK16 hETask = 0;
133 WORD Table_ETask[62];
136 * This lock count counts the number of times CoInitialize is called. It is
137 * decreased every time CoUninitialize is called. When it hits 0, the COM
138 * libraries are freed
140 static ULONG s_COMLockCount = 0;
143 * This linked list contains the list of registered class objects. These
144 * are mostly used to register the factories for out-of-proc servers of OLE
145 * objects.
147 * TODO: Make this data structure aware of inter-process communication. This
148 * means that parts of this will be exported to the Wine Server.
150 typedef struct tagRegisteredClass
152 CLSID classIdentifier;
153 LPUNKNOWN classObject;
154 DWORD runContext;
155 DWORD connectFlags;
156 DWORD dwCookie;
157 struct tagRegisteredClass* nextClass;
158 } RegisteredClass;
160 static RegisteredClass* firstRegisteredClass = NULL;
162 /* this open DLL table belongs in a per process table, but my guess is that
163 * it shouldn't live in the kernel, so I'll put them out here in DLL
164 * space assuming that there is one OLE32 per process.
166 typedef struct tagOpenDll {
167 char *DllName; /* really only needed for debugging */
168 HINSTANCE hLibrary;
169 struct tagOpenDll *next;
170 } OpenDll;
172 static OpenDll *openDllList = NULL; /* linked list of open dlls */
174 /*****************************************************************************
175 * This section contains prototypes to internal methods for this
176 * module
178 static HRESULT COM_GetRegisteredClassObject(REFCLSID rclsid,
179 DWORD dwClsContext,
180 LPUNKNOWN* ppUnk);
182 static void COM_RevokeAllClasses();
185 /******************************************************************************
186 * CoBuildVersion [COMPOBJ.1]
188 * RETURNS
189 * Current built version, hiword is majornumber, loword is minornumber
191 DWORD WINAPI CoBuildVersion(void)
193 TRACE("(void)\n");
194 return (rmm<<16)+rup;
197 /******************************************************************************
198 * CoInitialize16 [COMPOBJ.2]
199 * Set the win16 IMalloc used for memory management
201 HRESULT WINAPI CoInitialize16(
202 LPVOID lpReserved /* [in] pointer to win16 malloc interface */
204 currentMalloc16 = (LPMALLOC16)lpReserved;
205 return S_OK;
208 /******************************************************************************
209 * CoInitialize32 [OLE32.26]
211 * Initializes the COM libraries.
213 * See CoInitializeEx32
215 HRESULT WINAPI CoInitialize(
216 LPVOID lpReserved /* [in] pointer to win32 malloc interface
217 (obsolete, should be NULL) */
221 * Just delegate to the newer method.
223 return CoInitializeEx(lpReserved, COINIT_APARTMENTTHREADED);
226 /******************************************************************************
227 * CoInitializeEx32 [OLE32.163]
229 * Initializes the COM libraries. The behavior used to set the win32 IMalloc
230 * used for memory management is obsolete.
232 * RETURNS
233 * S_OK if successful,
234 * S_FALSE if this function was called already.
235 * RPC_E_CHANGED_MODE if a previous call to CoInitialize specified another
236 * threading model.
238 * BUGS
239 * Only the single threaded model is supported. As a result RPC_E_CHANGED_MODE
240 * is never returned.
242 * See the windows documentation for more details.
244 HRESULT WINAPI CoInitializeEx(
245 LPVOID lpReserved, /* [in] pointer to win32 malloc interface
246 (obsolete, should be NULL) */
247 DWORD dwCoInit /* [in] A value from COINIT specifies the threading model */
250 HRESULT hr;
252 TRACE("(%p, %x)\n", lpReserved, (int)dwCoInit);
254 if (lpReserved!=NULL)
256 ERR("(%p, %x) - Bad parameter passed-in %p, must be an old Windows Application\n", lpReserved, (int)dwCoInit, lpReserved);
260 * Check for unsupported features.
262 if (dwCoInit!=COINIT_APARTMENTTHREADED)
264 FIXME(":(%p,%x): unsupported flag %x\n", lpReserved, (int)dwCoInit, (int)dwCoInit);
265 /* Hope for the best and continue anyway */
269 * Check the lock count. If this is the first time going through the initialize
270 * process, we have to initialize the libraries.
272 if (s_COMLockCount==0)
275 * Initialize the various COM libraries and data structures.
277 TRACE("() - Initializing the COM libraries\n");
279 RunningObjectTableImpl_Initialize();
281 hr = S_OK;
283 else
284 hr = S_FALSE;
287 * Crank-up that lock count.
289 s_COMLockCount++;
291 return hr;
294 /***********************************************************************
295 * CoUninitialize16 [COMPOBJ.3]
296 * Don't know what it does.
297 * 3-Nov-98 -- this was originally misspelled, I changed it to what I
298 * believe is the correct spelling
300 void WINAPI CoUninitialize16(void)
302 TRACE("()\n");
303 CoFreeAllLibraries();
306 /***********************************************************************
307 * CoUninitialize32 [OLE32.47]
309 * This method will release the COM libraries.
311 * See the windows documentation for more details.
313 void WINAPI CoUninitialize(void)
315 TRACE("()\n");
318 * Decrease the reference count.
320 s_COMLockCount--;
323 * If we are back to 0 locks on the COM library, make sure we free
324 * all the associated data structures.
326 if (s_COMLockCount==0)
329 * Release the various COM libraries and data structures.
331 TRACE("() - Releasing the COM libraries\n");
333 RunningObjectTableImpl_UnInitialize();
335 * Release the references to the registered class objects.
337 COM_RevokeAllClasses();
340 * This will free the loaded COM Dlls.
342 CoFreeAllLibraries();
345 * This will free list of external references to COM objects.
347 COM_ExternalLockFreeList();
351 /***********************************************************************
352 * CoGetMalloc16 [COMPOBJ.4]
353 * RETURNS
354 * The current win16 IMalloc
356 HRESULT WINAPI CoGetMalloc16(
357 DWORD dwMemContext, /* [in] unknown */
358 LPMALLOC16 * lpMalloc /* [out] current win16 malloc interface */
360 if(!currentMalloc16)
361 currentMalloc16 = IMalloc16_Constructor();
362 *lpMalloc = currentMalloc16;
363 return S_OK;
366 /******************************************************************************
367 * CoGetMalloc32 [OLE32.20]
369 * RETURNS
370 * The current win32 IMalloc
372 HRESULT WINAPI CoGetMalloc(
373 DWORD dwMemContext, /* [in] unknown */
374 LPMALLOC *lpMalloc /* [out] current win32 malloc interface */
376 if(!currentMalloc32)
377 currentMalloc32 = IMalloc_Constructor();
378 *lpMalloc = currentMalloc32;
379 return S_OK;
382 /***********************************************************************
383 * CoCreateStandardMalloc16 [COMPOBJ.71]
385 HRESULT WINAPI CoCreateStandardMalloc16(DWORD dwMemContext,
386 LPMALLOC16 *lpMalloc)
388 /* FIXME: docu says we shouldn't return the same allocator as in
389 * CoGetMalloc16 */
390 *lpMalloc = IMalloc16_Constructor();
391 return S_OK;
394 /******************************************************************************
395 * CoDisconnectObject [COMPOBJ.15]
397 HRESULT WINAPI CoDisconnectObject( LPUNKNOWN lpUnk, DWORD reserved )
399 TRACE("%p %lx\n",lpUnk,reserved);
400 return S_OK;
403 /***********************************************************************
404 * IsEqualGUID16 [COMPOBJ.18]
406 * Compares two Unique Identifiers.
408 * RETURNS
409 * TRUE if equal
411 BOOL16 WINAPI IsEqualGUID16(
412 GUID* g1, /* [in] unique id 1 */
413 GUID* g2 /**/
415 return !memcmp( g1, g2, sizeof(GUID) );
418 /***********************************************************************
419 * IsEqualGUID32 [OLE32.76]
421 * Compares two Unique Identifiers.
423 * RETURNS
424 * TRUE if equal
426 BOOL WINAPI IsEqualGUID32(
427 REFGUID rguid1, /* [in] unique id 1 */
428 REFGUID rguid2 /* [in] unique id 2 */
431 return !memcmp(rguid1,rguid2,sizeof(GUID));
434 /******************************************************************************
435 * CLSIDFromString16 [COMPOBJ.20]
436 * Converts a unique identifier from it's string representation into
437 * the GUID struct.
439 * Class id: DWORD-WORD-WORD-BYTES[2]-BYTES[6]
441 * RETURNS
442 * the converted GUID
444 HRESULT WINAPI CLSIDFromString16(
445 LPCOLESTR16 idstr, /* [in] string representation of guid */
446 CLSID *id /* [out] GUID converted from string */
448 BYTE *s = (BYTE *) idstr;
449 BYTE *p;
450 int i;
451 BYTE table[256];
453 TRACE("%s -> %p\n", idstr, id);
455 /* quick lookup table */
456 memset(table, 0, 256);
458 for (i = 0; i < 10; i++) {
459 table['0' + i] = i;
461 for (i = 0; i < 6; i++) {
462 table['A' + i] = i+10;
463 table['a' + i] = i+10;
466 /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
468 if (strlen(idstr) != 38)
469 return OLE_ERROR_OBJECT;
471 p = (BYTE *) id;
473 s++; /* skip leading brace */
474 for (i = 0; i < 4; i++) {
475 p[3 - i] = table[*s]<<4 | table[*(s+1)];
476 s += 2;
478 p += 4;
479 s++; /* skip - */
481 for (i = 0; i < 2; i++) {
482 p[1-i] = table[*s]<<4 | table[*(s+1)];
483 s += 2;
485 p += 2;
486 s++; /* skip - */
488 for (i = 0; i < 2; i++) {
489 p[1-i] = table[*s]<<4 | table[*(s+1)];
490 s += 2;
492 p += 2;
493 s++; /* skip - */
495 /* these are just sequential bytes */
496 for (i = 0; i < 2; i++) {
497 *p++ = table[*s]<<4 | table[*(s+1)];
498 s += 2;
500 s++; /* skip - */
502 for (i = 0; i < 6; i++) {
503 *p++ = table[*s]<<4 | table[*(s+1)];
504 s += 2;
507 return S_OK;
510 /******************************************************************************
511 * CoCreateGuid[OLE32.6]
513 * Creates a 128bit GUID.
514 * Implemented according the DCE specification for UUID generation.
515 * Code is based upon uuid library in e2fsprogs by Theodore Ts'o.
516 * Copyright (C) 1996, 1997 Theodore Ts'o.
518 * RETURNS
520 * S_OK if successful.
522 HRESULT WINAPI CoCreateGuid(
523 GUID *pguid /* [out] points to the GUID to initialize */
525 static char has_init = 0;
526 unsigned char a[6];
527 static int adjustment = 0;
528 static struct timeval last = {0, 0};
529 static UINT16 clock_seq;
530 struct timeval tv;
531 unsigned long long clock_reg;
532 UINT clock_high, clock_low;
533 UINT16 temp_clock_seq, temp_clock_mid, temp_clock_hi_and_version;
534 #ifdef HAVE_NET_IF_H
535 int sd;
536 struct ifreq ifr, *ifrp;
537 struct ifconf ifc;
538 char buf[1024];
539 int n, i;
540 #endif
542 /* Have we already tried to get the MAC address? */
543 if (!has_init) {
544 #ifdef HAVE_NET_IF_H
545 /* BSD 4.4 defines the size of an ifreq to be
546 * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
547 * However, under earlier systems, sa_len isn't present, so
548 * the size is just sizeof(struct ifreq)
550 # ifdef HAVE_SA_LEN
551 # ifndef max
552 # define max(a,b) ((a) > (b) ? (a) : (b))
553 # endif
554 # define ifreq_size(i) max(sizeof(struct ifreq),\
555 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
556 # else
557 # define ifreq_size(i) sizeof(struct ifreq)
558 # endif /* HAVE_SA_LEN */
560 sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
561 if (sd < 0) {
562 /* if we can't open a socket, just use random numbers */
563 /* set the multicast bit to prevent conflicts with real cards */
564 a[0] = (rand() & 0xff) | 0x80;
565 a[1] = rand() & 0xff;
566 a[2] = rand() & 0xff;
567 a[3] = rand() & 0xff;
568 a[4] = rand() & 0xff;
569 a[5] = rand() & 0xff;
570 } else {
571 memset(buf, 0, sizeof(buf));
572 ifc.ifc_len = sizeof(buf);
573 ifc.ifc_buf = buf;
574 /* get the ifconf interface */
575 if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) {
576 close(sd);
577 /* no ifconf, so just use random numbers */
578 /* set the multicast bit to prevent conflicts with real cards */
579 a[0] = (rand() & 0xff) | 0x80;
580 a[1] = rand() & 0xff;
581 a[2] = rand() & 0xff;
582 a[3] = rand() & 0xff;
583 a[4] = rand() & 0xff;
584 a[5] = rand() & 0xff;
585 } else {
586 /* loop through the interfaces, looking for a valid one */
587 n = ifc.ifc_len;
588 for (i = 0; i < n; i+= ifreq_size(*ifr) ) {
589 ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i);
590 strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
591 /* try to get the address for this interface */
592 # ifdef SIOCGIFHWADDR
593 if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
594 continue;
595 memcpy(a, (unsigned char *)&ifr.ifr_hwaddr.sa_data, 6);
596 # else
597 # ifdef SIOCGENADDR
598 if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
599 continue;
600 memcpy(a, (unsigned char *) ifr.ifr_enaddr, 6);
601 # else
602 /* XXX we don't have a way of getting the hardware address */
603 close(sd);
604 a[0] = 0;
605 break;
606 # endif /* SIOCGENADDR */
607 # endif /* SIOCGIFHWADDR */
608 /* make sure it's not blank */
609 if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5])
610 continue;
612 goto valid_address;
614 /* if we didn't find a valid address, make a random one */
615 /* once again, set multicast bit to avoid conflicts */
616 a[0] = (rand() & 0xff) | 0x80;
617 a[1] = rand() & 0xff;
618 a[2] = rand() & 0xff;
619 a[3] = rand() & 0xff;
620 a[4] = rand() & 0xff;
621 a[5] = rand() & 0xff;
623 valid_address:
624 close(sd);
627 #else
628 /* no networking info, so generate a random address */
629 a[0] = (rand() & 0xff) | 0x80;
630 a[1] = rand() & 0xff;
631 a[2] = rand() & 0xff;
632 a[3] = rand() & 0xff;
633 a[4] = rand() & 0xff;
634 a[5] = rand() & 0xff;
635 #endif /* HAVE_NET_IF_H */
636 has_init = 1;
639 /* generate time element of GUID */
641 /* Assume that the gettimeofday() has microsecond granularity */
642 #define MAX_ADJUSTMENT 10
644 try_again:
645 gettimeofday(&tv, 0);
646 if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
647 clock_seq = ((rand() & 0xff) << 8) + (rand() & 0xff);
648 clock_seq &= 0x1FFF;
649 last = tv;
650 last.tv_sec--;
652 if ((tv.tv_sec < last.tv_sec) ||
653 ((tv.tv_sec == last.tv_sec) &&
654 (tv.tv_usec < last.tv_usec))) {
655 clock_seq = (clock_seq+1) & 0x1FFF;
656 adjustment = 0;
657 } else if ((tv.tv_sec == last.tv_sec) &&
658 (tv.tv_usec == last.tv_usec)) {
659 if (adjustment >= MAX_ADJUSTMENT)
660 goto try_again;
661 adjustment++;
662 } else
663 adjustment = 0;
665 clock_reg = tv.tv_usec*10 + adjustment;
666 clock_reg += ((unsigned long long) tv.tv_sec)*10000000;
667 clock_reg += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
669 clock_high = clock_reg >> 32;
670 clock_low = clock_reg;
671 temp_clock_seq = clock_seq | 0x8000;
672 temp_clock_mid = (UINT16)clock_high;
673 temp_clock_hi_and_version = (clock_high >> 16) | 0x1000;
675 /* pack the information into the GUID structure */
677 ((unsigned char*)&pguid->Data1)[3] = (unsigned char)clock_low;
678 clock_low >>= 8;
679 ((unsigned char*)&pguid->Data1)[2] = (unsigned char)clock_low;
680 clock_low >>= 8;
681 ((unsigned char*)&pguid->Data1)[1] = (unsigned char)clock_low;
682 clock_low >>= 8;
683 ((unsigned char*)&pguid->Data1)[0] = (unsigned char)clock_low;
685 ((unsigned char*)&pguid->Data2)[1] = (unsigned char)temp_clock_mid;
686 temp_clock_mid >>= 8;
687 ((unsigned char*)&pguid->Data2)[0] = (unsigned char)temp_clock_mid;
689 ((unsigned char*)&pguid->Data3)[1] = (unsigned char)temp_clock_hi_and_version;
690 temp_clock_hi_and_version >>= 8;
691 ((unsigned char*)&pguid->Data3)[0] = (unsigned char)temp_clock_hi_and_version;
693 ((unsigned char*)pguid->Data4)[1] = (unsigned char)temp_clock_seq;
694 temp_clock_seq >>= 8;
695 ((unsigned char*)pguid->Data4)[0] = (unsigned char)temp_clock_seq;
697 ((unsigned char*)pguid->Data4)[2] = a[0];
698 ((unsigned char*)pguid->Data4)[3] = a[1];
699 ((unsigned char*)pguid->Data4)[4] = a[2];
700 ((unsigned char*)pguid->Data4)[5] = a[3];
701 ((unsigned char*)pguid->Data4)[6] = a[4];
702 ((unsigned char*)pguid->Data4)[7] = a[5];
704 TRACE("%p", pguid);
706 return S_OK;
709 /******************************************************************************
710 * CLSIDFromString32 [OLE32.3]
711 * Converts a unique identifier from it's string representation into
712 * the GUID struct.
713 * RETURNS
714 * the converted GUID
716 HRESULT WINAPI CLSIDFromString(
717 LPCOLESTR idstr, /* [in] string representation of GUID */
718 CLSID *id /* [out] GUID represented by above string */
720 LPOLESTR16 xid = HEAP_strdupWtoA(GetProcessHeap(),0,idstr);
721 OLESTATUS ret = CLSIDFromString16(xid,id);
723 HeapFree(GetProcessHeap(),0,xid);
724 return ret;
727 /******************************************************************************
728 * WINE_StringFromCLSID [Internal]
729 * Converts a GUID into the respective string representation.
731 * NOTES
733 * RETURNS
734 * the string representation and OLESTATUS
736 HRESULT WINE_StringFromCLSID(
737 const CLSID *id, /* [in] GUID to be converted */
738 LPSTR idstr /* [out] pointer to buffer to contain converted guid */
740 static const char *hex = "0123456789ABCDEF";
741 char *s;
742 int i;
744 if (!id)
745 { ERR("called with id=Null\n");
746 *idstr = 0x00;
747 return E_FAIL;
750 sprintf(idstr, "{%08lX-%04X-%04X-%02X%02X-",
751 id->Data1, id->Data2, id->Data3,
752 id->Data4[0], id->Data4[1]);
753 s = &idstr[25];
755 /* 6 hex bytes */
756 for (i = 2; i < 8; i++) {
757 *s++ = hex[id->Data4[i]>>4];
758 *s++ = hex[id->Data4[i] & 0xf];
761 *s++ = '}';
762 *s++ = '\0';
764 TRACE("%p->%s\n", id, idstr);
766 return OLE_OK;
769 /******************************************************************************
770 * StringFromCLSID16 [COMPOBJ.19]
771 * Converts a GUID into the respective string representation.
772 * The target string is allocated using the OLE IMalloc.
773 * RETURNS
774 * the string representation and OLESTATUS
776 HRESULT WINAPI StringFromCLSID16(
777 REFCLSID id, /* [in] the GUID to be converted */
778 LPOLESTR16 *idstr /* [out] a pointer to a to-be-allocated segmented pointer pointing to the resulting string */
781 LPMALLOC16 mllc;
782 OLESTATUS ret;
783 DWORD args[2];
785 ret = CoGetMalloc16(0,&mllc);
786 if (ret) return ret;
788 args[0] = (DWORD)mllc;
789 args[1] = 40;
791 /* No need for a Callback entry, we have WOWCallback16Ex which does
792 * everything we need.
794 if (!WOWCallback16Ex(
795 (FARPROC16)((ICOM_VTABLE(IMalloc16)*)PTR_SEG_TO_LIN(
796 ((LPMALLOC16)PTR_SEG_TO_LIN(mllc))->lpvtbl)
797 )->fnAlloc,
798 WCB16_CDECL,
800 (LPVOID)args,
801 (LPDWORD)idstr
802 )) {
803 WARN("CallTo16 IMalloc16 failed\n");
804 return E_FAIL;
806 return WINE_StringFromCLSID(id,PTR_SEG_TO_LIN(*idstr));
809 /******************************************************************************
810 * StringFromCLSID32 [OLE32.151]
811 * Converts a GUID into the respective string representation.
812 * The target string is allocated using the OLE IMalloc.
813 * RETURNS
814 * the string representation and OLESTATUS
816 HRESULT WINAPI StringFromCLSID(
817 REFCLSID id, /* [in] the GUID to be converted */
818 LPOLESTR *idstr /* [out] a pointer to a to-be-allocated pointer pointing to the resulting string */
820 char buf[80];
821 OLESTATUS ret;
822 LPMALLOC mllc;
824 if ((ret=CoGetMalloc(0,&mllc)))
825 return ret;
827 ret=WINE_StringFromCLSID(id,buf);
828 if (!ret) {
829 *idstr = mllc->lpvtbl->fnAlloc(mllc,strlen(buf)*2+2);
830 lstrcpyAtoW(*idstr,buf);
832 return ret;
835 /******************************************************************************
836 * StringFromGUID2 [COMPOBJ.76] [OLE32.152]
838 * Converts a global unique identifier into a string of an API-
839 * specified fixed format. (The usual {.....} stuff.)
841 * RETURNS
842 * The (UNICODE) string representation of the GUID in 'str'
843 * The length of the resulting string, 0 if there was any problem.
845 INT WINAPI
846 StringFromGUID2(REFGUID id, LPOLESTR str, INT cmax)
848 char xguid[80];
850 if (WINE_StringFromCLSID(id,xguid))
851 return 0;
852 if (strlen(xguid)>=cmax)
853 return 0;
854 lstrcpyAtoW(str,xguid);
855 return strlen(xguid);
858 /******************************************************************************
859 * CLSIDFromProgID16 [COMPOBJ.61]
860 * Converts a program id into the respective GUID. (By using a registry lookup)
861 * RETURNS
862 * riid associated with the progid
864 HRESULT WINAPI CLSIDFromProgID16(
865 LPCOLESTR16 progid, /* [in] program id as found in registry */
866 LPCLSID riid /* [out] associated CLSID */
868 char *buf,buf2[80];
869 DWORD buf2len;
870 HRESULT err;
871 HKEY xhkey;
873 buf = HeapAlloc(GetProcessHeap(),0,strlen(progid)+8);
874 sprintf(buf,"%s\\CLSID",progid);
875 if ((err=RegOpenKeyA(HKEY_CLASSES_ROOT,buf,&xhkey))) {
876 HeapFree(GetProcessHeap(),0,buf);
877 return OLE_ERROR_GENERIC;
879 HeapFree(GetProcessHeap(),0,buf);
880 buf2len = sizeof(buf2);
881 if ((err=RegQueryValueA(xhkey,NULL,buf2,&buf2len))) {
882 RegCloseKey(xhkey);
883 return OLE_ERROR_GENERIC;
885 RegCloseKey(xhkey);
886 return CLSIDFromString16(buf2,riid);
889 /******************************************************************************
890 * CLSIDFromProgID32 [OLE32.2]
891 * Converts a program id into the respective GUID. (By using a registry lookup)
892 * RETURNS
893 * riid associated with the progid
895 HRESULT WINAPI CLSIDFromProgID(
896 LPCOLESTR progid, /* [in] program id as found in registry */
897 LPCLSID riid /* [out] associated CLSID */
899 LPOLESTR16 pid = HEAP_strdupWtoA(GetProcessHeap(),0,progid);
900 OLESTATUS ret = CLSIDFromProgID16(pid,riid);
902 HeapFree(GetProcessHeap(),0,pid);
903 return ret;
906 /************************************************************************************************
907 * OleSaveToStream
909 * This function write a CLSID on stream
911 HRESULT WINAPI WriteClassStm(IStream *pStm,REFCLSID rclsid)
913 TRACE("(%p,%p)\n",pStm,rclsid);
915 if (rclsid==NULL)
916 return E_INVALIDARG;
918 return IStream_Write(pStm,rclsid,sizeof(CLSID),NULL);
921 /************************************************************************************************
922 * OleSaveToStream
924 * This function read a CLSID from a stream
926 HRESULT WINAPI ReadClassStm(IStream *pStm,REFCLSID rclsid)
928 ULONG nbByte;
929 HRESULT res;
931 TRACE("(%p,%p)\n",pStm,rclsid);
933 if (rclsid==NULL)
934 return E_INVALIDARG;
936 res = IStream_Read(pStm,(void*)rclsid,sizeof(CLSID),&nbByte);
938 if (FAILED(res))
939 return res;
941 if (nbByte != sizeof(CLSID))
942 return S_FALSE;
943 else
944 return S_OK;
947 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
948 /***********************************************************************
949 * LookupETask (COMPOBJ.94)
951 OLESTATUS WINAPI LookupETask16(HTASK16 *hTask,LPVOID p) {
952 FIXME("(%p,%p),stub!\n",hTask,p);
953 if ((*hTask = GetCurrentTask()) == hETask) {
954 memcpy(p, Table_ETask, sizeof(Table_ETask));
956 return 0;
959 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
960 /***********************************************************************
961 * SetETask (COMPOBJ.95)
963 OLESTATUS WINAPI SetETask16(HTASK16 hTask, LPVOID p) {
964 FIXME("(%04x,%p),stub!\n",hTask,p);
965 hETask = hTask;
966 return 0;
969 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
970 /***********************************************************************
971 * CallObjectInWOW (COMPOBJ.201)
973 OLESTATUS WINAPI CallObjectInWOW(LPVOID p1,LPVOID p2) {
974 FIXME("(%p,%p),stub!\n",p1,p2);
975 return 0;
978 /******************************************************************************
979 * CoRegisterClassObject16 [COMPOBJ.5]
981 * Don't know where it registers it ...
983 HRESULT WINAPI CoRegisterClassObject16(
984 REFCLSID rclsid,
985 LPUNKNOWN pUnk,
986 DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
987 DWORD flags, /* [in] REGCLS flags indicating how connections are made */
988 LPDWORD lpdwRegister
990 char buf[80];
992 WINE_StringFromCLSID(rclsid,buf);
994 FIXME("(%s,%p,0x%08lx,0x%08lx,%p),stub\n",
995 buf,pUnk,dwClsContext,flags,lpdwRegister
997 return 0;
1000 /***
1001 * COM_GetRegisteredClassObject
1003 * This internal method is used to scan the registered class list to
1004 * find a class object.
1006 * Params:
1007 * rclsid Class ID of the class to find.
1008 * dwClsContext Class context to match.
1009 * ppv [out] returns a pointer to the class object. Complying
1010 * to normal COM usage, this method will increase the
1011 * reference count on this object.
1013 static HRESULT COM_GetRegisteredClassObject(
1014 REFCLSID rclsid,
1015 DWORD dwClsContext,
1016 LPUNKNOWN* ppUnk)
1018 RegisteredClass* curClass;
1021 * Sanity check
1023 assert(ppUnk!=0);
1026 * Iterate through the whole list and try to match the class ID.
1028 curClass = firstRegisteredClass;
1030 while (curClass != 0)
1033 * Check if we have a match on the class ID.
1035 if (IsEqualGUID32(&(curClass->classIdentifier), rclsid))
1038 * Since we don't do out-of process or DCOM just right away, let's ignore the
1039 * class context.
1043 * We have a match, return the pointer to the class object.
1045 *ppUnk = curClass->classObject;
1047 IUnknown_AddRef(curClass->classObject);
1049 return S_OK;
1053 * Step to the next class in the list.
1055 curClass = curClass->nextClass;
1059 * If we get to here, we haven't found our class.
1061 return S_FALSE;
1064 /******************************************************************************
1065 * CoRegisterClassObject32 [OLE32.36]
1067 * This method will register the class object for a given class ID.
1069 * See the Windows documentation for more details.
1071 HRESULT WINAPI CoRegisterClassObject(
1072 REFCLSID rclsid,
1073 LPUNKNOWN pUnk,
1074 DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
1075 DWORD flags, /* [in] REGCLS flags indicating how connections are made */
1076 LPDWORD lpdwRegister
1079 RegisteredClass* newClass;
1080 LPUNKNOWN foundObject;
1081 HRESULT hr;
1082 char buf[80];
1084 WINE_StringFromCLSID(rclsid,buf);
1086 TRACE("(%s,%p,0x%08lx,0x%08lx,%p)\n",
1087 buf,pUnk,dwClsContext,flags,lpdwRegister);
1090 * Perform a sanity check on the parameters
1092 if ( (lpdwRegister==0) || (pUnk==0) )
1094 return E_INVALIDARG;
1098 * Initialize the cookie (out parameter)
1100 *lpdwRegister = 0;
1103 * First, check if the class is already registered.
1104 * If it is, this should cause an error.
1106 hr = COM_GetRegisteredClassObject(rclsid, dwClsContext, &foundObject);
1108 if (hr == S_OK)
1111 * The COM_GetRegisteredClassObject increased the reference count on the
1112 * object so it has to be released.
1114 IUnknown_Release(foundObject);
1116 return CO_E_OBJISREG;
1120 * If it is not registered, we must create a new entry for this class and
1121 * append it to the registered class list.
1122 * We use the address of the chain node as the cookie since we are sure it's
1123 * unique.
1125 newClass = HeapAlloc(GetProcessHeap(), 0, sizeof(RegisteredClass));
1128 * Initialize the node.
1130 newClass->classIdentifier = *rclsid;
1131 newClass->runContext = dwClsContext;
1132 newClass->connectFlags = flags;
1133 newClass->dwCookie = (DWORD)newClass;
1134 newClass->nextClass = firstRegisteredClass;
1137 * Since we're making a copy of the object pointer, we have to increase it's
1138 * reference count.
1140 newClass->classObject = pUnk;
1141 IUnknown_AddRef(newClass->classObject);
1143 firstRegisteredClass = newClass;
1146 * Assign the out parameter (cookie)
1148 *lpdwRegister = newClass->dwCookie;
1151 * We're successfyl Yippee!
1153 return S_OK;
1156 /***********************************************************************
1157 * CoRevokeClassObject32 [OLE32.40]
1159 * This method will remove a class object from the class registry
1161 * See the Windows documentation for more details.
1163 HRESULT WINAPI CoRevokeClassObject(
1164 DWORD dwRegister)
1166 RegisteredClass** prevClassLink;
1167 RegisteredClass* curClass;
1169 TRACE("(%08lx)\n",dwRegister);
1172 * Iterate through the whole list and try to match the cookie.
1174 curClass = firstRegisteredClass;
1175 prevClassLink = &firstRegisteredClass;
1177 while (curClass != 0)
1180 * Check if we have a match on the cookie.
1182 if (curClass->dwCookie == dwRegister)
1185 * Remove the class from the chain.
1187 *prevClassLink = curClass->nextClass;
1190 * Release the reference to the class object.
1192 IUnknown_Release(curClass->classObject);
1195 * Free the memory used by the chain node.
1197 HeapFree(GetProcessHeap(), 0, curClass);
1199 return S_OK;
1203 * Step to the next class in the list.
1205 prevClassLink = &(curClass->nextClass);
1206 curClass = curClass->nextClass;
1210 * If we get to here, we haven't found our class.
1212 return E_INVALIDARG;
1215 /***********************************************************************
1216 * CoGetClassObject [COMPOBJ.7]
1218 HRESULT WINAPI CoGetClassObject(REFCLSID rclsid, DWORD dwClsContext,
1219 LPVOID pvReserved, REFIID iid, LPVOID *ppv)
1221 LPUNKNOWN regClassObject;
1222 char xclsid[50],xiid[50];
1223 HRESULT hres = E_UNEXPECTED;
1225 char dllName[MAX_PATH+1];
1226 DWORD dllNameLen = sizeof(dllName);
1227 HINSTANCE hLibrary;
1228 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid,
1229 REFIID iid, LPVOID *ppv);
1230 DllGetClassObjectFunc DllGetClassObject;
1232 WINE_StringFromCLSID((LPCLSID)rclsid,xclsid);
1233 WINE_StringFromCLSID((LPCLSID)iid,xiid);
1234 TRACE("\n\tCLSID:\t%s,\n\tIID:\t%s\n",xclsid,xiid);
1237 * First, try and see if we can't match the class ID with one of the
1238 * registered classes.
1240 if (S_OK == COM_GetRegisteredClassObject(rclsid, dwClsContext, &regClassObject))
1243 * Get the required interface from the retrieved pointer.
1245 hres = IUnknown_QueryInterface(regClassObject, iid, ppv);
1248 * Since QI got another reference on the pointer, we want to release the
1249 * one we already have. If QI was unsuccessful, this will release the object. This
1250 * is good since we are not returning it in the "out" parameter.
1252 IUnknown_Release(regClassObject);
1254 return hres;
1257 /* out of process and remote servers not supported yet */
1258 if ((CLSCTX_LOCAL_SERVER|CLSCTX_REMOTE_SERVER) & dwClsContext) {
1259 FIXME("CLSCTX_LOCAL_SERVER and CLSCTX_REMOTE_SERVER not supported!\n");
1260 return E_ACCESSDENIED;
1263 if ((CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER) & dwClsContext) {
1264 HKEY CLSIDkey,key;
1266 /* lookup CLSID in registry key HKCR/CLSID */
1267 hres = RegOpenKeyExA(HKEY_CLASSES_ROOT, "CLSID", 0,
1268 KEY_READ, &CLSIDkey);
1270 if (hres != ERROR_SUCCESS)
1271 return REGDB_E_READREGDB;
1272 hres = RegOpenKeyExA(CLSIDkey,xclsid,0,KEY_QUERY_VALUE,&key);
1273 if (hres != ERROR_SUCCESS) {
1274 RegCloseKey(CLSIDkey);
1275 return REGDB_E_CLASSNOTREG;
1277 hres = RegQueryValueA(key, "InprocServer32", dllName, &dllNameLen);
1278 RegCloseKey(key);
1279 RegCloseKey(CLSIDkey);
1280 if (hres != ERROR_SUCCESS)
1281 return REGDB_E_READREGDB;
1282 TRACE("found InprocServer32 dll %s\n", dllName);
1284 /* open dll, call DllGetClassFactory */
1285 hLibrary = CoLoadLibrary(dllName, TRUE);
1286 if (hLibrary == 0) {
1287 TRACE("couldn't load InprocServer32 dll %s\n", dllName);
1288 return E_ACCESSDENIED; /* or should this be CO_E_DLLNOTFOUND? */
1290 DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress(hLibrary, "DllGetClassObject");
1291 if (!DllGetClassObject) {
1292 /* not sure if this should be called here CoFreeLibrary(hLibrary);*/
1293 TRACE("couldn't find function DllGetClassObject in %s\n", dllName);
1294 return E_ACCESSDENIED;
1298 * Ask the DLL for it's class object. (there was a note here about class
1299 * factories but this is good.
1301 return DllGetClassObject(rclsid, iid, ppv);
1303 return hres;
1306 /****************************************************************************************
1307 * GetClassFile
1309 * This function supplies the CLSID associated with the given filename.
1311 HRESULT WINAPI GetClassFile(LPOLESTR filePathName,CLSID *pclsid)
1313 IStorage *pstg=0;
1314 HRESULT res;
1315 int nbElm=0,length=0,i=0;
1316 LONG sizeProgId=20;
1317 LPOLESTR *pathDec=0,absFile=0,progId=0;
1318 WCHAR extention[100]={0};
1320 TRACE("()\n");
1322 /* if the file contain a storage object the return the CLSID writen by IStorage_SetClass method*/
1323 if((StgIsStorageFile(filePathName))==S_OK){
1325 res=StgOpenStorage(filePathName,NULL,STGM_READ | STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
1327 if (SUCCEEDED(res))
1328 res=ReadClassStg(pstg,pclsid);
1330 IStorage_Release(pstg);
1332 return res;
1334 /* if the file is not a storage object then attemps to match various bits in the file against a
1335 pattern in the registry. this case is not frequently used ! so I present only the psodocode for
1336 this case
1338 for(i=0;i<nFileTypes;i++)
1340 for(i=0;j<nPatternsForType;j++){
1342 PATTERN pat;
1343 HANDLE hFile;
1345 pat=ReadPatternFromRegistry(i,j);
1346 hFile=CreateFileW(filePathName,,,,,,hFile);
1347 SetFilePosition(hFile,pat.offset);
1348 ReadFile(hFile,buf,pat.size,NULL,NULL);
1349 if (memcmp(buf&pat.mask,pat.pattern.pat.size)==0){
1351 *pclsid=ReadCLSIDFromRegistry(i);
1352 return S_OK;
1357 /* if the obove strategies fail then search for the extension key in the registry */
1359 /* get the last element (absolute file) in the path name */
1360 nbElm=FileMonikerImpl_DecomposePath(filePathName,&pathDec);
1361 absFile=pathDec[nbElm-1];
1363 /* failed if the path represente a directory and not an absolute file name*/
1364 if (lstrcmpW(absFile,(LPOLESTR)"\\"))
1365 return MK_E_INVALIDEXTENSION;
1367 /* get the extension of the file */
1368 length=lstrlenW(absFile);
1369 for(i=length-1; ( (i>=0) && (extention[i]=absFile[i]) );i--);
1371 /* get the progId associated to the extension */
1372 progId=CoTaskMemAlloc(sizeProgId);
1374 res=RegQueryValueW(HKEY_CLASSES_ROOT,extention,progId,&sizeProgId);
1376 if (res==ERROR_MORE_DATA){
1378 CoTaskMemRealloc(progId,sizeProgId);
1380 res=RegQueryValueW(HKEY_CLASSES_ROOT,extention,progId,&sizeProgId);
1382 if (res==ERROR_SUCCESS)
1383 /* return the clsid associated to the progId */
1384 res= CLSIDFromProgID(progId,pclsid);
1386 for(i=0; pathDec[i]!=NULL;i++)
1387 CoTaskMemFree(pathDec[i]);
1388 CoTaskMemFree(pathDec);
1390 CoTaskMemFree(progId);
1392 if (res==ERROR_SUCCESS)
1393 return res;
1395 return MK_E_INVALIDEXTENSION;
1397 /******************************************************************************
1398 * CoRegisterMessageFilter16 [COMPOBJ.27]
1400 HRESULT WINAPI CoRegisterMessageFilter16(
1401 LPMESSAGEFILTER lpMessageFilter,
1402 LPMESSAGEFILTER *lplpMessageFilter
1404 FIXME("(%p,%p),stub!\n",lpMessageFilter,lplpMessageFilter);
1405 return 0;
1408 /***********************************************************************
1409 * CoCreateInstance [COMPOBJ.13, OLE32.7]
1411 HRESULT WINAPI CoCreateInstance(
1412 REFCLSID rclsid,
1413 LPUNKNOWN pUnkOuter,
1414 DWORD dwClsContext,
1415 REFIID iid,
1416 LPVOID *ppv)
1418 HRESULT hres;
1419 LPCLASSFACTORY lpclf = 0;
1422 * Sanity check
1424 if (ppv==0)
1425 return E_POINTER;
1428 * Initialize the "out" parameter
1430 *ppv = 0;
1433 * Get a class factory to construct the object we want.
1435 hres = CoGetClassObject(rclsid,
1436 dwClsContext,
1437 NULL,
1438 &IID_IClassFactory,
1439 (LPVOID)&lpclf);
1441 if (FAILED(hres))
1442 return hres;
1445 * Create the object and don't forget to release the factory
1447 hres = IClassFactory_CreateInstance(lpclf, pUnkOuter, iid, ppv);
1448 IClassFactory_Release(lpclf);
1450 return hres;
1453 /***********************************************************************
1454 * CoCreateInstanceEx [OLE32.165]
1456 HRESULT WINAPI CoCreateInstanceEx(
1457 REFCLSID rclsid,
1458 LPUNKNOWN pUnkOuter,
1459 DWORD dwClsContext,
1460 COSERVERINFO* pServerInfo,
1461 ULONG cmq,
1462 MULTI_QI* pResults)
1464 IUnknown* pUnk = NULL;
1465 HRESULT hr;
1466 ULONG index;
1467 int successCount = 0;
1470 * Sanity check
1472 if ( (cmq==0) || (pResults==NULL))
1473 return E_INVALIDARG;
1475 if (pServerInfo!=NULL)
1476 FIXME("() non-NULL pServerInfo not supported!\n");
1479 * Initialize all the "out" parameters.
1481 for (index = 0; index < cmq; index++)
1483 pResults[index].pItf = NULL;
1484 pResults[index].hr = E_NOINTERFACE;
1488 * Get the object and get it's IUnknown pointer.
1490 hr = CoCreateInstance(rclsid,
1491 pUnkOuter,
1492 dwClsContext,
1493 &IID_IUnknown,
1494 (VOID**)&pUnk);
1496 if (hr)
1497 return hr;
1500 * Then, query for all the interfaces requested.
1502 for (index = 0; index < cmq; index++)
1504 pResults[index].hr = IUnknown_QueryInterface(pUnk,
1505 pResults[index].pIID,
1506 (VOID**)&(pResults[index].pItf));
1508 if (pResults[index].hr == S_OK)
1509 successCount++;
1513 * Release our temporary unknown pointer.
1515 IUnknown_Release(pUnk);
1517 if (successCount == 0)
1518 return E_NOINTERFACE;
1520 if (successCount!=cmq)
1521 return CO_S_NOTALLINTERFACES;
1523 return S_OK;
1526 /***********************************************************************
1527 * CoFreeLibrary [COMPOBJ.13]
1529 void WINAPI CoFreeLibrary(HINSTANCE hLibrary)
1531 OpenDll *ptr, *prev;
1532 OpenDll *tmp;
1534 /* lookup library in linked list */
1535 prev = NULL;
1536 for (ptr = openDllList; ptr != NULL; ptr=ptr->next) {
1537 if (ptr->hLibrary == hLibrary) {
1538 break;
1540 prev = ptr;
1543 if (ptr == NULL) {
1544 /* shouldn't happen if user passed in a valid hLibrary */
1545 return;
1547 /* assert: ptr points to the library entry to free */
1549 /* free library and remove node from list */
1550 FreeLibrary(hLibrary);
1551 if (ptr == openDllList) {
1552 tmp = openDllList->next;
1553 HeapFree(GetProcessHeap(), 0, openDllList->DllName);
1554 HeapFree(GetProcessHeap(), 0, openDllList);
1555 openDllList = tmp;
1556 } else {
1557 tmp = ptr->next;
1558 HeapFree(GetProcessHeap(), 0, ptr->DllName);
1559 HeapFree(GetProcessHeap(), 0, ptr);
1560 prev->next = tmp;
1566 /***********************************************************************
1567 * CoFreeAllLibraries [COMPOBJ.12]
1569 void WINAPI CoFreeAllLibraries(void)
1571 OpenDll *ptr, *tmp;
1573 for (ptr = openDllList; ptr != NULL; ) {
1574 tmp=ptr->next;
1575 CoFreeLibrary(ptr->hLibrary);
1576 ptr = tmp;
1582 /***********************************************************************
1583 * CoFreeUnusedLibraries [COMPOBJ.17]
1585 void WINAPI CoFreeUnusedLibraries(void)
1587 OpenDll *ptr, *tmp;
1588 typedef HRESULT(*DllCanUnloadNowFunc)(void);
1589 DllCanUnloadNowFunc DllCanUnloadNow;
1591 for (ptr = openDllList; ptr != NULL; ) {
1592 DllCanUnloadNow = (DllCanUnloadNowFunc)
1593 GetProcAddress(ptr->hLibrary, "DllCanUnloadNow");
1595 if ( (DllCanUnloadNow != NULL) &&
1596 (DllCanUnloadNow() == S_OK) ) {
1597 tmp=ptr->next;
1598 CoFreeLibrary(ptr->hLibrary);
1599 ptr = tmp;
1600 } else {
1601 ptr=ptr->next;
1606 /***********************************************************************
1607 * CoFileTimeNow [COMPOBJ.82, OLE32.10]
1608 * RETURNS
1609 * the current system time in lpFileTime
1611 HRESULT WINAPI CoFileTimeNow(
1612 FILETIME *lpFileTime /* [out] the current time */
1614 DOSFS_UnixTimeToFileTime(time(NULL), lpFileTime, 0);
1615 return S_OK;
1618 /***********************************************************************
1619 * CoTaskMemAlloc (OLE32.43)
1620 * RETURNS
1621 * pointer to newly allocated block
1623 LPVOID WINAPI CoTaskMemAlloc(
1624 ULONG size /* [in] size of memoryblock to be allocated */
1626 LPMALLOC lpmalloc;
1627 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1629 if (FAILED(ret))
1630 return NULL;
1632 return IMalloc_Alloc(lpmalloc,size);
1634 /***********************************************************************
1635 * CoTaskMemFree (OLE32.44)
1637 VOID WINAPI CoTaskMemFree(
1638 LPVOID ptr /* [in] pointer to be freed */
1640 LPMALLOC lpmalloc;
1641 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1643 if (FAILED(ret))
1644 return;
1646 IMalloc_Free(lpmalloc, ptr);
1649 /***********************************************************************
1650 * CoTaskMemRealloc (OLE32.45)
1651 * RETURNS
1652 * pointer to newly allocated block
1654 LPVOID WINAPI CoTaskMemRealloc(
1655 LPVOID pvOld,
1656 ULONG size) /* [in] size of memoryblock to be allocated */
1658 LPMALLOC lpmalloc;
1659 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1661 if (FAILED(ret))
1662 return NULL;
1664 return IMalloc_Realloc(lpmalloc, pvOld, size);
1667 /***********************************************************************
1668 * CoLoadLibrary (OLE32.30)
1670 HINSTANCE WINAPI CoLoadLibrary(LPOLESTR16 lpszLibName, BOOL bAutoFree)
1672 HINSTANCE hLibrary;
1673 OpenDll *ptr;
1674 OpenDll *tmp;
1676 TRACE("CoLoadLibrary(%p, %d\n", lpszLibName, bAutoFree);
1678 hLibrary = LoadLibraryExA(lpszLibName, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
1680 if (!bAutoFree)
1681 return hLibrary;
1683 if (openDllList == NULL) {
1684 /* empty list -- add first node */
1685 openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
1686 openDllList->DllName = HEAP_strdupA(GetProcessHeap(), 0, lpszLibName);
1687 openDllList->hLibrary = hLibrary;
1688 openDllList->next = NULL;
1689 } else {
1690 /* search for this dll */
1691 int found = FALSE;
1692 for (ptr = openDllList; ptr->next != NULL; ptr=ptr->next) {
1693 if (ptr->hLibrary == hLibrary) {
1694 found = TRUE;
1695 break;
1698 if (!found) {
1699 /* dll not found, add it */
1700 tmp = openDllList;
1701 openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
1702 openDllList->DllName = HEAP_strdupA(GetProcessHeap(), 0, lpszLibName);
1703 openDllList->hLibrary = hLibrary;
1704 openDllList->next = tmp;
1708 return hLibrary;
1711 /***********************************************************************
1712 * CoInitializeWOW (OLE32.27)
1714 HRESULT WINAPI CoInitializeWOW(DWORD x,DWORD y) {
1715 FIXME("(0x%08lx,0x%08lx),stub!\n",x,y);
1716 return 0;
1719 /******************************************************************************
1720 * CoLockObjectExternal16 [COMPOBJ.63]
1722 HRESULT WINAPI CoLockObjectExternal16(
1723 LPUNKNOWN pUnk, /* [in] object to be locked */
1724 BOOL16 fLock, /* [in] do lock */
1725 BOOL16 fLastUnlockReleases /* [in] ? */
1727 FIXME("(%p,%d,%d),stub!\n",pUnk,fLock,fLastUnlockReleases);
1728 return S_OK;
1731 /******************************************************************************
1732 * CoLockObjectExternal32 [OLE32.31]
1734 HRESULT WINAPI CoLockObjectExternal(
1735 LPUNKNOWN pUnk, /* [in] object to be locked */
1736 BOOL fLock, /* [in] do lock */
1737 BOOL fLastUnlockReleases) /* [in] unlock all */
1740 if (fLock)
1743 * Increment the external lock coutner, COM_ExternalLockAddRef also
1744 * increment the object's internal lock counter.
1746 COM_ExternalLockAddRef( pUnk);
1748 else
1751 * Decrement the external lock coutner, COM_ExternalLockRelease also
1752 * decrement the object's internal lock counter.
1754 COM_ExternalLockRelease( pUnk, fLastUnlockReleases);
1757 return S_OK;
1760 /***********************************************************************
1761 * CoGetState16 [COMPOBJ.115]
1763 HRESULT WINAPI CoGetState16(LPDWORD state)
1765 FIXME("(%p),stub!\n", state);
1766 *state = 0;
1767 return S_OK;
1769 /***********************************************************************
1770 * CoSetState32 [COM32.42]
1772 HRESULT WINAPI CoSetState(LPDWORD state)
1774 FIXME("(%p),stub!\n", state);
1775 if (state) *state = 0;
1776 return S_OK;
1779 /***
1780 * COM_RevokeAllClasses
1782 * This method is called when the COM libraries are uninitialized to
1783 * release all the references to the class objects registered with
1784 * the library
1786 static void COM_RevokeAllClasses()
1788 while (firstRegisteredClass!=0)
1790 CoRevokeClassObject(firstRegisteredClass->dwCookie);
1794 /****************************************************************************
1795 * COM External Lock methods implementation
1798 /****************************************************************************
1799 * Public - Method that increments the count for a IUnknown* in the linked
1800 * list. The item is inserted if not already in the list.
1802 static void COM_ExternalLockAddRef(
1803 IUnknown *pUnk)
1805 COM_ExternalLock *externalLock = COM_ExternalLockFind(pUnk);
1808 * Add an external lock to the object. If it was already externally
1809 * locked, just increase the reference count. If it was not.
1810 * add the item to the list.
1812 if ( externalLock == EL_NOT_FOUND )
1813 COM_ExternalLockInsert(pUnk);
1814 else
1815 externalLock->uRefCount++;
1818 * Add an internal lock to the object
1820 IUnknown_AddRef(pUnk);
1823 /****************************************************************************
1824 * Public - Method that decrements the count for a IUnknown* in the linked
1825 * list. The item is removed from the list if its count end up at zero or if
1826 * bRelAll is TRUE.
1828 static void COM_ExternalLockRelease(
1829 IUnknown *pUnk,
1830 BOOL bRelAll)
1832 COM_ExternalLock *externalLock = COM_ExternalLockFind(pUnk);
1834 if ( externalLock != EL_NOT_FOUND )
1838 externalLock->uRefCount--; /* release external locks */
1839 IUnknown_Release(pUnk); /* release local locks as well */
1841 if ( bRelAll == FALSE )
1842 break; /* perform single release */
1844 } while ( externalLock->uRefCount > 0 );
1846 if ( externalLock->uRefCount == 0 ) /* get rid of the list entry */
1847 COM_ExternalLockDelete(externalLock);
1850 /****************************************************************************
1851 * Public - Method that frees the content of the list.
1853 static void COM_ExternalLockFreeList()
1855 COM_ExternalLock *head;
1857 head = elList.head; /* grab it by the head */
1858 while ( head != EL_END_OF_LIST )
1860 COM_ExternalLockDelete(head); /* get rid of the head stuff */
1862 head = elList.head; /* get the new head... */
1866 /****************************************************************************
1867 * Public - Method that dump the content of the list.
1869 void COM_ExternalLockDump()
1871 COM_ExternalLock *current = elList.head;
1873 printf("\nExternal lock list contains:\n");
1875 while ( current != EL_END_OF_LIST )
1877 printf( "\t%p with %lu references count.\n",
1878 current->pUnk,
1879 current->uRefCount);
1881 /* Skip to the next item */
1882 current = current->next;
1887 /****************************************************************************
1888 * Internal - Find a IUnknown* in the linked list
1890 static COM_ExternalLock* COM_ExternalLockFind(
1891 IUnknown *pUnk)
1893 return COM_ExternalLockLocate(elList.head, pUnk);
1896 /****************************************************************************
1897 * Internal - Recursivity agent for IUnknownExternalLockList_Find
1899 static COM_ExternalLock* COM_ExternalLockLocate(
1900 COM_ExternalLock *element,
1901 IUnknown *pUnk)
1903 if ( element == EL_END_OF_LIST )
1904 return EL_NOT_FOUND;
1906 else if ( element->pUnk == pUnk ) /* We found it */
1907 return element;
1909 else /* Not the right guy, keep on looking */
1910 return COM_ExternalLockLocate( element->next, pUnk);
1913 /****************************************************************************
1914 * Internal - Insert a new IUnknown* to the linked list
1916 static BOOL COM_ExternalLockInsert(
1917 IUnknown *pUnk)
1919 COM_ExternalLock *newLock = NULL;
1920 COM_ExternalLock *previousHead = NULL;
1923 * Allocate space for the new storage object
1925 newLock = HeapAlloc(GetProcessHeap(), 0, sizeof(COM_ExternalLock));
1927 if (newLock!=NULL)
1929 if ( elList.head == EL_END_OF_LIST )
1931 elList.head = newLock; /* The list is empty */
1933 else
1936 * insert does it at the head
1938 previousHead = elList.head;
1939 elList.head = newLock;
1943 * Set new list item data member
1945 newLock->pUnk = pUnk;
1946 newLock->uRefCount = 1;
1947 newLock->next = previousHead;
1949 return TRUE;
1951 else
1952 return FALSE;
1955 /****************************************************************************
1956 * Internal - Method that removes an item from the linked list.
1958 static void COM_ExternalLockDelete(
1959 COM_ExternalLock *itemList)
1961 COM_ExternalLock *current = elList.head;
1963 if ( current == itemList )
1966 * this section handles the deletion of the first node
1968 elList.head = itemList->next;
1969 HeapFree( GetProcessHeap(), 0, itemList);
1971 else
1975 if ( current->next == itemList ) /* We found the item to free */
1977 current->next = itemList->next; /* readjust the list pointers */
1979 HeapFree( GetProcessHeap(), 0, itemList);
1980 break;
1983 /* Skip to the next item */
1984 current = current->next;
1986 } while ( current != EL_END_OF_LIST );
1990 /***********************************************************************
1991 * COMPOBJ_DllEntryPoint [COMPOBJ.entry]
1993 * Initialization code for the COMPOBJ DLL
1995 * RETURNS:
1997 BOOL WINAPI COMPOBJ_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst, WORD ds, WORD HeapSize, DWORD res1, WORD res2)
1999 TRACE("(%08lx, %04x, %04x, %04x, %08lx, %04x)\n", Reason, hInst, ds, HeapSize,
2000 res1, res2);
2001 switch(Reason)
2003 case DLL_PROCESS_ATTACH:
2004 COMPOBJ_Attach++;
2005 if(COMPOBJ_hInstance)
2007 ERR("compobj.dll instantiated twice!\n");
2009 * We should return FALSE here, but that will break
2010 * most apps that use CreateProcess because we do
2011 * not yet support seperate address-spaces.
2013 return TRUE;
2016 COMPOBJ_hInstance = hInst;
2017 break;
2019 case DLL_PROCESS_DETACH:
2020 if(!--COMPOBJ_Attach)
2021 COMPOBJ_hInstance = 0;
2022 break;
2024 return TRUE;