OleConvertOLESTREAMToIStorage fails (returns REGDB_E_CLASSNOTREG) when
[wine/testsucceed.git] / dlls / ole32 / compobj.c
blob81d2280a79f9076e5705010902f63051481dc451
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"
12 #include <unistd.h>
13 #include <fcntl.h>
14 #include <sys/types.h>
15 #include <sys/time.h>
16 #include <sys/stat.h>
17 #ifdef HAVE_SYS_FILE_H
18 # include <sys/file.h>
19 #endif
20 #include <sys/ioctl.h>
21 #ifdef HAVE_SYS_SOCKET_H
22 # include <sys/socket.h>
23 #endif
24 #ifdef HAVE_SYS_SOCKIO_H
25 # include <sys/sockio.h>
26 #endif
27 #ifdef HAVE_NET_IF_H
28 # include <net/if.h>
29 #endif
30 #ifdef HAVE_NETINET_IN_H
31 # include <netinet/in.h>
32 #endif
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <time.h>
37 #include <assert.h>
38 #include "windef.h"
39 #include "wtypes.h"
40 #include "wingdi.h"
41 #include "wine/winbase16.h"
42 #include "winerror.h"
43 #include "wownt32.h"
44 #include "ole.h"
45 #include "ole2ver.h"
46 #include "debugtools.h"
47 #include "file.h"
48 #include "heap.h"
49 #include "ldt.h"
50 #include "winreg.h"
52 #include "wine/obj_base.h"
53 #include "wine/obj_misc.h"
54 #include "wine/obj_storage.h"
55 #include "wine/obj_clientserver.h"
57 #include "ifs.h"
58 #include "compobj.h"
60 DEFAULT_DEBUG_CHANNEL(ole);
62 /****************************************************************************
63 * COM External Lock structures and methods declaration
65 * This api provides a linked list to managed external references to
66 * COM objects.
68 * The public interface consists of three calls:
69 * COM_ExternalLockAddRef
70 * COM_ExternalLockRelease
71 * COM_ExternalLockFreeList
74 #define EL_END_OF_LIST 0
75 #define EL_NOT_FOUND 0
78 * Declaration of the static structure that manage the
79 * external lock to COM objects.
81 typedef struct COM_ExternalLock COM_ExternalLock;
82 typedef struct COM_ExternalLockList COM_ExternalLockList;
84 struct COM_ExternalLock
86 IUnknown *pUnk; /* IUnknown referenced */
87 ULONG uRefCount; /* external lock counter to IUnknown object*/
88 COM_ExternalLock *next; /* Pointer to next element in list */
91 struct COM_ExternalLockList
93 COM_ExternalLock *head; /* head of list */
97 * Declaration and initialization of the static structure that manages
98 * the external lock to COM objects.
100 static COM_ExternalLockList elList = { EL_END_OF_LIST };
103 * Public Interface to the external lock list
105 static void COM_ExternalLockFreeList();
106 static void COM_ExternalLockAddRef(IUnknown *pUnk);
107 static void COM_ExternalLockRelease(IUnknown *pUnk, BOOL bRelAll);
108 void COM_ExternalLockDump(); /* testing purposes, not static to avoid warning */
111 * Private methods used to managed the linked list
113 static BOOL COM_ExternalLockInsert(
114 IUnknown *pUnk);
116 static void COM_ExternalLockDelete(
117 COM_ExternalLock *element);
119 static COM_ExternalLock* COM_ExternalLockFind(
120 IUnknown *pUnk);
122 static COM_ExternalLock* COM_ExternalLockLocate(
123 COM_ExternalLock *element,
124 IUnknown *pUnk);
126 /****************************************************************************
127 * This section defines variables internal to the COM module.
129 * TODO: Most of these things will have to be made thread-safe.
131 HINSTANCE16 COMPOBJ_hInstance = 0;
132 HINSTANCE COMPOBJ_hInstance32 = 0;
133 static int COMPOBJ_Attach = 0;
135 LPMALLOC16 currentMalloc16=NULL;
136 LPMALLOC currentMalloc32=NULL;
138 HTASK16 hETask = 0;
139 WORD Table_ETask[62];
142 * This lock count counts the number of times CoInitialize is called. It is
143 * decreased every time CoUninitialize is called. When it hits 0, the COM
144 * libraries are freed
146 static ULONG s_COMLockCount = 0;
149 * This linked list contains the list of registered class objects. These
150 * are mostly used to register the factories for out-of-proc servers of OLE
151 * objects.
153 * TODO: Make this data structure aware of inter-process communication. This
154 * means that parts of this will be exported to the Wine Server.
156 typedef struct tagRegisteredClass
158 CLSID classIdentifier;
159 LPUNKNOWN classObject;
160 DWORD runContext;
161 DWORD connectFlags;
162 DWORD dwCookie;
163 struct tagRegisteredClass* nextClass;
164 } RegisteredClass;
166 static RegisteredClass* firstRegisteredClass = NULL;
168 /* this open DLL table belongs in a per process table, but my guess is that
169 * it shouldn't live in the kernel, so I'll put them out here in DLL
170 * space assuming that there is one OLE32 per process.
172 typedef struct tagOpenDll {
173 HINSTANCE hLibrary;
174 struct tagOpenDll *next;
175 } OpenDll;
177 static OpenDll *openDllList = NULL; /* linked list of open dlls */
179 /*****************************************************************************
180 * This section contains prototypes to internal methods for this
181 * module
183 static HRESULT COM_GetRegisteredClassObject(REFCLSID rclsid,
184 DWORD dwClsContext,
185 LPUNKNOWN* ppUnk);
187 static void COM_RevokeAllClasses();
190 /******************************************************************************
191 * CoBuildVersion [COMPOBJ.1]
193 * RETURNS
194 * Current build version, hiword is majornumber, loword is minornumber
196 DWORD WINAPI CoBuildVersion(void)
198 TRACE("Returning version %d, build %d.\n", rmm, rup);
199 return (rmm<<16)+rup;
202 /******************************************************************************
203 * CoInitialize16 [COMPOBJ.2]
204 * Set the win16 IMalloc used for memory management
206 HRESULT WINAPI CoInitialize16(
207 LPVOID lpReserved /* [in] pointer to win16 malloc interface */
209 currentMalloc16 = (LPMALLOC16)lpReserved;
210 return S_OK;
213 /******************************************************************************
214 * CoInitialize [OLE32.26]
216 * Initializes the COM libraries.
218 * See CoInitializeEx
220 HRESULT WINAPI CoInitialize(
221 LPVOID lpReserved /* [in] pointer to win32 malloc interface
222 (obsolete, should be NULL) */
226 * Just delegate to the newer method.
228 return CoInitializeEx(lpReserved, COINIT_APARTMENTTHREADED);
231 /******************************************************************************
232 * CoInitializeEx [OLE32.163]
234 * Initializes the COM libraries. The behavior used to set the win32 IMalloc
235 * used for memory management is obsolete.
237 * RETURNS
238 * S_OK if successful,
239 * S_FALSE if this function was called already.
240 * RPC_E_CHANGED_MODE if a previous call to CoInitialize specified another
241 * threading model.
243 * BUGS
244 * Only the single threaded model is supported. As a result RPC_E_CHANGED_MODE
245 * is never returned.
247 * See the windows documentation for more details.
249 HRESULT WINAPI CoInitializeEx(
250 LPVOID lpReserved, /* [in] pointer to win32 malloc interface
251 (obsolete, should be NULL) */
252 DWORD dwCoInit /* [in] A value from COINIT specifies the threading model */
255 HRESULT hr;
257 TRACE("(%p, %x)\n", lpReserved, (int)dwCoInit);
259 if (lpReserved!=NULL)
261 ERR("(%p, %x) - Bad parameter passed-in %p, must be an old Windows Application\n", lpReserved, (int)dwCoInit, lpReserved);
265 * Check for unsupported features.
267 if (dwCoInit!=COINIT_APARTMENTTHREADED)
269 FIXME(":(%p,%x): unsupported flag %x\n", lpReserved, (int)dwCoInit, (int)dwCoInit);
270 /* Hope for the best and continue anyway */
274 * Check the lock count. If this is the first time going through the initialize
275 * process, we have to initialize the libraries.
277 if (s_COMLockCount==0)
280 * Initialize the various COM libraries and data structures.
282 TRACE("() - Initializing the COM libraries\n");
284 RunningObjectTableImpl_Initialize();
286 hr = S_OK;
288 else
289 hr = S_FALSE;
292 * Crank-up that lock count.
294 s_COMLockCount++;
296 return hr;
299 /***********************************************************************
300 * CoUninitialize16 [COMPOBJ.3]
301 * Don't know what it does.
302 * 3-Nov-98 -- this was originally misspelled, I changed it to what I
303 * believe is the correct spelling
305 void WINAPI CoUninitialize16(void)
307 TRACE("()\n");
308 CoFreeAllLibraries();
311 /***********************************************************************
312 * CoUninitialize [OLE32.47]
314 * This method will release the COM libraries.
316 * See the windows documentation for more details.
318 void WINAPI CoUninitialize(void)
320 TRACE("()\n");
323 * Decrease the reference count.
325 s_COMLockCount--;
328 * If we are back to 0 locks on the COM library, make sure we free
329 * all the associated data structures.
331 if (s_COMLockCount==0)
334 * Release the various COM libraries and data structures.
336 TRACE("() - Releasing the COM libraries\n");
338 RunningObjectTableImpl_UnInitialize();
340 * Release the references to the registered class objects.
342 COM_RevokeAllClasses();
345 * This will free the loaded COM Dlls.
347 CoFreeAllLibraries();
350 * This will free list of external references to COM objects.
352 COM_ExternalLockFreeList();
356 /***********************************************************************
357 * CoGetMalloc16 [COMPOBJ.4]
358 * RETURNS
359 * The current win16 IMalloc
361 HRESULT WINAPI CoGetMalloc16(
362 DWORD dwMemContext, /* [in] unknown */
363 LPMALLOC16 * lpMalloc /* [out] current win16 malloc interface */
365 if(!currentMalloc16)
366 currentMalloc16 = IMalloc16_Constructor();
367 *lpMalloc = currentMalloc16;
368 return S_OK;
371 /******************************************************************************
372 * CoGetMalloc [OLE32.20]
374 * RETURNS
375 * The current win32 IMalloc
377 HRESULT WINAPI CoGetMalloc(
378 DWORD dwMemContext, /* [in] unknown */
379 LPMALLOC *lpMalloc /* [out] current win32 malloc interface */
381 if(!currentMalloc32)
382 currentMalloc32 = IMalloc_Constructor();
383 *lpMalloc = currentMalloc32;
384 return S_OK;
387 /***********************************************************************
388 * CoCreateStandardMalloc16 [COMPOBJ.71]
390 HRESULT WINAPI CoCreateStandardMalloc16(DWORD dwMemContext,
391 LPMALLOC16 *lpMalloc)
393 /* FIXME: docu says we shouldn't return the same allocator as in
394 * CoGetMalloc16 */
395 *lpMalloc = IMalloc16_Constructor();
396 return S_OK;
399 /******************************************************************************
400 * CoDisconnectObject [COMPOBJ.15]
402 HRESULT WINAPI CoDisconnectObject( LPUNKNOWN lpUnk, DWORD reserved )
404 TRACE("%p %lx\n",lpUnk,reserved);
405 return S_OK;
408 /***********************************************************************
409 * IsEqualGUID16 [COMPOBJ.18]
411 * Compares two Unique Identifiers.
413 * RETURNS
414 * TRUE if equal
416 BOOL16 WINAPI IsEqualGUID16(
417 GUID* g1, /* [in] unique id 1 */
418 GUID* g2 /* [in] unique id 2 */
420 return !memcmp( g1, g2, sizeof(GUID) );
423 /***********************************************************************
424 * IsEqualGUID32 [OLE32.76]
426 * Compares two Unique Identifiers.
428 * RETURNS
429 * TRUE if equal
431 BOOL WINAPI IsEqualGUID32(
432 REFGUID rguid1, /* [in] unique id 1 */
433 REFGUID rguid2 /* [in] unique id 2 */
436 return !memcmp(rguid1,rguid2,sizeof(GUID));
439 /******************************************************************************
440 * CLSIDFromString16 [COMPOBJ.20]
441 * Converts a unique identifier from it's string representation into
442 * the GUID struct.
444 * Class id: DWORD-WORD-WORD-BYTES[2]-BYTES[6]
446 * RETURNS
447 * the converted GUID
449 HRESULT WINAPI CLSIDFromString16(
450 LPCOLESTR16 idstr, /* [in] string representation of guid */
451 CLSID *id /* [out] GUID converted from string */
453 BYTE *s = (BYTE *) idstr;
454 BYTE *p;
455 int i;
456 BYTE table[256];
458 if (!s)
459 s = "{00000000-0000-0000-0000-000000000000}";
460 else { /* validate the CLSID string */
462 if (strlen(s) != 38)
463 return CO_E_CLASSSTRING;
465 if ((s[0]!='{') || (s[9]!='-') || (s[14]!='-') || (s[19]!='-') || (s[24]!='-') || (s[37]!='}'))
466 return CO_E_CLASSSTRING;
468 for (i=1; i<37; i++)
470 if ((i == 9)||(i == 14)||(i == 19)||(i == 24)) continue;
471 if (!(((s[i] >= '0') && (s[i] <= '9')) ||
472 ((s[i] >= 'a') && (s[i] <= 'f')) ||
473 ((s[i] >= 'A') && (s[i] <= 'F')))
475 return CO_E_CLASSSTRING;
479 TRACE("%s -> %p\n", s, id);
481 /* quick lookup table */
482 memset(table, 0, 256);
484 for (i = 0; i < 10; i++) {
485 table['0' + i] = i;
487 for (i = 0; i < 6; i++) {
488 table['A' + i] = i+10;
489 table['a' + i] = i+10;
492 /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
494 p = (BYTE *) id;
496 s++; /* skip leading brace */
497 for (i = 0; i < 4; i++) {
498 p[3 - i] = table[*s]<<4 | table[*(s+1)];
499 s += 2;
501 p += 4;
502 s++; /* skip - */
504 for (i = 0; i < 2; i++) {
505 p[1-i] = table[*s]<<4 | table[*(s+1)];
506 s += 2;
508 p += 2;
509 s++; /* skip - */
511 for (i = 0; i < 2; i++) {
512 p[1-i] = table[*s]<<4 | table[*(s+1)];
513 s += 2;
515 p += 2;
516 s++; /* skip - */
518 /* these are just sequential bytes */
519 for (i = 0; i < 2; i++) {
520 *p++ = table[*s]<<4 | table[*(s+1)];
521 s += 2;
523 s++; /* skip - */
525 for (i = 0; i < 6; i++) {
526 *p++ = table[*s]<<4 | table[*(s+1)];
527 s += 2;
530 return S_OK;
533 /******************************************************************************
534 * CoCreateGuid[OLE32.6]
536 * Creates a 128bit GUID.
537 * Implemented according the DCE specification for UUID generation.
538 * Code is based upon uuid library in e2fsprogs by Theodore Ts'o.
539 * Copyright (C) 1996, 1997 Theodore Ts'o.
541 * RETURNS
543 * S_OK if successful.
545 HRESULT WINAPI CoCreateGuid(
546 GUID *pguid /* [out] points to the GUID to initialize */
548 static char has_init = 0;
549 unsigned char a[6];
550 static int adjustment = 0;
551 static struct timeval last = {0, 0};
552 static UINT16 clock_seq;
553 struct timeval tv;
554 unsigned long long clock_reg;
555 UINT clock_high, clock_low;
556 UINT16 temp_clock_seq, temp_clock_mid, temp_clock_hi_and_version;
557 #ifdef HAVE_NET_IF_H
558 int sd;
559 struct ifreq ifr, *ifrp;
560 struct ifconf ifc;
561 char buf[1024];
562 int n, i;
563 #endif
565 /* Have we already tried to get the MAC address? */
566 if (!has_init) {
567 #ifdef HAVE_NET_IF_H
568 /* BSD 4.4 defines the size of an ifreq to be
569 * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
570 * However, under earlier systems, sa_len isn't present, so
571 * the size is just sizeof(struct ifreq)
573 #ifdef HAVE_SA_LEN
574 # ifndef max
575 # define max(a,b) ((a) > (b) ? (a) : (b))
576 # endif
577 # define ifreq_size(i) max(sizeof(struct ifreq),\
578 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
579 # else
580 # define ifreq_size(i) sizeof(struct ifreq)
581 # endif /* HAVE_SA_LEN */
583 sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
584 if (sd < 0) {
585 /* if we can't open a socket, just use random numbers */
586 /* set the multicast bit to prevent conflicts with real cards */
587 a[0] = (rand() & 0xff) | 0x80;
588 a[1] = rand() & 0xff;
589 a[2] = rand() & 0xff;
590 a[3] = rand() & 0xff;
591 a[4] = rand() & 0xff;
592 a[5] = rand() & 0xff;
593 } else {
594 memset(buf, 0, sizeof(buf));
595 ifc.ifc_len = sizeof(buf);
596 ifc.ifc_buf = buf;
597 /* get the ifconf interface */
598 if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) {
599 close(sd);
600 /* no ifconf, so just use random numbers */
601 /* set the multicast bit to prevent conflicts with real cards */
602 a[0] = (rand() & 0xff) | 0x80;
603 a[1] = rand() & 0xff;
604 a[2] = rand() & 0xff;
605 a[3] = rand() & 0xff;
606 a[4] = rand() & 0xff;
607 a[5] = rand() & 0xff;
608 } else {
609 /* loop through the interfaces, looking for a valid one */
610 n = ifc.ifc_len;
611 for (i = 0; i < n; i+= ifreq_size(*ifr) ) {
612 ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i);
613 strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
614 /* try to get the address for this interface */
615 # ifdef SIOCGIFHWADDR
616 if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
617 continue;
618 memcpy(a, (unsigned char *)&ifr.ifr_hwaddr.sa_data, 6);
619 # else
620 # ifdef SIOCGENADDR
621 if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
622 continue;
623 memcpy(a, (unsigned char *) ifr.ifr_enaddr, 6);
624 # else
625 /* XXX we don't have a way of getting the hardware address */
626 close(sd);
627 a[0] = 0;
628 break;
629 # endif /* SIOCGENADDR */
630 # endif /* SIOCGIFHWADDR */
631 /* make sure it's not blank */
632 if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5])
633 continue;
635 goto valid_address;
637 /* if we didn't find a valid address, make a random one */
638 /* once again, set multicast bit to avoid conflicts */
639 a[0] = (rand() & 0xff) | 0x80;
640 a[1] = rand() & 0xff;
641 a[2] = rand() & 0xff;
642 a[3] = rand() & 0xff;
643 a[4] = rand() & 0xff;
644 a[5] = rand() & 0xff;
646 valid_address:
647 close(sd);
650 #else
651 /* no networking info, so generate a random address */
652 a[0] = (rand() & 0xff) | 0x80;
653 a[1] = rand() & 0xff;
654 a[2] = rand() & 0xff;
655 a[3] = rand() & 0xff;
656 a[4] = rand() & 0xff;
657 a[5] = rand() & 0xff;
658 #endif /* HAVE_NET_IF_H */
659 has_init = 1;
662 /* generate time element of GUID */
664 /* Assume that the gettimeofday() has microsecond granularity */
665 #define MAX_ADJUSTMENT 10
667 try_again:
668 gettimeofday(&tv, 0);
669 if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
670 clock_seq = ((rand() & 0xff) << 8) + (rand() & 0xff);
671 clock_seq &= 0x1FFF;
672 last = tv;
673 last.tv_sec--;
675 if ((tv.tv_sec < last.tv_sec) ||
676 ((tv.tv_sec == last.tv_sec) &&
677 (tv.tv_usec < last.tv_usec))) {
678 clock_seq = (clock_seq+1) & 0x1FFF;
679 adjustment = 0;
680 } else if ((tv.tv_sec == last.tv_sec) &&
681 (tv.tv_usec == last.tv_usec)) {
682 if (adjustment >= MAX_ADJUSTMENT)
683 goto try_again;
684 adjustment++;
685 } else
686 adjustment = 0;
688 clock_reg = tv.tv_usec*10 + adjustment;
689 clock_reg += ((unsigned long long) tv.tv_sec)*10000000;
690 clock_reg += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
692 clock_high = clock_reg >> 32;
693 clock_low = clock_reg;
694 temp_clock_seq = clock_seq | 0x8000;
695 temp_clock_mid = (UINT16)clock_high;
696 temp_clock_hi_and_version = (clock_high >> 16) | 0x1000;
698 /* pack the information into the GUID structure */
700 ((unsigned char*)&pguid->Data1)[3] = (unsigned char)clock_low;
701 clock_low >>= 8;
702 ((unsigned char*)&pguid->Data1)[2] = (unsigned char)clock_low;
703 clock_low >>= 8;
704 ((unsigned char*)&pguid->Data1)[1] = (unsigned char)clock_low;
705 clock_low >>= 8;
706 ((unsigned char*)&pguid->Data1)[0] = (unsigned char)clock_low;
708 ((unsigned char*)&pguid->Data2)[1] = (unsigned char)temp_clock_mid;
709 temp_clock_mid >>= 8;
710 ((unsigned char*)&pguid->Data2)[0] = (unsigned char)temp_clock_mid;
712 ((unsigned char*)&pguid->Data3)[1] = (unsigned char)temp_clock_hi_and_version;
713 temp_clock_hi_and_version >>= 8;
714 ((unsigned char*)&pguid->Data3)[0] = (unsigned char)temp_clock_hi_and_version;
716 ((unsigned char*)pguid->Data4)[1] = (unsigned char)temp_clock_seq;
717 temp_clock_seq >>= 8;
718 ((unsigned char*)pguid->Data4)[0] = (unsigned char)temp_clock_seq;
720 ((unsigned char*)pguid->Data4)[2] = a[0];
721 ((unsigned char*)pguid->Data4)[3] = a[1];
722 ((unsigned char*)pguid->Data4)[4] = a[2];
723 ((unsigned char*)pguid->Data4)[5] = a[3];
724 ((unsigned char*)pguid->Data4)[6] = a[4];
725 ((unsigned char*)pguid->Data4)[7] = a[5];
727 TRACE("%p", pguid);
729 return S_OK;
732 /******************************************************************************
733 * CLSIDFromString [OLE32.3]
734 * Converts a unique identifier from it's string representation into
735 * the GUID struct.
736 * RETURNS
737 * the converted GUID
739 HRESULT WINAPI CLSIDFromString(
740 LPCOLESTR idstr, /* [in] string representation of GUID */
741 CLSID *id /* [out] GUID represented by above string */
743 LPOLESTR16 xid = HEAP_strdupWtoA(GetProcessHeap(),0,idstr);
744 OLESTATUS ret = CLSIDFromString16(xid,id);
746 HeapFree(GetProcessHeap(),0,xid);
747 return ret;
750 /******************************************************************************
751 * WINE_StringFromCLSID [Internal]
752 * Converts a GUID into the respective string representation.
754 * NOTES
756 * RETURNS
757 * the string representation and OLESTATUS
759 HRESULT WINE_StringFromCLSID(
760 const CLSID *id, /* [in] GUID to be converted */
761 LPSTR idstr /* [out] pointer to buffer to contain converted guid */
763 static const char *hex = "0123456789ABCDEF";
764 char *s;
765 int i;
767 if (!id)
768 { ERR("called with id=Null\n");
769 *idstr = 0x00;
770 return E_FAIL;
773 sprintf(idstr, "{%08lX-%04X-%04X-%02X%02X-",
774 id->Data1, id->Data2, id->Data3,
775 id->Data4[0], id->Data4[1]);
776 s = &idstr[25];
778 /* 6 hex bytes */
779 for (i = 2; i < 8; i++) {
780 *s++ = hex[id->Data4[i]>>4];
781 *s++ = hex[id->Data4[i] & 0xf];
784 *s++ = '}';
785 *s++ = '\0';
787 TRACE("%p->%s\n", id, idstr);
789 return OLE_OK;
792 /******************************************************************************
793 * StringFromCLSID16 [COMPOBJ.19]
794 * Converts a GUID into the respective string representation.
795 * The target string is allocated using the OLE IMalloc.
796 * RETURNS
797 * the string representation and OLESTATUS
799 HRESULT WINAPI StringFromCLSID16(
800 REFCLSID id, /* [in] the GUID to be converted */
801 LPOLESTR16 *idstr /* [out] a pointer to a to-be-allocated segmented pointer pointing to the resulting string */
804 LPMALLOC16 mllc;
805 OLESTATUS ret;
806 DWORD args[2];
808 ret = CoGetMalloc16(0,&mllc);
809 if (ret) return ret;
811 args[0] = (DWORD)mllc;
812 args[1] = 40;
814 /* No need for a Callback entry, we have WOWCallback16Ex which does
815 * everything we need.
817 if (!WOWCallback16Ex(
818 (DWORD)((ICOM_VTABLE(IMalloc16)*)PTR_SEG_TO_LIN(
819 ICOM_VTBL(((LPMALLOC16)PTR_SEG_TO_LIN(mllc))))
820 )->fnAlloc,
821 WCB16_CDECL,
822 2*sizeof(DWORD),
823 (LPVOID)args,
824 (LPDWORD)idstr
825 )) {
826 WARN("CallTo16 IMalloc16 failed\n");
827 return E_FAIL;
829 return WINE_StringFromCLSID(id,PTR_SEG_TO_LIN(*idstr));
832 /******************************************************************************
833 * StringFromCLSID [OLE32.151]
834 * Converts a GUID into the respective string representation.
835 * The target string is allocated using the OLE IMalloc.
836 * RETURNS
837 * the string representation and OLESTATUS
839 HRESULT WINAPI StringFromCLSID(
840 REFCLSID id, /* [in] the GUID to be converted */
841 LPOLESTR *idstr /* [out] a pointer to a to-be-allocated pointer pointing to the resulting string */
843 char buf[80];
844 OLESTATUS ret;
845 LPMALLOC mllc;
847 if ((ret=CoGetMalloc(0,&mllc)))
848 return ret;
850 ret=WINE_StringFromCLSID(id,buf);
851 if (!ret) {
852 *idstr = IMalloc_Alloc(mllc,strlen(buf)*2+2);
853 lstrcpyAtoW(*idstr,buf);
855 return ret;
858 /******************************************************************************
859 * StringFromGUID2 [COMPOBJ.76] [OLE32.152]
861 * Converts a global unique identifier into a string of an API-
862 * specified fixed format. (The usual {.....} stuff.)
864 * RETURNS
865 * The (UNICODE) string representation of the GUID in 'str'
866 * The length of the resulting string, 0 if there was any problem.
868 INT WINAPI
869 StringFromGUID2(REFGUID id, LPOLESTR str, INT cmax)
871 char xguid[80];
873 if (WINE_StringFromCLSID(id,xguid))
874 return 0;
875 if (strlen(xguid)>=cmax)
876 return 0;
877 lstrcpyAtoW(str,xguid);
878 return strlen(xguid) + 1;
881 /******************************************************************************
882 * ProgIDFromCLSID [OLE32.133]
883 * Converts a class id into the respective Program ID. (By using a registry lookup)
884 * RETURNS S_OK on success
885 * riid associated with the progid
888 HRESULT WINAPI ProgIDFromCLSID(
889 REFCLSID clsid, /* [in] class id as found in registry */
890 LPOLESTR *lplpszProgID/* [out] associated Prog ID */
893 char strCLSID[50], *buf, *buf2;
894 DWORD buf2len;
895 HKEY xhkey;
896 LPMALLOC mllc;
897 HRESULT ret = S_OK;
899 WINE_StringFromCLSID(clsid, strCLSID);
901 buf = HeapAlloc(GetProcessHeap(), 0, strlen(strCLSID)+14);
902 sprintf(buf,"CLSID\\%s\\ProgID", strCLSID);
903 if (RegOpenKeyA(HKEY_CLASSES_ROOT, buf, &xhkey))
904 ret = REGDB_E_CLASSNOTREG;
906 HeapFree(GetProcessHeap(), 0, buf);
908 if (ret == S_OK)
910 buf2 = HeapAlloc(GetProcessHeap(), 0, 255);
911 buf2len = 255;
912 if (RegQueryValueA(xhkey, NULL, buf2, &buf2len))
913 ret = REGDB_E_CLASSNOTREG;
915 if (ret == S_OK)
917 if (CoGetMalloc(0,&mllc))
918 ret = E_OUTOFMEMORY;
919 else
921 *lplpszProgID = IMalloc_Alloc(mllc, (buf2len+1)*2);
922 lstrcpyAtoW(*lplpszProgID, buf2);
925 HeapFree(GetProcessHeap(), 0, buf2);
928 RegCloseKey(xhkey);
929 return ret;
932 /******************************************************************************
933 * CLSIDFromProgID16 [COMPOBJ.61]
934 * Converts a program id into the respective GUID. (By using a registry lookup)
935 * RETURNS
936 * riid associated with the progid
938 HRESULT WINAPI CLSIDFromProgID16(
939 LPCOLESTR16 progid, /* [in] program id as found in registry */
940 LPCLSID riid /* [out] associated CLSID */
942 char *buf,buf2[80];
943 DWORD buf2len;
944 HRESULT err;
945 HKEY xhkey;
947 buf = HeapAlloc(GetProcessHeap(),0,strlen(progid)+8);
948 sprintf(buf,"%s\\CLSID",progid);
949 if ((err=RegOpenKeyA(HKEY_CLASSES_ROOT,buf,&xhkey))) {
950 HeapFree(GetProcessHeap(),0,buf);
951 return CO_E_CLASSSTRING;
953 HeapFree(GetProcessHeap(),0,buf);
954 buf2len = sizeof(buf2);
955 if ((err=RegQueryValueA(xhkey,NULL,buf2,&buf2len))) {
956 RegCloseKey(xhkey);
957 return CO_E_CLASSSTRING;
959 RegCloseKey(xhkey);
960 return CLSIDFromString16(buf2,riid);
963 /******************************************************************************
964 * CLSIDFromProgID [OLE32.2]
965 * Converts a program id into the respective GUID. (By using a registry lookup)
966 * RETURNS
967 * riid associated with the progid
969 HRESULT WINAPI CLSIDFromProgID(
970 LPCOLESTR progid, /* [in] program id as found in registry */
971 LPCLSID riid /* [out] associated CLSID */
973 LPOLESTR16 pid = HEAP_strdupWtoA(GetProcessHeap(),0,progid);
974 OLESTATUS ret = CLSIDFromProgID16(pid,riid);
976 HeapFree(GetProcessHeap(),0,pid);
977 return ret;
980 /***********************************************************************
981 * WriteClassStm
983 * This function write a CLSID on stream
985 HRESULT WINAPI WriteClassStm(IStream *pStm,REFCLSID rclsid)
987 TRACE("(%p,%p)\n",pStm,rclsid);
989 if (rclsid==NULL)
990 return E_INVALIDARG;
992 return IStream_Write(pStm,rclsid,sizeof(CLSID),NULL);
995 /***********************************************************************
996 * ReadClassStm
998 * This function read a CLSID from a stream
1000 HRESULT WINAPI ReadClassStm(IStream *pStm,REFCLSID rclsid)
1002 ULONG nbByte;
1003 HRESULT res;
1005 TRACE("(%p,%p)\n",pStm,rclsid);
1007 if (rclsid==NULL)
1008 return E_INVALIDARG;
1010 res = IStream_Read(pStm,(void*)rclsid,sizeof(CLSID),&nbByte);
1012 if (FAILED(res))
1013 return res;
1015 if (nbByte != sizeof(CLSID))
1016 return S_FALSE;
1017 else
1018 return S_OK;
1021 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
1022 /***********************************************************************
1023 * LookupETask (COMPOBJ.94)
1025 OLESTATUS WINAPI LookupETask16(HTASK16 *hTask,LPVOID p) {
1026 FIXME("(%p,%p),stub!\n",hTask,p);
1027 if ((*hTask = GetCurrentTask()) == hETask) {
1028 memcpy(p, Table_ETask, sizeof(Table_ETask));
1030 return 0;
1033 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
1034 /***********************************************************************
1035 * SetETask (COMPOBJ.95)
1037 OLESTATUS WINAPI SetETask16(HTASK16 hTask, LPVOID p) {
1038 FIXME("(%04x,%p),stub!\n",hTask,p);
1039 hETask = hTask;
1040 return 0;
1043 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
1044 /***********************************************************************
1045 * CallObjectInWOW (COMPOBJ.201)
1047 OLESTATUS WINAPI CallObjectInWOW(LPVOID p1,LPVOID p2) {
1048 FIXME("(%p,%p),stub!\n",p1,p2);
1049 return 0;
1052 /******************************************************************************
1053 * CoRegisterClassObject16 [COMPOBJ.5]
1055 * Don't know where it registers it ...
1057 HRESULT WINAPI CoRegisterClassObject16(
1058 REFCLSID rclsid,
1059 LPUNKNOWN pUnk,
1060 DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
1061 DWORD flags, /* [in] REGCLS flags indicating how connections are made */
1062 LPDWORD lpdwRegister
1064 char buf[80];
1066 WINE_StringFromCLSID(rclsid,buf);
1068 FIXME("(%s,%p,0x%08lx,0x%08lx,%p),stub\n",
1069 buf,pUnk,dwClsContext,flags,lpdwRegister
1071 return 0;
1075 /******************************************************************************
1076 * CoRevokeClassObject16 [COMPOBJ.6]
1079 HRESULT WINAPI CoRevokeClassObject16(DWORD dwRegister /* token on class obj */)
1081 FIXME("(0x%08lx),stub!\n", dwRegister);
1082 return 0;
1086 /***
1087 * COM_GetRegisteredClassObject
1089 * This internal method is used to scan the registered class list to
1090 * find a class object.
1092 * Params:
1093 * rclsid Class ID of the class to find.
1094 * dwClsContext Class context to match.
1095 * ppv [out] returns a pointer to the class object. Complying
1096 * to normal COM usage, this method will increase the
1097 * reference count on this object.
1099 static HRESULT COM_GetRegisteredClassObject(
1100 REFCLSID rclsid,
1101 DWORD dwClsContext,
1102 LPUNKNOWN* ppUnk)
1104 RegisteredClass* curClass;
1107 * Sanity check
1109 assert(ppUnk!=0);
1112 * Iterate through the whole list and try to match the class ID.
1114 curClass = firstRegisteredClass;
1116 while (curClass != 0)
1119 * Check if we have a match on the class ID.
1121 if (IsEqualGUID32(&(curClass->classIdentifier), rclsid))
1124 * Since we don't do out-of process or DCOM just right away, let's ignore the
1125 * class context.
1129 * We have a match, return the pointer to the class object.
1131 *ppUnk = curClass->classObject;
1133 IUnknown_AddRef(curClass->classObject);
1135 return S_OK;
1139 * Step to the next class in the list.
1141 curClass = curClass->nextClass;
1145 * If we get to here, we haven't found our class.
1147 return S_FALSE;
1150 /******************************************************************************
1151 * CoRegisterClassObject [OLE32.36]
1153 * This method will register the class object for a given class ID.
1155 * See the Windows documentation for more details.
1157 HRESULT WINAPI CoRegisterClassObject(
1158 REFCLSID rclsid,
1159 LPUNKNOWN pUnk,
1160 DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
1161 DWORD flags, /* [in] REGCLS flags indicating how connections are made */
1162 LPDWORD lpdwRegister
1165 RegisteredClass* newClass;
1166 LPUNKNOWN foundObject;
1167 HRESULT hr;
1168 char buf[80];
1170 WINE_StringFromCLSID(rclsid,buf);
1172 TRACE("(%s,%p,0x%08lx,0x%08lx,%p)\n",
1173 buf,pUnk,dwClsContext,flags,lpdwRegister);
1176 * Perform a sanity check on the parameters
1178 if ( (lpdwRegister==0) || (pUnk==0) )
1180 return E_INVALIDARG;
1184 * Initialize the cookie (out parameter)
1186 *lpdwRegister = 0;
1189 * First, check if the class is already registered.
1190 * If it is, this should cause an error.
1192 hr = COM_GetRegisteredClassObject(rclsid, dwClsContext, &foundObject);
1194 if (hr == S_OK)
1197 * The COM_GetRegisteredClassObject increased the reference count on the
1198 * object so it has to be released.
1200 IUnknown_Release(foundObject);
1202 return CO_E_OBJISREG;
1206 * If it is not registered, we must create a new entry for this class and
1207 * append it to the registered class list.
1208 * We use the address of the chain node as the cookie since we are sure it's
1209 * unique.
1211 newClass = HeapAlloc(GetProcessHeap(), 0, sizeof(RegisteredClass));
1214 * Initialize the node.
1216 newClass->classIdentifier = *rclsid;
1217 newClass->runContext = dwClsContext;
1218 newClass->connectFlags = flags;
1219 newClass->dwCookie = (DWORD)newClass;
1220 newClass->nextClass = firstRegisteredClass;
1223 * Since we're making a copy of the object pointer, we have to increase it's
1224 * reference count.
1226 newClass->classObject = pUnk;
1227 IUnknown_AddRef(newClass->classObject);
1229 firstRegisteredClass = newClass;
1232 * Assign the out parameter (cookie)
1234 *lpdwRegister = newClass->dwCookie;
1237 * We're successful Yippee!
1239 return S_OK;
1242 /***********************************************************************
1243 * CoRevokeClassObject [OLE32.40]
1245 * This method will remove a class object from the class registry
1247 * See the Windows documentation for more details.
1249 HRESULT WINAPI CoRevokeClassObject(
1250 DWORD dwRegister)
1252 RegisteredClass** prevClassLink;
1253 RegisteredClass* curClass;
1255 TRACE("(%08lx)\n",dwRegister);
1258 * Iterate through the whole list and try to match the cookie.
1260 curClass = firstRegisteredClass;
1261 prevClassLink = &firstRegisteredClass;
1263 while (curClass != 0)
1266 * Check if we have a match on the cookie.
1268 if (curClass->dwCookie == dwRegister)
1271 * Remove the class from the chain.
1273 *prevClassLink = curClass->nextClass;
1276 * Release the reference to the class object.
1278 IUnknown_Release(curClass->classObject);
1281 * Free the memory used by the chain node.
1283 HeapFree(GetProcessHeap(), 0, curClass);
1285 return S_OK;
1289 * Step to the next class in the list.
1291 prevClassLink = &(curClass->nextClass);
1292 curClass = curClass->nextClass;
1296 * If we get to here, we haven't found our class.
1298 return E_INVALIDARG;
1301 /***********************************************************************
1302 * CoGetClassObject [COMPOBJ.7]
1304 HRESULT WINAPI CoGetClassObject(REFCLSID rclsid, DWORD dwClsContext,
1305 LPVOID pvReserved, REFIID iid, LPVOID *ppv)
1307 LPUNKNOWN regClassObject;
1308 HRESULT hres = E_UNEXPECTED;
1309 char xclsid[80];
1310 WCHAR dllName[MAX_PATH+1];
1311 DWORD dllNameLen = sizeof(dllName);
1312 HINSTANCE hLibrary;
1313 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid,
1314 REFIID iid, LPVOID *ppv);
1315 DllGetClassObjectFunc DllGetClassObject;
1317 WINE_StringFromCLSID((LPCLSID)rclsid,xclsid);
1319 TRACE("\n\tCLSID:\t%s,\n\tIID:\t%s\n",
1320 debugstr_guid(rclsid),
1321 debugstr_guid(iid)
1325 * First, try and see if we can't match the class ID with one of the
1326 * registered classes.
1328 if (S_OK == COM_GetRegisteredClassObject(rclsid, dwClsContext, &regClassObject))
1331 * Get the required interface from the retrieved pointer.
1333 hres = IUnknown_QueryInterface(regClassObject, iid, ppv);
1336 * Since QI got another reference on the pointer, we want to release the
1337 * one we already have. If QI was unsuccessful, this will release the object. This
1338 * is good since we are not returning it in the "out" parameter.
1340 IUnknown_Release(regClassObject);
1342 return hres;
1345 /* out of process and remote servers not supported yet */
1346 if (((CLSCTX_LOCAL_SERVER|CLSCTX_REMOTE_SERVER) & dwClsContext)
1347 && !((CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER) & dwClsContext)){
1348 FIXME("CLSCTX_LOCAL_SERVER and CLSCTX_REMOTE_SERVER not supported!\n");
1349 return E_ACCESSDENIED;
1352 if ((CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER) & dwClsContext) {
1353 HKEY CLSIDkey,key;
1354 WCHAR valname[]={ 'I','n','p','r','o','c',
1355 'S','e','r','v','e','r','3','2',0};
1357 /* lookup CLSID in registry key HKCR/CLSID */
1358 hres = RegOpenKeyExA(HKEY_CLASSES_ROOT, "CLSID", 0,
1359 KEY_READ, &CLSIDkey);
1361 if (hres != ERROR_SUCCESS)
1362 return REGDB_E_READREGDB;
1363 hres = RegOpenKeyExA(CLSIDkey,xclsid,0,KEY_QUERY_VALUE,&key);
1364 if (hres != ERROR_SUCCESS) {
1365 RegCloseKey(CLSIDkey);
1366 return REGDB_E_CLASSNOTREG;
1368 memset(dllName,0,sizeof(dllName));
1369 hres = RegQueryValueW(key, valname, dllName, &dllNameLen);
1370 if (hres) {
1371 ERR("RegQueryValue of %s failed with hres %lx\n",debugstr_w(dllName),hres);
1372 return REGDB_E_CLASSNOTREG; /* FIXME: check retval */
1374 RegCloseKey(key);
1375 RegCloseKey(CLSIDkey);
1376 if (hres != ERROR_SUCCESS)
1377 return REGDB_E_READREGDB;
1378 TRACE("found InprocServer32 dll %s\n", debugstr_w(dllName));
1380 /* open dll, call DllGetClassFactory */
1381 hLibrary = CoLoadLibrary(dllName, TRUE);
1382 if (hLibrary == 0) {
1383 FIXME("couldn't load InprocServer32 dll %s\n", debugstr_w(dllName));
1384 return E_ACCESSDENIED; /* or should this be CO_E_DLLNOTFOUND? */
1386 DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress(hLibrary, "DllGetClassObject");
1387 if (!DllGetClassObject) {
1388 /* not sure if this should be called here CoFreeLibrary(hLibrary);*/
1389 FIXME("couldn't find function DllGetClassObject in %s\n", debugstr_w(dllName));
1390 return E_ACCESSDENIED;
1394 * Ask the DLL for it's class object. (there was a note here about class
1395 * factories but this is good.
1397 return DllGetClassObject(rclsid, iid, ppv);
1399 return hres;
1402 /***********************************************************************
1403 * CoResumeClassObjects
1405 * Resumes classobjects registered with REGCLS suspended
1407 HRESULT WINAPI CoResumeClassObjects(void)
1409 FIXME("\n");
1410 return S_OK;
1413 /***********************************************************************
1414 * GetClassFile
1416 * This function supplies the CLSID associated with the given filename.
1418 HRESULT WINAPI GetClassFile(LPOLESTR filePathName,CLSID *pclsid)
1420 IStorage *pstg=0;
1421 HRESULT res;
1422 int nbElm=0,length=0,i=0;
1423 LONG sizeProgId=20;
1424 LPOLESTR *pathDec=0,absFile=0,progId=0;
1425 WCHAR extention[100]={0};
1427 TRACE("()\n");
1429 /* if the file contain a storage object the return the CLSID writen by IStorage_SetClass method*/
1430 if((StgIsStorageFile(filePathName))==S_OK){
1432 res=StgOpenStorage(filePathName,NULL,STGM_READ | STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
1434 if (SUCCEEDED(res))
1435 res=ReadClassStg(pstg,pclsid);
1437 IStorage_Release(pstg);
1439 return res;
1441 /* if the file is not a storage object then attemps to match various bits in the file against a
1442 pattern in the registry. this case is not frequently used ! so I present only the psodocode for
1443 this case
1445 for(i=0;i<nFileTypes;i++)
1447 for(i=0;j<nPatternsForType;j++){
1449 PATTERN pat;
1450 HANDLE hFile;
1452 pat=ReadPatternFromRegistry(i,j);
1453 hFile=CreateFileW(filePathName,,,,,,hFile);
1454 SetFilePosition(hFile,pat.offset);
1455 ReadFile(hFile,buf,pat.size,NULL,NULL);
1456 if (memcmp(buf&pat.mask,pat.pattern.pat.size)==0){
1458 *pclsid=ReadCLSIDFromRegistry(i);
1459 return S_OK;
1464 /* if the obove strategies fail then search for the extension key in the registry */
1466 /* get the last element (absolute file) in the path name */
1467 nbElm=FileMonikerImpl_DecomposePath(filePathName,&pathDec);
1468 absFile=pathDec[nbElm-1];
1470 /* failed if the path represente a directory and not an absolute file name*/
1471 if (lstrcmpW(absFile,(LPOLESTR)"\\"))
1472 return MK_E_INVALIDEXTENSION;
1474 /* get the extension of the file */
1475 length=lstrlenW(absFile);
1476 for(i=length-1; ( (i>=0) && (extention[i]=absFile[i]) );i--);
1478 /* get the progId associated to the extension */
1479 progId=CoTaskMemAlloc(sizeProgId);
1481 res=RegQueryValueW(HKEY_CLASSES_ROOT,extention,progId,&sizeProgId);
1483 if (res==ERROR_MORE_DATA){
1485 progId = CoTaskMemRealloc(progId,sizeProgId);
1486 res=RegQueryValueW(HKEY_CLASSES_ROOT,extention,progId,&sizeProgId);
1488 if (res==ERROR_SUCCESS)
1489 /* return the clsid associated to the progId */
1490 res= CLSIDFromProgID(progId,pclsid);
1492 for(i=0; pathDec[i]!=NULL;i++)
1493 CoTaskMemFree(pathDec[i]);
1494 CoTaskMemFree(pathDec);
1496 CoTaskMemFree(progId);
1498 if (res==ERROR_SUCCESS)
1499 return res;
1501 return MK_E_INVALIDEXTENSION;
1503 /******************************************************************************
1504 * CoRegisterMessageFilter16 [COMPOBJ.27]
1506 HRESULT WINAPI CoRegisterMessageFilter16(
1507 LPMESSAGEFILTER lpMessageFilter,
1508 LPMESSAGEFILTER *lplpMessageFilter
1510 FIXME("(%p,%p),stub!\n",lpMessageFilter,lplpMessageFilter);
1511 return 0;
1514 /***********************************************************************
1515 * CoCreateInstance [COMPOBJ.13, OLE32.7]
1517 HRESULT WINAPI CoCreateInstance(
1518 REFCLSID rclsid,
1519 LPUNKNOWN pUnkOuter,
1520 DWORD dwClsContext,
1521 REFIID iid,
1522 LPVOID *ppv)
1524 HRESULT hres;
1525 LPCLASSFACTORY lpclf = 0;
1528 * Sanity check
1530 if (ppv==0)
1531 return E_POINTER;
1534 * Initialize the "out" parameter
1536 *ppv = 0;
1539 * Get a class factory to construct the object we want.
1541 hres = CoGetClassObject(rclsid,
1542 dwClsContext,
1543 NULL,
1544 &IID_IClassFactory,
1545 (LPVOID)&lpclf);
1547 if (FAILED(hres))
1548 return hres;
1551 * Create the object and don't forget to release the factory
1553 hres = IClassFactory_CreateInstance(lpclf, pUnkOuter, iid, ppv);
1554 IClassFactory_Release(lpclf);
1556 return hres;
1559 /***********************************************************************
1560 * CoCreateInstanceEx [OLE32.165]
1562 HRESULT WINAPI CoCreateInstanceEx(
1563 REFCLSID rclsid,
1564 LPUNKNOWN pUnkOuter,
1565 DWORD dwClsContext,
1566 COSERVERINFO* pServerInfo,
1567 ULONG cmq,
1568 MULTI_QI* pResults)
1570 IUnknown* pUnk = NULL;
1571 HRESULT hr;
1572 ULONG index;
1573 int successCount = 0;
1576 * Sanity check
1578 if ( (cmq==0) || (pResults==NULL))
1579 return E_INVALIDARG;
1581 if (pServerInfo!=NULL)
1582 FIXME("() non-NULL pServerInfo not supported!\n");
1585 * Initialize all the "out" parameters.
1587 for (index = 0; index < cmq; index++)
1589 pResults[index].pItf = NULL;
1590 pResults[index].hr = E_NOINTERFACE;
1594 * Get the object and get it's IUnknown pointer.
1596 hr = CoCreateInstance(rclsid,
1597 pUnkOuter,
1598 dwClsContext,
1599 &IID_IUnknown,
1600 (VOID**)&pUnk);
1602 if (hr)
1603 return hr;
1606 * Then, query for all the interfaces requested.
1608 for (index = 0; index < cmq; index++)
1610 pResults[index].hr = IUnknown_QueryInterface(pUnk,
1611 pResults[index].pIID,
1612 (VOID**)&(pResults[index].pItf));
1614 if (pResults[index].hr == S_OK)
1615 successCount++;
1619 * Release our temporary unknown pointer.
1621 IUnknown_Release(pUnk);
1623 if (successCount == 0)
1624 return E_NOINTERFACE;
1626 if (successCount!=cmq)
1627 return CO_S_NOTALLINTERFACES;
1629 return S_OK;
1632 /***********************************************************************
1633 * CoFreeLibrary [COMPOBJ.13]
1635 void WINAPI CoFreeLibrary(HINSTANCE hLibrary)
1637 OpenDll *ptr, *prev;
1638 OpenDll *tmp;
1640 /* lookup library in linked list */
1641 prev = NULL;
1642 for (ptr = openDllList; ptr != NULL; ptr=ptr->next) {
1643 if (ptr->hLibrary == hLibrary) {
1644 break;
1646 prev = ptr;
1649 if (ptr == NULL) {
1650 /* shouldn't happen if user passed in a valid hLibrary */
1651 return;
1653 /* assert: ptr points to the library entry to free */
1655 /* free library and remove node from list */
1656 FreeLibrary(hLibrary);
1657 if (ptr == openDllList) {
1658 tmp = openDllList->next;
1659 HeapFree(GetProcessHeap(), 0, openDllList);
1660 openDllList = tmp;
1661 } else {
1662 tmp = ptr->next;
1663 HeapFree(GetProcessHeap(), 0, ptr);
1664 prev->next = tmp;
1670 /***********************************************************************
1671 * CoFreeAllLibraries [COMPOBJ.12]
1673 void WINAPI CoFreeAllLibraries(void)
1675 OpenDll *ptr, *tmp;
1677 for (ptr = openDllList; ptr != NULL; ) {
1678 tmp=ptr->next;
1679 CoFreeLibrary(ptr->hLibrary);
1680 ptr = tmp;
1686 /***********************************************************************
1687 * CoFreeUnusedLibraries [COMPOBJ.17]
1689 void WINAPI CoFreeUnusedLibraries(void)
1691 OpenDll *ptr, *tmp;
1692 typedef HRESULT(*DllCanUnloadNowFunc)(void);
1693 DllCanUnloadNowFunc DllCanUnloadNow;
1695 for (ptr = openDllList; ptr != NULL; ) {
1696 DllCanUnloadNow = (DllCanUnloadNowFunc)
1697 GetProcAddress(ptr->hLibrary, "DllCanUnloadNow");
1699 if ( (DllCanUnloadNow != NULL) &&
1700 (DllCanUnloadNow() == S_OK) ) {
1701 tmp=ptr->next;
1702 CoFreeLibrary(ptr->hLibrary);
1703 ptr = tmp;
1704 } else {
1705 ptr=ptr->next;
1710 /***********************************************************************
1711 * CoFileTimeNow [COMPOBJ.82, OLE32.10]
1712 * RETURNS
1713 * the current system time in lpFileTime
1715 HRESULT WINAPI CoFileTimeNow(
1716 FILETIME *lpFileTime /* [out] the current time */
1718 DOSFS_UnixTimeToFileTime(time(NULL), lpFileTime, 0);
1719 return S_OK;
1722 /***********************************************************************
1723 * CoTaskMemAlloc (OLE32.43)
1724 * RETURNS
1725 * pointer to newly allocated block
1727 LPVOID WINAPI CoTaskMemAlloc(
1728 ULONG size /* [in] size of memoryblock to be allocated */
1730 LPMALLOC lpmalloc;
1731 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1733 if (FAILED(ret))
1734 return NULL;
1736 return IMalloc_Alloc(lpmalloc,size);
1738 /***********************************************************************
1739 * CoTaskMemFree (OLE32.44)
1741 VOID WINAPI CoTaskMemFree(
1742 LPVOID ptr /* [in] pointer to be freed */
1744 LPMALLOC lpmalloc;
1745 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1747 if (FAILED(ret))
1748 return;
1750 IMalloc_Free(lpmalloc, ptr);
1753 /***********************************************************************
1754 * CoTaskMemRealloc (OLE32.45)
1755 * RETURNS
1756 * pointer to newly allocated block
1758 LPVOID WINAPI CoTaskMemRealloc(
1759 LPVOID pvOld,
1760 ULONG size) /* [in] size of memoryblock to be allocated */
1762 LPMALLOC lpmalloc;
1763 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1765 if (FAILED(ret))
1766 return NULL;
1768 return IMalloc_Realloc(lpmalloc, pvOld, size);
1771 /***********************************************************************
1772 * CoLoadLibrary (OLE32.30)
1774 HINSTANCE WINAPI CoLoadLibrary(LPOLESTR lpszLibName, BOOL bAutoFree)
1776 HINSTANCE hLibrary;
1777 OpenDll *ptr;
1778 OpenDll *tmp;
1780 TRACE("CoLoadLibrary(%p, %d\n", debugstr_w(lpszLibName), bAutoFree);
1782 hLibrary = LoadLibraryExW(lpszLibName, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
1784 if (!bAutoFree)
1785 return hLibrary;
1787 if (openDllList == NULL) {
1788 /* empty list -- add first node */
1789 openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
1790 openDllList->hLibrary=hLibrary;
1791 openDllList->next = NULL;
1792 } else {
1793 /* search for this dll */
1794 int found = FALSE;
1795 for (ptr = openDllList; ptr->next != NULL; ptr=ptr->next) {
1796 if (ptr->hLibrary == hLibrary) {
1797 found = TRUE;
1798 break;
1801 if (!found) {
1802 /* dll not found, add it */
1803 tmp = openDllList;
1804 openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
1805 openDllList->hLibrary = hLibrary;
1806 openDllList->next = tmp;
1810 return hLibrary;
1813 /***********************************************************************
1814 * CoInitializeWOW (OLE32.27)
1816 HRESULT WINAPI CoInitializeWOW(DWORD x,DWORD y) {
1817 FIXME("(0x%08lx,0x%08lx),stub!\n",x,y);
1818 return 0;
1821 /******************************************************************************
1822 * CoLockObjectExternal16 [COMPOBJ.63]
1824 HRESULT WINAPI CoLockObjectExternal16(
1825 LPUNKNOWN pUnk, /* [in] object to be locked */
1826 BOOL16 fLock, /* [in] do lock */
1827 BOOL16 fLastUnlockReleases /* [in] ? */
1829 FIXME("(%p,%d,%d),stub!\n",pUnk,fLock,fLastUnlockReleases);
1830 return S_OK;
1833 /******************************************************************************
1834 * CoLockObjectExternal [OLE32.31]
1836 HRESULT WINAPI CoLockObjectExternal(
1837 LPUNKNOWN pUnk, /* [in] object to be locked */
1838 BOOL fLock, /* [in] do lock */
1839 BOOL fLastUnlockReleases) /* [in] unlock all */
1842 if (fLock)
1845 * Increment the external lock coutner, COM_ExternalLockAddRef also
1846 * increment the object's internal lock counter.
1848 COM_ExternalLockAddRef( pUnk);
1850 else
1853 * Decrement the external lock coutner, COM_ExternalLockRelease also
1854 * decrement the object's internal lock counter.
1856 COM_ExternalLockRelease( pUnk, fLastUnlockReleases);
1859 return S_OK;
1862 /***********************************************************************
1863 * CoGetState16 [COMPOBJ.115]
1865 HRESULT WINAPI CoGetState16(LPDWORD state)
1867 FIXME("(%p),stub!\n", state);
1868 *state = 0;
1869 return S_OK;
1871 /***********************************************************************
1872 * CoSetState [COM32.42]
1874 HRESULT WINAPI CoSetState(LPDWORD state)
1876 FIXME("(%p),stub!\n", state);
1877 if (state) *state = 0;
1878 return S_OK;
1880 /***********************************************************************
1881 * CoCreateFreeThreadedMarshaler [OLE32.5]
1883 HRESULT WINAPI CoCreateFreeThreadedMarshaler (LPUNKNOWN punkOuter, LPUNKNOWN* ppunkMarshal)
1885 FIXME ("(%p %p): stub\n", punkOuter, ppunkMarshal);
1887 return S_OK;
1891 /***********************************************************************
1892 * DllGetClassObject [OLE32.63]
1894 HRESULT WINAPI OLE32_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
1896 FIXME("\n\tCLSID:\t%s,\n\tIID:\t%s\n",debugstr_guid(rclsid),debugstr_guid(iid));
1897 *ppv = NULL;
1898 return CLASS_E_CLASSNOTAVAILABLE;
1902 /***
1903 * COM_RevokeAllClasses
1905 * This method is called when the COM libraries are uninitialized to
1906 * release all the references to the class objects registered with
1907 * the library
1909 static void COM_RevokeAllClasses()
1911 while (firstRegisteredClass!=0)
1913 CoRevokeClassObject(firstRegisteredClass->dwCookie);
1917 /****************************************************************************
1918 * COM External Lock methods implementation
1921 /****************************************************************************
1922 * Public - Method that increments the count for a IUnknown* in the linked
1923 * list. The item is inserted if not already in the list.
1925 static void COM_ExternalLockAddRef(
1926 IUnknown *pUnk)
1928 COM_ExternalLock *externalLock = COM_ExternalLockFind(pUnk);
1931 * Add an external lock to the object. If it was already externally
1932 * locked, just increase the reference count. If it was not.
1933 * add the item to the list.
1935 if ( externalLock == EL_NOT_FOUND )
1936 COM_ExternalLockInsert(pUnk);
1937 else
1938 externalLock->uRefCount++;
1941 * Add an internal lock to the object
1943 IUnknown_AddRef(pUnk);
1946 /****************************************************************************
1947 * Public - Method that decrements the count for a IUnknown* in the linked
1948 * list. The item is removed from the list if its count end up at zero or if
1949 * bRelAll is TRUE.
1951 static void COM_ExternalLockRelease(
1952 IUnknown *pUnk,
1953 BOOL bRelAll)
1955 COM_ExternalLock *externalLock = COM_ExternalLockFind(pUnk);
1957 if ( externalLock != EL_NOT_FOUND )
1961 externalLock->uRefCount--; /* release external locks */
1962 IUnknown_Release(pUnk); /* release local locks as well */
1964 if ( bRelAll == FALSE )
1965 break; /* perform single release */
1967 } while ( externalLock->uRefCount > 0 );
1969 if ( externalLock->uRefCount == 0 ) /* get rid of the list entry */
1970 COM_ExternalLockDelete(externalLock);
1973 /****************************************************************************
1974 * Public - Method that frees the content of the list.
1976 static void COM_ExternalLockFreeList()
1978 COM_ExternalLock *head;
1980 head = elList.head; /* grab it by the head */
1981 while ( head != EL_END_OF_LIST )
1983 COM_ExternalLockDelete(head); /* get rid of the head stuff */
1985 head = elList.head; /* get the new head... */
1989 /****************************************************************************
1990 * Public - Method that dump the content of the list.
1992 void COM_ExternalLockDump()
1994 COM_ExternalLock *current = elList.head;
1996 DPRINTF("\nExternal lock list contains:\n");
1998 while ( current != EL_END_OF_LIST )
2000 DPRINTF( "\t%p with %lu references count.\n", current->pUnk, current->uRefCount);
2002 /* Skip to the next item */
2003 current = current->next;
2008 /****************************************************************************
2009 * Internal - Find a IUnknown* in the linked list
2011 static COM_ExternalLock* COM_ExternalLockFind(
2012 IUnknown *pUnk)
2014 return COM_ExternalLockLocate(elList.head, pUnk);
2017 /****************************************************************************
2018 * Internal - Recursivity agent for IUnknownExternalLockList_Find
2020 static COM_ExternalLock* COM_ExternalLockLocate(
2021 COM_ExternalLock *element,
2022 IUnknown *pUnk)
2024 if ( element == EL_END_OF_LIST )
2025 return EL_NOT_FOUND;
2027 else if ( element->pUnk == pUnk ) /* We found it */
2028 return element;
2030 else /* Not the right guy, keep on looking */
2031 return COM_ExternalLockLocate( element->next, pUnk);
2034 /****************************************************************************
2035 * Internal - Insert a new IUnknown* to the linked list
2037 static BOOL COM_ExternalLockInsert(
2038 IUnknown *pUnk)
2040 COM_ExternalLock *newLock = NULL;
2041 COM_ExternalLock *previousHead = NULL;
2044 * Allocate space for the new storage object
2046 newLock = HeapAlloc(GetProcessHeap(), 0, sizeof(COM_ExternalLock));
2048 if (newLock!=NULL)
2050 if ( elList.head == EL_END_OF_LIST )
2052 elList.head = newLock; /* The list is empty */
2054 else
2057 * insert does it at the head
2059 previousHead = elList.head;
2060 elList.head = newLock;
2064 * Set new list item data member
2066 newLock->pUnk = pUnk;
2067 newLock->uRefCount = 1;
2068 newLock->next = previousHead;
2070 return TRUE;
2072 else
2073 return FALSE;
2076 /****************************************************************************
2077 * Internal - Method that removes an item from the linked list.
2079 static void COM_ExternalLockDelete(
2080 COM_ExternalLock *itemList)
2082 COM_ExternalLock *current = elList.head;
2084 if ( current == itemList )
2087 * this section handles the deletion of the first node
2089 elList.head = itemList->next;
2090 HeapFree( GetProcessHeap(), 0, itemList);
2092 else
2096 if ( current->next == itemList ) /* We found the item to free */
2098 current->next = itemList->next; /* readjust the list pointers */
2100 HeapFree( GetProcessHeap(), 0, itemList);
2101 break;
2104 /* Skip to the next item */
2105 current = current->next;
2107 } while ( current != EL_END_OF_LIST );
2111 /***********************************************************************
2112 * COMPOBJ_DllEntryPoint [COMPOBJ.entry]
2114 * Initialization code for the COMPOBJ DLL
2116 * RETURNS:
2118 BOOL WINAPI COMPOBJ_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst, WORD ds, WORD HeapSize, DWORD res1, WORD res2)
2120 TRACE("(%08lx, %04x, %04x, %04x, %08lx, %04x)\n", Reason, hInst, ds, HeapSize,
2121 res1, res2);
2122 switch(Reason)
2124 case DLL_PROCESS_ATTACH:
2125 COMPOBJ_Attach++;
2126 if(COMPOBJ_hInstance)
2128 ERR("compobj.dll instantiated twice!\n");
2130 * We should return FALSE here, but that will break
2131 * most apps that use CreateProcess because we do
2132 * not yet support seperate address-spaces.
2134 return TRUE;
2137 COMPOBJ_hInstance = hInst;
2138 break;
2140 case DLL_PROCESS_DETACH:
2141 if(!--COMPOBJ_Attach)
2142 COMPOBJ_hInstance = 0;
2143 break;
2145 return TRUE;