- Added missing configuration #if:s and #includes:s.
[wine.git] / dlls / ole32 / compobj.c
blob242d30ceb945c44059fbf8cf6ca2b60e7dc5ae09
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 "wine/winbase16.h"
39 #include "winerror.h"
40 #include "wownt32.h"
41 #include "ole.h"
42 #include "ole2ver.h"
43 #include "debugtools.h"
44 #include "file.h"
45 #include "heap.h"
46 #include "ldt.h"
47 #include "winreg.h"
49 #include "wine/obj_base.h"
50 #include "wine/obj_misc.h"
51 #include "wine/obj_storage.h"
52 #include "wine/obj_clientserver.h"
54 #include "ifs.h"
55 #include "compobj.h"
57 DEFAULT_DEBUG_CHANNEL(ole)
58 /****************************************************************************
59 * COM External Lock structures and methods declaration
61 * This api provides a linked list to managed external references to
62 * COM objects.
64 * The public interface consists of three calls:
65 * COM_ExternalLockAddRef
66 * COM_ExternalLockRelease
67 * COM_ExternalLockFreeList
70 #define EL_END_OF_LIST 0
71 #define EL_NOT_FOUND 0
74 * Declaration of the static structure that manage the
75 * external lock to COM objects.
77 typedef struct COM_ExternalLock COM_ExternalLock;
78 typedef struct COM_ExternalLockList COM_ExternalLockList;
80 struct COM_ExternalLock
82 IUnknown *pUnk; /* IUnknown referenced */
83 ULONG uRefCount; /* external lock counter to IUnknown object*/
84 COM_ExternalLock *next; /* Pointer to next element in list */
87 struct COM_ExternalLockList
89 COM_ExternalLock *head; /* head of list */
93 * Declaration and initialization of the static structure that manages
94 * the external lock to COM objects.
96 static COM_ExternalLockList elList = { EL_END_OF_LIST };
99 * Public Interface to the external lock list
101 static void COM_ExternalLockFreeList();
102 static void COM_ExternalLockAddRef(IUnknown *pUnk);
103 static void COM_ExternalLockRelease(IUnknown *pUnk, BOOL bRelAll);
104 void COM_ExternalLockDump(); /* testing purposes, not static to avoid warning */
107 * Private methods used to managed the linked list
109 static BOOL COM_ExternalLockInsert(
110 IUnknown *pUnk);
112 static void COM_ExternalLockDelete(
113 COM_ExternalLock *element);
115 static COM_ExternalLock* COM_ExternalLockFind(
116 IUnknown *pUnk);
118 static COM_ExternalLock* COM_ExternalLockLocate(
119 COM_ExternalLock *element,
120 IUnknown *pUnk);
122 /****************************************************************************
123 * This section defines variables internal to the COM module.
125 * TODO: Most of these things will have to be made thread-safe.
127 HINSTANCE16 COMPOBJ_hInstance = 0;
128 HINSTANCE COMPOBJ_hInstance32 = 0;
129 static int COMPOBJ_Attach = 0;
131 LPMALLOC16 currentMalloc16=NULL;
132 LPMALLOC currentMalloc32=NULL;
134 HTASK16 hETask = 0;
135 WORD Table_ETask[62];
138 * This lock count counts the number of times CoInitialize is called. It is
139 * decreased every time CoUninitialize is called. When it hits 0, the COM
140 * libraries are freed
142 static ULONG s_COMLockCount = 0;
145 * This linked list contains the list of registered class objects. These
146 * are mostly used to register the factories for out-of-proc servers of OLE
147 * objects.
149 * TODO: Make this data structure aware of inter-process communication. This
150 * means that parts of this will be exported to the Wine Server.
152 typedef struct tagRegisteredClass
154 CLSID classIdentifier;
155 LPUNKNOWN classObject;
156 DWORD runContext;
157 DWORD connectFlags;
158 DWORD dwCookie;
159 struct tagRegisteredClass* nextClass;
160 } RegisteredClass;
162 static RegisteredClass* firstRegisteredClass = NULL;
164 /* this open DLL table belongs in a per process table, but my guess is that
165 * it shouldn't live in the kernel, so I'll put them out here in DLL
166 * space assuming that there is one OLE32 per process.
168 typedef struct tagOpenDll {
169 char *DllName; /* really only needed for debugging */
170 HINSTANCE hLibrary;
171 struct tagOpenDll *next;
172 } OpenDll;
174 static OpenDll *openDllList = NULL; /* linked list of open dlls */
176 /*****************************************************************************
177 * This section contains prototypes to internal methods for this
178 * module
180 static HRESULT COM_GetRegisteredClassObject(REFCLSID rclsid,
181 DWORD dwClsContext,
182 LPUNKNOWN* ppUnk);
184 static void COM_RevokeAllClasses();
187 /******************************************************************************
188 * CoBuildVersion [COMPOBJ.1]
190 * RETURNS
191 * Current built version, hiword is majornumber, loword is minornumber
193 DWORD WINAPI CoBuildVersion(void)
195 TRACE("(void)\n");
196 return (rmm<<16)+rup;
199 /******************************************************************************
200 * CoInitialize16 [COMPOBJ.2]
201 * Set the win16 IMalloc used for memory management
203 HRESULT WINAPI CoInitialize16(
204 LPVOID lpReserved /* [in] pointer to win16 malloc interface */
206 currentMalloc16 = (LPMALLOC16)lpReserved;
207 return S_OK;
210 /******************************************************************************
211 * CoInitialize32 [OLE32.26]
213 * Initializes the COM libraries.
215 * See CoInitializeEx32
217 HRESULT WINAPI CoInitialize(
218 LPVOID lpReserved /* [in] pointer to win32 malloc interface
219 (obsolete, should be NULL) */
223 * Just delegate to the newer method.
225 return CoInitializeEx(lpReserved, COINIT_APARTMENTTHREADED);
228 /******************************************************************************
229 * CoInitializeEx32 [OLE32.163]
231 * Initializes the COM libraries. The behavior used to set the win32 IMalloc
232 * used for memory management is obsolete.
234 * RETURNS
235 * S_OK if successful,
236 * S_FALSE if this function was called already.
237 * RPC_E_CHANGED_MODE if a previous call to CoInitialize specified another
238 * threading model.
240 * BUGS
241 * Only the single threaded model is supported. As a result RPC_E_CHANGED_MODE
242 * is never returned.
244 * See the windows documentation for more details.
246 HRESULT WINAPI CoInitializeEx(
247 LPVOID lpReserved, /* [in] pointer to win32 malloc interface
248 (obsolete, should be NULL) */
249 DWORD dwCoInit /* [in] A value from COINIT specifies the threading model */
252 HRESULT hr;
254 TRACE("(%p, %x)\n", lpReserved, (int)dwCoInit);
256 if (lpReserved!=NULL)
258 ERR("(%p, %x) - Bad parameter passed-in %p, must be an old Windows Application\n", lpReserved, (int)dwCoInit, lpReserved);
262 * Check for unsupported features.
264 if (dwCoInit!=COINIT_APARTMENTTHREADED)
266 FIXME(":(%p,%x): unsupported flag %x\n", lpReserved, (int)dwCoInit, (int)dwCoInit);
267 /* Hope for the best and continue anyway */
271 * Check the lock count. If this is the first time going through the initialize
272 * process, we have to initialize the libraries.
274 if (s_COMLockCount==0)
277 * Initialize the various COM libraries and data structures.
279 TRACE("() - Initializing the COM libraries\n");
281 RunningObjectTableImpl_Initialize();
283 hr = S_OK;
285 else
286 hr = S_FALSE;
289 * Crank-up that lock count.
291 s_COMLockCount++;
293 return hr;
296 /***********************************************************************
297 * CoUninitialize16 [COMPOBJ.3]
298 * Don't know what it does.
299 * 3-Nov-98 -- this was originally misspelled, I changed it to what I
300 * believe is the correct spelling
302 void WINAPI CoUninitialize16(void)
304 TRACE("()\n");
305 CoFreeAllLibraries();
308 /***********************************************************************
309 * CoUninitialize32 [OLE32.47]
311 * This method will release the COM libraries.
313 * See the windows documentation for more details.
315 void WINAPI CoUninitialize(void)
317 TRACE("()\n");
320 * Decrease the reference count.
322 s_COMLockCount--;
325 * If we are back to 0 locks on the COM library, make sure we free
326 * all the associated data structures.
328 if (s_COMLockCount==0)
331 * Release the various COM libraries and data structures.
333 TRACE("() - Releasing the COM libraries\n");
335 RunningObjectTableImpl_UnInitialize();
337 * Release the references to the registered class objects.
339 COM_RevokeAllClasses();
342 * This will free the loaded COM Dlls.
344 CoFreeAllLibraries();
347 * This will free list of external references to COM objects.
349 COM_ExternalLockFreeList();
353 /***********************************************************************
354 * CoGetMalloc16 [COMPOBJ.4]
355 * RETURNS
356 * The current win16 IMalloc
358 HRESULT WINAPI CoGetMalloc16(
359 DWORD dwMemContext, /* [in] unknown */
360 LPMALLOC16 * lpMalloc /* [out] current win16 malloc interface */
362 if(!currentMalloc16)
363 currentMalloc16 = IMalloc16_Constructor();
364 *lpMalloc = currentMalloc16;
365 return S_OK;
368 /******************************************************************************
369 * CoGetMalloc32 [OLE32.20]
371 * RETURNS
372 * The current win32 IMalloc
374 HRESULT WINAPI CoGetMalloc(
375 DWORD dwMemContext, /* [in] unknown */
376 LPMALLOC *lpMalloc /* [out] current win32 malloc interface */
378 if(!currentMalloc32)
379 currentMalloc32 = IMalloc_Constructor();
380 *lpMalloc = currentMalloc32;
381 return S_OK;
384 /***********************************************************************
385 * CoCreateStandardMalloc16 [COMPOBJ.71]
387 HRESULT WINAPI CoCreateStandardMalloc16(DWORD dwMemContext,
388 LPMALLOC16 *lpMalloc)
390 /* FIXME: docu says we shouldn't return the same allocator as in
391 * CoGetMalloc16 */
392 *lpMalloc = IMalloc16_Constructor();
393 return S_OK;
396 /******************************************************************************
397 * CoDisconnectObject [COMPOBJ.15]
399 HRESULT WINAPI CoDisconnectObject( LPUNKNOWN lpUnk, DWORD reserved )
401 TRACE("%p %lx\n",lpUnk,reserved);
402 return S_OK;
405 /***********************************************************************
406 * IsEqualGUID16 [COMPOBJ.18]
408 * Compares two Unique Identifiers.
410 * RETURNS
411 * TRUE if equal
413 BOOL16 WINAPI IsEqualGUID16(
414 GUID* g1, /* [in] unique id 1 */
415 GUID* g2 /**/
417 return !memcmp( g1, g2, sizeof(GUID) );
420 /***********************************************************************
421 * IsEqualGUID32 [OLE32.76]
423 * Compares two Unique Identifiers.
425 * RETURNS
426 * TRUE if equal
428 BOOL WINAPI IsEqualGUID32(
429 REFGUID rguid1, /* [in] unique id 1 */
430 REFGUID rguid2 /* [in] unique id 2 */
433 return !memcmp(rguid1,rguid2,sizeof(GUID));
436 /******************************************************************************
437 * CLSIDFromString16 [COMPOBJ.20]
438 * Converts a unique identifier from it's string representation into
439 * the GUID struct.
441 * Class id: DWORD-WORD-WORD-BYTES[2]-BYTES[6]
443 * RETURNS
444 * the converted GUID
446 HRESULT WINAPI CLSIDFromString16(
447 LPCOLESTR16 idstr, /* [in] string representation of guid */
448 CLSID *id /* [out] GUID converted from string */
450 BYTE *s = (BYTE *) idstr;
451 BYTE *p;
452 int i;
453 BYTE table[256];
455 TRACE("%s -> %p\n", idstr, id);
457 /* quick lookup table */
458 memset(table, 0, 256);
460 for (i = 0; i < 10; i++) {
461 table['0' + i] = i;
463 for (i = 0; i < 6; i++) {
464 table['A' + i] = i+10;
465 table['a' + i] = i+10;
468 /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
470 if (strlen(idstr) != 38)
471 return OLE_ERROR_OBJECT;
473 p = (BYTE *) id;
475 s++; /* skip leading brace */
476 for (i = 0; i < 4; i++) {
477 p[3 - i] = table[*s]<<4 | table[*(s+1)];
478 s += 2;
480 p += 4;
481 s++; /* skip - */
483 for (i = 0; i < 2; i++) {
484 p[1-i] = table[*s]<<4 | table[*(s+1)];
485 s += 2;
487 p += 2;
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 /* these are just sequential bytes */
498 for (i = 0; i < 2; i++) {
499 *p++ = table[*s]<<4 | table[*(s+1)];
500 s += 2;
502 s++; /* skip - */
504 for (i = 0; i < 6; i++) {
505 *p++ = table[*s]<<4 | table[*(s+1)];
506 s += 2;
509 return S_OK;
512 /******************************************************************************
513 * CoCreateGuid[OLE32.6]
515 * Creates a 128bit GUID.
516 * Implemented according the DCE specification for UUID generation.
517 * Code is based upon uuid library in e2fsprogs by Theodore Ts'o.
518 * Copyright (C) 1996, 1997 Theodore Ts'o.
520 * RETURNS
522 * S_OK if successful.
524 HRESULT WINAPI CoCreateGuid(
525 GUID *pguid /* [out] points to the GUID to initialize */
527 static char has_init = 0;
528 unsigned char a[6];
529 static int adjustment = 0;
530 static struct timeval last = {0, 0};
531 static UINT16 clock_seq;
532 struct timeval tv;
533 unsigned long long clock_reg;
534 UINT clock_high, clock_low;
535 UINT16 temp_clock_seq, temp_clock_mid, temp_clock_hi_and_version;
536 #ifdef HAVE_NET_IF_H
537 int sd;
538 struct ifreq ifr, *ifrp;
539 struct ifconf ifc;
540 char buf[1024];
541 int n, i;
542 #endif
544 /* Have we already tried to get the MAC address? */
545 if (!has_init) {
546 #ifdef HAVE_NET_IF_H
547 /* BSD 4.4 defines the size of an ifreq to be
548 * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
549 * However, under earlier systems, sa_len isn't present, so
550 * the size is just sizeof(struct ifreq)
552 # ifdef HAVE_SA_LEN
553 # ifndef max
554 # define max(a,b) ((a) > (b) ? (a) : (b))
555 # endif
556 # define ifreq_size(i) max(sizeof(struct ifreq),\
557 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
558 # else
559 # define ifreq_size(i) sizeof(struct ifreq)
560 # endif /* HAVE_SA_LEN */
562 sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
563 if (sd < 0) {
564 /* if we can't open a socket, just use random numbers */
565 /* set the multicast bit to prevent conflicts with real cards */
566 a[0] = (rand() & 0xff) | 0x80;
567 a[1] = rand() & 0xff;
568 a[2] = rand() & 0xff;
569 a[3] = rand() & 0xff;
570 a[4] = rand() & 0xff;
571 a[5] = rand() & 0xff;
572 } else {
573 memset(buf, 0, sizeof(buf));
574 ifc.ifc_len = sizeof(buf);
575 ifc.ifc_buf = buf;
576 /* get the ifconf interface */
577 if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) {
578 close(sd);
579 /* no ifconf, so just use random numbers */
580 /* set the multicast bit to prevent conflicts with real cards */
581 a[0] = (rand() & 0xff) | 0x80;
582 a[1] = rand() & 0xff;
583 a[2] = rand() & 0xff;
584 a[3] = rand() & 0xff;
585 a[4] = rand() & 0xff;
586 a[5] = rand() & 0xff;
587 } else {
588 /* loop through the interfaces, looking for a valid one */
589 n = ifc.ifc_len;
590 for (i = 0; i < n; i+= ifreq_size(*ifr) ) {
591 ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i);
592 strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
593 /* try to get the address for this interface */
594 # ifdef SIOCGIFHWADDR
595 if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
596 continue;
597 memcpy(a, (unsigned char *)&ifr.ifr_hwaddr.sa_data, 6);
598 # else
599 # ifdef SIOCGENADDR
600 if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
601 continue;
602 memcpy(a, (unsigned char *) ifr.ifr_enaddr, 6);
603 # else
604 /* XXX we don't have a way of getting the hardware address */
605 close(sd);
606 a[0] = 0;
607 break;
608 # endif /* SIOCGENADDR */
609 # endif /* SIOCGIFHWADDR */
610 /* make sure it's not blank */
611 if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5])
612 continue;
614 goto valid_address;
616 /* if we didn't find a valid address, make a random one */
617 /* once again, set multicast bit to avoid conflicts */
618 a[0] = (rand() & 0xff) | 0x80;
619 a[1] = rand() & 0xff;
620 a[2] = rand() & 0xff;
621 a[3] = rand() & 0xff;
622 a[4] = rand() & 0xff;
623 a[5] = rand() & 0xff;
625 valid_address:
626 close(sd);
629 #else
630 /* no networking info, so generate a random address */
631 a[0] = (rand() & 0xff) | 0x80;
632 a[1] = rand() & 0xff;
633 a[2] = rand() & 0xff;
634 a[3] = rand() & 0xff;
635 a[4] = rand() & 0xff;
636 a[5] = rand() & 0xff;
637 #endif /* HAVE_NET_IF_H */
638 has_init = 1;
641 /* generate time element of GUID */
643 /* Assume that the gettimeofday() has microsecond granularity */
644 #define MAX_ADJUSTMENT 10
646 try_again:
647 gettimeofday(&tv, 0);
648 if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
649 clock_seq = ((rand() & 0xff) << 8) + (rand() & 0xff);
650 clock_seq &= 0x1FFF;
651 last = tv;
652 last.tv_sec--;
654 if ((tv.tv_sec < last.tv_sec) ||
655 ((tv.tv_sec == last.tv_sec) &&
656 (tv.tv_usec < last.tv_usec))) {
657 clock_seq = (clock_seq+1) & 0x1FFF;
658 adjustment = 0;
659 } else if ((tv.tv_sec == last.tv_sec) &&
660 (tv.tv_usec == last.tv_usec)) {
661 if (adjustment >= MAX_ADJUSTMENT)
662 goto try_again;
663 adjustment++;
664 } else
665 adjustment = 0;
667 clock_reg = tv.tv_usec*10 + adjustment;
668 clock_reg += ((unsigned long long) tv.tv_sec)*10000000;
669 clock_reg += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
671 clock_high = clock_reg >> 32;
672 clock_low = clock_reg;
673 temp_clock_seq = clock_seq | 0x8000;
674 temp_clock_mid = (UINT16)clock_high;
675 temp_clock_hi_and_version = (clock_high >> 16) | 0x1000;
677 /* pack the information into the GUID structure */
679 ((unsigned char*)&pguid->Data1)[3] = (unsigned char)clock_low;
680 clock_low >>= 8;
681 ((unsigned char*)&pguid->Data1)[2] = (unsigned char)clock_low;
682 clock_low >>= 8;
683 ((unsigned char*)&pguid->Data1)[1] = (unsigned char)clock_low;
684 clock_low >>= 8;
685 ((unsigned char*)&pguid->Data1)[0] = (unsigned char)clock_low;
687 ((unsigned char*)&pguid->Data2)[1] = (unsigned char)temp_clock_mid;
688 temp_clock_mid >>= 8;
689 ((unsigned char*)&pguid->Data2)[0] = (unsigned char)temp_clock_mid;
691 ((unsigned char*)&pguid->Data3)[1] = (unsigned char)temp_clock_hi_and_version;
692 temp_clock_hi_and_version >>= 8;
693 ((unsigned char*)&pguid->Data3)[0] = (unsigned char)temp_clock_hi_and_version;
695 ((unsigned char*)pguid->Data4)[1] = (unsigned char)temp_clock_seq;
696 temp_clock_seq >>= 8;
697 ((unsigned char*)pguid->Data4)[0] = (unsigned char)temp_clock_seq;
699 ((unsigned char*)pguid->Data4)[2] = a[0];
700 ((unsigned char*)pguid->Data4)[3] = a[1];
701 ((unsigned char*)pguid->Data4)[4] = a[2];
702 ((unsigned char*)pguid->Data4)[5] = a[3];
703 ((unsigned char*)pguid->Data4)[6] = a[4];
704 ((unsigned char*)pguid->Data4)[7] = a[5];
706 TRACE("%p", pguid);
708 return S_OK;
711 /******************************************************************************
712 * CLSIDFromString32 [OLE32.3]
713 * Converts a unique identifier from it's string representation into
714 * the GUID struct.
715 * RETURNS
716 * the converted GUID
718 HRESULT WINAPI CLSIDFromString(
719 LPCOLESTR idstr, /* [in] string representation of GUID */
720 CLSID *id /* [out] GUID represented by above string */
722 LPOLESTR16 xid = HEAP_strdupWtoA(GetProcessHeap(),0,idstr);
723 OLESTATUS ret = CLSIDFromString16(xid,id);
725 HeapFree(GetProcessHeap(),0,xid);
726 return ret;
729 /******************************************************************************
730 * WINE_StringFromCLSID [Internal]
731 * Converts a GUID into the respective string representation.
733 * NOTES
735 * RETURNS
736 * the string representation and OLESTATUS
738 HRESULT WINE_StringFromCLSID(
739 const CLSID *id, /* [in] GUID to be converted */
740 LPSTR idstr /* [out] pointer to buffer to contain converted guid */
742 static const char *hex = "0123456789ABCDEF";
743 char *s;
744 int i;
746 if (!id)
747 { ERR("called with id=Null\n");
748 *idstr = 0x00;
749 return E_FAIL;
752 sprintf(idstr, "{%08lX-%04X-%04X-%02X%02X-",
753 id->Data1, id->Data2, id->Data3,
754 id->Data4[0], id->Data4[1]);
755 s = &idstr[25];
757 /* 6 hex bytes */
758 for (i = 2; i < 8; i++) {
759 *s++ = hex[id->Data4[i]>>4];
760 *s++ = hex[id->Data4[i] & 0xf];
763 *s++ = '}';
764 *s++ = '\0';
766 TRACE("%p->%s\n", id, idstr);
768 return OLE_OK;
771 /******************************************************************************
772 * StringFromCLSID16 [COMPOBJ.19]
773 * Converts a GUID into the respective string representation.
774 * The target string is allocated using the OLE IMalloc.
775 * RETURNS
776 * the string representation and OLESTATUS
778 HRESULT WINAPI StringFromCLSID16(
779 REFCLSID id, /* [in] the GUID to be converted */
780 LPOLESTR16 *idstr /* [out] a pointer to a to-be-allocated segmented pointer pointing to the resulting string */
783 LPMALLOC16 mllc;
784 OLESTATUS ret;
785 DWORD args[2];
787 ret = CoGetMalloc16(0,&mllc);
788 if (ret) return ret;
790 args[0] = (DWORD)mllc;
791 args[1] = 40;
793 /* No need for a Callback entry, we have WOWCallback16Ex which does
794 * everything we need.
796 if (!WOWCallback16Ex(
797 (DWORD)((ICOM_VTABLE(IMalloc16)*)PTR_SEG_TO_LIN(
798 ((LPMALLOC16)PTR_SEG_TO_LIN(mllc))->lpvtbl)
799 )->fnAlloc,
800 WCB16_CDECL,
801 2*sizeof(DWORD),
802 (LPVOID)args,
803 (LPDWORD)idstr
804 )) {
805 WARN("CallTo16 IMalloc16 failed\n");
806 return E_FAIL;
808 return WINE_StringFromCLSID(id,PTR_SEG_TO_LIN(*idstr));
811 /******************************************************************************
812 * StringFromCLSID32 [OLE32.151]
813 * Converts a GUID into the respective string representation.
814 * The target string is allocated using the OLE IMalloc.
815 * RETURNS
816 * the string representation and OLESTATUS
818 HRESULT WINAPI StringFromCLSID(
819 REFCLSID id, /* [in] the GUID to be converted */
820 LPOLESTR *idstr /* [out] a pointer to a to-be-allocated pointer pointing to the resulting string */
822 char buf[80];
823 OLESTATUS ret;
824 LPMALLOC mllc;
826 if ((ret=CoGetMalloc(0,&mllc)))
827 return ret;
829 ret=WINE_StringFromCLSID(id,buf);
830 if (!ret) {
831 *idstr = mllc->lpvtbl->fnAlloc(mllc,strlen(buf)*2+2);
832 lstrcpyAtoW(*idstr,buf);
834 return ret;
837 /******************************************************************************
838 * StringFromGUID2 [COMPOBJ.76] [OLE32.152]
840 * Converts a global unique identifier into a string of an API-
841 * specified fixed format. (The usual {.....} stuff.)
843 * RETURNS
844 * The (UNICODE) string representation of the GUID in 'str'
845 * The length of the resulting string, 0 if there was any problem.
847 INT WINAPI
848 StringFromGUID2(REFGUID id, LPOLESTR str, INT cmax)
850 char xguid[80];
852 if (WINE_StringFromCLSID(id,xguid))
853 return 0;
854 if (strlen(xguid)>=cmax)
855 return 0;
856 lstrcpyAtoW(str,xguid);
857 return strlen(xguid);
860 /******************************************************************************
861 * CLSIDFromProgID16 [COMPOBJ.61]
862 * Converts a program id into the respective GUID. (By using a registry lookup)
863 * RETURNS
864 * riid associated with the progid
866 HRESULT WINAPI CLSIDFromProgID16(
867 LPCOLESTR16 progid, /* [in] program id as found in registry */
868 LPCLSID riid /* [out] associated CLSID */
870 char *buf,buf2[80];
871 DWORD buf2len;
872 HRESULT err;
873 HKEY xhkey;
875 buf = HeapAlloc(GetProcessHeap(),0,strlen(progid)+8);
876 sprintf(buf,"%s\\CLSID",progid);
877 if ((err=RegOpenKeyA(HKEY_CLASSES_ROOT,buf,&xhkey))) {
878 HeapFree(GetProcessHeap(),0,buf);
879 return OLE_ERROR_GENERIC;
881 HeapFree(GetProcessHeap(),0,buf);
882 buf2len = sizeof(buf2);
883 if ((err=RegQueryValueA(xhkey,NULL,buf2,&buf2len))) {
884 RegCloseKey(xhkey);
885 return OLE_ERROR_GENERIC;
887 RegCloseKey(xhkey);
888 return CLSIDFromString16(buf2,riid);
891 /******************************************************************************
892 * CLSIDFromProgID32 [OLE32.2]
893 * Converts a program id into the respective GUID. (By using a registry lookup)
894 * RETURNS
895 * riid associated with the progid
897 HRESULT WINAPI CLSIDFromProgID(
898 LPCOLESTR progid, /* [in] program id as found in registry */
899 LPCLSID riid /* [out] associated CLSID */
901 LPOLESTR16 pid = HEAP_strdupWtoA(GetProcessHeap(),0,progid);
902 OLESTATUS ret = CLSIDFromProgID16(pid,riid);
904 HeapFree(GetProcessHeap(),0,pid);
905 return ret;
908 /************************************************************************************************
909 * OleSaveToStream
911 * This function write a CLSID on stream
913 HRESULT WINAPI WriteClassStm(IStream *pStm,REFCLSID rclsid)
915 TRACE("(%p,%p)\n",pStm,rclsid);
917 if (rclsid==NULL)
918 return E_INVALIDARG;
920 return IStream_Write(pStm,rclsid,sizeof(CLSID),NULL);
923 /************************************************************************************************
924 * OleSaveToStream
926 * This function read a CLSID from a stream
928 HRESULT WINAPI ReadClassStm(IStream *pStm,REFCLSID rclsid)
930 ULONG nbByte;
931 HRESULT res;
933 TRACE("(%p,%p)\n",pStm,rclsid);
935 if (rclsid==NULL)
936 return E_INVALIDARG;
938 res = IStream_Read(pStm,(void*)rclsid,sizeof(CLSID),&nbByte);
940 if (FAILED(res))
941 return res;
943 if (nbByte != sizeof(CLSID))
944 return S_FALSE;
945 else
946 return S_OK;
949 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
950 /***********************************************************************
951 * LookupETask (COMPOBJ.94)
953 OLESTATUS WINAPI LookupETask16(HTASK16 *hTask,LPVOID p) {
954 FIXME("(%p,%p),stub!\n",hTask,p);
955 if ((*hTask = GetCurrentTask()) == hETask) {
956 memcpy(p, Table_ETask, sizeof(Table_ETask));
958 return 0;
961 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
962 /***********************************************************************
963 * SetETask (COMPOBJ.95)
965 OLESTATUS WINAPI SetETask16(HTASK16 hTask, LPVOID p) {
966 FIXME("(%04x,%p),stub!\n",hTask,p);
967 hETask = hTask;
968 return 0;
971 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
972 /***********************************************************************
973 * CallObjectInWOW (COMPOBJ.201)
975 OLESTATUS WINAPI CallObjectInWOW(LPVOID p1,LPVOID p2) {
976 FIXME("(%p,%p),stub!\n",p1,p2);
977 return 0;
980 /******************************************************************************
981 * CoRegisterClassObject16 [COMPOBJ.5]
983 * Don't know where it registers it ...
985 HRESULT WINAPI CoRegisterClassObject16(
986 REFCLSID rclsid,
987 LPUNKNOWN pUnk,
988 DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
989 DWORD flags, /* [in] REGCLS flags indicating how connections are made */
990 LPDWORD lpdwRegister
992 char buf[80];
994 WINE_StringFromCLSID(rclsid,buf);
996 FIXME("(%s,%p,0x%08lx,0x%08lx,%p),stub\n",
997 buf,pUnk,dwClsContext,flags,lpdwRegister
999 return 0;
1003 /******************************************************************************
1004 * CoRevokeClassObject16 [COMPOBJ.6]
1007 HRESULT WINAPI CoRevokeClassObject16(DWORD dwRegister /* token on class obj */)
1009 FIXME("(0x%08lx),stub!\n", dwRegister);
1010 return 0;
1014 /***
1015 * COM_GetRegisteredClassObject
1017 * This internal method is used to scan the registered class list to
1018 * find a class object.
1020 * Params:
1021 * rclsid Class ID of the class to find.
1022 * dwClsContext Class context to match.
1023 * ppv [out] returns a pointer to the class object. Complying
1024 * to normal COM usage, this method will increase the
1025 * reference count on this object.
1027 static HRESULT COM_GetRegisteredClassObject(
1028 REFCLSID rclsid,
1029 DWORD dwClsContext,
1030 LPUNKNOWN* ppUnk)
1032 RegisteredClass* curClass;
1035 * Sanity check
1037 assert(ppUnk!=0);
1040 * Iterate through the whole list and try to match the class ID.
1042 curClass = firstRegisteredClass;
1044 while (curClass != 0)
1047 * Check if we have a match on the class ID.
1049 if (IsEqualGUID32(&(curClass->classIdentifier), rclsid))
1052 * Since we don't do out-of process or DCOM just right away, let's ignore the
1053 * class context.
1057 * We have a match, return the pointer to the class object.
1059 *ppUnk = curClass->classObject;
1061 IUnknown_AddRef(curClass->classObject);
1063 return S_OK;
1067 * Step to the next class in the list.
1069 curClass = curClass->nextClass;
1073 * If we get to here, we haven't found our class.
1075 return S_FALSE;
1078 /******************************************************************************
1079 * CoRegisterClassObject32 [OLE32.36]
1081 * This method will register the class object for a given class ID.
1083 * See the Windows documentation for more details.
1085 HRESULT WINAPI CoRegisterClassObject(
1086 REFCLSID rclsid,
1087 LPUNKNOWN pUnk,
1088 DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
1089 DWORD flags, /* [in] REGCLS flags indicating how connections are made */
1090 LPDWORD lpdwRegister
1093 RegisteredClass* newClass;
1094 LPUNKNOWN foundObject;
1095 HRESULT hr;
1096 char buf[80];
1098 WINE_StringFromCLSID(rclsid,buf);
1100 TRACE("(%s,%p,0x%08lx,0x%08lx,%p)\n",
1101 buf,pUnk,dwClsContext,flags,lpdwRegister);
1104 * Perform a sanity check on the parameters
1106 if ( (lpdwRegister==0) || (pUnk==0) )
1108 return E_INVALIDARG;
1112 * Initialize the cookie (out parameter)
1114 *lpdwRegister = 0;
1117 * First, check if the class is already registered.
1118 * If it is, this should cause an error.
1120 hr = COM_GetRegisteredClassObject(rclsid, dwClsContext, &foundObject);
1122 if (hr == S_OK)
1125 * The COM_GetRegisteredClassObject increased the reference count on the
1126 * object so it has to be released.
1128 IUnknown_Release(foundObject);
1130 return CO_E_OBJISREG;
1134 * If it is not registered, we must create a new entry for this class and
1135 * append it to the registered class list.
1136 * We use the address of the chain node as the cookie since we are sure it's
1137 * unique.
1139 newClass = HeapAlloc(GetProcessHeap(), 0, sizeof(RegisteredClass));
1142 * Initialize the node.
1144 newClass->classIdentifier = *rclsid;
1145 newClass->runContext = dwClsContext;
1146 newClass->connectFlags = flags;
1147 newClass->dwCookie = (DWORD)newClass;
1148 newClass->nextClass = firstRegisteredClass;
1151 * Since we're making a copy of the object pointer, we have to increase it's
1152 * reference count.
1154 newClass->classObject = pUnk;
1155 IUnknown_AddRef(newClass->classObject);
1157 firstRegisteredClass = newClass;
1160 * Assign the out parameter (cookie)
1162 *lpdwRegister = newClass->dwCookie;
1165 * We're successfyl Yippee!
1167 return S_OK;
1170 /***********************************************************************
1171 * CoRevokeClassObject32 [OLE32.40]
1173 * This method will remove a class object from the class registry
1175 * See the Windows documentation for more details.
1177 HRESULT WINAPI CoRevokeClassObject(
1178 DWORD dwRegister)
1180 RegisteredClass** prevClassLink;
1181 RegisteredClass* curClass;
1183 TRACE("(%08lx)\n",dwRegister);
1186 * Iterate through the whole list and try to match the cookie.
1188 curClass = firstRegisteredClass;
1189 prevClassLink = &firstRegisteredClass;
1191 while (curClass != 0)
1194 * Check if we have a match on the cookie.
1196 if (curClass->dwCookie == dwRegister)
1199 * Remove the class from the chain.
1201 *prevClassLink = curClass->nextClass;
1204 * Release the reference to the class object.
1206 IUnknown_Release(curClass->classObject);
1209 * Free the memory used by the chain node.
1211 HeapFree(GetProcessHeap(), 0, curClass);
1213 return S_OK;
1217 * Step to the next class in the list.
1219 prevClassLink = &(curClass->nextClass);
1220 curClass = curClass->nextClass;
1224 * If we get to here, we haven't found our class.
1226 return E_INVALIDARG;
1229 /***********************************************************************
1230 * CoGetClassObject [COMPOBJ.7]
1232 HRESULT WINAPI CoGetClassObject(REFCLSID rclsid, DWORD dwClsContext,
1233 LPVOID pvReserved, REFIID iid, LPVOID *ppv)
1235 LPUNKNOWN regClassObject;
1236 char xclsid[50],xiid[50];
1237 HRESULT hres = E_UNEXPECTED;
1239 char dllName[MAX_PATH+1];
1240 DWORD dllNameLen = sizeof(dllName);
1241 HINSTANCE hLibrary;
1242 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid,
1243 REFIID iid, LPVOID *ppv);
1244 DllGetClassObjectFunc DllGetClassObject;
1246 WINE_StringFromCLSID((LPCLSID)rclsid,xclsid);
1247 WINE_StringFromCLSID((LPCLSID)iid,xiid);
1248 TRACE("\n\tCLSID:\t%s,\n\tIID:\t%s\n",xclsid,xiid);
1251 * First, try and see if we can't match the class ID with one of the
1252 * registered classes.
1254 if (S_OK == COM_GetRegisteredClassObject(rclsid, dwClsContext, &regClassObject))
1257 * Get the required interface from the retrieved pointer.
1259 hres = IUnknown_QueryInterface(regClassObject, iid, ppv);
1262 * Since QI got another reference on the pointer, we want to release the
1263 * one we already have. If QI was unsuccessful, this will release the object. This
1264 * is good since we are not returning it in the "out" parameter.
1266 IUnknown_Release(regClassObject);
1268 return hres;
1271 /* out of process and remote servers not supported yet */
1272 if ((CLSCTX_LOCAL_SERVER|CLSCTX_REMOTE_SERVER) & dwClsContext) {
1273 FIXME("CLSCTX_LOCAL_SERVER and CLSCTX_REMOTE_SERVER not supported!\n");
1274 return E_ACCESSDENIED;
1277 if ((CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER) & dwClsContext) {
1278 HKEY CLSIDkey,key;
1280 /* lookup CLSID in registry key HKCR/CLSID */
1281 hres = RegOpenKeyExA(HKEY_CLASSES_ROOT, "CLSID", 0,
1282 KEY_READ, &CLSIDkey);
1284 if (hres != ERROR_SUCCESS)
1285 return REGDB_E_READREGDB;
1286 hres = RegOpenKeyExA(CLSIDkey,xclsid,0,KEY_QUERY_VALUE,&key);
1287 if (hres != ERROR_SUCCESS) {
1288 RegCloseKey(CLSIDkey);
1289 return REGDB_E_CLASSNOTREG;
1291 hres = RegQueryValueA(key, "InprocServer32", dllName, &dllNameLen);
1292 RegCloseKey(key);
1293 RegCloseKey(CLSIDkey);
1294 if (hres != ERROR_SUCCESS)
1295 return REGDB_E_READREGDB;
1296 TRACE("found InprocServer32 dll %s\n", dllName);
1298 /* open dll, call DllGetClassFactory */
1299 hLibrary = CoLoadLibrary(dllName, TRUE);
1300 if (hLibrary == 0) {
1301 TRACE("couldn't load InprocServer32 dll %s\n", dllName);
1302 return E_ACCESSDENIED; /* or should this be CO_E_DLLNOTFOUND? */
1304 DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress(hLibrary, "DllGetClassObject");
1305 if (!DllGetClassObject) {
1306 /* not sure if this should be called here CoFreeLibrary(hLibrary);*/
1307 TRACE("couldn't find function DllGetClassObject in %s\n", dllName);
1308 return E_ACCESSDENIED;
1312 * Ask the DLL for it's class object. (there was a note here about class
1313 * factories but this is good.
1315 return DllGetClassObject(rclsid, iid, ppv);
1317 return hres;
1320 /****************************************************************************************
1321 * GetClassFile
1323 * This function supplies the CLSID associated with the given filename.
1325 HRESULT WINAPI GetClassFile(LPOLESTR filePathName,CLSID *pclsid)
1327 IStorage *pstg=0;
1328 HRESULT res;
1329 int nbElm=0,length=0,i=0;
1330 LONG sizeProgId=20;
1331 LPOLESTR *pathDec=0,absFile=0,progId=0;
1332 WCHAR extention[100]={0};
1334 TRACE("()\n");
1336 /* if the file contain a storage object the return the CLSID writen by IStorage_SetClass method*/
1337 if((StgIsStorageFile(filePathName))==S_OK){
1339 res=StgOpenStorage(filePathName,NULL,STGM_READ | STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
1341 if (SUCCEEDED(res))
1342 res=ReadClassStg(pstg,pclsid);
1344 IStorage_Release(pstg);
1346 return res;
1348 /* if the file is not a storage object then attemps to match various bits in the file against a
1349 pattern in the registry. this case is not frequently used ! so I present only the psodocode for
1350 this case
1352 for(i=0;i<nFileTypes;i++)
1354 for(i=0;j<nPatternsForType;j++){
1356 PATTERN pat;
1357 HANDLE hFile;
1359 pat=ReadPatternFromRegistry(i,j);
1360 hFile=CreateFileW(filePathName,,,,,,hFile);
1361 SetFilePosition(hFile,pat.offset);
1362 ReadFile(hFile,buf,pat.size,NULL,NULL);
1363 if (memcmp(buf&pat.mask,pat.pattern.pat.size)==0){
1365 *pclsid=ReadCLSIDFromRegistry(i);
1366 return S_OK;
1371 /* if the obove strategies fail then search for the extension key in the registry */
1373 /* get the last element (absolute file) in the path name */
1374 nbElm=FileMonikerImpl_DecomposePath(filePathName,&pathDec);
1375 absFile=pathDec[nbElm-1];
1377 /* failed if the path represente a directory and not an absolute file name*/
1378 if (lstrcmpW(absFile,(LPOLESTR)"\\"))
1379 return MK_E_INVALIDEXTENSION;
1381 /* get the extension of the file */
1382 length=lstrlenW(absFile);
1383 for(i=length-1; ( (i>=0) && (extention[i]=absFile[i]) );i--);
1385 /* get the progId associated to the extension */
1386 progId=CoTaskMemAlloc(sizeProgId);
1388 res=RegQueryValueW(HKEY_CLASSES_ROOT,extention,progId,&sizeProgId);
1390 if (res==ERROR_MORE_DATA){
1392 CoTaskMemRealloc(progId,sizeProgId);
1394 res=RegQueryValueW(HKEY_CLASSES_ROOT,extention,progId,&sizeProgId);
1396 if (res==ERROR_SUCCESS)
1397 /* return the clsid associated to the progId */
1398 res= CLSIDFromProgID(progId,pclsid);
1400 for(i=0; pathDec[i]!=NULL;i++)
1401 CoTaskMemFree(pathDec[i]);
1402 CoTaskMemFree(pathDec);
1404 CoTaskMemFree(progId);
1406 if (res==ERROR_SUCCESS)
1407 return res;
1409 return MK_E_INVALIDEXTENSION;
1411 /******************************************************************************
1412 * CoRegisterMessageFilter16 [COMPOBJ.27]
1414 HRESULT WINAPI CoRegisterMessageFilter16(
1415 LPMESSAGEFILTER lpMessageFilter,
1416 LPMESSAGEFILTER *lplpMessageFilter
1418 FIXME("(%p,%p),stub!\n",lpMessageFilter,lplpMessageFilter);
1419 return 0;
1422 /***********************************************************************
1423 * CoCreateInstance [COMPOBJ.13, OLE32.7]
1425 HRESULT WINAPI CoCreateInstance(
1426 REFCLSID rclsid,
1427 LPUNKNOWN pUnkOuter,
1428 DWORD dwClsContext,
1429 REFIID iid,
1430 LPVOID *ppv)
1432 HRESULT hres;
1433 LPCLASSFACTORY lpclf = 0;
1436 * Sanity check
1438 if (ppv==0)
1439 return E_POINTER;
1442 * Initialize the "out" parameter
1444 *ppv = 0;
1447 * Get a class factory to construct the object we want.
1449 hres = CoGetClassObject(rclsid,
1450 dwClsContext,
1451 NULL,
1452 &IID_IClassFactory,
1453 (LPVOID)&lpclf);
1455 if (FAILED(hres))
1456 return hres;
1459 * Create the object and don't forget to release the factory
1461 hres = IClassFactory_CreateInstance(lpclf, pUnkOuter, iid, ppv);
1462 IClassFactory_Release(lpclf);
1464 return hres;
1467 /***********************************************************************
1468 * CoCreateInstanceEx [OLE32.165]
1470 HRESULT WINAPI CoCreateInstanceEx(
1471 REFCLSID rclsid,
1472 LPUNKNOWN pUnkOuter,
1473 DWORD dwClsContext,
1474 COSERVERINFO* pServerInfo,
1475 ULONG cmq,
1476 MULTI_QI* pResults)
1478 IUnknown* pUnk = NULL;
1479 HRESULT hr;
1480 ULONG index;
1481 int successCount = 0;
1484 * Sanity check
1486 if ( (cmq==0) || (pResults==NULL))
1487 return E_INVALIDARG;
1489 if (pServerInfo!=NULL)
1490 FIXME("() non-NULL pServerInfo not supported!\n");
1493 * Initialize all the "out" parameters.
1495 for (index = 0; index < cmq; index++)
1497 pResults[index].pItf = NULL;
1498 pResults[index].hr = E_NOINTERFACE;
1502 * Get the object and get it's IUnknown pointer.
1504 hr = CoCreateInstance(rclsid,
1505 pUnkOuter,
1506 dwClsContext,
1507 &IID_IUnknown,
1508 (VOID**)&pUnk);
1510 if (hr)
1511 return hr;
1514 * Then, query for all the interfaces requested.
1516 for (index = 0; index < cmq; index++)
1518 pResults[index].hr = IUnknown_QueryInterface(pUnk,
1519 pResults[index].pIID,
1520 (VOID**)&(pResults[index].pItf));
1522 if (pResults[index].hr == S_OK)
1523 successCount++;
1527 * Release our temporary unknown pointer.
1529 IUnknown_Release(pUnk);
1531 if (successCount == 0)
1532 return E_NOINTERFACE;
1534 if (successCount!=cmq)
1535 return CO_S_NOTALLINTERFACES;
1537 return S_OK;
1540 /***********************************************************************
1541 * CoFreeLibrary [COMPOBJ.13]
1543 void WINAPI CoFreeLibrary(HINSTANCE hLibrary)
1545 OpenDll *ptr, *prev;
1546 OpenDll *tmp;
1548 /* lookup library in linked list */
1549 prev = NULL;
1550 for (ptr = openDllList; ptr != NULL; ptr=ptr->next) {
1551 if (ptr->hLibrary == hLibrary) {
1552 break;
1554 prev = ptr;
1557 if (ptr == NULL) {
1558 /* shouldn't happen if user passed in a valid hLibrary */
1559 return;
1561 /* assert: ptr points to the library entry to free */
1563 /* free library and remove node from list */
1564 FreeLibrary(hLibrary);
1565 if (ptr == openDllList) {
1566 tmp = openDllList->next;
1567 HeapFree(GetProcessHeap(), 0, openDllList->DllName);
1568 HeapFree(GetProcessHeap(), 0, openDllList);
1569 openDllList = tmp;
1570 } else {
1571 tmp = ptr->next;
1572 HeapFree(GetProcessHeap(), 0, ptr->DllName);
1573 HeapFree(GetProcessHeap(), 0, ptr);
1574 prev->next = tmp;
1580 /***********************************************************************
1581 * CoFreeAllLibraries [COMPOBJ.12]
1583 void WINAPI CoFreeAllLibraries(void)
1585 OpenDll *ptr, *tmp;
1587 for (ptr = openDllList; ptr != NULL; ) {
1588 tmp=ptr->next;
1589 CoFreeLibrary(ptr->hLibrary);
1590 ptr = tmp;
1596 /***********************************************************************
1597 * CoFreeUnusedLibraries [COMPOBJ.17]
1599 void WINAPI CoFreeUnusedLibraries(void)
1601 OpenDll *ptr, *tmp;
1602 typedef HRESULT(*DllCanUnloadNowFunc)(void);
1603 DllCanUnloadNowFunc DllCanUnloadNow;
1605 for (ptr = openDllList; ptr != NULL; ) {
1606 DllCanUnloadNow = (DllCanUnloadNowFunc)
1607 GetProcAddress(ptr->hLibrary, "DllCanUnloadNow");
1609 if ( (DllCanUnloadNow != NULL) &&
1610 (DllCanUnloadNow() == S_OK) ) {
1611 tmp=ptr->next;
1612 CoFreeLibrary(ptr->hLibrary);
1613 ptr = tmp;
1614 } else {
1615 ptr=ptr->next;
1620 /***********************************************************************
1621 * CoFileTimeNow [COMPOBJ.82, OLE32.10]
1622 * RETURNS
1623 * the current system time in lpFileTime
1625 HRESULT WINAPI CoFileTimeNow(
1626 FILETIME *lpFileTime /* [out] the current time */
1628 DOSFS_UnixTimeToFileTime(time(NULL), lpFileTime, 0);
1629 return S_OK;
1632 /***********************************************************************
1633 * CoTaskMemAlloc (OLE32.43)
1634 * RETURNS
1635 * pointer to newly allocated block
1637 LPVOID WINAPI CoTaskMemAlloc(
1638 ULONG size /* [in] size of memoryblock to be allocated */
1640 LPMALLOC lpmalloc;
1641 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1643 if (FAILED(ret))
1644 return NULL;
1646 return IMalloc_Alloc(lpmalloc,size);
1648 /***********************************************************************
1649 * CoTaskMemFree (OLE32.44)
1651 VOID WINAPI CoTaskMemFree(
1652 LPVOID ptr /* [in] pointer to be freed */
1654 LPMALLOC lpmalloc;
1655 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1657 if (FAILED(ret))
1658 return;
1660 IMalloc_Free(lpmalloc, ptr);
1663 /***********************************************************************
1664 * CoTaskMemRealloc (OLE32.45)
1665 * RETURNS
1666 * pointer to newly allocated block
1668 LPVOID WINAPI CoTaskMemRealloc(
1669 LPVOID pvOld,
1670 ULONG size) /* [in] size of memoryblock to be allocated */
1672 LPMALLOC lpmalloc;
1673 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1675 if (FAILED(ret))
1676 return NULL;
1678 return IMalloc_Realloc(lpmalloc, pvOld, size);
1681 /***********************************************************************
1682 * CoLoadLibrary (OLE32.30)
1684 HINSTANCE WINAPI CoLoadLibrary(LPOLESTR16 lpszLibName, BOOL bAutoFree)
1686 HINSTANCE hLibrary;
1687 OpenDll *ptr;
1688 OpenDll *tmp;
1690 TRACE("CoLoadLibrary(%p, %d\n", lpszLibName, bAutoFree);
1692 hLibrary = LoadLibraryExA(lpszLibName, 0, LOAD_WITH_ALTERED_SEARCH_PATH);
1694 if (!bAutoFree)
1695 return hLibrary;
1697 if (openDllList == NULL) {
1698 /* empty list -- add first node */
1699 openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
1700 openDllList->DllName = HEAP_strdupA(GetProcessHeap(), 0, lpszLibName);
1701 openDllList->hLibrary = hLibrary;
1702 openDllList->next = NULL;
1703 } else {
1704 /* search for this dll */
1705 int found = FALSE;
1706 for (ptr = openDllList; ptr->next != NULL; ptr=ptr->next) {
1707 if (ptr->hLibrary == hLibrary) {
1708 found = TRUE;
1709 break;
1712 if (!found) {
1713 /* dll not found, add it */
1714 tmp = openDllList;
1715 openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
1716 openDllList->DllName = HEAP_strdupA(GetProcessHeap(), 0, lpszLibName);
1717 openDllList->hLibrary = hLibrary;
1718 openDllList->next = tmp;
1722 return hLibrary;
1725 /***********************************************************************
1726 * CoInitializeWOW (OLE32.27)
1728 HRESULT WINAPI CoInitializeWOW(DWORD x,DWORD y) {
1729 FIXME("(0x%08lx,0x%08lx),stub!\n",x,y);
1730 return 0;
1733 /******************************************************************************
1734 * CoLockObjectExternal16 [COMPOBJ.63]
1736 HRESULT WINAPI CoLockObjectExternal16(
1737 LPUNKNOWN pUnk, /* [in] object to be locked */
1738 BOOL16 fLock, /* [in] do lock */
1739 BOOL16 fLastUnlockReleases /* [in] ? */
1741 FIXME("(%p,%d,%d),stub!\n",pUnk,fLock,fLastUnlockReleases);
1742 return S_OK;
1745 /******************************************************************************
1746 * CoLockObjectExternal32 [OLE32.31]
1748 HRESULT WINAPI CoLockObjectExternal(
1749 LPUNKNOWN pUnk, /* [in] object to be locked */
1750 BOOL fLock, /* [in] do lock */
1751 BOOL fLastUnlockReleases) /* [in] unlock all */
1754 if (fLock)
1757 * Increment the external lock coutner, COM_ExternalLockAddRef also
1758 * increment the object's internal lock counter.
1760 COM_ExternalLockAddRef( pUnk);
1762 else
1765 * Decrement the external lock coutner, COM_ExternalLockRelease also
1766 * decrement the object's internal lock counter.
1768 COM_ExternalLockRelease( pUnk, fLastUnlockReleases);
1771 return S_OK;
1774 /***********************************************************************
1775 * CoGetState16 [COMPOBJ.115]
1777 HRESULT WINAPI CoGetState16(LPDWORD state)
1779 FIXME("(%p),stub!\n", state);
1780 *state = 0;
1781 return S_OK;
1783 /***********************************************************************
1784 * CoSetState32 [COM32.42]
1786 HRESULT WINAPI CoSetState(LPDWORD state)
1788 FIXME("(%p),stub!\n", state);
1789 if (state) *state = 0;
1790 return S_OK;
1794 /***********************************************************************
1795 * DllGetClassObject [OLE32.63]
1797 HRESULT WINAPI OLE32_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
1799 char xclsid[50],xiid[50];
1800 WINE_StringFromCLSID((LPCLSID)rclsid,xclsid);
1801 WINE_StringFromCLSID((LPCLSID)iid,xiid);
1802 FIXME("\n\tCLSID:\t%s,\n\tIID:\t%s\n",xclsid,xiid);
1804 *ppv = NULL;
1806 return CLASS_E_CLASSNOTAVAILABLE;
1810 /***
1811 * COM_RevokeAllClasses
1813 * This method is called when the COM libraries are uninitialized to
1814 * release all the references to the class objects registered with
1815 * the library
1817 static void COM_RevokeAllClasses()
1819 while (firstRegisteredClass!=0)
1821 CoRevokeClassObject(firstRegisteredClass->dwCookie);
1825 /****************************************************************************
1826 * COM External Lock methods implementation
1829 /****************************************************************************
1830 * Public - Method that increments the count for a IUnknown* in the linked
1831 * list. The item is inserted if not already in the list.
1833 static void COM_ExternalLockAddRef(
1834 IUnknown *pUnk)
1836 COM_ExternalLock *externalLock = COM_ExternalLockFind(pUnk);
1839 * Add an external lock to the object. If it was already externally
1840 * locked, just increase the reference count. If it was not.
1841 * add the item to the list.
1843 if ( externalLock == EL_NOT_FOUND )
1844 COM_ExternalLockInsert(pUnk);
1845 else
1846 externalLock->uRefCount++;
1849 * Add an internal lock to the object
1851 IUnknown_AddRef(pUnk);
1854 /****************************************************************************
1855 * Public - Method that decrements the count for a IUnknown* in the linked
1856 * list. The item is removed from the list if its count end up at zero or if
1857 * bRelAll is TRUE.
1859 static void COM_ExternalLockRelease(
1860 IUnknown *pUnk,
1861 BOOL bRelAll)
1863 COM_ExternalLock *externalLock = COM_ExternalLockFind(pUnk);
1865 if ( externalLock != EL_NOT_FOUND )
1869 externalLock->uRefCount--; /* release external locks */
1870 IUnknown_Release(pUnk); /* release local locks as well */
1872 if ( bRelAll == FALSE )
1873 break; /* perform single release */
1875 } while ( externalLock->uRefCount > 0 );
1877 if ( externalLock->uRefCount == 0 ) /* get rid of the list entry */
1878 COM_ExternalLockDelete(externalLock);
1881 /****************************************************************************
1882 * Public - Method that frees the content of the list.
1884 static void COM_ExternalLockFreeList()
1886 COM_ExternalLock *head;
1888 head = elList.head; /* grab it by the head */
1889 while ( head != EL_END_OF_LIST )
1891 COM_ExternalLockDelete(head); /* get rid of the head stuff */
1893 head = elList.head; /* get the new head... */
1897 /****************************************************************************
1898 * Public - Method that dump the content of the list.
1900 void COM_ExternalLockDump()
1902 COM_ExternalLock *current = elList.head;
1904 printf("\nExternal lock list contains:\n");
1906 while ( current != EL_END_OF_LIST )
1908 printf( "\t%p with %lu references count.\n",
1909 current->pUnk,
1910 current->uRefCount);
1912 /* Skip to the next item */
1913 current = current->next;
1918 /****************************************************************************
1919 * Internal - Find a IUnknown* in the linked list
1921 static COM_ExternalLock* COM_ExternalLockFind(
1922 IUnknown *pUnk)
1924 return COM_ExternalLockLocate(elList.head, pUnk);
1927 /****************************************************************************
1928 * Internal - Recursivity agent for IUnknownExternalLockList_Find
1930 static COM_ExternalLock* COM_ExternalLockLocate(
1931 COM_ExternalLock *element,
1932 IUnknown *pUnk)
1934 if ( element == EL_END_OF_LIST )
1935 return EL_NOT_FOUND;
1937 else if ( element->pUnk == pUnk ) /* We found it */
1938 return element;
1940 else /* Not the right guy, keep on looking */
1941 return COM_ExternalLockLocate( element->next, pUnk);
1944 /****************************************************************************
1945 * Internal - Insert a new IUnknown* to the linked list
1947 static BOOL COM_ExternalLockInsert(
1948 IUnknown *pUnk)
1950 COM_ExternalLock *newLock = NULL;
1951 COM_ExternalLock *previousHead = NULL;
1954 * Allocate space for the new storage object
1956 newLock = HeapAlloc(GetProcessHeap(), 0, sizeof(COM_ExternalLock));
1958 if (newLock!=NULL)
1960 if ( elList.head == EL_END_OF_LIST )
1962 elList.head = newLock; /* The list is empty */
1964 else
1967 * insert does it at the head
1969 previousHead = elList.head;
1970 elList.head = newLock;
1974 * Set new list item data member
1976 newLock->pUnk = pUnk;
1977 newLock->uRefCount = 1;
1978 newLock->next = previousHead;
1980 return TRUE;
1982 else
1983 return FALSE;
1986 /****************************************************************************
1987 * Internal - Method that removes an item from the linked list.
1989 static void COM_ExternalLockDelete(
1990 COM_ExternalLock *itemList)
1992 COM_ExternalLock *current = elList.head;
1994 if ( current == itemList )
1997 * this section handles the deletion of the first node
1999 elList.head = itemList->next;
2000 HeapFree( GetProcessHeap(), 0, itemList);
2002 else
2006 if ( current->next == itemList ) /* We found the item to free */
2008 current->next = itemList->next; /* readjust the list pointers */
2010 HeapFree( GetProcessHeap(), 0, itemList);
2011 break;
2014 /* Skip to the next item */
2015 current = current->next;
2017 } while ( current != EL_END_OF_LIST );
2021 /***********************************************************************
2022 * COMPOBJ_DllEntryPoint [COMPOBJ.entry]
2024 * Initialization code for the COMPOBJ DLL
2026 * RETURNS:
2028 BOOL WINAPI COMPOBJ_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst, WORD ds, WORD HeapSize, DWORD res1, WORD res2)
2030 TRACE("(%08lx, %04x, %04x, %04x, %08lx, %04x)\n", Reason, hInst, ds, HeapSize,
2031 res1, res2);
2032 switch(Reason)
2034 case DLL_PROCESS_ATTACH:
2035 COMPOBJ_Attach++;
2036 if(COMPOBJ_hInstance)
2038 ERR("compobj.dll instantiated twice!\n");
2040 * We should return FALSE here, but that will break
2041 * most apps that use CreateProcess because we do
2042 * not yet support seperate address-spaces.
2044 return TRUE;
2047 COMPOBJ_hInstance = hInst;
2048 break;
2050 case DLL_PROCESS_DETACH:
2051 if(!--COMPOBJ_Attach)
2052 COMPOBJ_hInstance = 0;
2053 break;
2055 return TRUE;