Removed extra includes from ole.h and wingdi.h.
[wine.git] / dlls / ole32 / compobj.c
blob1dcb58bdd678b5a8dfc9fab218645ba2dc56d910
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 <string.h>
35 #include <time.h>
36 #include <assert.h>
37 #include "windef.h"
38 #include "wtypes.h"
39 #include "wingdi.h"
40 #include "wine/winbase16.h"
41 #include "winerror.h"
42 #include "wownt32.h"
43 #include "ole.h"
44 #include "ole2ver.h"
45 #include "debugtools.h"
46 #include "file.h"
47 #include "heap.h"
48 #include "ldt.h"
49 #include "winreg.h"
51 #include "wine/obj_base.h"
52 #include "wine/obj_misc.h"
53 #include "wine/obj_storage.h"
54 #include "wine/obj_clientserver.h"
56 #include "ifs.h"
57 #include "compobj.h"
59 DEFAULT_DEBUG_CHANNEL(ole)
60 /****************************************************************************
61 * COM External Lock structures and methods declaration
63 * This api provides a linked list to managed external references to
64 * COM objects.
66 * The public interface consists of three calls:
67 * COM_ExternalLockAddRef
68 * COM_ExternalLockRelease
69 * COM_ExternalLockFreeList
72 #define EL_END_OF_LIST 0
73 #define EL_NOT_FOUND 0
76 * Declaration of the static structure that manage the
77 * external lock to COM objects.
79 typedef struct COM_ExternalLock COM_ExternalLock;
80 typedef struct COM_ExternalLockList COM_ExternalLockList;
82 struct COM_ExternalLock
84 IUnknown *pUnk; /* IUnknown referenced */
85 ULONG uRefCount; /* external lock counter to IUnknown object*/
86 COM_ExternalLock *next; /* Pointer to next element in list */
89 struct COM_ExternalLockList
91 COM_ExternalLock *head; /* head of list */
95 * Declaration and initialization of the static structure that manages
96 * the external lock to COM objects.
98 static COM_ExternalLockList elList = { EL_END_OF_LIST };
101 * Public Interface to the external lock list
103 static void COM_ExternalLockFreeList();
104 static void COM_ExternalLockAddRef(IUnknown *pUnk);
105 static void COM_ExternalLockRelease(IUnknown *pUnk, BOOL bRelAll);
106 void COM_ExternalLockDump(); /* testing purposes, not static to avoid warning */
109 * Private methods used to managed the linked list
111 static BOOL COM_ExternalLockInsert(
112 IUnknown *pUnk);
114 static void COM_ExternalLockDelete(
115 COM_ExternalLock *element);
117 static COM_ExternalLock* COM_ExternalLockFind(
118 IUnknown *pUnk);
120 static COM_ExternalLock* COM_ExternalLockLocate(
121 COM_ExternalLock *element,
122 IUnknown *pUnk);
124 /****************************************************************************
125 * This section defines variables internal to the COM module.
127 * TODO: Most of these things will have to be made thread-safe.
129 HINSTANCE16 COMPOBJ_hInstance = 0;
130 HINSTANCE COMPOBJ_hInstance32 = 0;
131 static int COMPOBJ_Attach = 0;
133 LPMALLOC16 currentMalloc16=NULL;
134 LPMALLOC currentMalloc32=NULL;
136 HTASK16 hETask = 0;
137 WORD Table_ETask[62];
140 * This lock count counts the number of times CoInitialize is called. It is
141 * decreased every time CoUninitialize is called. When it hits 0, the COM
142 * libraries are freed
144 static ULONG s_COMLockCount = 0;
147 * This linked list contains the list of registered class objects. These
148 * are mostly used to register the factories for out-of-proc servers of OLE
149 * objects.
151 * TODO: Make this data structure aware of inter-process communication. This
152 * means that parts of this will be exported to the Wine Server.
154 typedef struct tagRegisteredClass
156 CLSID classIdentifier;
157 LPUNKNOWN classObject;
158 DWORD runContext;
159 DWORD connectFlags;
160 DWORD dwCookie;
161 struct tagRegisteredClass* nextClass;
162 } RegisteredClass;
164 static RegisteredClass* firstRegisteredClass = NULL;
166 /* this open DLL table belongs in a per process table, but my guess is that
167 * it shouldn't live in the kernel, so I'll put them out here in DLL
168 * space assuming that there is one OLE32 per process.
170 typedef struct tagOpenDll {
171 char *DllName; /* really only needed for debugging */
172 HINSTANCE hLibrary;
173 struct tagOpenDll *next;
174 } OpenDll;
176 static OpenDll *openDllList = NULL; /* linked list of open dlls */
178 /*****************************************************************************
179 * This section contains prototypes to internal methods for this
180 * module
182 static HRESULT COM_GetRegisteredClassObject(REFCLSID rclsid,
183 DWORD dwClsContext,
184 LPUNKNOWN* ppUnk);
186 static void COM_RevokeAllClasses();
189 /******************************************************************************
190 * CoBuildVersion [COMPOBJ.1]
192 * RETURNS
193 * Current build version, hiword is majornumber, loword is minornumber
195 DWORD WINAPI CoBuildVersion(void)
197 TRACE("Returning version %d, build %d.\n", rmm, rup);
198 return (rmm<<16)+rup;
201 /******************************************************************************
202 * CoInitialize16 [COMPOBJ.2]
203 * Set the win16 IMalloc used for memory management
205 HRESULT WINAPI CoInitialize16(
206 LPVOID lpReserved /* [in] pointer to win16 malloc interface */
208 currentMalloc16 = (LPMALLOC16)lpReserved;
209 return S_OK;
212 /******************************************************************************
213 * CoInitialize32 [OLE32.26]
215 * Initializes the COM libraries.
217 * See CoInitializeEx32
219 HRESULT WINAPI CoInitialize(
220 LPVOID lpReserved /* [in] pointer to win32 malloc interface
221 (obsolete, should be NULL) */
225 * Just delegate to the newer method.
227 return CoInitializeEx(lpReserved, COINIT_APARTMENTTHREADED);
230 /******************************************************************************
231 * CoInitializeEx32 [OLE32.163]
233 * Initializes the COM libraries. The behavior used to set the win32 IMalloc
234 * used for memory management is obsolete.
236 * RETURNS
237 * S_OK if successful,
238 * S_FALSE if this function was called already.
239 * RPC_E_CHANGED_MODE if a previous call to CoInitialize specified another
240 * threading model.
242 * BUGS
243 * Only the single threaded model is supported. As a result RPC_E_CHANGED_MODE
244 * is never returned.
246 * See the windows documentation for more details.
248 HRESULT WINAPI CoInitializeEx(
249 LPVOID lpReserved, /* [in] pointer to win32 malloc interface
250 (obsolete, should be NULL) */
251 DWORD dwCoInit /* [in] A value from COINIT specifies the threading model */
254 HRESULT hr;
256 TRACE("(%p, %x)\n", lpReserved, (int)dwCoInit);
258 if (lpReserved!=NULL)
260 ERR("(%p, %x) - Bad parameter passed-in %p, must be an old Windows Application\n", lpReserved, (int)dwCoInit, lpReserved);
264 * Check for unsupported features.
266 if (dwCoInit!=COINIT_APARTMENTTHREADED)
268 FIXME(":(%p,%x): unsupported flag %x\n", lpReserved, (int)dwCoInit, (int)dwCoInit);
269 /* Hope for the best and continue anyway */
273 * Check the lock count. If this is the first time going through the initialize
274 * process, we have to initialize the libraries.
276 if (s_COMLockCount==0)
279 * Initialize the various COM libraries and data structures.
281 TRACE("() - Initializing the COM libraries\n");
283 RunningObjectTableImpl_Initialize();
285 hr = S_OK;
287 else
288 hr = S_FALSE;
291 * Crank-up that lock count.
293 s_COMLockCount++;
295 return hr;
298 /***********************************************************************
299 * CoUninitialize16 [COMPOBJ.3]
300 * Don't know what it does.
301 * 3-Nov-98 -- this was originally misspelled, I changed it to what I
302 * believe is the correct spelling
304 void WINAPI CoUninitialize16(void)
306 TRACE("()\n");
307 CoFreeAllLibraries();
310 /***********************************************************************
311 * CoUninitialize32 [OLE32.47]
313 * This method will release the COM libraries.
315 * See the windows documentation for more details.
317 void WINAPI CoUninitialize(void)
319 TRACE("()\n");
322 * Decrease the reference count.
324 s_COMLockCount--;
327 * If we are back to 0 locks on the COM library, make sure we free
328 * all the associated data structures.
330 if (s_COMLockCount==0)
333 * Release the various COM libraries and data structures.
335 TRACE("() - Releasing the COM libraries\n");
337 RunningObjectTableImpl_UnInitialize();
339 * Release the references to the registered class objects.
341 COM_RevokeAllClasses();
344 * This will free the loaded COM Dlls.
346 CoFreeAllLibraries();
349 * This will free list of external references to COM objects.
351 COM_ExternalLockFreeList();
355 /***********************************************************************
356 * CoGetMalloc16 [COMPOBJ.4]
357 * RETURNS
358 * The current win16 IMalloc
360 HRESULT WINAPI CoGetMalloc16(
361 DWORD dwMemContext, /* [in] unknown */
362 LPMALLOC16 * lpMalloc /* [out] current win16 malloc interface */
364 if(!currentMalloc16)
365 currentMalloc16 = IMalloc16_Constructor();
366 *lpMalloc = currentMalloc16;
367 return S_OK;
370 /******************************************************************************
371 * CoGetMalloc32 [OLE32.20]
373 * RETURNS
374 * The current win32 IMalloc
376 HRESULT WINAPI CoGetMalloc(
377 DWORD dwMemContext, /* [in] unknown */
378 LPMALLOC *lpMalloc /* [out] current win32 malloc interface */
380 if(!currentMalloc32)
381 currentMalloc32 = IMalloc_Constructor();
382 *lpMalloc = currentMalloc32;
383 return S_OK;
386 /***********************************************************************
387 * CoCreateStandardMalloc16 [COMPOBJ.71]
389 HRESULT WINAPI CoCreateStandardMalloc16(DWORD dwMemContext,
390 LPMALLOC16 *lpMalloc)
392 /* FIXME: docu says we shouldn't return the same allocator as in
393 * CoGetMalloc16 */
394 *lpMalloc = IMalloc16_Constructor();
395 return S_OK;
398 /******************************************************************************
399 * CoDisconnectObject [COMPOBJ.15]
401 HRESULT WINAPI CoDisconnectObject( LPUNKNOWN lpUnk, DWORD reserved )
403 TRACE("%p %lx\n",lpUnk,reserved);
404 return S_OK;
407 /***********************************************************************
408 * IsEqualGUID16 [COMPOBJ.18]
410 * Compares two Unique Identifiers.
412 * RETURNS
413 * TRUE if equal
415 BOOL16 WINAPI IsEqualGUID16(
416 GUID* g1, /* [in] unique id 1 */
417 GUID* g2 /**/
419 return !memcmp( g1, g2, sizeof(GUID) );
422 /***********************************************************************
423 * IsEqualGUID32 [OLE32.76]
425 * Compares two Unique Identifiers.
427 * RETURNS
428 * TRUE if equal
430 BOOL WINAPI IsEqualGUID32(
431 REFGUID rguid1, /* [in] unique id 1 */
432 REFGUID rguid2 /* [in] unique id 2 */
435 return !memcmp(rguid1,rguid2,sizeof(GUID));
438 /******************************************************************************
439 * CLSIDFromString16 [COMPOBJ.20]
440 * Converts a unique identifier from it's string representation into
441 * the GUID struct.
443 * Class id: DWORD-WORD-WORD-BYTES[2]-BYTES[6]
445 * RETURNS
446 * the converted GUID
448 HRESULT WINAPI CLSIDFromString16(
449 LPCOLESTR16 idstr, /* [in] string representation of guid */
450 CLSID *id /* [out] GUID converted from string */
452 BYTE *s = (BYTE *) idstr;
453 BYTE *p;
454 int i;
455 BYTE table[256];
457 if (!s)
458 s = "{00000000-0000-0000-0000-000000000000}";
460 TRACE("%s -> %p\n", s, id);
462 /* quick lookup table */
463 memset(table, 0, 256);
465 for (i = 0; i < 10; i++) {
466 table['0' + i] = i;
468 for (i = 0; i < 6; i++) {
469 table['A' + i] = i+10;
470 table['a' + i] = i+10;
473 /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
475 if (strlen(s) != 38)
476 return OLE_ERROR_OBJECT;
478 p = (BYTE *) id;
480 s++; /* skip leading brace */
481 for (i = 0; i < 4; i++) {
482 p[3 - i] = table[*s]<<4 | table[*(s+1)];
483 s += 2;
485 p += 4;
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 for (i = 0; i < 2; i++) {
496 p[1-i] = table[*s]<<4 | table[*(s+1)];
497 s += 2;
499 p += 2;
500 s++; /* skip - */
502 /* these are just sequential bytes */
503 for (i = 0; i < 2; i++) {
504 *p++ = table[*s]<<4 | table[*(s+1)];
505 s += 2;
507 s++; /* skip - */
509 for (i = 0; i < 6; i++) {
510 *p++ = table[*s]<<4 | table[*(s+1)];
511 s += 2;
514 return S_OK;
517 /******************************************************************************
518 * CoCreateGuid[OLE32.6]
520 * Creates a 128bit GUID.
521 * Implemented according the DCE specification for UUID generation.
522 * Code is based upon uuid library in e2fsprogs by Theodore Ts'o.
523 * Copyright (C) 1996, 1997 Theodore Ts'o.
525 * RETURNS
527 * S_OK if successful.
529 HRESULT WINAPI CoCreateGuid(
530 GUID *pguid /* [out] points to the GUID to initialize */
532 static char has_init = 0;
533 unsigned char a[6];
534 static int adjustment = 0;
535 static struct timeval last = {0, 0};
536 static UINT16 clock_seq;
537 struct timeval tv;
538 unsigned long long clock_reg;
539 UINT clock_high, clock_low;
540 UINT16 temp_clock_seq, temp_clock_mid, temp_clock_hi_and_version;
541 #ifdef HAVE_NET_IF_H
542 int sd;
543 struct ifreq ifr, *ifrp;
544 struct ifconf ifc;
545 char buf[1024];
546 int n, i;
547 #endif
549 /* Have we already tried to get the MAC address? */
550 if (!has_init) {
551 #ifdef HAVE_NET_IF_H
552 /* BSD 4.4 defines the size of an ifreq to be
553 * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
554 * However, under earlier systems, sa_len isn't present, so
555 * the size is just sizeof(struct ifreq)
557 # ifdef HAVE_SA_LEN
558 # ifndef max
559 # define max(a,b) ((a) > (b) ? (a) : (b))
560 # endif
561 # define ifreq_size(i) max(sizeof(struct ifreq),\
562 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
563 # else
564 # define ifreq_size(i) sizeof(struct ifreq)
565 # endif /* HAVE_SA_LEN */
567 sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
568 if (sd < 0) {
569 /* if we can't open a socket, just use random numbers */
570 /* set the multicast bit to prevent conflicts with real cards */
571 a[0] = (rand() & 0xff) | 0x80;
572 a[1] = rand() & 0xff;
573 a[2] = rand() & 0xff;
574 a[3] = rand() & 0xff;
575 a[4] = rand() & 0xff;
576 a[5] = rand() & 0xff;
577 } else {
578 memset(buf, 0, sizeof(buf));
579 ifc.ifc_len = sizeof(buf);
580 ifc.ifc_buf = buf;
581 /* get the ifconf interface */
582 if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) {
583 close(sd);
584 /* no ifconf, so just use random numbers */
585 /* set the multicast bit to prevent conflicts with real cards */
586 a[0] = (rand() & 0xff) | 0x80;
587 a[1] = rand() & 0xff;
588 a[2] = rand() & 0xff;
589 a[3] = rand() & 0xff;
590 a[4] = rand() & 0xff;
591 a[5] = rand() & 0xff;
592 } else {
593 /* loop through the interfaces, looking for a valid one */
594 n = ifc.ifc_len;
595 for (i = 0; i < n; i+= ifreq_size(*ifr) ) {
596 ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i);
597 strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
598 /* try to get the address for this interface */
599 # ifdef SIOCGIFHWADDR
600 if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
601 continue;
602 memcpy(a, (unsigned char *)&ifr.ifr_hwaddr.sa_data, 6);
603 # else
604 # ifdef SIOCGENADDR
605 if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
606 continue;
607 memcpy(a, (unsigned char *) ifr.ifr_enaddr, 6);
608 # else
609 /* XXX we don't have a way of getting the hardware address */
610 close(sd);
611 a[0] = 0;
612 break;
613 # endif /* SIOCGENADDR */
614 # endif /* SIOCGIFHWADDR */
615 /* make sure it's not blank */
616 if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5])
617 continue;
619 goto valid_address;
621 /* if we didn't find a valid address, make a random one */
622 /* once again, set multicast bit to avoid conflicts */
623 a[0] = (rand() & 0xff) | 0x80;
624 a[1] = rand() & 0xff;
625 a[2] = rand() & 0xff;
626 a[3] = rand() & 0xff;
627 a[4] = rand() & 0xff;
628 a[5] = rand() & 0xff;
630 valid_address:
631 close(sd);
634 #else
635 /* no networking info, so generate a random address */
636 a[0] = (rand() & 0xff) | 0x80;
637 a[1] = rand() & 0xff;
638 a[2] = rand() & 0xff;
639 a[3] = rand() & 0xff;
640 a[4] = rand() & 0xff;
641 a[5] = rand() & 0xff;
642 #endif /* HAVE_NET_IF_H */
643 has_init = 1;
646 /* generate time element of GUID */
648 /* Assume that the gettimeofday() has microsecond granularity */
649 #define MAX_ADJUSTMENT 10
651 try_again:
652 gettimeofday(&tv, 0);
653 if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
654 clock_seq = ((rand() & 0xff) << 8) + (rand() & 0xff);
655 clock_seq &= 0x1FFF;
656 last = tv;
657 last.tv_sec--;
659 if ((tv.tv_sec < last.tv_sec) ||
660 ((tv.tv_sec == last.tv_sec) &&
661 (tv.tv_usec < last.tv_usec))) {
662 clock_seq = (clock_seq+1) & 0x1FFF;
663 adjustment = 0;
664 } else if ((tv.tv_sec == last.tv_sec) &&
665 (tv.tv_usec == last.tv_usec)) {
666 if (adjustment >= MAX_ADJUSTMENT)
667 goto try_again;
668 adjustment++;
669 } else
670 adjustment = 0;
672 clock_reg = tv.tv_usec*10 + adjustment;
673 clock_reg += ((unsigned long long) tv.tv_sec)*10000000;
674 clock_reg += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
676 clock_high = clock_reg >> 32;
677 clock_low = clock_reg;
678 temp_clock_seq = clock_seq | 0x8000;
679 temp_clock_mid = (UINT16)clock_high;
680 temp_clock_hi_and_version = (clock_high >> 16) | 0x1000;
682 /* pack the information into the GUID structure */
684 ((unsigned char*)&pguid->Data1)[3] = (unsigned char)clock_low;
685 clock_low >>= 8;
686 ((unsigned char*)&pguid->Data1)[2] = (unsigned char)clock_low;
687 clock_low >>= 8;
688 ((unsigned char*)&pguid->Data1)[1] = (unsigned char)clock_low;
689 clock_low >>= 8;
690 ((unsigned char*)&pguid->Data1)[0] = (unsigned char)clock_low;
692 ((unsigned char*)&pguid->Data2)[1] = (unsigned char)temp_clock_mid;
693 temp_clock_mid >>= 8;
694 ((unsigned char*)&pguid->Data2)[0] = (unsigned char)temp_clock_mid;
696 ((unsigned char*)&pguid->Data3)[1] = (unsigned char)temp_clock_hi_and_version;
697 temp_clock_hi_and_version >>= 8;
698 ((unsigned char*)&pguid->Data3)[0] = (unsigned char)temp_clock_hi_and_version;
700 ((unsigned char*)pguid->Data4)[1] = (unsigned char)temp_clock_seq;
701 temp_clock_seq >>= 8;
702 ((unsigned char*)pguid->Data4)[0] = (unsigned char)temp_clock_seq;
704 ((unsigned char*)pguid->Data4)[2] = a[0];
705 ((unsigned char*)pguid->Data4)[3] = a[1];
706 ((unsigned char*)pguid->Data4)[4] = a[2];
707 ((unsigned char*)pguid->Data4)[5] = a[3];
708 ((unsigned char*)pguid->Data4)[6] = a[4];
709 ((unsigned char*)pguid->Data4)[7] = a[5];
711 TRACE("%p", pguid);
713 return S_OK;
716 /******************************************************************************
717 * CLSIDFromString32 [OLE32.3]
718 * Converts a unique identifier from it's string representation into
719 * the GUID struct.
720 * RETURNS
721 * the converted GUID
723 HRESULT WINAPI CLSIDFromString(
724 LPCOLESTR idstr, /* [in] string representation of GUID */
725 CLSID *id /* [out] GUID represented by above string */
727 LPOLESTR16 xid = HEAP_strdupWtoA(GetProcessHeap(),0,idstr);
728 OLESTATUS ret = CLSIDFromString16(xid,id);
730 HeapFree(GetProcessHeap(),0,xid);
731 return ret;
734 /******************************************************************************
735 * WINE_StringFromCLSID [Internal]
736 * Converts a GUID into the respective string representation.
738 * NOTES
740 * RETURNS
741 * the string representation and OLESTATUS
743 HRESULT WINE_StringFromCLSID(
744 const CLSID *id, /* [in] GUID to be converted */
745 LPSTR idstr /* [out] pointer to buffer to contain converted guid */
747 static const char *hex = "0123456789ABCDEF";
748 char *s;
749 int i;
751 if (!id)
752 { ERR("called with id=Null\n");
753 *idstr = 0x00;
754 return E_FAIL;
757 sprintf(idstr, "{%08lX-%04X-%04X-%02X%02X-",
758 id->Data1, id->Data2, id->Data3,
759 id->Data4[0], id->Data4[1]);
760 s = &idstr[25];
762 /* 6 hex bytes */
763 for (i = 2; i < 8; i++) {
764 *s++ = hex[id->Data4[i]>>4];
765 *s++ = hex[id->Data4[i] & 0xf];
768 *s++ = '}';
769 *s++ = '\0';
771 TRACE("%p->%s\n", id, idstr);
773 return OLE_OK;
776 /******************************************************************************
777 * StringFromCLSID16 [COMPOBJ.19]
778 * Converts a GUID into the respective string representation.
779 * The target string is allocated using the OLE IMalloc.
780 * RETURNS
781 * the string representation and OLESTATUS
783 HRESULT WINAPI StringFromCLSID16(
784 REFCLSID id, /* [in] the GUID to be converted */
785 LPOLESTR16 *idstr /* [out] a pointer to a to-be-allocated segmented pointer pointing to the resulting string */
788 LPMALLOC16 mllc;
789 OLESTATUS ret;
790 DWORD args[2];
792 ret = CoGetMalloc16(0,&mllc);
793 if (ret) return ret;
795 args[0] = (DWORD)mllc;
796 args[1] = 40;
798 /* No need for a Callback entry, we have WOWCallback16Ex which does
799 * everything we need.
801 if (!WOWCallback16Ex(
802 (DWORD)((ICOM_VTABLE(IMalloc16)*)PTR_SEG_TO_LIN(
803 ICOM_VTBL(((LPMALLOC16)PTR_SEG_TO_LIN(mllc))))
804 )->fnAlloc,
805 WCB16_CDECL,
806 2*sizeof(DWORD),
807 (LPVOID)args,
808 (LPDWORD)idstr
809 )) {
810 WARN("CallTo16 IMalloc16 failed\n");
811 return E_FAIL;
813 return WINE_StringFromCLSID(id,PTR_SEG_TO_LIN(*idstr));
816 /******************************************************************************
817 * StringFromCLSID32 [OLE32.151]
818 * Converts a GUID into the respective string representation.
819 * The target string is allocated using the OLE IMalloc.
820 * RETURNS
821 * the string representation and OLESTATUS
823 HRESULT WINAPI StringFromCLSID(
824 REFCLSID id, /* [in] the GUID to be converted */
825 LPOLESTR *idstr /* [out] a pointer to a to-be-allocated pointer pointing to the resulting string */
827 char buf[80];
828 OLESTATUS ret;
829 LPMALLOC mllc;
831 if ((ret=CoGetMalloc(0,&mllc)))
832 return ret;
834 ret=WINE_StringFromCLSID(id,buf);
835 if (!ret) {
836 *idstr = IMalloc_Alloc(mllc,strlen(buf)*2+2);
837 lstrcpyAtoW(*idstr,buf);
839 return ret;
842 /******************************************************************************
843 * StringFromGUID2 [COMPOBJ.76] [OLE32.152]
845 * Converts a global unique identifier into a string of an API-
846 * specified fixed format. (The usual {.....} stuff.)
848 * RETURNS
849 * The (UNICODE) string representation of the GUID in 'str'
850 * The length of the resulting string, 0 if there was any problem.
852 INT WINAPI
853 StringFromGUID2(REFGUID id, LPOLESTR str, INT cmax)
855 char xguid[80];
857 if (WINE_StringFromCLSID(id,xguid))
858 return 0;
859 if (strlen(xguid)>=cmax)
860 return 0;
861 lstrcpyAtoW(str,xguid);
862 return strlen(xguid);
865 /******************************************************************************
866 * ProgIDFromCLSID [OLE32.133]
867 * Converts a class id into the respective Program ID. (By using a registry lookup)
868 * RETURNS S_OK on success
869 * riid associated with the progid
872 HRESULT WINAPI ProgIDFromCLSID(
873 REFCLSID clsid, /* [in] class id as found in registry */
874 LPOLESTR *lplpszProgID/* [out] associated Prog ID */
877 char strCLSID[50], *buf, *buf2;
878 DWORD buf2len;
879 HKEY xhkey;
880 LPMALLOC mllc;
881 HRESULT ret = S_OK;
883 WINE_StringFromCLSID(clsid, strCLSID);
885 buf = HeapAlloc(GetProcessHeap(), 0, strlen(strCLSID)+14);
886 sprintf(buf,"CLSID\\%s\\ProgID", strCLSID);
887 if (RegOpenKeyA(HKEY_CLASSES_ROOT, buf, &xhkey))
888 ret = REGDB_E_CLASSNOTREG;
890 HeapFree(GetProcessHeap(), 0, buf);
892 if (ret == S_OK)
894 buf2 = HeapAlloc(GetProcessHeap(), 0, 255);
895 buf2len = 255;
896 if (RegQueryValueA(xhkey, NULL, buf2, &buf2len))
897 ret = REGDB_E_CLASSNOTREG;
899 if (ret == S_OK)
901 if (CoGetMalloc(0,&mllc))
902 ret = E_OUTOFMEMORY;
903 else
905 *lplpszProgID = IMalloc_Alloc(mllc, (buf2len+1)*2);
906 lstrcpyAtoW(*lplpszProgID, buf2);
909 HeapFree(GetProcessHeap(), 0, buf2);
912 RegCloseKey(xhkey);
913 return ret;
916 /******************************************************************************
917 * CLSIDFromProgID16 [COMPOBJ.61]
918 * Converts a program id into the respective GUID. (By using a registry lookup)
919 * RETURNS
920 * riid associated with the progid
922 HRESULT WINAPI CLSIDFromProgID16(
923 LPCOLESTR16 progid, /* [in] program id as found in registry */
924 LPCLSID riid /* [out] associated CLSID */
926 char *buf,buf2[80];
927 DWORD buf2len;
928 HRESULT err;
929 HKEY xhkey;
931 buf = HeapAlloc(GetProcessHeap(),0,strlen(progid)+8);
932 sprintf(buf,"%s\\CLSID",progid);
933 if ((err=RegOpenKeyA(HKEY_CLASSES_ROOT,buf,&xhkey))) {
934 HeapFree(GetProcessHeap(),0,buf);
935 return OLE_ERROR_GENERIC;
937 HeapFree(GetProcessHeap(),0,buf);
938 buf2len = sizeof(buf2);
939 if ((err=RegQueryValueA(xhkey,NULL,buf2,&buf2len))) {
940 RegCloseKey(xhkey);
941 return OLE_ERROR_GENERIC;
943 RegCloseKey(xhkey);
944 return CLSIDFromString16(buf2,riid);
947 /******************************************************************************
948 * CLSIDFromProgID32 [OLE32.2]
949 * Converts a program id into the respective GUID. (By using a registry lookup)
950 * RETURNS
951 * riid associated with the progid
953 HRESULT WINAPI CLSIDFromProgID(
954 LPCOLESTR progid, /* [in] program id as found in registry */
955 LPCLSID riid /* [out] associated CLSID */
957 LPOLESTR16 pid = HEAP_strdupWtoA(GetProcessHeap(),0,progid);
958 OLESTATUS ret = CLSIDFromProgID16(pid,riid);
960 HeapFree(GetProcessHeap(),0,pid);
961 return ret;
964 /************************************************************************************************
965 * OleSaveToStream
967 * This function write a CLSID on stream
969 HRESULT WINAPI WriteClassStm(IStream *pStm,REFCLSID rclsid)
971 TRACE("(%p,%p)\n",pStm,rclsid);
973 if (rclsid==NULL)
974 return E_INVALIDARG;
976 return IStream_Write(pStm,rclsid,sizeof(CLSID),NULL);
979 /************************************************************************************************
980 * OleSaveToStream
982 * This function read a CLSID from a stream
984 HRESULT WINAPI ReadClassStm(IStream *pStm,REFCLSID rclsid)
986 ULONG nbByte;
987 HRESULT res;
989 TRACE("(%p,%p)\n",pStm,rclsid);
991 if (rclsid==NULL)
992 return E_INVALIDARG;
994 res = IStream_Read(pStm,(void*)rclsid,sizeof(CLSID),&nbByte);
996 if (FAILED(res))
997 return res;
999 if (nbByte != sizeof(CLSID))
1000 return S_FALSE;
1001 else
1002 return S_OK;
1005 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
1006 /***********************************************************************
1007 * LookupETask (COMPOBJ.94)
1009 OLESTATUS WINAPI LookupETask16(HTASK16 *hTask,LPVOID p) {
1010 FIXME("(%p,%p),stub!\n",hTask,p);
1011 if ((*hTask = GetCurrentTask()) == hETask) {
1012 memcpy(p, Table_ETask, sizeof(Table_ETask));
1014 return 0;
1017 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
1018 /***********************************************************************
1019 * SetETask (COMPOBJ.95)
1021 OLESTATUS WINAPI SetETask16(HTASK16 hTask, LPVOID p) {
1022 FIXME("(%04x,%p),stub!\n",hTask,p);
1023 hETask = hTask;
1024 return 0;
1027 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
1028 /***********************************************************************
1029 * CallObjectInWOW (COMPOBJ.201)
1031 OLESTATUS WINAPI CallObjectInWOW(LPVOID p1,LPVOID p2) {
1032 FIXME("(%p,%p),stub!\n",p1,p2);
1033 return 0;
1036 /******************************************************************************
1037 * CoRegisterClassObject16 [COMPOBJ.5]
1039 * Don't know where it registers it ...
1041 HRESULT WINAPI CoRegisterClassObject16(
1042 REFCLSID rclsid,
1043 LPUNKNOWN pUnk,
1044 DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
1045 DWORD flags, /* [in] REGCLS flags indicating how connections are made */
1046 LPDWORD lpdwRegister
1048 char buf[80];
1050 WINE_StringFromCLSID(rclsid,buf);
1052 FIXME("(%s,%p,0x%08lx,0x%08lx,%p),stub\n",
1053 buf,pUnk,dwClsContext,flags,lpdwRegister
1055 return 0;
1059 /******************************************************************************
1060 * CoRevokeClassObject16 [COMPOBJ.6]
1063 HRESULT WINAPI CoRevokeClassObject16(DWORD dwRegister /* token on class obj */)
1065 FIXME("(0x%08lx),stub!\n", dwRegister);
1066 return 0;
1070 /***
1071 * COM_GetRegisteredClassObject
1073 * This internal method is used to scan the registered class list to
1074 * find a class object.
1076 * Params:
1077 * rclsid Class ID of the class to find.
1078 * dwClsContext Class context to match.
1079 * ppv [out] returns a pointer to the class object. Complying
1080 * to normal COM usage, this method will increase the
1081 * reference count on this object.
1083 static HRESULT COM_GetRegisteredClassObject(
1084 REFCLSID rclsid,
1085 DWORD dwClsContext,
1086 LPUNKNOWN* ppUnk)
1088 RegisteredClass* curClass;
1091 * Sanity check
1093 assert(ppUnk!=0);
1096 * Iterate through the whole list and try to match the class ID.
1098 curClass = firstRegisteredClass;
1100 while (curClass != 0)
1103 * Check if we have a match on the class ID.
1105 if (IsEqualGUID32(&(curClass->classIdentifier), rclsid))
1108 * Since we don't do out-of process or DCOM just right away, let's ignore the
1109 * class context.
1113 * We have a match, return the pointer to the class object.
1115 *ppUnk = curClass->classObject;
1117 IUnknown_AddRef(curClass->classObject);
1119 return S_OK;
1123 * Step to the next class in the list.
1125 curClass = curClass->nextClass;
1129 * If we get to here, we haven't found our class.
1131 return S_FALSE;
1134 /******************************************************************************
1135 * CoRegisterClassObject32 [OLE32.36]
1137 * This method will register the class object for a given class ID.
1139 * See the Windows documentation for more details.
1141 HRESULT WINAPI CoRegisterClassObject(
1142 REFCLSID rclsid,
1143 LPUNKNOWN pUnk,
1144 DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
1145 DWORD flags, /* [in] REGCLS flags indicating how connections are made */
1146 LPDWORD lpdwRegister
1149 RegisteredClass* newClass;
1150 LPUNKNOWN foundObject;
1151 HRESULT hr;
1152 char buf[80];
1154 WINE_StringFromCLSID(rclsid,buf);
1156 TRACE("(%s,%p,0x%08lx,0x%08lx,%p)\n",
1157 buf,pUnk,dwClsContext,flags,lpdwRegister);
1160 * Perform a sanity check on the parameters
1162 if ( (lpdwRegister==0) || (pUnk==0) )
1164 return E_INVALIDARG;
1168 * Initialize the cookie (out parameter)
1170 *lpdwRegister = 0;
1173 * First, check if the class is already registered.
1174 * If it is, this should cause an error.
1176 hr = COM_GetRegisteredClassObject(rclsid, dwClsContext, &foundObject);
1178 if (hr == S_OK)
1181 * The COM_GetRegisteredClassObject increased the reference count on the
1182 * object so it has to be released.
1184 IUnknown_Release(foundObject);
1186 return CO_E_OBJISREG;
1190 * If it is not registered, we must create a new entry for this class and
1191 * append it to the registered class list.
1192 * We use the address of the chain node as the cookie since we are sure it's
1193 * unique.
1195 newClass = HeapAlloc(GetProcessHeap(), 0, sizeof(RegisteredClass));
1198 * Initialize the node.
1200 newClass->classIdentifier = *rclsid;
1201 newClass->runContext = dwClsContext;
1202 newClass->connectFlags = flags;
1203 newClass->dwCookie = (DWORD)newClass;
1204 newClass->nextClass = firstRegisteredClass;
1207 * Since we're making a copy of the object pointer, we have to increase it's
1208 * reference count.
1210 newClass->classObject = pUnk;
1211 IUnknown_AddRef(newClass->classObject);
1213 firstRegisteredClass = newClass;
1216 * Assign the out parameter (cookie)
1218 *lpdwRegister = newClass->dwCookie;
1221 * We're successful Yippee!
1223 return S_OK;
1226 /***********************************************************************
1227 * CoRevokeClassObject32 [OLE32.40]
1229 * This method will remove a class object from the class registry
1231 * See the Windows documentation for more details.
1233 HRESULT WINAPI CoRevokeClassObject(
1234 DWORD dwRegister)
1236 RegisteredClass** prevClassLink;
1237 RegisteredClass* curClass;
1239 TRACE("(%08lx)\n",dwRegister);
1242 * Iterate through the whole list and try to match the cookie.
1244 curClass = firstRegisteredClass;
1245 prevClassLink = &firstRegisteredClass;
1247 while (curClass != 0)
1250 * Check if we have a match on the cookie.
1252 if (curClass->dwCookie == dwRegister)
1255 * Remove the class from the chain.
1257 *prevClassLink = curClass->nextClass;
1260 * Release the reference to the class object.
1262 IUnknown_Release(curClass->classObject);
1265 * Free the memory used by the chain node.
1267 HeapFree(GetProcessHeap(), 0, curClass);
1269 return S_OK;
1273 * Step to the next class in the list.
1275 prevClassLink = &(curClass->nextClass);
1276 curClass = curClass->nextClass;
1280 * If we get to here, we haven't found our class.
1282 return E_INVALIDARG;
1285 /***********************************************************************
1286 * CoGetClassObject [COMPOBJ.7]
1288 HRESULT WINAPI CoGetClassObject(REFCLSID rclsid, DWORD dwClsContext,
1289 LPVOID pvReserved, REFIID iid, LPVOID *ppv)
1291 LPUNKNOWN regClassObject;
1292 char xclsid[50],xiid[50];
1293 HRESULT hres = E_UNEXPECTED;
1295 char dllName[MAX_PATH+1];
1296 DWORD dllNameLen = sizeof(dllName);
1297 HINSTANCE hLibrary;
1298 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid,
1299 REFIID iid, LPVOID *ppv);
1300 DllGetClassObjectFunc DllGetClassObject;
1302 WINE_StringFromCLSID((LPCLSID)rclsid,xclsid);
1303 WINE_StringFromCLSID((LPCLSID)iid,xiid);
1304 TRACE("\n\tCLSID:\t%s,\n\tIID:\t%s\n",xclsid,xiid);
1307 * First, try and see if we can't match the class ID with one of the
1308 * registered classes.
1310 if (S_OK == COM_GetRegisteredClassObject(rclsid, dwClsContext, &regClassObject))
1313 * Get the required interface from the retrieved pointer.
1315 hres = IUnknown_QueryInterface(regClassObject, iid, ppv);
1318 * Since QI got another reference on the pointer, we want to release the
1319 * one we already have. If QI was unsuccessful, this will release the object. This
1320 * is good since we are not returning it in the "out" parameter.
1322 IUnknown_Release(regClassObject);
1324 return hres;
1327 /* out of process and remote servers not supported yet */
1328 if (((CLSCTX_LOCAL_SERVER|CLSCTX_REMOTE_SERVER) & dwClsContext)
1329 && !((CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER) & dwClsContext)){
1330 FIXME("CLSCTX_LOCAL_SERVER and CLSCTX_REMOTE_SERVER not supported!\n");
1331 return E_ACCESSDENIED;
1334 if ((CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER) & dwClsContext) {
1335 HKEY CLSIDkey,key;
1337 /* lookup CLSID in registry key HKCR/CLSID */
1338 hres = RegOpenKeyExA(HKEY_CLASSES_ROOT, "CLSID", 0,
1339 KEY_READ, &CLSIDkey);
1341 if (hres != ERROR_SUCCESS)
1342 return REGDB_E_READREGDB;
1343 hres = RegOpenKeyExA(CLSIDkey,xclsid,0,KEY_QUERY_VALUE,&key);
1344 if (hres != ERROR_SUCCESS) {
1345 RegCloseKey(CLSIDkey);
1346 return REGDB_E_CLASSNOTREG;
1348 hres = RegQueryValueA(key, "InprocServer32", dllName, &dllNameLen);
1349 RegCloseKey(key);
1350 RegCloseKey(CLSIDkey);
1351 if (hres != ERROR_SUCCESS)
1352 return REGDB_E_READREGDB;
1353 TRACE("found InprocServer32 dll %s\n", dllName);
1355 /* open dll, call DllGetClassFactory */
1356 hLibrary = CoLoadLibrary(dllName, TRUE);
1357 if (hLibrary == 0) {
1358 TRACE("couldn't load InprocServer32 dll %s\n", dllName);
1359 return E_ACCESSDENIED; /* or should this be CO_E_DLLNOTFOUND? */
1361 DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress(hLibrary, "DllGetClassObject");
1362 if (!DllGetClassObject) {
1363 /* not sure if this should be called here CoFreeLibrary(hLibrary);*/
1364 TRACE("couldn't find function DllGetClassObject in %s\n", dllName);
1365 return E_ACCESSDENIED;
1369 * Ask the DLL for it's class object. (there was a note here about class
1370 * factories but this is good.
1372 return DllGetClassObject(rclsid, iid, ppv);
1374 return hres;
1377 /****************************************************************************************
1378 * GetClassFile
1380 * This function supplies the CLSID associated with the given filename.
1382 HRESULT WINAPI GetClassFile(LPOLESTR filePathName,CLSID *pclsid)
1384 IStorage *pstg=0;
1385 HRESULT res;
1386 int nbElm=0,length=0,i=0;
1387 LONG sizeProgId=20;
1388 LPOLESTR *pathDec=0,absFile=0,progId=0;
1389 WCHAR extention[100]={0};
1391 TRACE("()\n");
1393 /* if the file contain a storage object the return the CLSID writen by IStorage_SetClass method*/
1394 if((StgIsStorageFile(filePathName))==S_OK){
1396 res=StgOpenStorage(filePathName,NULL,STGM_READ | STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
1398 if (SUCCEEDED(res))
1399 res=ReadClassStg(pstg,pclsid);
1401 IStorage_Release(pstg);
1403 return res;
1405 /* if the file is not a storage object then attemps to match various bits in the file against a
1406 pattern in the registry. this case is not frequently used ! so I present only the psodocode for
1407 this case
1409 for(i=0;i<nFileTypes;i++)
1411 for(i=0;j<nPatternsForType;j++){
1413 PATTERN pat;
1414 HANDLE hFile;
1416 pat=ReadPatternFromRegistry(i,j);
1417 hFile=CreateFileW(filePathName,,,,,,hFile);
1418 SetFilePosition(hFile,pat.offset);
1419 ReadFile(hFile,buf,pat.size,NULL,NULL);
1420 if (memcmp(buf&pat.mask,pat.pattern.pat.size)==0){
1422 *pclsid=ReadCLSIDFromRegistry(i);
1423 return S_OK;
1428 /* if the obove strategies fail then search for the extension key in the registry */
1430 /* get the last element (absolute file) in the path name */
1431 nbElm=FileMonikerImpl_DecomposePath(filePathName,&pathDec);
1432 absFile=pathDec[nbElm-1];
1434 /* failed if the path represente a directory and not an absolute file name*/
1435 if (lstrcmpW(absFile,(LPOLESTR)"\\"))
1436 return MK_E_INVALIDEXTENSION;
1438 /* get the extension of the file */
1439 length=lstrlenW(absFile);
1440 for(i=length-1; ( (i>=0) && (extention[i]=absFile[i]) );i--);
1442 /* get the progId associated to the extension */
1443 progId=CoTaskMemAlloc(sizeProgId);
1445 res=RegQueryValueW(HKEY_CLASSES_ROOT,extention,progId,&sizeProgId);
1447 if (res==ERROR_MORE_DATA){
1449 CoTaskMemRealloc(progId,sizeProgId);
1451 res=RegQueryValueW(HKEY_CLASSES_ROOT,extention,progId,&sizeProgId);
1453 if (res==ERROR_SUCCESS)
1454 /* return the clsid associated to the progId */
1455 res= CLSIDFromProgID(progId,pclsid);
1457 for(i=0; pathDec[i]!=NULL;i++)
1458 CoTaskMemFree(pathDec[i]);
1459 CoTaskMemFree(pathDec);
1461 CoTaskMemFree(progId);
1463 if (res==ERROR_SUCCESS)
1464 return res;
1466 return MK_E_INVALIDEXTENSION;
1468 /******************************************************************************
1469 * CoRegisterMessageFilter16 [COMPOBJ.27]
1471 HRESULT WINAPI CoRegisterMessageFilter16(
1472 LPMESSAGEFILTER lpMessageFilter,
1473 LPMESSAGEFILTER *lplpMessageFilter
1475 FIXME("(%p,%p),stub!\n",lpMessageFilter,lplpMessageFilter);
1476 return 0;
1479 /***********************************************************************
1480 * CoCreateInstance [COMPOBJ.13, OLE32.7]
1482 HRESULT WINAPI CoCreateInstance(
1483 REFCLSID rclsid,
1484 LPUNKNOWN pUnkOuter,
1485 DWORD dwClsContext,
1486 REFIID iid,
1487 LPVOID *ppv)
1489 HRESULT hres;
1490 LPCLASSFACTORY lpclf = 0;
1493 * Sanity check
1495 if (ppv==0)
1496 return E_POINTER;
1499 * Initialize the "out" parameter
1501 *ppv = 0;
1504 * Get a class factory to construct the object we want.
1506 hres = CoGetClassObject(rclsid,
1507 dwClsContext,
1508 NULL,
1509 &IID_IClassFactory,
1510 (LPVOID)&lpclf);
1512 if (FAILED(hres))
1513 return hres;
1516 * Create the object and don't forget to release the factory
1518 hres = IClassFactory_CreateInstance(lpclf, pUnkOuter, iid, ppv);
1519 IClassFactory_Release(lpclf);
1521 return hres;
1524 /***********************************************************************
1525 * CoCreateInstanceEx [OLE32.165]
1527 HRESULT WINAPI CoCreateInstanceEx(
1528 REFCLSID rclsid,
1529 LPUNKNOWN pUnkOuter,
1530 DWORD dwClsContext,
1531 COSERVERINFO* pServerInfo,
1532 ULONG cmq,
1533 MULTI_QI* pResults)
1535 IUnknown* pUnk = NULL;
1536 HRESULT hr;
1537 ULONG index;
1538 int successCount = 0;
1541 * Sanity check
1543 if ( (cmq==0) || (pResults==NULL))
1544 return E_INVALIDARG;
1546 if (pServerInfo!=NULL)
1547 FIXME("() non-NULL pServerInfo not supported!\n");
1550 * Initialize all the "out" parameters.
1552 for (index = 0; index < cmq; index++)
1554 pResults[index].pItf = NULL;
1555 pResults[index].hr = E_NOINTERFACE;
1559 * Get the object and get it's IUnknown pointer.
1561 hr = CoCreateInstance(rclsid,
1562 pUnkOuter,
1563 dwClsContext,
1564 &IID_IUnknown,
1565 (VOID**)&pUnk);
1567 if (hr)
1568 return hr;
1571 * Then, query for all the interfaces requested.
1573 for (index = 0; index < cmq; index++)
1575 pResults[index].hr = IUnknown_QueryInterface(pUnk,
1576 pResults[index].pIID,
1577 (VOID**)&(pResults[index].pItf));
1579 if (pResults[index].hr == S_OK)
1580 successCount++;
1584 * Release our temporary unknown pointer.
1586 IUnknown_Release(pUnk);
1588 if (successCount == 0)
1589 return E_NOINTERFACE;
1591 if (successCount!=cmq)
1592 return CO_S_NOTALLINTERFACES;
1594 return S_OK;
1597 /***********************************************************************
1598 * CoFreeLibrary [COMPOBJ.13]
1600 void WINAPI CoFreeLibrary(HINSTANCE hLibrary)
1602 OpenDll *ptr, *prev;
1603 OpenDll *tmp;
1605 /* lookup library in linked list */
1606 prev = NULL;
1607 for (ptr = openDllList; ptr != NULL; ptr=ptr->next) {
1608 if (ptr->hLibrary == hLibrary) {
1609 break;
1611 prev = ptr;
1614 if (ptr == NULL) {
1615 /* shouldn't happen if user passed in a valid hLibrary */
1616 return;
1618 /* assert: ptr points to the library entry to free */
1620 /* free library and remove node from list */
1621 FreeLibrary(hLibrary);
1622 if (ptr == openDllList) {
1623 tmp = openDllList->next;
1624 HeapFree(GetProcessHeap(), 0, openDllList->DllName);
1625 HeapFree(GetProcessHeap(), 0, openDllList);
1626 openDllList = tmp;
1627 } else {
1628 tmp = ptr->next;
1629 HeapFree(GetProcessHeap(), 0, ptr->DllName);
1630 HeapFree(GetProcessHeap(), 0, ptr);
1631 prev->next = tmp;
1637 /***********************************************************************
1638 * CoFreeAllLibraries [COMPOBJ.12]
1640 void WINAPI CoFreeAllLibraries(void)
1642 OpenDll *ptr, *tmp;
1644 for (ptr = openDllList; ptr != NULL; ) {
1645 tmp=ptr->next;
1646 CoFreeLibrary(ptr->hLibrary);
1647 ptr = tmp;
1653 /***********************************************************************
1654 * CoFreeUnusedLibraries [COMPOBJ.17]
1656 void WINAPI CoFreeUnusedLibraries(void)
1658 OpenDll *ptr, *tmp;
1659 typedef HRESULT(*DllCanUnloadNowFunc)(void);
1660 DllCanUnloadNowFunc DllCanUnloadNow;
1662 for (ptr = openDllList; ptr != NULL; ) {
1663 DllCanUnloadNow = (DllCanUnloadNowFunc)
1664 GetProcAddress(ptr->hLibrary, "DllCanUnloadNow");
1666 if ( (DllCanUnloadNow != NULL) &&
1667 (DllCanUnloadNow() == S_OK) ) {
1668 tmp=ptr->next;
1669 CoFreeLibrary(ptr->hLibrary);
1670 ptr = tmp;
1671 } else {
1672 ptr=ptr->next;
1677 /***********************************************************************
1678 * CoFileTimeNow [COMPOBJ.82, OLE32.10]
1679 * RETURNS
1680 * the current system time in lpFileTime
1682 HRESULT WINAPI CoFileTimeNow(
1683 FILETIME *lpFileTime /* [out] the current time */
1685 DOSFS_UnixTimeToFileTime(time(NULL), lpFileTime, 0);
1686 return S_OK;
1689 /***********************************************************************
1690 * CoTaskMemAlloc (OLE32.43)
1691 * RETURNS
1692 * pointer to newly allocated block
1694 LPVOID WINAPI CoTaskMemAlloc(
1695 ULONG size /* [in] size of memoryblock to be allocated */
1697 LPMALLOC lpmalloc;
1698 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1700 if (FAILED(ret))
1701 return NULL;
1703 return IMalloc_Alloc(lpmalloc,size);
1705 /***********************************************************************
1706 * CoTaskMemFree (OLE32.44)
1708 VOID WINAPI CoTaskMemFree(
1709 LPVOID ptr /* [in] pointer to be freed */
1711 LPMALLOC lpmalloc;
1712 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1714 if (FAILED(ret))
1715 return;
1717 IMalloc_Free(lpmalloc, ptr);
1720 /***********************************************************************
1721 * CoTaskMemRealloc (OLE32.45)
1722 * RETURNS
1723 * pointer to newly allocated block
1725 LPVOID WINAPI CoTaskMemRealloc(
1726 LPVOID pvOld,
1727 ULONG size) /* [in] size of memoryblock to be allocated */
1729 LPMALLOC lpmalloc;
1730 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1732 if (FAILED(ret))
1733 return NULL;
1735 return IMalloc_Realloc(lpmalloc, pvOld, size);
1738 /***********************************************************************
1739 * CoLoadLibrary (OLE32.30)
1741 HINSTANCE WINAPI CoLoadLibrary(LPOLESTR16 lpszLibName, BOOL bAutoFree)
1743 HINSTANCE hLibrary;
1744 OpenDll *ptr;
1745 OpenDll *tmp;
1747 TRACE("CoLoadLibrary(%p, %d\n", lpszLibName, bAutoFree);
1749 hLibrary = LoadLibraryExA(lpszLibName, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
1751 if (!bAutoFree)
1752 return hLibrary;
1754 if (openDllList == NULL) {
1755 /* empty list -- add first node */
1756 openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
1757 openDllList->DllName = HEAP_strdupA(GetProcessHeap(), 0, lpszLibName);
1758 openDllList->hLibrary = hLibrary;
1759 openDllList->next = NULL;
1760 } else {
1761 /* search for this dll */
1762 int found = FALSE;
1763 for (ptr = openDllList; ptr->next != NULL; ptr=ptr->next) {
1764 if (ptr->hLibrary == hLibrary) {
1765 found = TRUE;
1766 break;
1769 if (!found) {
1770 /* dll not found, add it */
1771 tmp = openDllList;
1772 openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
1773 openDllList->DllName = HEAP_strdupA(GetProcessHeap(), 0, lpszLibName);
1774 openDllList->hLibrary = hLibrary;
1775 openDllList->next = tmp;
1779 return hLibrary;
1782 /***********************************************************************
1783 * CoInitializeWOW (OLE32.27)
1785 HRESULT WINAPI CoInitializeWOW(DWORD x,DWORD y) {
1786 FIXME("(0x%08lx,0x%08lx),stub!\n",x,y);
1787 return 0;
1790 /******************************************************************************
1791 * CoLockObjectExternal16 [COMPOBJ.63]
1793 HRESULT WINAPI CoLockObjectExternal16(
1794 LPUNKNOWN pUnk, /* [in] object to be locked */
1795 BOOL16 fLock, /* [in] do lock */
1796 BOOL16 fLastUnlockReleases /* [in] ? */
1798 FIXME("(%p,%d,%d),stub!\n",pUnk,fLock,fLastUnlockReleases);
1799 return S_OK;
1802 /******************************************************************************
1803 * CoLockObjectExternal32 [OLE32.31]
1805 HRESULT WINAPI CoLockObjectExternal(
1806 LPUNKNOWN pUnk, /* [in] object to be locked */
1807 BOOL fLock, /* [in] do lock */
1808 BOOL fLastUnlockReleases) /* [in] unlock all */
1811 if (fLock)
1814 * Increment the external lock coutner, COM_ExternalLockAddRef also
1815 * increment the object's internal lock counter.
1817 COM_ExternalLockAddRef( pUnk);
1819 else
1822 * Decrement the external lock coutner, COM_ExternalLockRelease also
1823 * decrement the object's internal lock counter.
1825 COM_ExternalLockRelease( pUnk, fLastUnlockReleases);
1828 return S_OK;
1831 /***********************************************************************
1832 * CoGetState16 [COMPOBJ.115]
1834 HRESULT WINAPI CoGetState16(LPDWORD state)
1836 FIXME("(%p),stub!\n", state);
1837 *state = 0;
1838 return S_OK;
1840 /***********************************************************************
1841 * CoSetState32 [COM32.42]
1843 HRESULT WINAPI CoSetState(LPDWORD state)
1845 FIXME("(%p),stub!\n", state);
1846 if (state) *state = 0;
1847 return S_OK;
1851 /***********************************************************************
1852 * DllGetClassObject [OLE32.63]
1854 HRESULT WINAPI OLE32_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
1856 char xclsid[50],xiid[50];
1857 WINE_StringFromCLSID((LPCLSID)rclsid,xclsid);
1858 WINE_StringFromCLSID((LPCLSID)iid,xiid);
1859 FIXME("\n\tCLSID:\t%s,\n\tIID:\t%s\n",xclsid,xiid);
1861 *ppv = NULL;
1863 return CLASS_E_CLASSNOTAVAILABLE;
1867 /***
1868 * COM_RevokeAllClasses
1870 * This method is called when the COM libraries are uninitialized to
1871 * release all the references to the class objects registered with
1872 * the library
1874 static void COM_RevokeAllClasses()
1876 while (firstRegisteredClass!=0)
1878 CoRevokeClassObject(firstRegisteredClass->dwCookie);
1882 /****************************************************************************
1883 * COM External Lock methods implementation
1886 /****************************************************************************
1887 * Public - Method that increments the count for a IUnknown* in the linked
1888 * list. The item is inserted if not already in the list.
1890 static void COM_ExternalLockAddRef(
1891 IUnknown *pUnk)
1893 COM_ExternalLock *externalLock = COM_ExternalLockFind(pUnk);
1896 * Add an external lock to the object. If it was already externally
1897 * locked, just increase the reference count. If it was not.
1898 * add the item to the list.
1900 if ( externalLock == EL_NOT_FOUND )
1901 COM_ExternalLockInsert(pUnk);
1902 else
1903 externalLock->uRefCount++;
1906 * Add an internal lock to the object
1908 IUnknown_AddRef(pUnk);
1911 /****************************************************************************
1912 * Public - Method that decrements the count for a IUnknown* in the linked
1913 * list. The item is removed from the list if its count end up at zero or if
1914 * bRelAll is TRUE.
1916 static void COM_ExternalLockRelease(
1917 IUnknown *pUnk,
1918 BOOL bRelAll)
1920 COM_ExternalLock *externalLock = COM_ExternalLockFind(pUnk);
1922 if ( externalLock != EL_NOT_FOUND )
1926 externalLock->uRefCount--; /* release external locks */
1927 IUnknown_Release(pUnk); /* release local locks as well */
1929 if ( bRelAll == FALSE )
1930 break; /* perform single release */
1932 } while ( externalLock->uRefCount > 0 );
1934 if ( externalLock->uRefCount == 0 ) /* get rid of the list entry */
1935 COM_ExternalLockDelete(externalLock);
1938 /****************************************************************************
1939 * Public - Method that frees the content of the list.
1941 static void COM_ExternalLockFreeList()
1943 COM_ExternalLock *head;
1945 head = elList.head; /* grab it by the head */
1946 while ( head != EL_END_OF_LIST )
1948 COM_ExternalLockDelete(head); /* get rid of the head stuff */
1950 head = elList.head; /* get the new head... */
1954 /****************************************************************************
1955 * Public - Method that dump the content of the list.
1957 void COM_ExternalLockDump()
1959 COM_ExternalLock *current = elList.head;
1961 printf("\nExternal lock list contains:\n");
1963 while ( current != EL_END_OF_LIST )
1965 printf( "\t%p with %lu references count.\n",
1966 current->pUnk,
1967 current->uRefCount);
1969 /* Skip to the next item */
1970 current = current->next;
1975 /****************************************************************************
1976 * Internal - Find a IUnknown* in the linked list
1978 static COM_ExternalLock* COM_ExternalLockFind(
1979 IUnknown *pUnk)
1981 return COM_ExternalLockLocate(elList.head, pUnk);
1984 /****************************************************************************
1985 * Internal - Recursivity agent for IUnknownExternalLockList_Find
1987 static COM_ExternalLock* COM_ExternalLockLocate(
1988 COM_ExternalLock *element,
1989 IUnknown *pUnk)
1991 if ( element == EL_END_OF_LIST )
1992 return EL_NOT_FOUND;
1994 else if ( element->pUnk == pUnk ) /* We found it */
1995 return element;
1997 else /* Not the right guy, keep on looking */
1998 return COM_ExternalLockLocate( element->next, pUnk);
2001 /****************************************************************************
2002 * Internal - Insert a new IUnknown* to the linked list
2004 static BOOL COM_ExternalLockInsert(
2005 IUnknown *pUnk)
2007 COM_ExternalLock *newLock = NULL;
2008 COM_ExternalLock *previousHead = NULL;
2011 * Allocate space for the new storage object
2013 newLock = HeapAlloc(GetProcessHeap(), 0, sizeof(COM_ExternalLock));
2015 if (newLock!=NULL)
2017 if ( elList.head == EL_END_OF_LIST )
2019 elList.head = newLock; /* The list is empty */
2021 else
2024 * insert does it at the head
2026 previousHead = elList.head;
2027 elList.head = newLock;
2031 * Set new list item data member
2033 newLock->pUnk = pUnk;
2034 newLock->uRefCount = 1;
2035 newLock->next = previousHead;
2037 return TRUE;
2039 else
2040 return FALSE;
2043 /****************************************************************************
2044 * Internal - Method that removes an item from the linked list.
2046 static void COM_ExternalLockDelete(
2047 COM_ExternalLock *itemList)
2049 COM_ExternalLock *current = elList.head;
2051 if ( current == itemList )
2054 * this section handles the deletion of the first node
2056 elList.head = itemList->next;
2057 HeapFree( GetProcessHeap(), 0, itemList);
2059 else
2063 if ( current->next == itemList ) /* We found the item to free */
2065 current->next = itemList->next; /* readjust the list pointers */
2067 HeapFree( GetProcessHeap(), 0, itemList);
2068 break;
2071 /* Skip to the next item */
2072 current = current->next;
2074 } while ( current != EL_END_OF_LIST );
2078 /***********************************************************************
2079 * COMPOBJ_DllEntryPoint [COMPOBJ.entry]
2081 * Initialization code for the COMPOBJ DLL
2083 * RETURNS:
2085 BOOL WINAPI COMPOBJ_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst, WORD ds, WORD HeapSize, DWORD res1, WORD res2)
2087 TRACE("(%08lx, %04x, %04x, %04x, %08lx, %04x)\n", Reason, hInst, ds, HeapSize,
2088 res1, res2);
2089 switch(Reason)
2091 case DLL_PROCESS_ATTACH:
2092 COMPOBJ_Attach++;
2093 if(COMPOBJ_hInstance)
2095 ERR("compobj.dll instantiated twice!\n");
2097 * We should return FALSE here, but that will break
2098 * most apps that use CreateProcess because we do
2099 * not yet support seperate address-spaces.
2101 return TRUE;
2104 COMPOBJ_hInstance = hInst;
2105 break;
2107 case DLL_PROCESS_DETACH:
2108 if(!--COMPOBJ_Attach)
2109 COMPOBJ_hInstance = 0;
2110 break;
2112 return TRUE;