Added stub for CoResumeClassObjects.
[wine.git] / dlls / ole32 / compobj.c
blobde56bbdbaccca0bb60abd91c775f185bb462409f
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 char *DllName; /* really only needed for debugging */
174 HINSTANCE hLibrary;
175 struct tagOpenDll *next;
176 } OpenDll;
178 static OpenDll *openDllList = NULL; /* linked list of open dlls */
180 /*****************************************************************************
181 * This section contains prototypes to internal methods for this
182 * module
184 static HRESULT COM_GetRegisteredClassObject(REFCLSID rclsid,
185 DWORD dwClsContext,
186 LPUNKNOWN* ppUnk);
188 static void COM_RevokeAllClasses();
191 /******************************************************************************
192 * CoBuildVersion [COMPOBJ.1]
194 * RETURNS
195 * Current build version, hiword is majornumber, loword is minornumber
197 DWORD WINAPI CoBuildVersion(void)
199 TRACE("Returning version %d, build %d.\n", rmm, rup);
200 return (rmm<<16)+rup;
203 /******************************************************************************
204 * CoInitialize16 [COMPOBJ.2]
205 * Set the win16 IMalloc used for memory management
207 HRESULT WINAPI CoInitialize16(
208 LPVOID lpReserved /* [in] pointer to win16 malloc interface */
210 currentMalloc16 = (LPMALLOC16)lpReserved;
211 return S_OK;
214 /******************************************************************************
215 * CoInitialize32 [OLE32.26]
217 * Initializes the COM libraries.
219 * See CoInitializeEx32
221 HRESULT WINAPI CoInitialize(
222 LPVOID lpReserved /* [in] pointer to win32 malloc interface
223 (obsolete, should be NULL) */
227 * Just delegate to the newer method.
229 return CoInitializeEx(lpReserved, COINIT_APARTMENTTHREADED);
232 /******************************************************************************
233 * CoInitializeEx32 [OLE32.163]
235 * Initializes the COM libraries. The behavior used to set the win32 IMalloc
236 * used for memory management is obsolete.
238 * RETURNS
239 * S_OK if successful,
240 * S_FALSE if this function was called already.
241 * RPC_E_CHANGED_MODE if a previous call to CoInitialize specified another
242 * threading model.
244 * BUGS
245 * Only the single threaded model is supported. As a result RPC_E_CHANGED_MODE
246 * is never returned.
248 * See the windows documentation for more details.
250 HRESULT WINAPI CoInitializeEx(
251 LPVOID lpReserved, /* [in] pointer to win32 malloc interface
252 (obsolete, should be NULL) */
253 DWORD dwCoInit /* [in] A value from COINIT specifies the threading model */
256 HRESULT hr;
258 TRACE("(%p, %x)\n", lpReserved, (int)dwCoInit);
260 if (lpReserved!=NULL)
262 ERR("(%p, %x) - Bad parameter passed-in %p, must be an old Windows Application\n", lpReserved, (int)dwCoInit, lpReserved);
266 * Check for unsupported features.
268 if (dwCoInit!=COINIT_APARTMENTTHREADED)
270 FIXME(":(%p,%x): unsupported flag %x\n", lpReserved, (int)dwCoInit, (int)dwCoInit);
271 /* Hope for the best and continue anyway */
275 * Check the lock count. If this is the first time going through the initialize
276 * process, we have to initialize the libraries.
278 if (s_COMLockCount==0)
281 * Initialize the various COM libraries and data structures.
283 TRACE("() - Initializing the COM libraries\n");
285 RunningObjectTableImpl_Initialize();
287 hr = S_OK;
289 else
290 hr = S_FALSE;
293 * Crank-up that lock count.
295 s_COMLockCount++;
297 return hr;
300 /***********************************************************************
301 * CoUninitialize16 [COMPOBJ.3]
302 * Don't know what it does.
303 * 3-Nov-98 -- this was originally misspelled, I changed it to what I
304 * believe is the correct spelling
306 void WINAPI CoUninitialize16(void)
308 TRACE("()\n");
309 CoFreeAllLibraries();
312 /***********************************************************************
313 * CoUninitialize32 [OLE32.47]
315 * This method will release the COM libraries.
317 * See the windows documentation for more details.
319 void WINAPI CoUninitialize(void)
321 TRACE("()\n");
324 * Decrease the reference count.
326 s_COMLockCount--;
329 * If we are back to 0 locks on the COM library, make sure we free
330 * all the associated data structures.
332 if (s_COMLockCount==0)
335 * Release the various COM libraries and data structures.
337 TRACE("() - Releasing the COM libraries\n");
339 RunningObjectTableImpl_UnInitialize();
341 * Release the references to the registered class objects.
343 COM_RevokeAllClasses();
346 * This will free the loaded COM Dlls.
348 CoFreeAllLibraries();
351 * This will free list of external references to COM objects.
353 COM_ExternalLockFreeList();
357 /***********************************************************************
358 * CoGetMalloc16 [COMPOBJ.4]
359 * RETURNS
360 * The current win16 IMalloc
362 HRESULT WINAPI CoGetMalloc16(
363 DWORD dwMemContext, /* [in] unknown */
364 LPMALLOC16 * lpMalloc /* [out] current win16 malloc interface */
366 if(!currentMalloc16)
367 currentMalloc16 = IMalloc16_Constructor();
368 *lpMalloc = currentMalloc16;
369 return S_OK;
372 /******************************************************************************
373 * CoGetMalloc32 [OLE32.20]
375 * RETURNS
376 * The current win32 IMalloc
378 HRESULT WINAPI CoGetMalloc(
379 DWORD dwMemContext, /* [in] unknown */
380 LPMALLOC *lpMalloc /* [out] current win32 malloc interface */
382 if(!currentMalloc32)
383 currentMalloc32 = IMalloc_Constructor();
384 *lpMalloc = currentMalloc32;
385 return S_OK;
388 /***********************************************************************
389 * CoCreateStandardMalloc16 [COMPOBJ.71]
391 HRESULT WINAPI CoCreateStandardMalloc16(DWORD dwMemContext,
392 LPMALLOC16 *lpMalloc)
394 /* FIXME: docu says we shouldn't return the same allocator as in
395 * CoGetMalloc16 */
396 *lpMalloc = IMalloc16_Constructor();
397 return S_OK;
400 /******************************************************************************
401 * CoDisconnectObject [COMPOBJ.15]
403 HRESULT WINAPI CoDisconnectObject( LPUNKNOWN lpUnk, DWORD reserved )
405 TRACE("%p %lx\n",lpUnk,reserved);
406 return S_OK;
409 /***********************************************************************
410 * IsEqualGUID16 [COMPOBJ.18]
412 * Compares two Unique Identifiers.
414 * RETURNS
415 * TRUE if equal
417 BOOL16 WINAPI IsEqualGUID16(
418 GUID* g1, /* [in] unique id 1 */
419 GUID* g2 /**/
421 return !memcmp( g1, g2, sizeof(GUID) );
424 /***********************************************************************
425 * IsEqualGUID32 [OLE32.76]
427 * Compares two Unique Identifiers.
429 * RETURNS
430 * TRUE if equal
432 BOOL WINAPI IsEqualGUID32(
433 REFGUID rguid1, /* [in] unique id 1 */
434 REFGUID rguid2 /* [in] unique id 2 */
437 return !memcmp(rguid1,rguid2,sizeof(GUID));
440 /******************************************************************************
441 * CLSIDFromString16 [COMPOBJ.20]
442 * Converts a unique identifier from it's string representation into
443 * the GUID struct.
445 * Class id: DWORD-WORD-WORD-BYTES[2]-BYTES[6]
447 * RETURNS
448 * the converted GUID
450 HRESULT WINAPI CLSIDFromString16(
451 LPCOLESTR16 idstr, /* [in] string representation of guid */
452 CLSID *id /* [out] GUID converted from string */
454 BYTE *s = (BYTE *) idstr;
455 BYTE *p;
456 int i;
457 BYTE table[256];
459 if (!s)
460 s = "{00000000-0000-0000-0000-000000000000}";
462 TRACE("%s -> %p\n", s, id);
464 /* quick lookup table */
465 memset(table, 0, 256);
467 for (i = 0; i < 10; i++) {
468 table['0' + i] = i;
470 for (i = 0; i < 6; i++) {
471 table['A' + i] = i+10;
472 table['a' + i] = i+10;
475 /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
477 if (strlen(s) != 38)
478 return OLE_ERROR_OBJECT;
480 p = (BYTE *) id;
482 s++; /* skip leading brace */
483 for (i = 0; i < 4; i++) {
484 p[3 - i] = table[*s]<<4 | table[*(s+1)];
485 s += 2;
487 p += 4;
488 s++; /* skip - */
490 for (i = 0; i < 2; i++) {
491 p[1-i] = table[*s]<<4 | table[*(s+1)];
492 s += 2;
494 p += 2;
495 s++; /* skip - */
497 for (i = 0; i < 2; i++) {
498 p[1-i] = table[*s]<<4 | table[*(s+1)];
499 s += 2;
501 p += 2;
502 s++; /* skip - */
504 /* these are just sequential bytes */
505 for (i = 0; i < 2; i++) {
506 *p++ = table[*s]<<4 | table[*(s+1)];
507 s += 2;
509 s++; /* skip - */
511 for (i = 0; i < 6; i++) {
512 *p++ = table[*s]<<4 | table[*(s+1)];
513 s += 2;
516 return S_OK;
519 /******************************************************************************
520 * CoCreateGuid[OLE32.6]
522 * Creates a 128bit GUID.
523 * Implemented according the DCE specification for UUID generation.
524 * Code is based upon uuid library in e2fsprogs by Theodore Ts'o.
525 * Copyright (C) 1996, 1997 Theodore Ts'o.
527 * RETURNS
529 * S_OK if successful.
531 HRESULT WINAPI CoCreateGuid(
532 GUID *pguid /* [out] points to the GUID to initialize */
534 static char has_init = 0;
535 unsigned char a[6];
536 static int adjustment = 0;
537 static struct timeval last = {0, 0};
538 static UINT16 clock_seq;
539 struct timeval tv;
540 unsigned long long clock_reg;
541 UINT clock_high, clock_low;
542 UINT16 temp_clock_seq, temp_clock_mid, temp_clock_hi_and_version;
543 #ifdef HAVE_NET_IF_H
544 int sd;
545 struct ifreq ifr, *ifrp;
546 struct ifconf ifc;
547 char buf[1024];
548 int n, i;
549 #endif
551 /* Have we already tried to get the MAC address? */
552 if (!has_init) {
553 #ifdef HAVE_NET_IF_H
554 /* BSD 4.4 defines the size of an ifreq to be
555 * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
556 * However, under earlier systems, sa_len isn't present, so
557 * the size is just sizeof(struct ifreq)
559 # ifdef HAVE_SA_LEN
560 # ifndef max
561 # define max(a,b) ((a) > (b) ? (a) : (b))
562 # endif
563 # define ifreq_size(i) max(sizeof(struct ifreq),\
564 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
565 # else
566 # define ifreq_size(i) sizeof(struct ifreq)
567 # endif /* HAVE_SA_LEN */
569 sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
570 if (sd < 0) {
571 /* if we can't open a socket, just use random numbers */
572 /* set the multicast bit to prevent conflicts with real cards */
573 a[0] = (rand() & 0xff) | 0x80;
574 a[1] = rand() & 0xff;
575 a[2] = rand() & 0xff;
576 a[3] = rand() & 0xff;
577 a[4] = rand() & 0xff;
578 a[5] = rand() & 0xff;
579 } else {
580 memset(buf, 0, sizeof(buf));
581 ifc.ifc_len = sizeof(buf);
582 ifc.ifc_buf = buf;
583 /* get the ifconf interface */
584 if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) {
585 close(sd);
586 /* no ifconf, so just use random numbers */
587 /* set the multicast bit to prevent conflicts with real cards */
588 a[0] = (rand() & 0xff) | 0x80;
589 a[1] = rand() & 0xff;
590 a[2] = rand() & 0xff;
591 a[3] = rand() & 0xff;
592 a[4] = rand() & 0xff;
593 a[5] = rand() & 0xff;
594 } else {
595 /* loop through the interfaces, looking for a valid one */
596 n = ifc.ifc_len;
597 for (i = 0; i < n; i+= ifreq_size(*ifr) ) {
598 ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i);
599 strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
600 /* try to get the address for this interface */
601 # ifdef SIOCGIFHWADDR
602 if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
603 continue;
604 memcpy(a, (unsigned char *)&ifr.ifr_hwaddr.sa_data, 6);
605 # else
606 # ifdef SIOCGENADDR
607 if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
608 continue;
609 memcpy(a, (unsigned char *) ifr.ifr_enaddr, 6);
610 # else
611 /* XXX we don't have a way of getting the hardware address */
612 close(sd);
613 a[0] = 0;
614 break;
615 # endif /* SIOCGENADDR */
616 # endif /* SIOCGIFHWADDR */
617 /* make sure it's not blank */
618 if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5])
619 continue;
621 goto valid_address;
623 /* if we didn't find a valid address, make a random one */
624 /* once again, set multicast bit to avoid conflicts */
625 a[0] = (rand() & 0xff) | 0x80;
626 a[1] = rand() & 0xff;
627 a[2] = rand() & 0xff;
628 a[3] = rand() & 0xff;
629 a[4] = rand() & 0xff;
630 a[5] = rand() & 0xff;
632 valid_address:
633 close(sd);
636 #else
637 /* no networking info, so generate a random address */
638 a[0] = (rand() & 0xff) | 0x80;
639 a[1] = rand() & 0xff;
640 a[2] = rand() & 0xff;
641 a[3] = rand() & 0xff;
642 a[4] = rand() & 0xff;
643 a[5] = rand() & 0xff;
644 #endif /* HAVE_NET_IF_H */
645 has_init = 1;
648 /* generate time element of GUID */
650 /* Assume that the gettimeofday() has microsecond granularity */
651 #define MAX_ADJUSTMENT 10
653 try_again:
654 gettimeofday(&tv, 0);
655 if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
656 clock_seq = ((rand() & 0xff) << 8) + (rand() & 0xff);
657 clock_seq &= 0x1FFF;
658 last = tv;
659 last.tv_sec--;
661 if ((tv.tv_sec < last.tv_sec) ||
662 ((tv.tv_sec == last.tv_sec) &&
663 (tv.tv_usec < last.tv_usec))) {
664 clock_seq = (clock_seq+1) & 0x1FFF;
665 adjustment = 0;
666 } else if ((tv.tv_sec == last.tv_sec) &&
667 (tv.tv_usec == last.tv_usec)) {
668 if (adjustment >= MAX_ADJUSTMENT)
669 goto try_again;
670 adjustment++;
671 } else
672 adjustment = 0;
674 clock_reg = tv.tv_usec*10 + adjustment;
675 clock_reg += ((unsigned long long) tv.tv_sec)*10000000;
676 clock_reg += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
678 clock_high = clock_reg >> 32;
679 clock_low = clock_reg;
680 temp_clock_seq = clock_seq | 0x8000;
681 temp_clock_mid = (UINT16)clock_high;
682 temp_clock_hi_and_version = (clock_high >> 16) | 0x1000;
684 /* pack the information into the GUID structure */
686 ((unsigned char*)&pguid->Data1)[3] = (unsigned char)clock_low;
687 clock_low >>= 8;
688 ((unsigned char*)&pguid->Data1)[2] = (unsigned char)clock_low;
689 clock_low >>= 8;
690 ((unsigned char*)&pguid->Data1)[1] = (unsigned char)clock_low;
691 clock_low >>= 8;
692 ((unsigned char*)&pguid->Data1)[0] = (unsigned char)clock_low;
694 ((unsigned char*)&pguid->Data2)[1] = (unsigned char)temp_clock_mid;
695 temp_clock_mid >>= 8;
696 ((unsigned char*)&pguid->Data2)[0] = (unsigned char)temp_clock_mid;
698 ((unsigned char*)&pguid->Data3)[1] = (unsigned char)temp_clock_hi_and_version;
699 temp_clock_hi_and_version >>= 8;
700 ((unsigned char*)&pguid->Data3)[0] = (unsigned char)temp_clock_hi_and_version;
702 ((unsigned char*)pguid->Data4)[1] = (unsigned char)temp_clock_seq;
703 temp_clock_seq >>= 8;
704 ((unsigned char*)pguid->Data4)[0] = (unsigned char)temp_clock_seq;
706 ((unsigned char*)pguid->Data4)[2] = a[0];
707 ((unsigned char*)pguid->Data4)[3] = a[1];
708 ((unsigned char*)pguid->Data4)[4] = a[2];
709 ((unsigned char*)pguid->Data4)[5] = a[3];
710 ((unsigned char*)pguid->Data4)[6] = a[4];
711 ((unsigned char*)pguid->Data4)[7] = a[5];
713 TRACE("%p", pguid);
715 return S_OK;
718 /******************************************************************************
719 * CLSIDFromString32 [OLE32.3]
720 * Converts a unique identifier from it's string representation into
721 * the GUID struct.
722 * RETURNS
723 * the converted GUID
725 HRESULT WINAPI CLSIDFromString(
726 LPCOLESTR idstr, /* [in] string representation of GUID */
727 CLSID *id /* [out] GUID represented by above string */
729 LPOLESTR16 xid = HEAP_strdupWtoA(GetProcessHeap(),0,idstr);
730 OLESTATUS ret = CLSIDFromString16(xid,id);
732 HeapFree(GetProcessHeap(),0,xid);
733 return ret;
736 /******************************************************************************
737 * WINE_StringFromCLSID [Internal]
738 * Converts a GUID into the respective string representation.
740 * NOTES
742 * RETURNS
743 * the string representation and OLESTATUS
745 HRESULT WINE_StringFromCLSID(
746 const CLSID *id, /* [in] GUID to be converted */
747 LPSTR idstr /* [out] pointer to buffer to contain converted guid */
749 static const char *hex = "0123456789ABCDEF";
750 char *s;
751 int i;
753 if (!id)
754 { ERR("called with id=Null\n");
755 *idstr = 0x00;
756 return E_FAIL;
759 sprintf(idstr, "{%08lX-%04X-%04X-%02X%02X-",
760 id->Data1, id->Data2, id->Data3,
761 id->Data4[0], id->Data4[1]);
762 s = &idstr[25];
764 /* 6 hex bytes */
765 for (i = 2; i < 8; i++) {
766 *s++ = hex[id->Data4[i]>>4];
767 *s++ = hex[id->Data4[i] & 0xf];
770 *s++ = '}';
771 *s++ = '\0';
773 TRACE("%p->%s\n", id, idstr);
775 return OLE_OK;
778 /******************************************************************************
779 * StringFromCLSID16 [COMPOBJ.19]
780 * Converts a GUID into the respective string representation.
781 * The target string is allocated using the OLE IMalloc.
782 * RETURNS
783 * the string representation and OLESTATUS
785 HRESULT WINAPI StringFromCLSID16(
786 REFCLSID id, /* [in] the GUID to be converted */
787 LPOLESTR16 *idstr /* [out] a pointer to a to-be-allocated segmented pointer pointing to the resulting string */
790 LPMALLOC16 mllc;
791 OLESTATUS ret;
792 DWORD args[2];
794 ret = CoGetMalloc16(0,&mllc);
795 if (ret) return ret;
797 args[0] = (DWORD)mllc;
798 args[1] = 40;
800 /* No need for a Callback entry, we have WOWCallback16Ex which does
801 * everything we need.
803 if (!WOWCallback16Ex(
804 (DWORD)((ICOM_VTABLE(IMalloc16)*)PTR_SEG_TO_LIN(
805 ICOM_VTBL(((LPMALLOC16)PTR_SEG_TO_LIN(mllc))))
806 )->fnAlloc,
807 WCB16_CDECL,
808 2*sizeof(DWORD),
809 (LPVOID)args,
810 (LPDWORD)idstr
811 )) {
812 WARN("CallTo16 IMalloc16 failed\n");
813 return E_FAIL;
815 return WINE_StringFromCLSID(id,PTR_SEG_TO_LIN(*idstr));
818 /******************************************************************************
819 * StringFromCLSID32 [OLE32.151]
820 * Converts a GUID into the respective string representation.
821 * The target string is allocated using the OLE IMalloc.
822 * RETURNS
823 * the string representation and OLESTATUS
825 HRESULT WINAPI StringFromCLSID(
826 REFCLSID id, /* [in] the GUID to be converted */
827 LPOLESTR *idstr /* [out] a pointer to a to-be-allocated pointer pointing to the resulting string */
829 char buf[80];
830 OLESTATUS ret;
831 LPMALLOC mllc;
833 if ((ret=CoGetMalloc(0,&mllc)))
834 return ret;
836 ret=WINE_StringFromCLSID(id,buf);
837 if (!ret) {
838 *idstr = IMalloc_Alloc(mllc,strlen(buf)*2+2);
839 lstrcpyAtoW(*idstr,buf);
841 return ret;
844 /******************************************************************************
845 * StringFromGUID2 [COMPOBJ.76] [OLE32.152]
847 * Converts a global unique identifier into a string of an API-
848 * specified fixed format. (The usual {.....} stuff.)
850 * RETURNS
851 * The (UNICODE) string representation of the GUID in 'str'
852 * The length of the resulting string, 0 if there was any problem.
854 INT WINAPI
855 StringFromGUID2(REFGUID id, LPOLESTR str, INT cmax)
857 char xguid[80];
859 if (WINE_StringFromCLSID(id,xguid))
860 return 0;
861 if (strlen(xguid)>=cmax)
862 return 0;
863 lstrcpyAtoW(str,xguid);
864 return strlen(xguid);
867 /******************************************************************************
868 * ProgIDFromCLSID [OLE32.133]
869 * Converts a class id into the respective Program ID. (By using a registry lookup)
870 * RETURNS S_OK on success
871 * riid associated with the progid
874 HRESULT WINAPI ProgIDFromCLSID(
875 REFCLSID clsid, /* [in] class id as found in registry */
876 LPOLESTR *lplpszProgID/* [out] associated Prog ID */
879 char strCLSID[50], *buf, *buf2;
880 DWORD buf2len;
881 HKEY xhkey;
882 LPMALLOC mllc;
883 HRESULT ret = S_OK;
885 WINE_StringFromCLSID(clsid, strCLSID);
887 buf = HeapAlloc(GetProcessHeap(), 0, strlen(strCLSID)+14);
888 sprintf(buf,"CLSID\\%s\\ProgID", strCLSID);
889 if (RegOpenKeyA(HKEY_CLASSES_ROOT, buf, &xhkey))
890 ret = REGDB_E_CLASSNOTREG;
892 HeapFree(GetProcessHeap(), 0, buf);
894 if (ret == S_OK)
896 buf2 = HeapAlloc(GetProcessHeap(), 0, 255);
897 buf2len = 255;
898 if (RegQueryValueA(xhkey, NULL, buf2, &buf2len))
899 ret = REGDB_E_CLASSNOTREG;
901 if (ret == S_OK)
903 if (CoGetMalloc(0,&mllc))
904 ret = E_OUTOFMEMORY;
905 else
907 *lplpszProgID = IMalloc_Alloc(mllc, (buf2len+1)*2);
908 lstrcpyAtoW(*lplpszProgID, buf2);
911 HeapFree(GetProcessHeap(), 0, buf2);
914 RegCloseKey(xhkey);
915 return ret;
918 /******************************************************************************
919 * CLSIDFromProgID16 [COMPOBJ.61]
920 * Converts a program id into the respective GUID. (By using a registry lookup)
921 * RETURNS
922 * riid associated with the progid
924 HRESULT WINAPI CLSIDFromProgID16(
925 LPCOLESTR16 progid, /* [in] program id as found in registry */
926 LPCLSID riid /* [out] associated CLSID */
928 char *buf,buf2[80];
929 DWORD buf2len;
930 HRESULT err;
931 HKEY xhkey;
933 buf = HeapAlloc(GetProcessHeap(),0,strlen(progid)+8);
934 sprintf(buf,"%s\\CLSID",progid);
935 if ((err=RegOpenKeyA(HKEY_CLASSES_ROOT,buf,&xhkey))) {
936 HeapFree(GetProcessHeap(),0,buf);
937 return OLE_ERROR_GENERIC;
939 HeapFree(GetProcessHeap(),0,buf);
940 buf2len = sizeof(buf2);
941 if ((err=RegQueryValueA(xhkey,NULL,buf2,&buf2len))) {
942 RegCloseKey(xhkey);
943 return OLE_ERROR_GENERIC;
945 RegCloseKey(xhkey);
946 return CLSIDFromString16(buf2,riid);
949 /******************************************************************************
950 * CLSIDFromProgID32 [OLE32.2]
951 * Converts a program id into the respective GUID. (By using a registry lookup)
952 * RETURNS
953 * riid associated with the progid
955 HRESULT WINAPI CLSIDFromProgID(
956 LPCOLESTR progid, /* [in] program id as found in registry */
957 LPCLSID riid /* [out] associated CLSID */
959 LPOLESTR16 pid = HEAP_strdupWtoA(GetProcessHeap(),0,progid);
960 OLESTATUS ret = CLSIDFromProgID16(pid,riid);
962 HeapFree(GetProcessHeap(),0,pid);
963 return ret;
966 /************************************************************************************************
967 * OleSaveToStream
969 * This function write a CLSID on stream
971 HRESULT WINAPI WriteClassStm(IStream *pStm,REFCLSID rclsid)
973 TRACE("(%p,%p)\n",pStm,rclsid);
975 if (rclsid==NULL)
976 return E_INVALIDARG;
978 return IStream_Write(pStm,rclsid,sizeof(CLSID),NULL);
981 /************************************************************************************************
982 * OleSaveToStream
984 * This function read a CLSID from a stream
986 HRESULT WINAPI ReadClassStm(IStream *pStm,REFCLSID rclsid)
988 ULONG nbByte;
989 HRESULT res;
991 TRACE("(%p,%p)\n",pStm,rclsid);
993 if (rclsid==NULL)
994 return E_INVALIDARG;
996 res = IStream_Read(pStm,(void*)rclsid,sizeof(CLSID),&nbByte);
998 if (FAILED(res))
999 return res;
1001 if (nbByte != sizeof(CLSID))
1002 return S_FALSE;
1003 else
1004 return S_OK;
1007 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
1008 /***********************************************************************
1009 * LookupETask (COMPOBJ.94)
1011 OLESTATUS WINAPI LookupETask16(HTASK16 *hTask,LPVOID p) {
1012 FIXME("(%p,%p),stub!\n",hTask,p);
1013 if ((*hTask = GetCurrentTask()) == hETask) {
1014 memcpy(p, Table_ETask, sizeof(Table_ETask));
1016 return 0;
1019 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
1020 /***********************************************************************
1021 * SetETask (COMPOBJ.95)
1023 OLESTATUS WINAPI SetETask16(HTASK16 hTask, LPVOID p) {
1024 FIXME("(%04x,%p),stub!\n",hTask,p);
1025 hETask = hTask;
1026 return 0;
1029 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
1030 /***********************************************************************
1031 * CallObjectInWOW (COMPOBJ.201)
1033 OLESTATUS WINAPI CallObjectInWOW(LPVOID p1,LPVOID p2) {
1034 FIXME("(%p,%p),stub!\n",p1,p2);
1035 return 0;
1038 /******************************************************************************
1039 * CoRegisterClassObject16 [COMPOBJ.5]
1041 * Don't know where it registers it ...
1043 HRESULT WINAPI CoRegisterClassObject16(
1044 REFCLSID rclsid,
1045 LPUNKNOWN pUnk,
1046 DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
1047 DWORD flags, /* [in] REGCLS flags indicating how connections are made */
1048 LPDWORD lpdwRegister
1050 char buf[80];
1052 WINE_StringFromCLSID(rclsid,buf);
1054 FIXME("(%s,%p,0x%08lx,0x%08lx,%p),stub\n",
1055 buf,pUnk,dwClsContext,flags,lpdwRegister
1057 return 0;
1061 /******************************************************************************
1062 * CoRevokeClassObject16 [COMPOBJ.6]
1065 HRESULT WINAPI CoRevokeClassObject16(DWORD dwRegister /* token on class obj */)
1067 FIXME("(0x%08lx),stub!\n", dwRegister);
1068 return 0;
1072 /***
1073 * COM_GetRegisteredClassObject
1075 * This internal method is used to scan the registered class list to
1076 * find a class object.
1078 * Params:
1079 * rclsid Class ID of the class to find.
1080 * dwClsContext Class context to match.
1081 * ppv [out] returns a pointer to the class object. Complying
1082 * to normal COM usage, this method will increase the
1083 * reference count on this object.
1085 static HRESULT COM_GetRegisteredClassObject(
1086 REFCLSID rclsid,
1087 DWORD dwClsContext,
1088 LPUNKNOWN* ppUnk)
1090 RegisteredClass* curClass;
1093 * Sanity check
1095 assert(ppUnk!=0);
1098 * Iterate through the whole list and try to match the class ID.
1100 curClass = firstRegisteredClass;
1102 while (curClass != 0)
1105 * Check if we have a match on the class ID.
1107 if (IsEqualGUID32(&(curClass->classIdentifier), rclsid))
1110 * Since we don't do out-of process or DCOM just right away, let's ignore the
1111 * class context.
1115 * We have a match, return the pointer to the class object.
1117 *ppUnk = curClass->classObject;
1119 IUnknown_AddRef(curClass->classObject);
1121 return S_OK;
1125 * Step to the next class in the list.
1127 curClass = curClass->nextClass;
1131 * If we get to here, we haven't found our class.
1133 return S_FALSE;
1136 /******************************************************************************
1137 * CoRegisterClassObject32 [OLE32.36]
1139 * This method will register the class object for a given class ID.
1141 * See the Windows documentation for more details.
1143 HRESULT WINAPI CoRegisterClassObject(
1144 REFCLSID rclsid,
1145 LPUNKNOWN pUnk,
1146 DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
1147 DWORD flags, /* [in] REGCLS flags indicating how connections are made */
1148 LPDWORD lpdwRegister
1151 RegisteredClass* newClass;
1152 LPUNKNOWN foundObject;
1153 HRESULT hr;
1154 char buf[80];
1156 WINE_StringFromCLSID(rclsid,buf);
1158 TRACE("(%s,%p,0x%08lx,0x%08lx,%p)\n",
1159 buf,pUnk,dwClsContext,flags,lpdwRegister);
1162 * Perform a sanity check on the parameters
1164 if ( (lpdwRegister==0) || (pUnk==0) )
1166 return E_INVALIDARG;
1170 * Initialize the cookie (out parameter)
1172 *lpdwRegister = 0;
1175 * First, check if the class is already registered.
1176 * If it is, this should cause an error.
1178 hr = COM_GetRegisteredClassObject(rclsid, dwClsContext, &foundObject);
1180 if (hr == S_OK)
1183 * The COM_GetRegisteredClassObject increased the reference count on the
1184 * object so it has to be released.
1186 IUnknown_Release(foundObject);
1188 return CO_E_OBJISREG;
1192 * If it is not registered, we must create a new entry for this class and
1193 * append it to the registered class list.
1194 * We use the address of the chain node as the cookie since we are sure it's
1195 * unique.
1197 newClass = HeapAlloc(GetProcessHeap(), 0, sizeof(RegisteredClass));
1200 * Initialize the node.
1202 newClass->classIdentifier = *rclsid;
1203 newClass->runContext = dwClsContext;
1204 newClass->connectFlags = flags;
1205 newClass->dwCookie = (DWORD)newClass;
1206 newClass->nextClass = firstRegisteredClass;
1209 * Since we're making a copy of the object pointer, we have to increase it's
1210 * reference count.
1212 newClass->classObject = pUnk;
1213 IUnknown_AddRef(newClass->classObject);
1215 firstRegisteredClass = newClass;
1218 * Assign the out parameter (cookie)
1220 *lpdwRegister = newClass->dwCookie;
1223 * We're successful Yippee!
1225 return S_OK;
1228 /***********************************************************************
1229 * CoRevokeClassObject32 [OLE32.40]
1231 * This method will remove a class object from the class registry
1233 * See the Windows documentation for more details.
1235 HRESULT WINAPI CoRevokeClassObject(
1236 DWORD dwRegister)
1238 RegisteredClass** prevClassLink;
1239 RegisteredClass* curClass;
1241 TRACE("(%08lx)\n",dwRegister);
1244 * Iterate through the whole list and try to match the cookie.
1246 curClass = firstRegisteredClass;
1247 prevClassLink = &firstRegisteredClass;
1249 while (curClass != 0)
1252 * Check if we have a match on the cookie.
1254 if (curClass->dwCookie == dwRegister)
1257 * Remove the class from the chain.
1259 *prevClassLink = curClass->nextClass;
1262 * Release the reference to the class object.
1264 IUnknown_Release(curClass->classObject);
1267 * Free the memory used by the chain node.
1269 HeapFree(GetProcessHeap(), 0, curClass);
1271 return S_OK;
1275 * Step to the next class in the list.
1277 prevClassLink = &(curClass->nextClass);
1278 curClass = curClass->nextClass;
1282 * If we get to here, we haven't found our class.
1284 return E_INVALIDARG;
1287 /***********************************************************************
1288 * CoGetClassObject [COMPOBJ.7]
1290 HRESULT WINAPI CoGetClassObject(REFCLSID rclsid, DWORD dwClsContext,
1291 LPVOID pvReserved, REFIID iid, LPVOID *ppv)
1293 LPUNKNOWN regClassObject;
1294 char xclsid[50],xiid[50];
1295 HRESULT hres = E_UNEXPECTED;
1297 char dllName[MAX_PATH+1];
1298 DWORD dllNameLen = sizeof(dllName);
1299 HINSTANCE hLibrary;
1300 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid,
1301 REFIID iid, LPVOID *ppv);
1302 DllGetClassObjectFunc DllGetClassObject;
1304 WINE_StringFromCLSID((LPCLSID)rclsid,xclsid);
1305 WINE_StringFromCLSID((LPCLSID)iid,xiid);
1306 TRACE("\n\tCLSID:\t%s,\n\tIID:\t%s\n",xclsid,xiid);
1309 * First, try and see if we can't match the class ID with one of the
1310 * registered classes.
1312 if (S_OK == COM_GetRegisteredClassObject(rclsid, dwClsContext, &regClassObject))
1315 * Get the required interface from the retrieved pointer.
1317 hres = IUnknown_QueryInterface(regClassObject, iid, ppv);
1320 * Since QI got another reference on the pointer, we want to release the
1321 * one we already have. If QI was unsuccessful, this will release the object. This
1322 * is good since we are not returning it in the "out" parameter.
1324 IUnknown_Release(regClassObject);
1326 return hres;
1329 /* out of process and remote servers not supported yet */
1330 if (((CLSCTX_LOCAL_SERVER|CLSCTX_REMOTE_SERVER) & dwClsContext)
1331 && !((CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER) & dwClsContext)){
1332 FIXME("CLSCTX_LOCAL_SERVER and CLSCTX_REMOTE_SERVER not supported!\n");
1333 return E_ACCESSDENIED;
1336 if ((CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER) & dwClsContext) {
1337 HKEY CLSIDkey,key;
1339 /* lookup CLSID in registry key HKCR/CLSID */
1340 hres = RegOpenKeyExA(HKEY_CLASSES_ROOT, "CLSID", 0,
1341 KEY_READ, &CLSIDkey);
1343 if (hres != ERROR_SUCCESS)
1344 return REGDB_E_READREGDB;
1345 hres = RegOpenKeyExA(CLSIDkey,xclsid,0,KEY_QUERY_VALUE,&key);
1346 if (hres != ERROR_SUCCESS) {
1347 RegCloseKey(CLSIDkey);
1348 return REGDB_E_CLASSNOTREG;
1350 hres = RegQueryValueA(key, "InprocServer32", dllName, &dllNameLen);
1351 RegCloseKey(key);
1352 RegCloseKey(CLSIDkey);
1353 if (hres != ERROR_SUCCESS)
1354 return REGDB_E_READREGDB;
1355 TRACE("found InprocServer32 dll %s\n", dllName);
1357 /* open dll, call DllGetClassFactory */
1358 hLibrary = CoLoadLibrary(dllName, TRUE);
1359 if (hLibrary == 0) {
1360 TRACE("couldn't load InprocServer32 dll %s\n", dllName);
1361 return E_ACCESSDENIED; /* or should this be CO_E_DLLNOTFOUND? */
1363 DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress(hLibrary, "DllGetClassObject");
1364 if (!DllGetClassObject) {
1365 /* not sure if this should be called here CoFreeLibrary(hLibrary);*/
1366 TRACE("couldn't find function DllGetClassObject in %s\n", dllName);
1367 return E_ACCESSDENIED;
1371 * Ask the DLL for it's class object. (there was a note here about class
1372 * factories but this is good.
1374 return DllGetClassObject(rclsid, iid, ppv);
1376 return hres;
1379 /****************************************************************************************
1380 * CoResumeClassObjects
1382 * Resumes classobjects registered with REGCLS suspended
1384 HRESULT WINAPI CoResumeClassObjects(void)
1386 FIXME("\n");
1387 return S_OK;
1390 /****************************************************************************************
1391 * GetClassFile
1393 * This function supplies the CLSID associated with the given filename.
1395 HRESULT WINAPI GetClassFile(LPOLESTR filePathName,CLSID *pclsid)
1397 IStorage *pstg=0;
1398 HRESULT res;
1399 int nbElm=0,length=0,i=0;
1400 LONG sizeProgId=20;
1401 LPOLESTR *pathDec=0,absFile=0,progId=0;
1402 WCHAR extention[100]={0};
1404 TRACE("()\n");
1406 /* if the file contain a storage object the return the CLSID writen by IStorage_SetClass method*/
1407 if((StgIsStorageFile(filePathName))==S_OK){
1409 res=StgOpenStorage(filePathName,NULL,STGM_READ | STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
1411 if (SUCCEEDED(res))
1412 res=ReadClassStg(pstg,pclsid);
1414 IStorage_Release(pstg);
1416 return res;
1418 /* if the file is not a storage object then attemps to match various bits in the file against a
1419 pattern in the registry. this case is not frequently used ! so I present only the psodocode for
1420 this case
1422 for(i=0;i<nFileTypes;i++)
1424 for(i=0;j<nPatternsForType;j++){
1426 PATTERN pat;
1427 HANDLE hFile;
1429 pat=ReadPatternFromRegistry(i,j);
1430 hFile=CreateFileW(filePathName,,,,,,hFile);
1431 SetFilePosition(hFile,pat.offset);
1432 ReadFile(hFile,buf,pat.size,NULL,NULL);
1433 if (memcmp(buf&pat.mask,pat.pattern.pat.size)==0){
1435 *pclsid=ReadCLSIDFromRegistry(i);
1436 return S_OK;
1441 /* if the obove strategies fail then search for the extension key in the registry */
1443 /* get the last element (absolute file) in the path name */
1444 nbElm=FileMonikerImpl_DecomposePath(filePathName,&pathDec);
1445 absFile=pathDec[nbElm-1];
1447 /* failed if the path represente a directory and not an absolute file name*/
1448 if (lstrcmpW(absFile,(LPOLESTR)"\\"))
1449 return MK_E_INVALIDEXTENSION;
1451 /* get the extension of the file */
1452 length=lstrlenW(absFile);
1453 for(i=length-1; ( (i>=0) && (extention[i]=absFile[i]) );i--);
1455 /* get the progId associated to the extension */
1456 progId=CoTaskMemAlloc(sizeProgId);
1458 res=RegQueryValueW(HKEY_CLASSES_ROOT,extention,progId,&sizeProgId);
1460 if (res==ERROR_MORE_DATA){
1462 CoTaskMemRealloc(progId,sizeProgId);
1464 res=RegQueryValueW(HKEY_CLASSES_ROOT,extention,progId,&sizeProgId);
1466 if (res==ERROR_SUCCESS)
1467 /* return the clsid associated to the progId */
1468 res= CLSIDFromProgID(progId,pclsid);
1470 for(i=0; pathDec[i]!=NULL;i++)
1471 CoTaskMemFree(pathDec[i]);
1472 CoTaskMemFree(pathDec);
1474 CoTaskMemFree(progId);
1476 if (res==ERROR_SUCCESS)
1477 return res;
1479 return MK_E_INVALIDEXTENSION;
1481 /******************************************************************************
1482 * CoRegisterMessageFilter16 [COMPOBJ.27]
1484 HRESULT WINAPI CoRegisterMessageFilter16(
1485 LPMESSAGEFILTER lpMessageFilter,
1486 LPMESSAGEFILTER *lplpMessageFilter
1488 FIXME("(%p,%p),stub!\n",lpMessageFilter,lplpMessageFilter);
1489 return 0;
1492 /***********************************************************************
1493 * CoCreateInstance [COMPOBJ.13, OLE32.7]
1495 HRESULT WINAPI CoCreateInstance(
1496 REFCLSID rclsid,
1497 LPUNKNOWN pUnkOuter,
1498 DWORD dwClsContext,
1499 REFIID iid,
1500 LPVOID *ppv)
1502 HRESULT hres;
1503 LPCLASSFACTORY lpclf = 0;
1506 * Sanity check
1508 if (ppv==0)
1509 return E_POINTER;
1512 * Initialize the "out" parameter
1514 *ppv = 0;
1517 * Get a class factory to construct the object we want.
1519 hres = CoGetClassObject(rclsid,
1520 dwClsContext,
1521 NULL,
1522 &IID_IClassFactory,
1523 (LPVOID)&lpclf);
1525 if (FAILED(hres))
1526 return hres;
1529 * Create the object and don't forget to release the factory
1531 hres = IClassFactory_CreateInstance(lpclf, pUnkOuter, iid, ppv);
1532 IClassFactory_Release(lpclf);
1534 return hres;
1537 /***********************************************************************
1538 * CoCreateInstanceEx [OLE32.165]
1540 HRESULT WINAPI CoCreateInstanceEx(
1541 REFCLSID rclsid,
1542 LPUNKNOWN pUnkOuter,
1543 DWORD dwClsContext,
1544 COSERVERINFO* pServerInfo,
1545 ULONG cmq,
1546 MULTI_QI* pResults)
1548 IUnknown* pUnk = NULL;
1549 HRESULT hr;
1550 ULONG index;
1551 int successCount = 0;
1554 * Sanity check
1556 if ( (cmq==0) || (pResults==NULL))
1557 return E_INVALIDARG;
1559 if (pServerInfo!=NULL)
1560 FIXME("() non-NULL pServerInfo not supported!\n");
1563 * Initialize all the "out" parameters.
1565 for (index = 0; index < cmq; index++)
1567 pResults[index].pItf = NULL;
1568 pResults[index].hr = E_NOINTERFACE;
1572 * Get the object and get it's IUnknown pointer.
1574 hr = CoCreateInstance(rclsid,
1575 pUnkOuter,
1576 dwClsContext,
1577 &IID_IUnknown,
1578 (VOID**)&pUnk);
1580 if (hr)
1581 return hr;
1584 * Then, query for all the interfaces requested.
1586 for (index = 0; index < cmq; index++)
1588 pResults[index].hr = IUnknown_QueryInterface(pUnk,
1589 pResults[index].pIID,
1590 (VOID**)&(pResults[index].pItf));
1592 if (pResults[index].hr == S_OK)
1593 successCount++;
1597 * Release our temporary unknown pointer.
1599 IUnknown_Release(pUnk);
1601 if (successCount == 0)
1602 return E_NOINTERFACE;
1604 if (successCount!=cmq)
1605 return CO_S_NOTALLINTERFACES;
1607 return S_OK;
1610 /***********************************************************************
1611 * CoFreeLibrary [COMPOBJ.13]
1613 void WINAPI CoFreeLibrary(HINSTANCE hLibrary)
1615 OpenDll *ptr, *prev;
1616 OpenDll *tmp;
1618 /* lookup library in linked list */
1619 prev = NULL;
1620 for (ptr = openDllList; ptr != NULL; ptr=ptr->next) {
1621 if (ptr->hLibrary == hLibrary) {
1622 break;
1624 prev = ptr;
1627 if (ptr == NULL) {
1628 /* shouldn't happen if user passed in a valid hLibrary */
1629 return;
1631 /* assert: ptr points to the library entry to free */
1633 /* free library and remove node from list */
1634 FreeLibrary(hLibrary);
1635 if (ptr == openDllList) {
1636 tmp = openDllList->next;
1637 HeapFree(GetProcessHeap(), 0, openDllList->DllName);
1638 HeapFree(GetProcessHeap(), 0, openDllList);
1639 openDllList = tmp;
1640 } else {
1641 tmp = ptr->next;
1642 HeapFree(GetProcessHeap(), 0, ptr->DllName);
1643 HeapFree(GetProcessHeap(), 0, ptr);
1644 prev->next = tmp;
1650 /***********************************************************************
1651 * CoFreeAllLibraries [COMPOBJ.12]
1653 void WINAPI CoFreeAllLibraries(void)
1655 OpenDll *ptr, *tmp;
1657 for (ptr = openDllList; ptr != NULL; ) {
1658 tmp=ptr->next;
1659 CoFreeLibrary(ptr->hLibrary);
1660 ptr = tmp;
1666 /***********************************************************************
1667 * CoFreeUnusedLibraries [COMPOBJ.17]
1669 void WINAPI CoFreeUnusedLibraries(void)
1671 OpenDll *ptr, *tmp;
1672 typedef HRESULT(*DllCanUnloadNowFunc)(void);
1673 DllCanUnloadNowFunc DllCanUnloadNow;
1675 for (ptr = openDllList; ptr != NULL; ) {
1676 DllCanUnloadNow = (DllCanUnloadNowFunc)
1677 GetProcAddress(ptr->hLibrary, "DllCanUnloadNow");
1679 if ( (DllCanUnloadNow != NULL) &&
1680 (DllCanUnloadNow() == S_OK) ) {
1681 tmp=ptr->next;
1682 CoFreeLibrary(ptr->hLibrary);
1683 ptr = tmp;
1684 } else {
1685 ptr=ptr->next;
1690 /***********************************************************************
1691 * CoFileTimeNow [COMPOBJ.82, OLE32.10]
1692 * RETURNS
1693 * the current system time in lpFileTime
1695 HRESULT WINAPI CoFileTimeNow(
1696 FILETIME *lpFileTime /* [out] the current time */
1698 DOSFS_UnixTimeToFileTime(time(NULL), lpFileTime, 0);
1699 return S_OK;
1702 /***********************************************************************
1703 * CoTaskMemAlloc (OLE32.43)
1704 * RETURNS
1705 * pointer to newly allocated block
1707 LPVOID WINAPI CoTaskMemAlloc(
1708 ULONG size /* [in] size of memoryblock to be allocated */
1710 LPMALLOC lpmalloc;
1711 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1713 if (FAILED(ret))
1714 return NULL;
1716 return IMalloc_Alloc(lpmalloc,size);
1718 /***********************************************************************
1719 * CoTaskMemFree (OLE32.44)
1721 VOID WINAPI CoTaskMemFree(
1722 LPVOID ptr /* [in] pointer to be freed */
1724 LPMALLOC lpmalloc;
1725 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1727 if (FAILED(ret))
1728 return;
1730 IMalloc_Free(lpmalloc, ptr);
1733 /***********************************************************************
1734 * CoTaskMemRealloc (OLE32.45)
1735 * RETURNS
1736 * pointer to newly allocated block
1738 LPVOID WINAPI CoTaskMemRealloc(
1739 LPVOID pvOld,
1740 ULONG size) /* [in] size of memoryblock to be allocated */
1742 LPMALLOC lpmalloc;
1743 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1745 if (FAILED(ret))
1746 return NULL;
1748 return IMalloc_Realloc(lpmalloc, pvOld, size);
1751 /***********************************************************************
1752 * CoLoadLibrary (OLE32.30)
1754 HINSTANCE WINAPI CoLoadLibrary(LPOLESTR16 lpszLibName, BOOL bAutoFree)
1756 HINSTANCE hLibrary;
1757 OpenDll *ptr;
1758 OpenDll *tmp;
1760 TRACE("CoLoadLibrary(%p, %d\n", lpszLibName, bAutoFree);
1762 hLibrary = LoadLibraryExA(lpszLibName, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
1764 if (!bAutoFree)
1765 return hLibrary;
1767 if (openDllList == NULL) {
1768 /* empty list -- add first node */
1769 openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
1770 openDllList->DllName = HEAP_strdupA(GetProcessHeap(), 0, lpszLibName);
1771 openDllList->hLibrary = hLibrary;
1772 openDllList->next = NULL;
1773 } else {
1774 /* search for this dll */
1775 int found = FALSE;
1776 for (ptr = openDllList; ptr->next != NULL; ptr=ptr->next) {
1777 if (ptr->hLibrary == hLibrary) {
1778 found = TRUE;
1779 break;
1782 if (!found) {
1783 /* dll not found, add it */
1784 tmp = openDllList;
1785 openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
1786 openDllList->DllName = HEAP_strdupA(GetProcessHeap(), 0, lpszLibName);
1787 openDllList->hLibrary = hLibrary;
1788 openDllList->next = tmp;
1792 return hLibrary;
1795 /***********************************************************************
1796 * CoInitializeWOW (OLE32.27)
1798 HRESULT WINAPI CoInitializeWOW(DWORD x,DWORD y) {
1799 FIXME("(0x%08lx,0x%08lx),stub!\n",x,y);
1800 return 0;
1803 /******************************************************************************
1804 * CoLockObjectExternal16 [COMPOBJ.63]
1806 HRESULT WINAPI CoLockObjectExternal16(
1807 LPUNKNOWN pUnk, /* [in] object to be locked */
1808 BOOL16 fLock, /* [in] do lock */
1809 BOOL16 fLastUnlockReleases /* [in] ? */
1811 FIXME("(%p,%d,%d),stub!\n",pUnk,fLock,fLastUnlockReleases);
1812 return S_OK;
1815 /******************************************************************************
1816 * CoLockObjectExternal32 [OLE32.31]
1818 HRESULT WINAPI CoLockObjectExternal(
1819 LPUNKNOWN pUnk, /* [in] object to be locked */
1820 BOOL fLock, /* [in] do lock */
1821 BOOL fLastUnlockReleases) /* [in] unlock all */
1824 if (fLock)
1827 * Increment the external lock coutner, COM_ExternalLockAddRef also
1828 * increment the object's internal lock counter.
1830 COM_ExternalLockAddRef( pUnk);
1832 else
1835 * Decrement the external lock coutner, COM_ExternalLockRelease also
1836 * decrement the object's internal lock counter.
1838 COM_ExternalLockRelease( pUnk, fLastUnlockReleases);
1841 return S_OK;
1844 /***********************************************************************
1845 * CoGetState16 [COMPOBJ.115]
1847 HRESULT WINAPI CoGetState16(LPDWORD state)
1849 FIXME("(%p),stub!\n", state);
1850 *state = 0;
1851 return S_OK;
1853 /***********************************************************************
1854 * CoSetState32 [COM32.42]
1856 HRESULT WINAPI CoSetState(LPDWORD state)
1858 FIXME("(%p),stub!\n", state);
1859 if (state) *state = 0;
1860 return S_OK;
1864 /***********************************************************************
1865 * DllGetClassObject [OLE32.63]
1867 HRESULT WINAPI OLE32_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
1869 char xclsid[50],xiid[50];
1870 WINE_StringFromCLSID((LPCLSID)rclsid,xclsid);
1871 WINE_StringFromCLSID((LPCLSID)iid,xiid);
1872 FIXME("\n\tCLSID:\t%s,\n\tIID:\t%s\n",xclsid,xiid);
1874 *ppv = NULL;
1876 return CLASS_E_CLASSNOTAVAILABLE;
1880 /***
1881 * COM_RevokeAllClasses
1883 * This method is called when the COM libraries are uninitialized to
1884 * release all the references to the class objects registered with
1885 * the library
1887 static void COM_RevokeAllClasses()
1889 while (firstRegisteredClass!=0)
1891 CoRevokeClassObject(firstRegisteredClass->dwCookie);
1895 /****************************************************************************
1896 * COM External Lock methods implementation
1899 /****************************************************************************
1900 * Public - Method that increments the count for a IUnknown* in the linked
1901 * list. The item is inserted if not already in the list.
1903 static void COM_ExternalLockAddRef(
1904 IUnknown *pUnk)
1906 COM_ExternalLock *externalLock = COM_ExternalLockFind(pUnk);
1909 * Add an external lock to the object. If it was already externally
1910 * locked, just increase the reference count. If it was not.
1911 * add the item to the list.
1913 if ( externalLock == EL_NOT_FOUND )
1914 COM_ExternalLockInsert(pUnk);
1915 else
1916 externalLock->uRefCount++;
1919 * Add an internal lock to the object
1921 IUnknown_AddRef(pUnk);
1924 /****************************************************************************
1925 * Public - Method that decrements the count for a IUnknown* in the linked
1926 * list. The item is removed from the list if its count end up at zero or if
1927 * bRelAll is TRUE.
1929 static void COM_ExternalLockRelease(
1930 IUnknown *pUnk,
1931 BOOL bRelAll)
1933 COM_ExternalLock *externalLock = COM_ExternalLockFind(pUnk);
1935 if ( externalLock != EL_NOT_FOUND )
1939 externalLock->uRefCount--; /* release external locks */
1940 IUnknown_Release(pUnk); /* release local locks as well */
1942 if ( bRelAll == FALSE )
1943 break; /* perform single release */
1945 } while ( externalLock->uRefCount > 0 );
1947 if ( externalLock->uRefCount == 0 ) /* get rid of the list entry */
1948 COM_ExternalLockDelete(externalLock);
1951 /****************************************************************************
1952 * Public - Method that frees the content of the list.
1954 static void COM_ExternalLockFreeList()
1956 COM_ExternalLock *head;
1958 head = elList.head; /* grab it by the head */
1959 while ( head != EL_END_OF_LIST )
1961 COM_ExternalLockDelete(head); /* get rid of the head stuff */
1963 head = elList.head; /* get the new head... */
1967 /****************************************************************************
1968 * Public - Method that dump the content of the list.
1970 void COM_ExternalLockDump()
1972 COM_ExternalLock *current = elList.head;
1974 DPRINTF("\nExternal lock list contains:\n");
1976 while ( current != EL_END_OF_LIST )
1978 DPRINTF( "\t%p with %lu references count.\n", current->pUnk, current->uRefCount);
1980 /* Skip to the next item */
1981 current = current->next;
1986 /****************************************************************************
1987 * Internal - Find a IUnknown* in the linked list
1989 static COM_ExternalLock* COM_ExternalLockFind(
1990 IUnknown *pUnk)
1992 return COM_ExternalLockLocate(elList.head, pUnk);
1995 /****************************************************************************
1996 * Internal - Recursivity agent for IUnknownExternalLockList_Find
1998 static COM_ExternalLock* COM_ExternalLockLocate(
1999 COM_ExternalLock *element,
2000 IUnknown *pUnk)
2002 if ( element == EL_END_OF_LIST )
2003 return EL_NOT_FOUND;
2005 else if ( element->pUnk == pUnk ) /* We found it */
2006 return element;
2008 else /* Not the right guy, keep on looking */
2009 return COM_ExternalLockLocate( element->next, pUnk);
2012 /****************************************************************************
2013 * Internal - Insert a new IUnknown* to the linked list
2015 static BOOL COM_ExternalLockInsert(
2016 IUnknown *pUnk)
2018 COM_ExternalLock *newLock = NULL;
2019 COM_ExternalLock *previousHead = NULL;
2022 * Allocate space for the new storage object
2024 newLock = HeapAlloc(GetProcessHeap(), 0, sizeof(COM_ExternalLock));
2026 if (newLock!=NULL)
2028 if ( elList.head == EL_END_OF_LIST )
2030 elList.head = newLock; /* The list is empty */
2032 else
2035 * insert does it at the head
2037 previousHead = elList.head;
2038 elList.head = newLock;
2042 * Set new list item data member
2044 newLock->pUnk = pUnk;
2045 newLock->uRefCount = 1;
2046 newLock->next = previousHead;
2048 return TRUE;
2050 else
2051 return FALSE;
2054 /****************************************************************************
2055 * Internal - Method that removes an item from the linked list.
2057 static void COM_ExternalLockDelete(
2058 COM_ExternalLock *itemList)
2060 COM_ExternalLock *current = elList.head;
2062 if ( current == itemList )
2065 * this section handles the deletion of the first node
2067 elList.head = itemList->next;
2068 HeapFree( GetProcessHeap(), 0, itemList);
2070 else
2074 if ( current->next == itemList ) /* We found the item to free */
2076 current->next = itemList->next; /* readjust the list pointers */
2078 HeapFree( GetProcessHeap(), 0, itemList);
2079 break;
2082 /* Skip to the next item */
2083 current = current->next;
2085 } while ( current != EL_END_OF_LIST );
2089 /***********************************************************************
2090 * COMPOBJ_DllEntryPoint [COMPOBJ.entry]
2092 * Initialization code for the COMPOBJ DLL
2094 * RETURNS:
2096 BOOL WINAPI COMPOBJ_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst, WORD ds, WORD HeapSize, DWORD res1, WORD res2)
2098 TRACE("(%08lx, %04x, %04x, %04x, %08lx, %04x)\n", Reason, hInst, ds, HeapSize,
2099 res1, res2);
2100 switch(Reason)
2102 case DLL_PROCESS_ATTACH:
2103 COMPOBJ_Attach++;
2104 if(COMPOBJ_hInstance)
2106 ERR("compobj.dll instantiated twice!\n");
2108 * We should return FALSE here, but that will break
2109 * most apps that use CreateProcess because we do
2110 * not yet support seperate address-spaces.
2112 return TRUE;
2115 COMPOBJ_hInstance = hInst;
2116 break;
2118 case DLL_PROCESS_DETACH:
2119 if(!--COMPOBJ_Attach)
2120 COMPOBJ_hInstance = 0;
2121 break;
2123 return TRUE;