Implementation of IStorage::CopyTo and IStream::CopyTo.
[wine.git] / ole / compobj.c
blob4fa8f0be9c689af48569753cd5e85d76a99ae9c0
1 /*
2 * COMPOBJ library
4 * Copyright 1995 Martin von Loewis
5 * Copyright 1998 Justin Bradford
6 * Copyright 1999 Francis Beaudet
7 * Copyright 1999 Sylvain St-Germain
8 */
10 #include "config.h"
11 #ifdef HAVE_UNISTD_H
12 # include <unistd.h>
13 #endif
14 #include <fcntl.h>
15 #include <sys/types.h>
16 #include <sys/time.h>
17 #include <sys/stat.h>
18 #ifdef HAVE_SYS_FILE_H
19 # include <sys/file.h>
20 #endif
21 #include <sys/ioctl.h>
22 #include <sys/socket.h>
23 #ifdef HAVE_SYS_SOCKIO_H
24 # include <sys/sockio.h>
25 #endif
26 #ifdef HAVE_NET_IF_H
27 # include <net/if.h>
28 #endif
29 #ifdef HAVE_NETINET_IN_H
30 # include <netinet/in.h>
31 #endif
32 #include <stdlib.h>
33 #include <string.h>
34 #include <time.h>
35 #include <assert.h>
36 #include "windef.h"
37 #include "wine/winbase16.h"
38 #include "wine/winestring.h"
39 #include "winerror.h"
40 #include "ole.h"
41 #include "ole2ver.h"
42 #include "debug.h"
43 #include "file.h"
44 #include "heap.h"
45 #include "ldt.h"
46 #include "winreg.h"
48 #include "wine/obj_base.h"
49 #include "wine/obj_misc.h"
50 #include "wine/obj_clientserver.h"
52 #include "ifs.h"
53 #include "compobj.h"
54 /****************************************************************************
55 * COM External Lock structures and methods declaration
57 * This api provides a linked list to managed external references to
58 * COM objects.
60 * The public interface consists of three calls:
61 * COM_ExternalLockAddRef
62 * COM_ExternalLockRelease
63 * COM_ExternalLockFreeList
66 #define EL_END_OF_LIST 0
67 #define EL_NOT_FOUND 0
70 * Declaration of the static structure that manage the
71 * external lock to COM objects.
73 typedef struct COM_ExternalLock COM_ExternalLock;
74 typedef struct COM_ExternalLockList COM_ExternalLockList;
76 struct COM_ExternalLock
78 IUnknown *pUnk; /* IUnknown referenced */
79 ULONG uRefCount; /* external lock counter to IUnknown object*/
80 COM_ExternalLock *next; /* Pointer to next element in list */
83 struct COM_ExternalLockList
85 COM_ExternalLock *head; /* head of list */
89 * Declaration and initialization of the static structure that manages
90 * the external lock to COM objects.
92 static COM_ExternalLockList elList = { EL_END_OF_LIST };
95 * Public Interface to the external lock list
97 static void COM_ExternalLockFreeList();
98 static void COM_ExternalLockAddRef(IUnknown *pUnk);
99 static void COM_ExternalLockRelease(IUnknown *pUnk, BOOL bRelAll);
100 void COM_ExternalLockDump(); /* testing purposes, not static to avoid warning */
103 * Private methods used to managed the linked list
105 static BOOL COM_ExternalLockInsert(
106 IUnknown *pUnk);
108 static void COM_ExternalLockDelete(
109 COM_ExternalLock *element);
111 static COM_ExternalLock* COM_ExternalLockFind(
112 IUnknown *pUnk);
114 static COM_ExternalLock* COM_ExternalLockLocate(
115 COM_ExternalLock *element,
116 IUnknown *pUnk);
118 /****************************************************************************
119 * This section defines variables internal to the COM module.
121 * TODO: Most of these things will have to be made thread-safe.
123 LPMALLOC16 currentMalloc16=NULL;
124 LPMALLOC currentMalloc32=NULL;
126 HTASK16 hETask = 0;
127 WORD Table_ETask[62];
130 * This lock count counts the number of times CoInitialize is called. It is
131 * decreased every time CoUninitialize is called. When it hits 0, the COM
132 * libraries are freed
134 static ULONG s_COMLockCount = 0;
137 * This linked list contains the list of registered class objects. These
138 * are mostly used to register the factories for out-of-proc servers of OLE
139 * objects.
141 * TODO: Make this data structure aware of inter-process communication. This
142 * means that parts of this will be exported to the Wine Server.
144 typedef struct tagRegisteredClass
146 CLSID classIdentifier;
147 LPUNKNOWN classObject;
148 DWORD runContext;
149 DWORD connectFlags;
150 DWORD dwCookie;
151 struct tagRegisteredClass* nextClass;
152 } RegisteredClass;
154 static RegisteredClass* firstRegisteredClass = NULL;
156 /* this open DLL table belongs in a per process table, but my guess is that
157 * it shouldn't live in the kernel, so I'll put them out here in DLL
158 * space assuming that there is one OLE32 per process.
160 typedef struct tagOpenDll {
161 char *DllName; /* really only needed for debugging */
162 HINSTANCE hLibrary;
163 struct tagOpenDll *next;
164 } OpenDll;
166 static OpenDll *openDllList = NULL; /* linked list of open dlls */
168 /*****************************************************************************
169 * This section contains prototypes to internal methods for this
170 * module
172 static HRESULT COM_GetRegisteredClassObject(REFCLSID rclsid,
173 DWORD dwClsContext,
174 LPUNKNOWN* ppUnk);
176 static void COM_RevokeAllClasses();
179 /******************************************************************************
180 * CoBuildVersion [COMPOBJ.1]
182 * RETURNS
183 * Current built version, hiword is majornumber, loword is minornumber
185 DWORD WINAPI CoBuildVersion(void)
187 TRACE(ole,"(void)\n");
188 return (rmm<<16)+rup;
191 /******************************************************************************
192 * CoInitialize16 [COMPOBJ.2]
193 * Set the win16 IMalloc used for memory management
195 HRESULT WINAPI CoInitialize16(
196 LPVOID lpReserved /* [in] pointer to win16 malloc interface */
198 currentMalloc16 = (LPMALLOC16)lpReserved;
199 return S_OK;
202 /******************************************************************************
203 * CoInitialize32 [OLE32.26]
205 * Initializes the COM libraries.
207 * See CoInitializeEx32
209 HRESULT WINAPI CoInitialize(
210 LPVOID lpReserved /* [in] pointer to win32 malloc interface
211 (obsolete, should be NULL) */
215 * Just delegate to the newer method.
217 return CoInitializeEx(lpReserved, COINIT_APARTMENTTHREADED);
220 /******************************************************************************
221 * CoInitializeEx32 [OLE32.163]
223 * Initializes the COM libraries. The behavior used to set the win32 IMalloc
224 * used for memory management is obsolete.
226 * RETURNS
227 * S_OK if successful,
228 * S_FALSE if this function was called already.
229 * RPC_E_CHANGED_MODE if a previous call to CoInitialize specified another
230 * threading model.
232 * BUGS
233 * Only the single threaded model is supported. As a result RPC_E_CHANGED_MODE
234 * is never returned.
236 * See the windows documentation for more details.
238 HRESULT WINAPI CoInitializeEx(
239 LPVOID lpReserved, /* [in] pointer to win32 malloc interface
240 (obsolete, should be NULL) */
241 DWORD dwCoInit /* [in] A value from COINIT specifies the threading model */
244 HRESULT hr;
246 TRACE(ole, "(%p, %x)\n", lpReserved, (int)dwCoInit);
248 if (lpReserved!=NULL)
250 ERR(ole,"(%p, %x) - Bad parameter passed-in %p, must be an old Windows Application\n", lpReserved, (int)dwCoInit, lpReserved);
254 * Check for unsupported features.
256 if (dwCoInit!=COINIT_APARTMENTTHREADED)
258 FIXME(ole, ":(%p,%x): unsupported flag %x\n", lpReserved, (int)dwCoInit, (int)dwCoInit);
259 /* Hope for the best and continue anyway */
263 * Check the lock count. If this is the first time going through the initialize
264 * process, we have to initialize the libraries.
266 if (s_COMLockCount==0)
269 * Initialize the various COM libraries and data structures.
271 TRACE(ole, "() - Initializing the COM libraries\n");
273 RunningObjectTableImpl_Initialize();
275 hr = S_OK;
277 else
278 hr = S_FALSE;
281 * Crank-up that lock count.
283 s_COMLockCount++;
285 return hr;
288 /***********************************************************************
289 * CoUninitialize16 [COMPOBJ.3]
290 * Don't know what it does.
291 * 3-Nov-98 -- this was originally misspelled, I changed it to what I
292 * believe is the correct spelling
294 void WINAPI CoUninitialize16(void)
296 TRACE(ole,"()\n");
297 CoFreeAllLibraries();
300 /***********************************************************************
301 * CoUninitialize32 [OLE32.47]
303 * This method will release the COM libraries.
305 * See the windows documentation for more details.
307 void WINAPI CoUninitialize(void)
309 TRACE(ole,"()\n");
312 * Decrease the reference count.
314 s_COMLockCount--;
317 * If we are back to 0 locks on the COM library, make sure we free
318 * all the associated data structures.
320 if (s_COMLockCount==0)
323 * Release the various COM libraries and data structures.
325 TRACE(ole, "() - Releasing the COM libraries\n");
327 RunningObjectTableImpl_UnInitialize();
329 * Release the references to the registered class objects.
331 COM_RevokeAllClasses();
334 * This will free the loaded COM Dlls.
336 CoFreeAllLibraries();
339 * This will free list of external references to COM objects.
341 COM_ExternalLockFreeList();
345 /***********************************************************************
346 * CoGetMalloc16 [COMPOBJ.4]
347 * RETURNS
348 * The current win16 IMalloc
350 HRESULT WINAPI CoGetMalloc16(
351 DWORD dwMemContext, /* [in] unknown */
352 LPMALLOC16 * lpMalloc /* [out] current win16 malloc interface */
354 if(!currentMalloc16)
355 currentMalloc16 = IMalloc16_Constructor();
356 *lpMalloc = currentMalloc16;
357 return S_OK;
360 /******************************************************************************
361 * CoGetMalloc32 [OLE32.20]
363 * RETURNS
364 * The current win32 IMalloc
366 HRESULT WINAPI CoGetMalloc(
367 DWORD dwMemContext, /* [in] unknown */
368 LPMALLOC *lpMalloc /* [out] current win32 malloc interface */
370 if(!currentMalloc32)
371 currentMalloc32 = IMalloc_Constructor();
372 *lpMalloc = currentMalloc32;
373 return S_OK;
376 /***********************************************************************
377 * CoCreateStandardMalloc16 [COMPOBJ.71]
379 HRESULT WINAPI CoCreateStandardMalloc16(DWORD dwMemContext,
380 LPMALLOC16 *lpMalloc)
382 /* FIXME: docu says we shouldn't return the same allocator as in
383 * CoGetMalloc16 */
384 *lpMalloc = IMalloc16_Constructor();
385 return S_OK;
388 /******************************************************************************
389 * CoDisconnectObject [COMPOBJ.15]
391 HRESULT WINAPI CoDisconnectObject( LPUNKNOWN lpUnk, DWORD reserved )
393 TRACE(ole,"%p %lx\n",lpUnk,reserved);
394 return S_OK;
397 /***********************************************************************
398 * IsEqualGUID16 [COMPOBJ.18]
400 * Compares two Unique Identifiers.
402 * RETURNS
403 * TRUE if equal
405 BOOL16 WINAPI IsEqualGUID16(
406 GUID* g1, /* [in] unique id 1 */
407 GUID* g2 /**/
409 return !memcmp( g1, g2, sizeof(GUID) );
412 /***********************************************************************
413 * IsEqualGUID32 [OLE32.76]
415 * Compares two Unique Identifiers.
417 * RETURNS
418 * TRUE if equal
420 BOOL WINAPI IsEqualGUID32(
421 REFGUID rguid1, /* [in] unique id 1 */
422 REFGUID rguid2 /* [in] unique id 2 */
425 return !memcmp(rguid1,rguid2,sizeof(GUID));
428 /******************************************************************************
429 * CLSIDFromString16 [COMPOBJ.20]
430 * Converts a unique identifier from it's string representation into
431 * the GUID struct.
433 * Class id: DWORD-WORD-WORD-BYTES[2]-BYTES[6]
435 * RETURNS
436 * the converted GUID
438 HRESULT WINAPI CLSIDFromString16(
439 LPCOLESTR16 idstr, /* [in] string representation of guid */
440 CLSID *id /* [out] GUID converted from string */
442 BYTE *s = (BYTE *) idstr;
443 BYTE *p;
444 int i;
445 BYTE table[256];
447 TRACE(ole,"%s -> %p\n", idstr, id);
449 /* quick lookup table */
450 memset(table, 0, 256);
452 for (i = 0; i < 10; i++) {
453 table['0' + i] = i;
455 for (i = 0; i < 6; i++) {
456 table['A' + i] = i+10;
457 table['a' + i] = i+10;
460 /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
462 if (strlen(idstr) != 38)
463 return OLE_ERROR_OBJECT;
465 p = (BYTE *) id;
467 s++; /* skip leading brace */
468 for (i = 0; i < 4; i++) {
469 p[3 - i] = table[*s]<<4 | table[*(s+1)];
470 s += 2;
472 p += 4;
473 s++; /* skip - */
475 for (i = 0; i < 2; i++) {
476 p[1-i] = table[*s]<<4 | table[*(s+1)];
477 s += 2;
479 p += 2;
480 s++; /* skip - */
482 for (i = 0; i < 2; i++) {
483 p[1-i] = table[*s]<<4 | table[*(s+1)];
484 s += 2;
486 p += 2;
487 s++; /* skip - */
489 /* these are just sequential bytes */
490 for (i = 0; i < 2; i++) {
491 *p++ = table[*s]<<4 | table[*(s+1)];
492 s += 2;
494 s++; /* skip - */
496 for (i = 0; i < 6; i++) {
497 *p++ = table[*s]<<4 | table[*(s+1)];
498 s += 2;
501 return S_OK;
504 /******************************************************************************
505 * CoCreateGuid[OLE32.6]
507 * Creates a 128bit GUID.
508 * Implemented according the DCE specification for UUID generation.
509 * Code is based upon uuid library in e2fsprogs by Theodore Ts'o.
510 * Copyright (C) 1996, 1997 Theodore Ts'o.
512 * RETURNS
514 * S_OK if successful.
516 HRESULT WINAPI CoCreateGuid(
517 GUID *pguid /* [out] points to the GUID to initialize */
519 static char has_init = 0;
520 unsigned char a[6];
521 static int adjustment = 0;
522 static struct timeval last = {0, 0};
523 static UINT16 clock_seq;
524 struct timeval tv;
525 unsigned long long clock_reg;
526 UINT clock_high, clock_low;
527 UINT16 temp_clock_seq, temp_clock_mid, temp_clock_hi_and_version;
528 #ifdef HAVE_NET_IF_H
529 int sd;
530 struct ifreq ifr, *ifrp;
531 struct ifconf ifc;
532 char buf[1024];
533 int n, i;
534 #endif
536 /* Have we already tried to get the MAC address? */
537 if (!has_init) {
538 #ifdef HAVE_NET_IF_H
539 /* BSD 4.4 defines the size of an ifreq to be
540 * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
541 * However, under earlier systems, sa_len isn't present, so
542 * the size is just sizeof(struct ifreq)
544 # ifdef HAVE_SA_LEN
545 # ifndef max
546 # define max(a,b) ((a) > (b) ? (a) : (b))
547 # endif
548 # define ifreq_size(i) max(sizeof(struct ifreq),\
549 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
550 # else
551 # define ifreq_size(i) sizeof(struct ifreq)
552 # endif /* HAVE_SA_LEN */
554 sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
555 if (sd < 0) {
556 /* if we can't open a socket, just use random numbers */
557 /* set the multicast bit to prevent conflicts with real cards */
558 a[0] = (rand() & 0xff) | 0x80;
559 a[1] = rand() & 0xff;
560 a[2] = rand() & 0xff;
561 a[3] = rand() & 0xff;
562 a[4] = rand() & 0xff;
563 a[5] = rand() & 0xff;
564 } else {
565 memset(buf, 0, sizeof(buf));
566 ifc.ifc_len = sizeof(buf);
567 ifc.ifc_buf = buf;
568 /* get the ifconf interface */
569 if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) {
570 close(sd);
571 /* no ifconf, so 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 /* loop through the interfaces, looking for a valid one */
581 n = ifc.ifc_len;
582 for (i = 0; i < n; i+= ifreq_size(*ifr) ) {
583 ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i);
584 strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
585 /* try to get the address for this interface */
586 # ifdef SIOCGIFHWADDR
587 if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
588 continue;
589 memcpy(a, (unsigned char *)&ifr.ifr_hwaddr.sa_data, 6);
590 # else
591 # ifdef SIOCGENADDR
592 if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
593 continue;
594 memcpy(a, (unsigned char *) ifr.ifr_enaddr, 6);
595 # else
596 /* XXX we don't have a way of getting the hardware address */
597 close(sd);
598 a[0] = 0;
599 break;
600 # endif /* SIOCGENADDR */
601 # endif /* SIOCGIFHWADDR */
602 /* make sure it's not blank */
603 if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5])
604 continue;
606 goto valid_address;
608 /* if we didn't find a valid address, make a random one */
609 /* once again, set multicast bit to avoid conflicts */
610 a[0] = (rand() & 0xff) | 0x80;
611 a[1] = rand() & 0xff;
612 a[2] = rand() & 0xff;
613 a[3] = rand() & 0xff;
614 a[4] = rand() & 0xff;
615 a[5] = rand() & 0xff;
617 valid_address:
618 close(sd);
621 #else
622 /* no networking info, so generate a random address */
623 a[0] = (rand() & 0xff) | 0x80;
624 a[1] = rand() & 0xff;
625 a[2] = rand() & 0xff;
626 a[3] = rand() & 0xff;
627 a[4] = rand() & 0xff;
628 a[5] = rand() & 0xff;
629 #endif /* HAVE_NET_IF_H */
630 has_init = 1;
633 /* generate time element of GUID */
635 /* Assume that the gettimeofday() has microsecond granularity */
636 #define MAX_ADJUSTMENT 10
638 try_again:
639 gettimeofday(&tv, 0);
640 if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
641 clock_seq = ((rand() & 0xff) << 8) + (rand() & 0xff);
642 clock_seq &= 0x1FFF;
643 last = tv;
644 last.tv_sec--;
646 if ((tv.tv_sec < last.tv_sec) ||
647 ((tv.tv_sec == last.tv_sec) &&
648 (tv.tv_usec < last.tv_usec))) {
649 clock_seq = (clock_seq+1) & 0x1FFF;
650 adjustment = 0;
651 } else if ((tv.tv_sec == last.tv_sec) &&
652 (tv.tv_usec == last.tv_usec)) {
653 if (adjustment >= MAX_ADJUSTMENT)
654 goto try_again;
655 adjustment++;
656 } else
657 adjustment = 0;
659 clock_reg = tv.tv_usec*10 + adjustment;
660 clock_reg += ((unsigned long long) tv.tv_sec)*10000000;
661 clock_reg += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
663 clock_high = clock_reg >> 32;
664 clock_low = clock_reg;
665 temp_clock_seq = clock_seq | 0x8000;
666 temp_clock_mid = (UINT16)clock_high;
667 temp_clock_hi_and_version = (clock_high >> 16) | 0x1000;
669 /* pack the information into the GUID structure */
671 ((unsigned char*)&pguid->Data1)[3] = (unsigned char)clock_low;
672 clock_low >>= 8;
673 ((unsigned char*)&pguid->Data1)[2] = (unsigned char)clock_low;
674 clock_low >>= 8;
675 ((unsigned char*)&pguid->Data1)[1] = (unsigned char)clock_low;
676 clock_low >>= 8;
677 ((unsigned char*)&pguid->Data1)[0] = (unsigned char)clock_low;
679 ((unsigned char*)&pguid->Data2)[1] = (unsigned char)temp_clock_mid;
680 temp_clock_mid >>= 8;
681 ((unsigned char*)&pguid->Data2)[0] = (unsigned char)temp_clock_mid;
683 ((unsigned char*)&pguid->Data3)[1] = (unsigned char)temp_clock_hi_and_version;
684 temp_clock_hi_and_version >>= 8;
685 ((unsigned char*)&pguid->Data3)[0] = (unsigned char)temp_clock_hi_and_version;
687 ((unsigned char*)pguid->Data4)[1] = (unsigned char)temp_clock_seq;
688 temp_clock_seq >>= 8;
689 ((unsigned char*)pguid->Data4)[0] = (unsigned char)temp_clock_seq;
691 ((unsigned char*)pguid->Data4)[2] = a[0];
692 ((unsigned char*)pguid->Data4)[3] = a[1];
693 ((unsigned char*)pguid->Data4)[4] = a[2];
694 ((unsigned char*)pguid->Data4)[5] = a[3];
695 ((unsigned char*)pguid->Data4)[6] = a[4];
696 ((unsigned char*)pguid->Data4)[7] = a[5];
698 TRACE(ole, "%p", pguid);
700 return S_OK;
703 /******************************************************************************
704 * CLSIDFromString32 [OLE32.3]
705 * Converts a unique identifier from it's string representation into
706 * the GUID struct.
707 * RETURNS
708 * the converted GUID
710 HRESULT WINAPI CLSIDFromString(
711 LPCOLESTR idstr, /* [in] string representation of GUID */
712 CLSID *id /* [out] GUID represented by above string */
714 LPOLESTR16 xid = HEAP_strdupWtoA(GetProcessHeap(),0,idstr);
715 OLESTATUS ret = CLSIDFromString16(xid,id);
717 HeapFree(GetProcessHeap(),0,xid);
718 return ret;
721 /******************************************************************************
722 * WINE_StringFromCLSID [Internal]
723 * Converts a GUID into the respective string representation.
725 * NOTES
727 * RETURNS
728 * the string representation and OLESTATUS
730 HRESULT WINE_StringFromCLSID(
731 const CLSID *id, /* [in] GUID to be converted */
732 LPSTR idstr /* [out] pointer to buffer to contain converted guid */
734 static const char *hex = "0123456789ABCDEF";
735 char *s;
736 int i;
738 if (!id)
739 { ERR(ole,"called with id=Null\n");
740 *idstr = 0x00;
741 return E_FAIL;
744 sprintf(idstr, "{%08lX-%04X-%04X-%02X%02X-",
745 id->Data1, id->Data2, id->Data3,
746 id->Data4[0], id->Data4[1]);
747 s = &idstr[25];
749 /* 6 hex bytes */
750 for (i = 2; i < 8; i++) {
751 *s++ = hex[id->Data4[i]>>4];
752 *s++ = hex[id->Data4[i] & 0xf];
755 *s++ = '}';
756 *s++ = '\0';
758 TRACE(ole,"%p->%s\n", id, idstr);
760 return OLE_OK;
763 /******************************************************************************
764 * StringFromCLSID16 [COMPOBJ.19]
765 * Converts a GUID into the respective string representation.
766 * The target string is allocated using the OLE IMalloc.
767 * RETURNS
768 * the string representation and OLESTATUS
770 HRESULT WINAPI StringFromCLSID16(
771 REFCLSID id, /* [in] the GUID to be converted */
772 LPOLESTR16 *idstr /* [out] a pointer to a to-be-allocated segmented pointer pointing to the resulting string */
775 LPMALLOC16 mllc;
776 OLESTATUS ret;
777 DWORD args[2];
779 ret = CoGetMalloc16(0,&mllc);
780 if (ret) return ret;
782 args[0] = (DWORD)mllc;
783 args[1] = 40;
785 /* No need for a Callback entry, we have WOWCallback16Ex which does
786 * everything we need.
788 if (!WOWCallback16Ex(
789 (FARPROC16)((ICOM_VTABLE(IMalloc16)*)PTR_SEG_TO_LIN(
790 ((LPMALLOC16)PTR_SEG_TO_LIN(mllc))->lpvtbl)
791 )->fnAlloc,
792 WCB16_CDECL,
794 (LPVOID)args,
795 (LPDWORD)idstr
796 )) {
797 WARN(ole,"CallTo16 IMalloc16 failed\n");
798 return E_FAIL;
800 return WINE_StringFromCLSID(id,PTR_SEG_TO_LIN(*idstr));
803 /******************************************************************************
804 * StringFromCLSID32 [OLE32.151]
805 * Converts a GUID into the respective string representation.
806 * The target string is allocated using the OLE IMalloc.
807 * RETURNS
808 * the string representation and OLESTATUS
810 HRESULT WINAPI StringFromCLSID(
811 REFCLSID id, /* [in] the GUID to be converted */
812 LPOLESTR *idstr /* [out] a pointer to a to-be-allocated pointer pointing to the resulting string */
814 char buf[80];
815 OLESTATUS ret;
816 LPMALLOC mllc;
818 if ((ret=CoGetMalloc(0,&mllc)))
819 return ret;
821 ret=WINE_StringFromCLSID(id,buf);
822 if (!ret) {
823 *idstr = mllc->lpvtbl->fnAlloc(mllc,strlen(buf)*2+2);
824 lstrcpyAtoW(*idstr,buf);
826 return ret;
829 /******************************************************************************
830 * StringFromGUID2 [COMPOBJ.76] [OLE32.152]
832 * Converts a global unique identifier into a string of an API-
833 * specified fixed format. (The usual {.....} stuff.)
835 * RETURNS
836 * The (UNICODE) string representation of the GUID in 'str'
837 * The length of the resulting string, 0 if there was any problem.
839 INT WINAPI
840 StringFromGUID2(REFGUID id, LPOLESTR str, INT cmax)
842 char xguid[80];
844 if (WINE_StringFromCLSID(id,xguid))
845 return 0;
846 if (strlen(xguid)>=cmax)
847 return 0;
848 lstrcpyAtoW(str,xguid);
849 return strlen(xguid);
852 /******************************************************************************
853 * CLSIDFromProgID16 [COMPOBJ.61]
854 * Converts a program id into the respective GUID. (By using a registry lookup)
855 * RETURNS
856 * riid associated with the progid
858 HRESULT WINAPI CLSIDFromProgID16(
859 LPCOLESTR16 progid, /* [in] program id as found in registry */
860 LPCLSID riid /* [out] associated CLSID */
862 char *buf,buf2[80];
863 DWORD buf2len;
864 HRESULT err;
865 HKEY xhkey;
867 buf = HeapAlloc(GetProcessHeap(),0,strlen(progid)+8);
868 sprintf(buf,"%s\\CLSID",progid);
869 if ((err=RegOpenKeyA(HKEY_CLASSES_ROOT,buf,&xhkey))) {
870 HeapFree(GetProcessHeap(),0,buf);
871 return OLE_ERROR_GENERIC;
873 HeapFree(GetProcessHeap(),0,buf);
874 buf2len = sizeof(buf2);
875 if ((err=RegQueryValueA(xhkey,NULL,buf2,&buf2len))) {
876 RegCloseKey(xhkey);
877 return OLE_ERROR_GENERIC;
879 RegCloseKey(xhkey);
880 return CLSIDFromString16(buf2,riid);
883 /******************************************************************************
884 * CLSIDFromProgID32 [OLE32.2]
885 * Converts a program id into the respective GUID. (By using a registry lookup)
886 * RETURNS
887 * riid associated with the progid
889 HRESULT WINAPI CLSIDFromProgID(
890 LPCOLESTR progid, /* [in] program id as found in registry */
891 LPCLSID riid /* [out] associated CLSID */
893 LPOLESTR16 pid = HEAP_strdupWtoA(GetProcessHeap(),0,progid);
894 OLESTATUS ret = CLSIDFromProgID16(pid,riid);
896 HeapFree(GetProcessHeap(),0,pid);
897 return ret;
900 /************************************************************************************************
901 * OleSaveToStream
903 * This function write a CLSID on stream
905 HRESULT WINAPI WriteClassStm(IStream *pStm,REFCLSID rclsid)
907 TRACE(ole,"(%p,%p)\n",pStm,rclsid);
909 if (rclsid==NULL)
910 return E_INVALIDARG;
912 return IStream_Write(pStm,rclsid,sizeof(CLSID),NULL);
915 /************************************************************************************************
916 * OleSaveToStream
918 * This function read a CLSID from a stream
920 HRESULT WINAPI ReadClassStm(IStream *pStm,REFCLSID rclsid)
922 ULONG nbByte;
923 HRESULT res;
925 TRACE(ole,"(%p,%p)\n",pStm,rclsid);
927 if (rclsid==NULL)
928 return E_INVALIDARG;
930 res = IStream_Read(pStm,(void*)rclsid,sizeof(CLSID),&nbByte);
932 if (FAILED(res))
933 return res;
935 if (nbByte != sizeof(CLSID))
936 return S_FALSE;
937 else
938 return S_OK;
941 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
942 /***********************************************************************
943 * LookupETask (COMPOBJ.94)
945 OLESTATUS WINAPI LookupETask16(HTASK16 *hTask,LPVOID p) {
946 FIXME(ole,"(%p,%p),stub!\n",hTask,p);
947 if ((*hTask = GetCurrentTask()) == hETask) {
948 memcpy(p, Table_ETask, sizeof(Table_ETask));
950 return 0;
953 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
954 /***********************************************************************
955 * SetETask (COMPOBJ.95)
957 OLESTATUS WINAPI SetETask16(HTASK16 hTask, LPVOID p) {
958 FIXME(ole,"(%04x,%p),stub!\n",hTask,p);
959 hETask = hTask;
960 return 0;
963 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
964 /***********************************************************************
965 * CallObjectInWOW (COMPOBJ.201)
967 OLESTATUS WINAPI CallObjectInWOW(LPVOID p1,LPVOID p2) {
968 FIXME(ole,"(%p,%p),stub!\n",p1,p2);
969 return 0;
972 /******************************************************************************
973 * CoRegisterClassObject16 [COMPOBJ.5]
975 * Don't know where it registers it ...
977 HRESULT WINAPI CoRegisterClassObject16(
978 REFCLSID rclsid,
979 LPUNKNOWN pUnk,
980 DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
981 DWORD flags, /* [in] REGCLS flags indicating how connections are made */
982 LPDWORD lpdwRegister
984 char buf[80];
986 WINE_StringFromCLSID(rclsid,buf);
988 FIXME(ole,"(%s,%p,0x%08lx,0x%08lx,%p),stub\n",
989 buf,pUnk,dwClsContext,flags,lpdwRegister
991 return 0;
994 /***
995 * COM_GetRegisteredClassObject
997 * This internal method is used to scan the registered class list to
998 * find a class object.
1000 * Params:
1001 * rclsid Class ID of the class to find.
1002 * dwClsContext Class context to match.
1003 * ppv [out] returns a pointer to the class object. Complying
1004 * to normal COM usage, this method will increase the
1005 * reference count on this object.
1007 static HRESULT COM_GetRegisteredClassObject(
1008 REFCLSID rclsid,
1009 DWORD dwClsContext,
1010 LPUNKNOWN* ppUnk)
1012 RegisteredClass* curClass;
1015 * Sanity check
1017 assert(ppUnk!=0);
1020 * Iterate through the whole list and try to match the class ID.
1022 curClass = firstRegisteredClass;
1024 while (curClass != 0)
1027 * Check if we have a match on the class ID.
1029 if (IsEqualGUID32(&(curClass->classIdentifier), rclsid))
1032 * Since we don't do out-of process or DCOM just right away, let's ignore the
1033 * class context.
1037 * We have a match, return the pointer to the class object.
1039 *ppUnk = curClass->classObject;
1041 IUnknown_AddRef(curClass->classObject);
1043 return S_OK;
1047 * Step to the next class in the list.
1049 curClass = curClass->nextClass;
1053 * If we get to here, we haven't found our class.
1055 return S_FALSE;
1058 /******************************************************************************
1059 * CoRegisterClassObject32 [OLE32.36]
1061 * This method will register the class object for a given class ID.
1063 * See the Windows documentation for more details.
1065 HRESULT WINAPI CoRegisterClassObject(
1066 REFCLSID rclsid,
1067 LPUNKNOWN pUnk,
1068 DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
1069 DWORD flags, /* [in] REGCLS flags indicating how connections are made */
1070 LPDWORD lpdwRegister
1073 RegisteredClass* newClass;
1074 LPUNKNOWN foundObject;
1075 HRESULT hr;
1076 char buf[80];
1078 WINE_StringFromCLSID(rclsid,buf);
1080 TRACE(ole,"(%s,%p,0x%08lx,0x%08lx,%p)\n",
1081 buf,pUnk,dwClsContext,flags,lpdwRegister);
1084 * Perform a sanity check on the parameters
1086 if ( (lpdwRegister==0) || (pUnk==0) )
1088 return E_INVALIDARG;
1092 * Initialize the cookie (out parameter)
1094 *lpdwRegister = 0;
1097 * First, check if the class is already registered.
1098 * If it is, this should cause an error.
1100 hr = COM_GetRegisteredClassObject(rclsid, dwClsContext, &foundObject);
1102 if (hr == S_OK)
1105 * The COM_GetRegisteredClassObject increased the reference count on the
1106 * object so it has to be released.
1108 IUnknown_Release(foundObject);
1110 return CO_E_OBJISREG;
1114 * If it is not registered, we must create a new entry for this class and
1115 * append it to the registered class list.
1116 * We use the address of the chain node as the cookie since we are sure it's
1117 * unique.
1119 newClass = HeapAlloc(GetProcessHeap(), 0, sizeof(RegisteredClass));
1122 * Initialize the node.
1124 newClass->classIdentifier = *rclsid;
1125 newClass->runContext = dwClsContext;
1126 newClass->connectFlags = flags;
1127 newClass->dwCookie = (DWORD)newClass;
1128 newClass->nextClass = firstRegisteredClass;
1131 * Since we're making a copy of the object pointer, we have to increase it's
1132 * reference count.
1134 newClass->classObject = pUnk;
1135 IUnknown_AddRef(newClass->classObject);
1137 firstRegisteredClass = newClass;
1140 * We're successfyl Yippee!
1142 return S_OK;
1145 /***********************************************************************
1146 * CoRevokeClassObject32 [OLE32.40]
1148 * This method will remove a class object from the class registry
1150 * See the Windows documentation for more details.
1152 HRESULT WINAPI CoRevokeClassObject(
1153 DWORD dwRegister)
1155 RegisteredClass** prevClassLink;
1156 RegisteredClass* curClass;
1158 TRACE(ole,"(%08lx)\n",dwRegister);
1161 * Iterate through the whole list and try to match the cookie.
1163 curClass = firstRegisteredClass;
1164 prevClassLink = &firstRegisteredClass;
1166 while (curClass != 0)
1169 * Check if we have a match on the cookie.
1171 if (curClass->dwCookie == dwRegister)
1174 * Remove the class from the chain.
1176 *prevClassLink = curClass->nextClass;
1179 * Release the reference to the class object.
1181 IUnknown_Release(curClass->classObject);
1184 * Free the memory used by the chain node.
1186 HeapFree(GetProcessHeap(), 0, curClass);
1188 return S_OK;
1192 * Step to the next class in the list.
1194 prevClassLink = &(curClass->nextClass);
1195 curClass = curClass->nextClass;
1199 * If we get to here, we haven't found our class.
1201 return E_INVALIDARG;
1204 /***********************************************************************
1205 * CoGetClassObject [COMPOBJ.7]
1207 HRESULT WINAPI CoGetClassObject(REFCLSID rclsid, DWORD dwClsContext,
1208 LPVOID pvReserved, REFIID iid, LPVOID *ppv)
1210 LPUNKNOWN regClassObject;
1211 char xclsid[50],xiid[50];
1212 HRESULT hres = E_UNEXPECTED;
1214 char dllName[MAX_PATH+1];
1215 DWORD dllNameLen = sizeof(dllName);
1216 HINSTANCE hLibrary;
1217 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid,
1218 REFIID iid, LPVOID *ppv);
1219 DllGetClassObjectFunc DllGetClassObject;
1221 WINE_StringFromCLSID((LPCLSID)rclsid,xclsid);
1222 WINE_StringFromCLSID((LPCLSID)iid,xiid);
1223 TRACE(ole,"\n\tCLSID:\t%s,\n\tIID:\t%s\n",xclsid,xiid);
1226 * First, try and see if we can't match the class ID with one of the
1227 * registered classes.
1229 if (S_OK == COM_GetRegisteredClassObject(rclsid, dwClsContext, &regClassObject))
1232 * Get the required interface from the retrieved pointer.
1234 hres = IUnknown_QueryInterface(regClassObject, iid, ppv);
1237 * Since QI got another reference on the pointer, we want to release the
1238 * one we already have. If QI was unsuccessful, this will release the object. This
1239 * is good since we are not returning it in the "out" parameter.
1241 IUnknown_Release(regClassObject);
1243 return hres;
1246 /* out of process and remote servers not supported yet */
1247 if ((CLSCTX_LOCAL_SERVER|CLSCTX_REMOTE_SERVER) & dwClsContext) {
1248 FIXME(ole, "CLSCTX_LOCAL_SERVER and CLSCTX_REMOTE_SERVER not supported!\n");
1249 return E_ACCESSDENIED;
1252 if ((CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER) & dwClsContext) {
1253 HKEY CLSIDkey,key;
1255 /* lookup CLSID in registry key HKCR/CLSID */
1256 hres = RegOpenKeyExA(HKEY_CLASSES_ROOT, "CLSID", 0,
1257 KEY_READ, &CLSIDkey);
1259 if (hres != ERROR_SUCCESS)
1260 return REGDB_E_READREGDB;
1261 hres = RegOpenKeyExA(CLSIDkey,xclsid,0,KEY_QUERY_VALUE,&key);
1262 if (hres != ERROR_SUCCESS) {
1263 RegCloseKey(CLSIDkey);
1264 return REGDB_E_CLASSNOTREG;
1266 hres = RegQueryValueA(key, "InprocServer32", dllName, &dllNameLen);
1267 RegCloseKey(key);
1268 RegCloseKey(CLSIDkey);
1269 if (hres != ERROR_SUCCESS)
1270 return REGDB_E_READREGDB;
1271 TRACE(ole,"found InprocServer32 dll %s\n", dllName);
1273 /* open dll, call DllGetClassFactory */
1274 hLibrary = CoLoadLibrary(dllName, TRUE);
1275 if (hLibrary == 0) {
1276 TRACE(ole,"couldn't load InprocServer32 dll %s\n", dllName);
1277 return E_ACCESSDENIED; /* or should this be CO_E_DLLNOTFOUND? */
1279 DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress(hLibrary, "DllGetClassObject");
1280 if (!DllGetClassObject) {
1281 /* not sure if this should be called here CoFreeLibrary(hLibrary);*/
1282 TRACE(ole,"couldn't find function DllGetClassObject in %s\n", dllName);
1283 return E_ACCESSDENIED;
1287 * Ask the DLL for it's class object. (there was a note here about class
1288 * factories but this is good.
1290 return DllGetClassObject(rclsid, iid, ppv);
1292 return hres;
1295 /****************************************************************************************
1296 * GetClassFile
1298 * This function supplies the CLSID associated with the given filename.
1300 HRESULT WINAPI GetClassFile(LPOLESTR filePathName,CLSID *pclsid)
1302 IStorage *pstg=0;
1303 HRESULT res;
1304 int nbElm=0,length=0,i=0;
1305 LONG sizeProgId=20;
1306 LPOLESTR *pathDec=0,absFile=0,progId=0;
1307 WCHAR extention[100]={0};
1309 TRACE(ole,"()\n");
1311 /* if the file contain a storage object the return the CLSID writen by IStorage_SetClass method*/
1312 if((StgIsStorageFile(filePathName))==S_OK){
1314 res=StgOpenStorage(filePathName,NULL,STGM_READ | STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
1316 if (SUCCEEDED(res))
1317 res=ReadClassStg(pstg,pclsid);
1319 IStorage_Release(pstg);
1321 return res;
1323 /* if the file is not a storage object then attemps to match various bits in the file against a
1324 pattern in the registry. this case is not frequently used ! so I present only the psodocode for
1325 this case
1327 for(i=0;i<nFileTypes;i++)
1329 for(i=0;j<nPatternsForType;j++){
1331 PATTERN pat;
1332 HANDLE hFile;
1334 pat=ReadPatternFromRegistry(i,j);
1335 hFile=CreateFileW(filePathName,,,,,,hFile);
1336 SetFilePosition(hFile,pat.offset);
1337 ReadFile(hFile,buf,pat.size,NULL,NULL);
1338 if (memcmp(buf&pat.mask,pat.pattern.pat.size)==0){
1340 *pclsid=ReadCLSIDFromRegistry(i);
1341 return S_OK;
1346 /* if the obove strategies fail then search for the extension key in the registry */
1348 /* get the last element (absolute file) in the path name */
1349 nbElm=FileMonikerImpl_DecomposePath(filePathName,&pathDec);
1350 absFile=pathDec[nbElm-1];
1352 /* failed if the path represente a directory and not an absolute file name*/
1353 if (lstrcmpW(absFile,(LPOLESTR)"\\"))
1354 return MK_E_INVALIDEXTENSION;
1356 /* get the extension of the file */
1357 length=lstrlenW(absFile);
1358 for(i=length-1; ( (i>=0) && (extention[i]=absFile[i]) );i--);
1360 /* get the progId associated to the extension */
1361 progId=CoTaskMemAlloc(sizeProgId);
1363 res=RegQueryValueW(HKEY_CLASSES_ROOT,extention,progId,&sizeProgId);
1365 if (res==ERROR_MORE_DATA){
1367 CoTaskMemRealloc(progId,sizeProgId);
1369 res=RegQueryValueW(HKEY_CLASSES_ROOT,extention,progId,&sizeProgId);
1371 if (res==ERROR_SUCCESS)
1372 /* return the clsid associated to the progId */
1373 res= CLSIDFromProgID(progId,pclsid);
1375 for(i=0; pathDec[i]!=NULL;i++)
1376 CoTaskMemFree(pathDec[i]);
1377 CoTaskMemFree(pathDec);
1379 CoTaskMemFree(progId);
1381 if (res==ERROR_SUCCESS)
1382 return res;
1384 return MK_E_INVALIDEXTENSION;
1386 /******************************************************************************
1387 * CoRegisterMessageFilter16 [COMPOBJ.27]
1389 HRESULT WINAPI CoRegisterMessageFilter16(
1390 LPMESSAGEFILTER lpMessageFilter,
1391 LPMESSAGEFILTER *lplpMessageFilter
1393 FIXME(ole,"(%p,%p),stub!\n",lpMessageFilter,lplpMessageFilter);
1394 return 0;
1397 /***********************************************************************
1398 * CoCreateInstance [COMPOBJ.13, OLE32.7]
1400 HRESULT WINAPI CoCreateInstance(
1401 REFCLSID rclsid,
1402 LPUNKNOWN pUnkOuter,
1403 DWORD dwClsContext,
1404 REFIID iid,
1405 LPVOID *ppv)
1407 HRESULT hres;
1408 LPCLASSFACTORY lpclf = 0;
1411 * Sanity check
1413 if (ppv==0)
1414 return E_POINTER;
1417 * Initialize the "out" parameter
1419 *ppv = 0;
1422 * Get a class factory to construct the object we want.
1424 hres = CoGetClassObject(rclsid,
1425 dwClsContext,
1426 NULL,
1427 &IID_IClassFactory,
1428 (LPVOID)&lpclf);
1430 if (FAILED(hres))
1431 return hres;
1434 * Create the object and don't forget to release the factory
1436 hres = IClassFactory_CreateInstance(lpclf, pUnkOuter, iid, ppv);
1437 IClassFactory_Release(lpclf);
1439 return hres;
1442 /***********************************************************************
1443 * CoCreateInstanceEx [OLE32.165]
1445 HRESULT WINAPI CoCreateInstanceEx(
1446 REFCLSID rclsid,
1447 LPUNKNOWN pUnkOuter,
1448 DWORD dwClsContext,
1449 COSERVERINFO* pServerInfo,
1450 ULONG cmq,
1451 MULTI_QI* pResults)
1453 IUnknown* pUnk = NULL;
1454 HRESULT hr;
1455 ULONG index;
1456 int successCount = 0;
1459 * Sanity check
1461 if ( (cmq==0) || (pResults==NULL))
1462 return E_INVALIDARG;
1464 if (pServerInfo!=NULL)
1465 FIXME(ole, "() non-NULL pServerInfo not supported!\n");
1468 * Initialize all the "out" parameters.
1470 for (index = 0; index < cmq; index++)
1472 pResults[index].pItf = NULL;
1473 pResults[index].hr = E_NOINTERFACE;
1477 * Get the object and get it's IUnknown pointer.
1479 hr = CoCreateInstance(rclsid,
1480 pUnkOuter,
1481 dwClsContext,
1482 &IID_IUnknown,
1483 (VOID**)&pUnk);
1485 if (hr)
1486 return hr;
1489 * Then, query for all the interfaces requested.
1491 for (index = 0; index < cmq; index++)
1493 pResults[index].hr = IUnknown_QueryInterface(pUnk,
1494 pResults[index].pIID,
1495 (VOID**)&(pResults[index].pItf));
1497 if (pResults[index].hr == S_OK)
1498 successCount++;
1502 * Release our temporary unknown pointer.
1504 IUnknown_Release(pUnk);
1506 if (successCount == 0)
1507 return E_NOINTERFACE;
1509 if (successCount!=cmq)
1510 return CO_S_NOTALLINTERFACES;
1512 return S_OK;
1515 /***********************************************************************
1516 * CoFreeLibrary [COMPOBJ.13]
1518 void WINAPI CoFreeLibrary(HINSTANCE hLibrary)
1520 OpenDll *ptr, *prev;
1521 OpenDll *tmp;
1523 /* lookup library in linked list */
1524 prev = NULL;
1525 for (ptr = openDllList; ptr != NULL; ptr=ptr->next) {
1526 if (ptr->hLibrary == hLibrary) {
1527 break;
1529 prev = ptr;
1532 if (ptr == NULL) {
1533 /* shouldn't happen if user passed in a valid hLibrary */
1534 return;
1536 /* assert: ptr points to the library entry to free */
1538 /* free library and remove node from list */
1539 FreeLibrary(hLibrary);
1540 if (ptr == openDllList) {
1541 tmp = openDllList->next;
1542 HeapFree(GetProcessHeap(), 0, openDllList->DllName);
1543 HeapFree(GetProcessHeap(), 0, openDllList);
1544 openDllList = tmp;
1545 } else {
1546 tmp = ptr->next;
1547 HeapFree(GetProcessHeap(), 0, ptr->DllName);
1548 HeapFree(GetProcessHeap(), 0, ptr);
1549 prev->next = tmp;
1555 /***********************************************************************
1556 * CoFreeAllLibraries [COMPOBJ.12]
1558 void WINAPI CoFreeAllLibraries(void)
1560 OpenDll *ptr, *tmp;
1562 for (ptr = openDllList; ptr != NULL; ) {
1563 tmp=ptr->next;
1564 CoFreeLibrary(ptr->hLibrary);
1565 ptr = tmp;
1571 /***********************************************************************
1572 * CoFreeUnusedLibraries [COMPOBJ.17]
1574 void WINAPI CoFreeUnusedLibraries(void)
1576 OpenDll *ptr, *tmp;
1577 typedef HRESULT(*DllCanUnloadNowFunc)(void);
1578 DllCanUnloadNowFunc DllCanUnloadNow;
1580 for (ptr = openDllList; ptr != NULL; ) {
1581 DllCanUnloadNow = (DllCanUnloadNowFunc)
1582 GetProcAddress(ptr->hLibrary, "DllCanUnloadNow");
1584 if (DllCanUnloadNow() == S_OK) {
1585 tmp=ptr->next;
1586 CoFreeLibrary(ptr->hLibrary);
1587 ptr = tmp;
1588 } else {
1589 ptr=ptr->next;
1594 /***********************************************************************
1595 * CoFileTimeNow [COMPOBJ.82, OLE32.10]
1596 * RETURNS
1597 * the current system time in lpFileTime
1599 HRESULT WINAPI CoFileTimeNow(
1600 FILETIME *lpFileTime /* [out] the current time */
1602 DOSFS_UnixTimeToFileTime(time(NULL), lpFileTime, 0);
1603 return S_OK;
1606 /***********************************************************************
1607 * CoTaskMemAlloc (OLE32.43)
1608 * RETURNS
1609 * pointer to newly allocated block
1611 LPVOID WINAPI CoTaskMemAlloc(
1612 ULONG size /* [in] size of memoryblock to be allocated */
1614 LPMALLOC lpmalloc;
1615 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1617 if (FAILED(ret))
1618 return NULL;
1620 return IMalloc_Alloc(lpmalloc,size);
1622 /***********************************************************************
1623 * CoTaskMemFree (OLE32.44)
1625 VOID WINAPI CoTaskMemFree(
1626 LPVOID ptr /* [in] pointer to be freed */
1628 LPMALLOC lpmalloc;
1629 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1631 if (FAILED(ret))
1632 return;
1634 IMalloc_Free(lpmalloc, ptr);
1637 /***********************************************************************
1638 * CoTaskMemRealloc (OLE32.45)
1639 * RETURNS
1640 * pointer to newly allocated block
1642 LPVOID WINAPI CoTaskMemRealloc(
1643 LPVOID pvOld,
1644 ULONG size) /* [in] size of memoryblock to be allocated */
1646 LPMALLOC lpmalloc;
1647 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1649 if (FAILED(ret))
1650 return NULL;
1652 return IMalloc_Realloc(lpmalloc, pvOld, size);
1655 /***********************************************************************
1656 * CoLoadLibrary (OLE32.30)
1658 HINSTANCE WINAPI CoLoadLibrary(LPOLESTR16 lpszLibName, BOOL bAutoFree)
1660 HINSTANCE hLibrary;
1661 OpenDll *ptr;
1662 OpenDll *tmp;
1664 TRACE(ole,"CoLoadLibrary(%p, %d\n", lpszLibName, bAutoFree);
1666 hLibrary = LoadLibraryA(lpszLibName);
1668 if (!bAutoFree)
1669 return hLibrary;
1671 if (openDllList == NULL) {
1672 /* empty list -- add first node */
1673 openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
1674 openDllList->DllName = HEAP_strdupA(GetProcessHeap(), 0, lpszLibName);
1675 openDllList->hLibrary = hLibrary;
1676 openDllList->next = NULL;
1677 } else {
1678 /* search for this dll */
1679 int found = FALSE;
1680 for (ptr = openDllList; ptr->next != NULL; ptr=ptr->next) {
1681 if (ptr->hLibrary == hLibrary) {
1682 found = TRUE;
1683 break;
1686 if (!found) {
1687 /* dll not found, add it */
1688 tmp = openDllList;
1689 openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
1690 openDllList->DllName = HEAP_strdupA(GetProcessHeap(), 0, lpszLibName);
1691 openDllList->hLibrary = hLibrary;
1692 openDllList->next = tmp;
1696 return hLibrary;
1699 /***********************************************************************
1700 * CoInitializeWOW (OLE32.27)
1702 HRESULT WINAPI CoInitializeWOW(DWORD x,DWORD y) {
1703 FIXME(ole,"(0x%08lx,0x%08lx),stub!\n",x,y);
1704 return 0;
1707 /******************************************************************************
1708 * CoLockObjectExternal16 [COMPOBJ.63]
1710 HRESULT WINAPI CoLockObjectExternal16(
1711 LPUNKNOWN pUnk, /* [in] object to be locked */
1712 BOOL16 fLock, /* [in] do lock */
1713 BOOL16 fLastUnlockReleases /* [in] ? */
1715 FIXME(ole,"(%p,%d,%d),stub!\n",pUnk,fLock,fLastUnlockReleases);
1716 return S_OK;
1719 /******************************************************************************
1720 * CoLockObjectExternal32 [OLE32.31]
1722 HRESULT WINAPI CoLockObjectExternal(
1723 LPUNKNOWN pUnk, /* [in] object to be locked */
1724 BOOL fLock, /* [in] do lock */
1725 BOOL fLastUnlockReleases) /* [in] unlock all */
1728 if (fLock)
1731 * Increment the external lock coutner, COM_ExternalLockAddRef also
1732 * increment the object's internal lock counter.
1734 COM_ExternalLockAddRef( pUnk);
1736 else
1739 * Decrement the external lock coutner, COM_ExternalLockRelease also
1740 * decrement the object's internal lock counter.
1742 COM_ExternalLockRelease( pUnk, fLastUnlockReleases);
1745 return S_OK;
1748 /***********************************************************************
1749 * CoGetState16 [COMPOBJ.115]
1751 HRESULT WINAPI CoGetState16(LPDWORD state)
1753 FIXME(ole, "(%p),stub!\n", state);
1754 *state = 0;
1755 return S_OK;
1757 /***********************************************************************
1758 * CoSetState32 [COM32.42]
1760 HRESULT WINAPI CoSetState(LPDWORD state)
1762 FIXME(ole, "(%p),stub!\n", state);
1763 *state = 0;
1764 return S_OK;
1767 /***
1768 * COM_RevokeAllClasses
1770 * This method is called when the COM libraries are uninitialized to
1771 * release all the references to the class objects registered with
1772 * the library
1774 static void COM_RevokeAllClasses()
1776 while (firstRegisteredClass!=0)
1778 CoRevokeClassObject(firstRegisteredClass->dwCookie);
1782 /****************************************************************************
1783 * COM External Lock methods implementation
1786 /****************************************************************************
1787 * Public - Method that increments the count for a IUnknown* in the linked
1788 * list. The item is inserted if not already in the list.
1790 static void COM_ExternalLockAddRef(
1791 IUnknown *pUnk)
1793 COM_ExternalLock *externalLock = COM_ExternalLockFind(pUnk);
1796 * Add an external lock to the object. If it was already externally
1797 * locked, just increase the reference count. If it was not.
1798 * add the item to the list.
1800 if ( externalLock == EL_NOT_FOUND )
1801 COM_ExternalLockInsert(pUnk);
1802 else
1803 externalLock->uRefCount++;
1806 * Add an internal lock to the object
1808 IUnknown_AddRef(pUnk);
1811 /****************************************************************************
1812 * Public - Method that decrements the count for a IUnknown* in the linked
1813 * list. The item is removed from the list if its count end up at zero or if
1814 * bRelAll is TRUE.
1816 static void COM_ExternalLockRelease(
1817 IUnknown *pUnk,
1818 BOOL bRelAll)
1820 COM_ExternalLock *externalLock = COM_ExternalLockFind(pUnk);
1822 if ( externalLock != EL_NOT_FOUND )
1826 externalLock->uRefCount--; /* release external locks */
1827 IUnknown_Release(pUnk); /* release local locks as well */
1829 if ( bRelAll == FALSE )
1830 break; /* perform single release */
1832 } while ( externalLock->uRefCount > 0 );
1834 if ( externalLock->uRefCount == 0 ) /* get rid of the list entry */
1835 COM_ExternalLockDelete(externalLock);
1838 /****************************************************************************
1839 * Public - Method that frees the content of the list.
1841 static void COM_ExternalLockFreeList()
1843 COM_ExternalLock *head;
1845 head = elList.head; /* grab it by the head */
1846 while ( head != EL_END_OF_LIST )
1848 COM_ExternalLockDelete(head); /* get rid of the head stuff */
1850 head = elList.head; /* get the new head... */
1854 /****************************************************************************
1855 * Public - Method that dump the content of the list.
1857 void COM_ExternalLockDump()
1859 COM_ExternalLock *current = elList.head;
1861 printf("\nExternal lock list contains:\n");
1863 while ( current != EL_END_OF_LIST )
1865 printf( "\t%p with %lu references count.\n",
1866 current->pUnk,
1867 current->uRefCount);
1869 /* Skip to the next item */
1870 current = current->next;
1875 /****************************************************************************
1876 * Internal - Find a IUnknown* in the linked list
1878 static COM_ExternalLock* COM_ExternalLockFind(
1879 IUnknown *pUnk)
1881 return COM_ExternalLockLocate(elList.head, pUnk);
1884 /****************************************************************************
1885 * Internal - Recursivity agent for IUnknownExternalLockList_Find
1887 static COM_ExternalLock* COM_ExternalLockLocate(
1888 COM_ExternalLock *element,
1889 IUnknown *pUnk)
1891 if ( element == EL_END_OF_LIST )
1892 return EL_NOT_FOUND;
1894 else if ( element->pUnk == pUnk ) /* We found it */
1895 return element;
1897 else /* Not the right guy, keep on looking */
1898 return COM_ExternalLockLocate( element->next, pUnk);
1901 /****************************************************************************
1902 * Internal - Insert a new IUnknown* to the linked list
1904 static BOOL COM_ExternalLockInsert(
1905 IUnknown *pUnk)
1907 COM_ExternalLock *newLock = NULL;
1908 COM_ExternalLock *previousHead = NULL;
1911 * Allocate space for the new storage object
1913 newLock = HeapAlloc(GetProcessHeap(), 0, sizeof(COM_ExternalLock));
1915 if (newLock!=NULL)
1917 if ( elList.head == EL_END_OF_LIST )
1919 elList.head = newLock; /* The list is empty */
1921 else
1924 * insert does it at the head
1926 previousHead = elList.head;
1927 elList.head = newLock;
1931 * Set new list item data member
1933 newLock->pUnk = pUnk;
1934 newLock->uRefCount = 1;
1935 newLock->next = previousHead;
1937 return TRUE;
1939 else
1940 return FALSE;
1943 /****************************************************************************
1944 * Internal - Method that removes an item from the linked list.
1946 static void COM_ExternalLockDelete(
1947 COM_ExternalLock *itemList)
1949 COM_ExternalLock *current = elList.head;
1951 if ( current == itemList )
1954 * this section handles the deletion of the first node
1956 elList.head = itemList->next;
1957 HeapFree( GetProcessHeap(), 0, itemList);
1959 else
1963 if ( current->next == itemList ) /* We found the item to free */
1965 current->next = itemList->next; /* readjust the list pointers */
1967 HeapFree( GetProcessHeap(), 0, itemList);
1968 break;
1971 /* Skip to the next item */
1972 current = current->next;
1974 } while ( current != EL_END_OF_LIST );