Added support for forwarded ordinals in built-in dlls.
[wine/multimedia.git] / ole / compobj.c
blob8f84d04e2fe7d4d7a4fabaae6e8355dee41c6c1b
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"
55 DEFAULT_DEBUG_CHANNEL(ole)
56 /****************************************************************************
57 * COM External Lock structures and methods declaration
59 * This api provides a linked list to managed external references to
60 * COM objects.
62 * The public interface consists of three calls:
63 * COM_ExternalLockAddRef
64 * COM_ExternalLockRelease
65 * COM_ExternalLockFreeList
68 #define EL_END_OF_LIST 0
69 #define EL_NOT_FOUND 0
72 * Declaration of the static structure that manage the
73 * external lock to COM objects.
75 typedef struct COM_ExternalLock COM_ExternalLock;
76 typedef struct COM_ExternalLockList COM_ExternalLockList;
78 struct COM_ExternalLock
80 IUnknown *pUnk; /* IUnknown referenced */
81 ULONG uRefCount; /* external lock counter to IUnknown object*/
82 COM_ExternalLock *next; /* Pointer to next element in list */
85 struct COM_ExternalLockList
87 COM_ExternalLock *head; /* head of list */
91 * Declaration and initialization of the static structure that manages
92 * the external lock to COM objects.
94 static COM_ExternalLockList elList = { EL_END_OF_LIST };
97 * Public Interface to the external lock list
99 static void COM_ExternalLockFreeList();
100 static void COM_ExternalLockAddRef(IUnknown *pUnk);
101 static void COM_ExternalLockRelease(IUnknown *pUnk, BOOL bRelAll);
102 void COM_ExternalLockDump(); /* testing purposes, not static to avoid warning */
105 * Private methods used to managed the linked list
107 static BOOL COM_ExternalLockInsert(
108 IUnknown *pUnk);
110 static void COM_ExternalLockDelete(
111 COM_ExternalLock *element);
113 static COM_ExternalLock* COM_ExternalLockFind(
114 IUnknown *pUnk);
116 static COM_ExternalLock* COM_ExternalLockLocate(
117 COM_ExternalLock *element,
118 IUnknown *pUnk);
120 /****************************************************************************
121 * This section defines variables internal to the COM module.
123 * TODO: Most of these things will have to be made thread-safe.
125 LPMALLOC16 currentMalloc16=NULL;
126 LPMALLOC currentMalloc32=NULL;
128 HTASK16 hETask = 0;
129 WORD Table_ETask[62];
132 * This lock count counts the number of times CoInitialize is called. It is
133 * decreased every time CoUninitialize is called. When it hits 0, the COM
134 * libraries are freed
136 static ULONG s_COMLockCount = 0;
139 * This linked list contains the list of registered class objects. These
140 * are mostly used to register the factories for out-of-proc servers of OLE
141 * objects.
143 * TODO: Make this data structure aware of inter-process communication. This
144 * means that parts of this will be exported to the Wine Server.
146 typedef struct tagRegisteredClass
148 CLSID classIdentifier;
149 LPUNKNOWN classObject;
150 DWORD runContext;
151 DWORD connectFlags;
152 DWORD dwCookie;
153 struct tagRegisteredClass* nextClass;
154 } RegisteredClass;
156 static RegisteredClass* firstRegisteredClass = NULL;
158 /* this open DLL table belongs in a per process table, but my guess is that
159 * it shouldn't live in the kernel, so I'll put them out here in DLL
160 * space assuming that there is one OLE32 per process.
162 typedef struct tagOpenDll {
163 char *DllName; /* really only needed for debugging */
164 HINSTANCE hLibrary;
165 struct tagOpenDll *next;
166 } OpenDll;
168 static OpenDll *openDllList = NULL; /* linked list of open dlls */
170 /*****************************************************************************
171 * This section contains prototypes to internal methods for this
172 * module
174 static HRESULT COM_GetRegisteredClassObject(REFCLSID rclsid,
175 DWORD dwClsContext,
176 LPUNKNOWN* ppUnk);
178 static void COM_RevokeAllClasses();
181 /******************************************************************************
182 * CoBuildVersion [COMPOBJ.1]
184 * RETURNS
185 * Current built version, hiword is majornumber, loword is minornumber
187 DWORD WINAPI CoBuildVersion(void)
189 TRACE(ole,"(void)\n");
190 return (rmm<<16)+rup;
193 /******************************************************************************
194 * CoInitialize16 [COMPOBJ.2]
195 * Set the win16 IMalloc used for memory management
197 HRESULT WINAPI CoInitialize16(
198 LPVOID lpReserved /* [in] pointer to win16 malloc interface */
200 currentMalloc16 = (LPMALLOC16)lpReserved;
201 return S_OK;
204 /******************************************************************************
205 * CoInitialize32 [OLE32.26]
207 * Initializes the COM libraries.
209 * See CoInitializeEx32
211 HRESULT WINAPI CoInitialize(
212 LPVOID lpReserved /* [in] pointer to win32 malloc interface
213 (obsolete, should be NULL) */
217 * Just delegate to the newer method.
219 return CoInitializeEx(lpReserved, COINIT_APARTMENTTHREADED);
222 /******************************************************************************
223 * CoInitializeEx32 [OLE32.163]
225 * Initializes the COM libraries. The behavior used to set the win32 IMalloc
226 * used for memory management is obsolete.
228 * RETURNS
229 * S_OK if successful,
230 * S_FALSE if this function was called already.
231 * RPC_E_CHANGED_MODE if a previous call to CoInitialize specified another
232 * threading model.
234 * BUGS
235 * Only the single threaded model is supported. As a result RPC_E_CHANGED_MODE
236 * is never returned.
238 * See the windows documentation for more details.
240 HRESULT WINAPI CoInitializeEx(
241 LPVOID lpReserved, /* [in] pointer to win32 malloc interface
242 (obsolete, should be NULL) */
243 DWORD dwCoInit /* [in] A value from COINIT specifies the threading model */
246 HRESULT hr;
248 TRACE(ole, "(%p, %x)\n", lpReserved, (int)dwCoInit);
250 if (lpReserved!=NULL)
252 ERR(ole,"(%p, %x) - Bad parameter passed-in %p, must be an old Windows Application\n", lpReserved, (int)dwCoInit, lpReserved);
256 * Check for unsupported features.
258 if (dwCoInit!=COINIT_APARTMENTTHREADED)
260 FIXME(ole, ":(%p,%x): unsupported flag %x\n", lpReserved, (int)dwCoInit, (int)dwCoInit);
261 /* Hope for the best and continue anyway */
265 * Check the lock count. If this is the first time going through the initialize
266 * process, we have to initialize the libraries.
268 if (s_COMLockCount==0)
271 * Initialize the various COM libraries and data structures.
273 TRACE(ole, "() - Initializing the COM libraries\n");
275 RunningObjectTableImpl_Initialize();
277 hr = S_OK;
279 else
280 hr = S_FALSE;
283 * Crank-up that lock count.
285 s_COMLockCount++;
287 return hr;
290 /***********************************************************************
291 * CoUninitialize16 [COMPOBJ.3]
292 * Don't know what it does.
293 * 3-Nov-98 -- this was originally misspelled, I changed it to what I
294 * believe is the correct spelling
296 void WINAPI CoUninitialize16(void)
298 TRACE(ole,"()\n");
299 CoFreeAllLibraries();
302 /***********************************************************************
303 * CoUninitialize32 [OLE32.47]
305 * This method will release the COM libraries.
307 * See the windows documentation for more details.
309 void WINAPI CoUninitialize(void)
311 TRACE(ole,"()\n");
314 * Decrease the reference count.
316 s_COMLockCount--;
319 * If we are back to 0 locks on the COM library, make sure we free
320 * all the associated data structures.
322 if (s_COMLockCount==0)
325 * Release the various COM libraries and data structures.
327 TRACE(ole, "() - Releasing the COM libraries\n");
329 RunningObjectTableImpl_UnInitialize();
331 * Release the references to the registered class objects.
333 COM_RevokeAllClasses();
336 * This will free the loaded COM Dlls.
338 CoFreeAllLibraries();
341 * This will free list of external references to COM objects.
343 COM_ExternalLockFreeList();
347 /***********************************************************************
348 * CoGetMalloc16 [COMPOBJ.4]
349 * RETURNS
350 * The current win16 IMalloc
352 HRESULT WINAPI CoGetMalloc16(
353 DWORD dwMemContext, /* [in] unknown */
354 LPMALLOC16 * lpMalloc /* [out] current win16 malloc interface */
356 if(!currentMalloc16)
357 currentMalloc16 = IMalloc16_Constructor();
358 *lpMalloc = currentMalloc16;
359 return S_OK;
362 /******************************************************************************
363 * CoGetMalloc32 [OLE32.20]
365 * RETURNS
366 * The current win32 IMalloc
368 HRESULT WINAPI CoGetMalloc(
369 DWORD dwMemContext, /* [in] unknown */
370 LPMALLOC *lpMalloc /* [out] current win32 malloc interface */
372 if(!currentMalloc32)
373 currentMalloc32 = IMalloc_Constructor();
374 *lpMalloc = currentMalloc32;
375 return S_OK;
378 /***********************************************************************
379 * CoCreateStandardMalloc16 [COMPOBJ.71]
381 HRESULT WINAPI CoCreateStandardMalloc16(DWORD dwMemContext,
382 LPMALLOC16 *lpMalloc)
384 /* FIXME: docu says we shouldn't return the same allocator as in
385 * CoGetMalloc16 */
386 *lpMalloc = IMalloc16_Constructor();
387 return S_OK;
390 /******************************************************************************
391 * CoDisconnectObject [COMPOBJ.15]
393 HRESULT WINAPI CoDisconnectObject( LPUNKNOWN lpUnk, DWORD reserved )
395 TRACE(ole,"%p %lx\n",lpUnk,reserved);
396 return S_OK;
399 /***********************************************************************
400 * IsEqualGUID16 [COMPOBJ.18]
402 * Compares two Unique Identifiers.
404 * RETURNS
405 * TRUE if equal
407 BOOL16 WINAPI IsEqualGUID16(
408 GUID* g1, /* [in] unique id 1 */
409 GUID* g2 /**/
411 return !memcmp( g1, g2, sizeof(GUID) );
414 /***********************************************************************
415 * IsEqualGUID32 [OLE32.76]
417 * Compares two Unique Identifiers.
419 * RETURNS
420 * TRUE if equal
422 BOOL WINAPI IsEqualGUID32(
423 REFGUID rguid1, /* [in] unique id 1 */
424 REFGUID rguid2 /* [in] unique id 2 */
427 return !memcmp(rguid1,rguid2,sizeof(GUID));
430 /******************************************************************************
431 * CLSIDFromString16 [COMPOBJ.20]
432 * Converts a unique identifier from it's string representation into
433 * the GUID struct.
435 * Class id: DWORD-WORD-WORD-BYTES[2]-BYTES[6]
437 * RETURNS
438 * the converted GUID
440 HRESULT WINAPI CLSIDFromString16(
441 LPCOLESTR16 idstr, /* [in] string representation of guid */
442 CLSID *id /* [out] GUID converted from string */
444 BYTE *s = (BYTE *) idstr;
445 BYTE *p;
446 int i;
447 BYTE table[256];
449 TRACE(ole,"%s -> %p\n", idstr, id);
451 /* quick lookup table */
452 memset(table, 0, 256);
454 for (i = 0; i < 10; i++) {
455 table['0' + i] = i;
457 for (i = 0; i < 6; i++) {
458 table['A' + i] = i+10;
459 table['a' + i] = i+10;
462 /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
464 if (strlen(idstr) != 38)
465 return OLE_ERROR_OBJECT;
467 p = (BYTE *) id;
469 s++; /* skip leading brace */
470 for (i = 0; i < 4; i++) {
471 p[3 - i] = table[*s]<<4 | table[*(s+1)];
472 s += 2;
474 p += 4;
475 s++; /* skip - */
477 for (i = 0; i < 2; i++) {
478 p[1-i] = table[*s]<<4 | table[*(s+1)];
479 s += 2;
481 p += 2;
482 s++; /* skip - */
484 for (i = 0; i < 2; i++) {
485 p[1-i] = table[*s]<<4 | table[*(s+1)];
486 s += 2;
488 p += 2;
489 s++; /* skip - */
491 /* these are just sequential bytes */
492 for (i = 0; i < 2; i++) {
493 *p++ = table[*s]<<4 | table[*(s+1)];
494 s += 2;
496 s++; /* skip - */
498 for (i = 0; i < 6; i++) {
499 *p++ = table[*s]<<4 | table[*(s+1)];
500 s += 2;
503 return S_OK;
506 /******************************************************************************
507 * CoCreateGuid[OLE32.6]
509 * Creates a 128bit GUID.
510 * Implemented according the DCE specification for UUID generation.
511 * Code is based upon uuid library in e2fsprogs by Theodore Ts'o.
512 * Copyright (C) 1996, 1997 Theodore Ts'o.
514 * RETURNS
516 * S_OK if successful.
518 HRESULT WINAPI CoCreateGuid(
519 GUID *pguid /* [out] points to the GUID to initialize */
521 static char has_init = 0;
522 unsigned char a[6];
523 static int adjustment = 0;
524 static struct timeval last = {0, 0};
525 static UINT16 clock_seq;
526 struct timeval tv;
527 unsigned long long clock_reg;
528 UINT clock_high, clock_low;
529 UINT16 temp_clock_seq, temp_clock_mid, temp_clock_hi_and_version;
530 #ifdef HAVE_NET_IF_H
531 int sd;
532 struct ifreq ifr, *ifrp;
533 struct ifconf ifc;
534 char buf[1024];
535 int n, i;
536 #endif
538 /* Have we already tried to get the MAC address? */
539 if (!has_init) {
540 #ifdef HAVE_NET_IF_H
541 /* BSD 4.4 defines the size of an ifreq to be
542 * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
543 * However, under earlier systems, sa_len isn't present, so
544 * the size is just sizeof(struct ifreq)
546 # ifdef HAVE_SA_LEN
547 # ifndef max
548 # define max(a,b) ((a) > (b) ? (a) : (b))
549 # endif
550 # define ifreq_size(i) max(sizeof(struct ifreq),\
551 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
552 # else
553 # define ifreq_size(i) sizeof(struct ifreq)
554 # endif /* HAVE_SA_LEN */
556 sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
557 if (sd < 0) {
558 /* if we can't open a socket, just use random numbers */
559 /* set the multicast bit to prevent conflicts with real cards */
560 a[0] = (rand() & 0xff) | 0x80;
561 a[1] = rand() & 0xff;
562 a[2] = rand() & 0xff;
563 a[3] = rand() & 0xff;
564 a[4] = rand() & 0xff;
565 a[5] = rand() & 0xff;
566 } else {
567 memset(buf, 0, sizeof(buf));
568 ifc.ifc_len = sizeof(buf);
569 ifc.ifc_buf = buf;
570 /* get the ifconf interface */
571 if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) {
572 close(sd);
573 /* no ifconf, so just use random numbers */
574 /* set the multicast bit to prevent conflicts with real cards */
575 a[0] = (rand() & 0xff) | 0x80;
576 a[1] = rand() & 0xff;
577 a[2] = rand() & 0xff;
578 a[3] = rand() & 0xff;
579 a[4] = rand() & 0xff;
580 a[5] = rand() & 0xff;
581 } else {
582 /* loop through the interfaces, looking for a valid one */
583 n = ifc.ifc_len;
584 for (i = 0; i < n; i+= ifreq_size(*ifr) ) {
585 ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i);
586 strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
587 /* try to get the address for this interface */
588 # ifdef SIOCGIFHWADDR
589 if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
590 continue;
591 memcpy(a, (unsigned char *)&ifr.ifr_hwaddr.sa_data, 6);
592 # else
593 # ifdef SIOCGENADDR
594 if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
595 continue;
596 memcpy(a, (unsigned char *) ifr.ifr_enaddr, 6);
597 # else
598 /* XXX we don't have a way of getting the hardware address */
599 close(sd);
600 a[0] = 0;
601 break;
602 # endif /* SIOCGENADDR */
603 # endif /* SIOCGIFHWADDR */
604 /* make sure it's not blank */
605 if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5])
606 continue;
608 goto valid_address;
610 /* if we didn't find a valid address, make a random one */
611 /* once again, set multicast bit to avoid conflicts */
612 a[0] = (rand() & 0xff) | 0x80;
613 a[1] = rand() & 0xff;
614 a[2] = rand() & 0xff;
615 a[3] = rand() & 0xff;
616 a[4] = rand() & 0xff;
617 a[5] = rand() & 0xff;
619 valid_address:
620 close(sd);
623 #else
624 /* no networking info, so generate a random address */
625 a[0] = (rand() & 0xff) | 0x80;
626 a[1] = rand() & 0xff;
627 a[2] = rand() & 0xff;
628 a[3] = rand() & 0xff;
629 a[4] = rand() & 0xff;
630 a[5] = rand() & 0xff;
631 #endif /* HAVE_NET_IF_H */
632 has_init = 1;
635 /* generate time element of GUID */
637 /* Assume that the gettimeofday() has microsecond granularity */
638 #define MAX_ADJUSTMENT 10
640 try_again:
641 gettimeofday(&tv, 0);
642 if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
643 clock_seq = ((rand() & 0xff) << 8) + (rand() & 0xff);
644 clock_seq &= 0x1FFF;
645 last = tv;
646 last.tv_sec--;
648 if ((tv.tv_sec < last.tv_sec) ||
649 ((tv.tv_sec == last.tv_sec) &&
650 (tv.tv_usec < last.tv_usec))) {
651 clock_seq = (clock_seq+1) & 0x1FFF;
652 adjustment = 0;
653 } else if ((tv.tv_sec == last.tv_sec) &&
654 (tv.tv_usec == last.tv_usec)) {
655 if (adjustment >= MAX_ADJUSTMENT)
656 goto try_again;
657 adjustment++;
658 } else
659 adjustment = 0;
661 clock_reg = tv.tv_usec*10 + adjustment;
662 clock_reg += ((unsigned long long) tv.tv_sec)*10000000;
663 clock_reg += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
665 clock_high = clock_reg >> 32;
666 clock_low = clock_reg;
667 temp_clock_seq = clock_seq | 0x8000;
668 temp_clock_mid = (UINT16)clock_high;
669 temp_clock_hi_and_version = (clock_high >> 16) | 0x1000;
671 /* pack the information into the GUID structure */
673 ((unsigned char*)&pguid->Data1)[3] = (unsigned char)clock_low;
674 clock_low >>= 8;
675 ((unsigned char*)&pguid->Data1)[2] = (unsigned char)clock_low;
676 clock_low >>= 8;
677 ((unsigned char*)&pguid->Data1)[1] = (unsigned char)clock_low;
678 clock_low >>= 8;
679 ((unsigned char*)&pguid->Data1)[0] = (unsigned char)clock_low;
681 ((unsigned char*)&pguid->Data2)[1] = (unsigned char)temp_clock_mid;
682 temp_clock_mid >>= 8;
683 ((unsigned char*)&pguid->Data2)[0] = (unsigned char)temp_clock_mid;
685 ((unsigned char*)&pguid->Data3)[1] = (unsigned char)temp_clock_hi_and_version;
686 temp_clock_hi_and_version >>= 8;
687 ((unsigned char*)&pguid->Data3)[0] = (unsigned char)temp_clock_hi_and_version;
689 ((unsigned char*)pguid->Data4)[1] = (unsigned char)temp_clock_seq;
690 temp_clock_seq >>= 8;
691 ((unsigned char*)pguid->Data4)[0] = (unsigned char)temp_clock_seq;
693 ((unsigned char*)pguid->Data4)[2] = a[0];
694 ((unsigned char*)pguid->Data4)[3] = a[1];
695 ((unsigned char*)pguid->Data4)[4] = a[2];
696 ((unsigned char*)pguid->Data4)[5] = a[3];
697 ((unsigned char*)pguid->Data4)[6] = a[4];
698 ((unsigned char*)pguid->Data4)[7] = a[5];
700 TRACE(ole, "%p", pguid);
702 return S_OK;
705 /******************************************************************************
706 * CLSIDFromString32 [OLE32.3]
707 * Converts a unique identifier from it's string representation into
708 * the GUID struct.
709 * RETURNS
710 * the converted GUID
712 HRESULT WINAPI CLSIDFromString(
713 LPCOLESTR idstr, /* [in] string representation of GUID */
714 CLSID *id /* [out] GUID represented by above string */
716 LPOLESTR16 xid = HEAP_strdupWtoA(GetProcessHeap(),0,idstr);
717 OLESTATUS ret = CLSIDFromString16(xid,id);
719 HeapFree(GetProcessHeap(),0,xid);
720 return ret;
723 /******************************************************************************
724 * WINE_StringFromCLSID [Internal]
725 * Converts a GUID into the respective string representation.
727 * NOTES
729 * RETURNS
730 * the string representation and OLESTATUS
732 HRESULT WINE_StringFromCLSID(
733 const CLSID *id, /* [in] GUID to be converted */
734 LPSTR idstr /* [out] pointer to buffer to contain converted guid */
736 static const char *hex = "0123456789ABCDEF";
737 char *s;
738 int i;
740 if (!id)
741 { ERR(ole,"called with id=Null\n");
742 *idstr = 0x00;
743 return E_FAIL;
746 sprintf(idstr, "{%08lX-%04X-%04X-%02X%02X-",
747 id->Data1, id->Data2, id->Data3,
748 id->Data4[0], id->Data4[1]);
749 s = &idstr[25];
751 /* 6 hex bytes */
752 for (i = 2; i < 8; i++) {
753 *s++ = hex[id->Data4[i]>>4];
754 *s++ = hex[id->Data4[i] & 0xf];
757 *s++ = '}';
758 *s++ = '\0';
760 TRACE(ole,"%p->%s\n", id, idstr);
762 return OLE_OK;
765 /******************************************************************************
766 * StringFromCLSID16 [COMPOBJ.19]
767 * Converts a GUID into the respective string representation.
768 * The target string is allocated using the OLE IMalloc.
769 * RETURNS
770 * the string representation and OLESTATUS
772 HRESULT WINAPI StringFromCLSID16(
773 REFCLSID id, /* [in] the GUID to be converted */
774 LPOLESTR16 *idstr /* [out] a pointer to a to-be-allocated segmented pointer pointing to the resulting string */
777 LPMALLOC16 mllc;
778 OLESTATUS ret;
779 DWORD args[2];
781 ret = CoGetMalloc16(0,&mllc);
782 if (ret) return ret;
784 args[0] = (DWORD)mllc;
785 args[1] = 40;
787 /* No need for a Callback entry, we have WOWCallback16Ex which does
788 * everything we need.
790 if (!WOWCallback16Ex(
791 (FARPROC16)((ICOM_VTABLE(IMalloc16)*)PTR_SEG_TO_LIN(
792 ((LPMALLOC16)PTR_SEG_TO_LIN(mllc))->lpvtbl)
793 )->fnAlloc,
794 WCB16_CDECL,
796 (LPVOID)args,
797 (LPDWORD)idstr
798 )) {
799 WARN(ole,"CallTo16 IMalloc16 failed\n");
800 return E_FAIL;
802 return WINE_StringFromCLSID(id,PTR_SEG_TO_LIN(*idstr));
805 /******************************************************************************
806 * StringFromCLSID32 [OLE32.151]
807 * Converts a GUID into the respective string representation.
808 * The target string is allocated using the OLE IMalloc.
809 * RETURNS
810 * the string representation and OLESTATUS
812 HRESULT WINAPI StringFromCLSID(
813 REFCLSID id, /* [in] the GUID to be converted */
814 LPOLESTR *idstr /* [out] a pointer to a to-be-allocated pointer pointing to the resulting string */
816 char buf[80];
817 OLESTATUS ret;
818 LPMALLOC mllc;
820 if ((ret=CoGetMalloc(0,&mllc)))
821 return ret;
823 ret=WINE_StringFromCLSID(id,buf);
824 if (!ret) {
825 *idstr = mllc->lpvtbl->fnAlloc(mllc,strlen(buf)*2+2);
826 lstrcpyAtoW(*idstr,buf);
828 return ret;
831 /******************************************************************************
832 * StringFromGUID2 [COMPOBJ.76] [OLE32.152]
834 * Converts a global unique identifier into a string of an API-
835 * specified fixed format. (The usual {.....} stuff.)
837 * RETURNS
838 * The (UNICODE) string representation of the GUID in 'str'
839 * The length of the resulting string, 0 if there was any problem.
841 INT WINAPI
842 StringFromGUID2(REFGUID id, LPOLESTR str, INT cmax)
844 char xguid[80];
846 if (WINE_StringFromCLSID(id,xguid))
847 return 0;
848 if (strlen(xguid)>=cmax)
849 return 0;
850 lstrcpyAtoW(str,xguid);
851 return strlen(xguid);
854 /******************************************************************************
855 * CLSIDFromProgID16 [COMPOBJ.61]
856 * Converts a program id into the respective GUID. (By using a registry lookup)
857 * RETURNS
858 * riid associated with the progid
860 HRESULT WINAPI CLSIDFromProgID16(
861 LPCOLESTR16 progid, /* [in] program id as found in registry */
862 LPCLSID riid /* [out] associated CLSID */
864 char *buf,buf2[80];
865 DWORD buf2len;
866 HRESULT err;
867 HKEY xhkey;
869 buf = HeapAlloc(GetProcessHeap(),0,strlen(progid)+8);
870 sprintf(buf,"%s\\CLSID",progid);
871 if ((err=RegOpenKeyA(HKEY_CLASSES_ROOT,buf,&xhkey))) {
872 HeapFree(GetProcessHeap(),0,buf);
873 return OLE_ERROR_GENERIC;
875 HeapFree(GetProcessHeap(),0,buf);
876 buf2len = sizeof(buf2);
877 if ((err=RegQueryValueA(xhkey,NULL,buf2,&buf2len))) {
878 RegCloseKey(xhkey);
879 return OLE_ERROR_GENERIC;
881 RegCloseKey(xhkey);
882 return CLSIDFromString16(buf2,riid);
885 /******************************************************************************
886 * CLSIDFromProgID32 [OLE32.2]
887 * Converts a program id into the respective GUID. (By using a registry lookup)
888 * RETURNS
889 * riid associated with the progid
891 HRESULT WINAPI CLSIDFromProgID(
892 LPCOLESTR progid, /* [in] program id as found in registry */
893 LPCLSID riid /* [out] associated CLSID */
895 LPOLESTR16 pid = HEAP_strdupWtoA(GetProcessHeap(),0,progid);
896 OLESTATUS ret = CLSIDFromProgID16(pid,riid);
898 HeapFree(GetProcessHeap(),0,pid);
899 return ret;
902 /************************************************************************************************
903 * OleSaveToStream
905 * This function write a CLSID on stream
907 HRESULT WINAPI WriteClassStm(IStream *pStm,REFCLSID rclsid)
909 TRACE(ole,"(%p,%p)\n",pStm,rclsid);
911 if (rclsid==NULL)
912 return E_INVALIDARG;
914 return IStream_Write(pStm,rclsid,sizeof(CLSID),NULL);
917 /************************************************************************************************
918 * OleSaveToStream
920 * This function read a CLSID from a stream
922 HRESULT WINAPI ReadClassStm(IStream *pStm,REFCLSID rclsid)
924 ULONG nbByte;
925 HRESULT res;
927 TRACE(ole,"(%p,%p)\n",pStm,rclsid);
929 if (rclsid==NULL)
930 return E_INVALIDARG;
932 res = IStream_Read(pStm,(void*)rclsid,sizeof(CLSID),&nbByte);
934 if (FAILED(res))
935 return res;
937 if (nbByte != sizeof(CLSID))
938 return S_FALSE;
939 else
940 return S_OK;
943 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
944 /***********************************************************************
945 * LookupETask (COMPOBJ.94)
947 OLESTATUS WINAPI LookupETask16(HTASK16 *hTask,LPVOID p) {
948 FIXME(ole,"(%p,%p),stub!\n",hTask,p);
949 if ((*hTask = GetCurrentTask()) == hETask) {
950 memcpy(p, Table_ETask, sizeof(Table_ETask));
952 return 0;
955 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
956 /***********************************************************************
957 * SetETask (COMPOBJ.95)
959 OLESTATUS WINAPI SetETask16(HTASK16 hTask, LPVOID p) {
960 FIXME(ole,"(%04x,%p),stub!\n",hTask,p);
961 hETask = hTask;
962 return 0;
965 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
966 /***********************************************************************
967 * CallObjectInWOW (COMPOBJ.201)
969 OLESTATUS WINAPI CallObjectInWOW(LPVOID p1,LPVOID p2) {
970 FIXME(ole,"(%p,%p),stub!\n",p1,p2);
971 return 0;
974 /******************************************************************************
975 * CoRegisterClassObject16 [COMPOBJ.5]
977 * Don't know where it registers it ...
979 HRESULT WINAPI CoRegisterClassObject16(
980 REFCLSID rclsid,
981 LPUNKNOWN pUnk,
982 DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
983 DWORD flags, /* [in] REGCLS flags indicating how connections are made */
984 LPDWORD lpdwRegister
986 char buf[80];
988 WINE_StringFromCLSID(rclsid,buf);
990 FIXME(ole,"(%s,%p,0x%08lx,0x%08lx,%p),stub\n",
991 buf,pUnk,dwClsContext,flags,lpdwRegister
993 return 0;
996 /***
997 * COM_GetRegisteredClassObject
999 * This internal method is used to scan the registered class list to
1000 * find a class object.
1002 * Params:
1003 * rclsid Class ID of the class to find.
1004 * dwClsContext Class context to match.
1005 * ppv [out] returns a pointer to the class object. Complying
1006 * to normal COM usage, this method will increase the
1007 * reference count on this object.
1009 static HRESULT COM_GetRegisteredClassObject(
1010 REFCLSID rclsid,
1011 DWORD dwClsContext,
1012 LPUNKNOWN* ppUnk)
1014 RegisteredClass* curClass;
1017 * Sanity check
1019 assert(ppUnk!=0);
1022 * Iterate through the whole list and try to match the class ID.
1024 curClass = firstRegisteredClass;
1026 while (curClass != 0)
1029 * Check if we have a match on the class ID.
1031 if (IsEqualGUID32(&(curClass->classIdentifier), rclsid))
1034 * Since we don't do out-of process or DCOM just right away, let's ignore the
1035 * class context.
1039 * We have a match, return the pointer to the class object.
1041 *ppUnk = curClass->classObject;
1043 IUnknown_AddRef(curClass->classObject);
1045 return S_OK;
1049 * Step to the next class in the list.
1051 curClass = curClass->nextClass;
1055 * If we get to here, we haven't found our class.
1057 return S_FALSE;
1060 /******************************************************************************
1061 * CoRegisterClassObject32 [OLE32.36]
1063 * This method will register the class object for a given class ID.
1065 * See the Windows documentation for more details.
1067 HRESULT WINAPI CoRegisterClassObject(
1068 REFCLSID rclsid,
1069 LPUNKNOWN pUnk,
1070 DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
1071 DWORD flags, /* [in] REGCLS flags indicating how connections are made */
1072 LPDWORD lpdwRegister
1075 RegisteredClass* newClass;
1076 LPUNKNOWN foundObject;
1077 HRESULT hr;
1078 char buf[80];
1080 WINE_StringFromCLSID(rclsid,buf);
1082 TRACE(ole,"(%s,%p,0x%08lx,0x%08lx,%p)\n",
1083 buf,pUnk,dwClsContext,flags,lpdwRegister);
1086 * Perform a sanity check on the parameters
1088 if ( (lpdwRegister==0) || (pUnk==0) )
1090 return E_INVALIDARG;
1094 * Initialize the cookie (out parameter)
1096 *lpdwRegister = 0;
1099 * First, check if the class is already registered.
1100 * If it is, this should cause an error.
1102 hr = COM_GetRegisteredClassObject(rclsid, dwClsContext, &foundObject);
1104 if (hr == S_OK)
1107 * The COM_GetRegisteredClassObject increased the reference count on the
1108 * object so it has to be released.
1110 IUnknown_Release(foundObject);
1112 return CO_E_OBJISREG;
1116 * If it is not registered, we must create a new entry for this class and
1117 * append it to the registered class list.
1118 * We use the address of the chain node as the cookie since we are sure it's
1119 * unique.
1121 newClass = HeapAlloc(GetProcessHeap(), 0, sizeof(RegisteredClass));
1124 * Initialize the node.
1126 newClass->classIdentifier = *rclsid;
1127 newClass->runContext = dwClsContext;
1128 newClass->connectFlags = flags;
1129 newClass->dwCookie = (DWORD)newClass;
1130 newClass->nextClass = firstRegisteredClass;
1133 * Since we're making a copy of the object pointer, we have to increase it's
1134 * reference count.
1136 newClass->classObject = pUnk;
1137 IUnknown_AddRef(newClass->classObject);
1139 firstRegisteredClass = newClass;
1142 * Assign the out parameter (cookie)
1144 *lpdwRegister = newClass->dwCookie;
1147 * We're successfyl Yippee!
1149 return S_OK;
1152 /***********************************************************************
1153 * CoRevokeClassObject32 [OLE32.40]
1155 * This method will remove a class object from the class registry
1157 * See the Windows documentation for more details.
1159 HRESULT WINAPI CoRevokeClassObject(
1160 DWORD dwRegister)
1162 RegisteredClass** prevClassLink;
1163 RegisteredClass* curClass;
1165 TRACE(ole,"(%08lx)\n",dwRegister);
1168 * Iterate through the whole list and try to match the cookie.
1170 curClass = firstRegisteredClass;
1171 prevClassLink = &firstRegisteredClass;
1173 while (curClass != 0)
1176 * Check if we have a match on the cookie.
1178 if (curClass->dwCookie == dwRegister)
1181 * Remove the class from the chain.
1183 *prevClassLink = curClass->nextClass;
1186 * Release the reference to the class object.
1188 IUnknown_Release(curClass->classObject);
1191 * Free the memory used by the chain node.
1193 HeapFree(GetProcessHeap(), 0, curClass);
1195 return S_OK;
1199 * Step to the next class in the list.
1201 prevClassLink = &(curClass->nextClass);
1202 curClass = curClass->nextClass;
1206 * If we get to here, we haven't found our class.
1208 return E_INVALIDARG;
1211 /***********************************************************************
1212 * CoGetClassObject [COMPOBJ.7]
1214 HRESULT WINAPI CoGetClassObject(REFCLSID rclsid, DWORD dwClsContext,
1215 LPVOID pvReserved, REFIID iid, LPVOID *ppv)
1217 LPUNKNOWN regClassObject;
1218 char xclsid[50],xiid[50];
1219 HRESULT hres = E_UNEXPECTED;
1221 char dllName[MAX_PATH+1];
1222 DWORD dllNameLen = sizeof(dllName);
1223 HINSTANCE hLibrary;
1224 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid,
1225 REFIID iid, LPVOID *ppv);
1226 DllGetClassObjectFunc DllGetClassObject;
1228 WINE_StringFromCLSID((LPCLSID)rclsid,xclsid);
1229 WINE_StringFromCLSID((LPCLSID)iid,xiid);
1230 TRACE(ole,"\n\tCLSID:\t%s,\n\tIID:\t%s\n",xclsid,xiid);
1233 * First, try and see if we can't match the class ID with one of the
1234 * registered classes.
1236 if (S_OK == COM_GetRegisteredClassObject(rclsid, dwClsContext, &regClassObject))
1239 * Get the required interface from the retrieved pointer.
1241 hres = IUnknown_QueryInterface(regClassObject, iid, ppv);
1244 * Since QI got another reference on the pointer, we want to release the
1245 * one we already have. If QI was unsuccessful, this will release the object. This
1246 * is good since we are not returning it in the "out" parameter.
1248 IUnknown_Release(regClassObject);
1250 return hres;
1253 /* out of process and remote servers not supported yet */
1254 if ((CLSCTX_LOCAL_SERVER|CLSCTX_REMOTE_SERVER) & dwClsContext) {
1255 FIXME(ole, "CLSCTX_LOCAL_SERVER and CLSCTX_REMOTE_SERVER not supported!\n");
1256 return E_ACCESSDENIED;
1259 if ((CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER) & dwClsContext) {
1260 HKEY CLSIDkey,key;
1262 /* lookup CLSID in registry key HKCR/CLSID */
1263 hres = RegOpenKeyExA(HKEY_CLASSES_ROOT, "CLSID", 0,
1264 KEY_READ, &CLSIDkey);
1266 if (hres != ERROR_SUCCESS)
1267 return REGDB_E_READREGDB;
1268 hres = RegOpenKeyExA(CLSIDkey,xclsid,0,KEY_QUERY_VALUE,&key);
1269 if (hres != ERROR_SUCCESS) {
1270 RegCloseKey(CLSIDkey);
1271 return REGDB_E_CLASSNOTREG;
1273 hres = RegQueryValueA(key, "InprocServer32", dllName, &dllNameLen);
1274 RegCloseKey(key);
1275 RegCloseKey(CLSIDkey);
1276 if (hres != ERROR_SUCCESS)
1277 return REGDB_E_READREGDB;
1278 TRACE(ole,"found InprocServer32 dll %s\n", dllName);
1280 /* open dll, call DllGetClassFactory */
1281 hLibrary = CoLoadLibrary(dllName, TRUE);
1282 if (hLibrary == 0) {
1283 TRACE(ole,"couldn't load InprocServer32 dll %s\n", dllName);
1284 return E_ACCESSDENIED; /* or should this be CO_E_DLLNOTFOUND? */
1286 DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress(hLibrary, "DllGetClassObject");
1287 if (!DllGetClassObject) {
1288 /* not sure if this should be called here CoFreeLibrary(hLibrary);*/
1289 TRACE(ole,"couldn't find function DllGetClassObject in %s\n", dllName);
1290 return E_ACCESSDENIED;
1294 * Ask the DLL for it's class object. (there was a note here about class
1295 * factories but this is good.
1297 return DllGetClassObject(rclsid, iid, ppv);
1299 return hres;
1302 /****************************************************************************************
1303 * GetClassFile
1305 * This function supplies the CLSID associated with the given filename.
1307 HRESULT WINAPI GetClassFile(LPOLESTR filePathName,CLSID *pclsid)
1309 IStorage *pstg=0;
1310 HRESULT res;
1311 int nbElm=0,length=0,i=0;
1312 LONG sizeProgId=20;
1313 LPOLESTR *pathDec=0,absFile=0,progId=0;
1314 WCHAR extention[100]={0};
1316 TRACE(ole,"()\n");
1318 /* if the file contain a storage object the return the CLSID writen by IStorage_SetClass method*/
1319 if((StgIsStorageFile(filePathName))==S_OK){
1321 res=StgOpenStorage(filePathName,NULL,STGM_READ | STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
1323 if (SUCCEEDED(res))
1324 res=ReadClassStg(pstg,pclsid);
1326 IStorage_Release(pstg);
1328 return res;
1330 /* if the file is not a storage object then attemps to match various bits in the file against a
1331 pattern in the registry. this case is not frequently used ! so I present only the psodocode for
1332 this case
1334 for(i=0;i<nFileTypes;i++)
1336 for(i=0;j<nPatternsForType;j++){
1338 PATTERN pat;
1339 HANDLE hFile;
1341 pat=ReadPatternFromRegistry(i,j);
1342 hFile=CreateFileW(filePathName,,,,,,hFile);
1343 SetFilePosition(hFile,pat.offset);
1344 ReadFile(hFile,buf,pat.size,NULL,NULL);
1345 if (memcmp(buf&pat.mask,pat.pattern.pat.size)==0){
1347 *pclsid=ReadCLSIDFromRegistry(i);
1348 return S_OK;
1353 /* if the obove strategies fail then search for the extension key in the registry */
1355 /* get the last element (absolute file) in the path name */
1356 nbElm=FileMonikerImpl_DecomposePath(filePathName,&pathDec);
1357 absFile=pathDec[nbElm-1];
1359 /* failed if the path represente a directory and not an absolute file name*/
1360 if (lstrcmpW(absFile,(LPOLESTR)"\\"))
1361 return MK_E_INVALIDEXTENSION;
1363 /* get the extension of the file */
1364 length=lstrlenW(absFile);
1365 for(i=length-1; ( (i>=0) && (extention[i]=absFile[i]) );i--);
1367 /* get the progId associated to the extension */
1368 progId=CoTaskMemAlloc(sizeProgId);
1370 res=RegQueryValueW(HKEY_CLASSES_ROOT,extention,progId,&sizeProgId);
1372 if (res==ERROR_MORE_DATA){
1374 CoTaskMemRealloc(progId,sizeProgId);
1376 res=RegQueryValueW(HKEY_CLASSES_ROOT,extention,progId,&sizeProgId);
1378 if (res==ERROR_SUCCESS)
1379 /* return the clsid associated to the progId */
1380 res= CLSIDFromProgID(progId,pclsid);
1382 for(i=0; pathDec[i]!=NULL;i++)
1383 CoTaskMemFree(pathDec[i]);
1384 CoTaskMemFree(pathDec);
1386 CoTaskMemFree(progId);
1388 if (res==ERROR_SUCCESS)
1389 return res;
1391 return MK_E_INVALIDEXTENSION;
1393 /******************************************************************************
1394 * CoRegisterMessageFilter16 [COMPOBJ.27]
1396 HRESULT WINAPI CoRegisterMessageFilter16(
1397 LPMESSAGEFILTER lpMessageFilter,
1398 LPMESSAGEFILTER *lplpMessageFilter
1400 FIXME(ole,"(%p,%p),stub!\n",lpMessageFilter,lplpMessageFilter);
1401 return 0;
1404 /***********************************************************************
1405 * CoCreateInstance [COMPOBJ.13, OLE32.7]
1407 HRESULT WINAPI CoCreateInstance(
1408 REFCLSID rclsid,
1409 LPUNKNOWN pUnkOuter,
1410 DWORD dwClsContext,
1411 REFIID iid,
1412 LPVOID *ppv)
1414 HRESULT hres;
1415 LPCLASSFACTORY lpclf = 0;
1418 * Sanity check
1420 if (ppv==0)
1421 return E_POINTER;
1424 * Initialize the "out" parameter
1426 *ppv = 0;
1429 * Get a class factory to construct the object we want.
1431 hres = CoGetClassObject(rclsid,
1432 dwClsContext,
1433 NULL,
1434 &IID_IClassFactory,
1435 (LPVOID)&lpclf);
1437 if (FAILED(hres))
1438 return hres;
1441 * Create the object and don't forget to release the factory
1443 hres = IClassFactory_CreateInstance(lpclf, pUnkOuter, iid, ppv);
1444 IClassFactory_Release(lpclf);
1446 return hres;
1449 /***********************************************************************
1450 * CoCreateInstanceEx [OLE32.165]
1452 HRESULT WINAPI CoCreateInstanceEx(
1453 REFCLSID rclsid,
1454 LPUNKNOWN pUnkOuter,
1455 DWORD dwClsContext,
1456 COSERVERINFO* pServerInfo,
1457 ULONG cmq,
1458 MULTI_QI* pResults)
1460 IUnknown* pUnk = NULL;
1461 HRESULT hr;
1462 ULONG index;
1463 int successCount = 0;
1466 * Sanity check
1468 if ( (cmq==0) || (pResults==NULL))
1469 return E_INVALIDARG;
1471 if (pServerInfo!=NULL)
1472 FIXME(ole, "() non-NULL pServerInfo not supported!\n");
1475 * Initialize all the "out" parameters.
1477 for (index = 0; index < cmq; index++)
1479 pResults[index].pItf = NULL;
1480 pResults[index].hr = E_NOINTERFACE;
1484 * Get the object and get it's IUnknown pointer.
1486 hr = CoCreateInstance(rclsid,
1487 pUnkOuter,
1488 dwClsContext,
1489 &IID_IUnknown,
1490 (VOID**)&pUnk);
1492 if (hr)
1493 return hr;
1496 * Then, query for all the interfaces requested.
1498 for (index = 0; index < cmq; index++)
1500 pResults[index].hr = IUnknown_QueryInterface(pUnk,
1501 pResults[index].pIID,
1502 (VOID**)&(pResults[index].pItf));
1504 if (pResults[index].hr == S_OK)
1505 successCount++;
1509 * Release our temporary unknown pointer.
1511 IUnknown_Release(pUnk);
1513 if (successCount == 0)
1514 return E_NOINTERFACE;
1516 if (successCount!=cmq)
1517 return CO_S_NOTALLINTERFACES;
1519 return S_OK;
1522 /***********************************************************************
1523 * CoFreeLibrary [COMPOBJ.13]
1525 void WINAPI CoFreeLibrary(HINSTANCE hLibrary)
1527 OpenDll *ptr, *prev;
1528 OpenDll *tmp;
1530 /* lookup library in linked list */
1531 prev = NULL;
1532 for (ptr = openDllList; ptr != NULL; ptr=ptr->next) {
1533 if (ptr->hLibrary == hLibrary) {
1534 break;
1536 prev = ptr;
1539 if (ptr == NULL) {
1540 /* shouldn't happen if user passed in a valid hLibrary */
1541 return;
1543 /* assert: ptr points to the library entry to free */
1545 /* free library and remove node from list */
1546 FreeLibrary(hLibrary);
1547 if (ptr == openDllList) {
1548 tmp = openDllList->next;
1549 HeapFree(GetProcessHeap(), 0, openDllList->DllName);
1550 HeapFree(GetProcessHeap(), 0, openDllList);
1551 openDllList = tmp;
1552 } else {
1553 tmp = ptr->next;
1554 HeapFree(GetProcessHeap(), 0, ptr->DllName);
1555 HeapFree(GetProcessHeap(), 0, ptr);
1556 prev->next = tmp;
1562 /***********************************************************************
1563 * CoFreeAllLibraries [COMPOBJ.12]
1565 void WINAPI CoFreeAllLibraries(void)
1567 OpenDll *ptr, *tmp;
1569 for (ptr = openDllList; ptr != NULL; ) {
1570 tmp=ptr->next;
1571 CoFreeLibrary(ptr->hLibrary);
1572 ptr = tmp;
1578 /***********************************************************************
1579 * CoFreeUnusedLibraries [COMPOBJ.17]
1581 void WINAPI CoFreeUnusedLibraries(void)
1583 OpenDll *ptr, *tmp;
1584 typedef HRESULT(*DllCanUnloadNowFunc)(void);
1585 DllCanUnloadNowFunc DllCanUnloadNow;
1587 for (ptr = openDllList; ptr != NULL; ) {
1588 DllCanUnloadNow = (DllCanUnloadNowFunc)
1589 GetProcAddress(ptr->hLibrary, "DllCanUnloadNow");
1591 if ( (DllCanUnloadNow != NULL) &&
1592 (DllCanUnloadNow() == S_OK) ) {
1593 tmp=ptr->next;
1594 CoFreeLibrary(ptr->hLibrary);
1595 ptr = tmp;
1596 } else {
1597 ptr=ptr->next;
1602 /***********************************************************************
1603 * CoFileTimeNow [COMPOBJ.82, OLE32.10]
1604 * RETURNS
1605 * the current system time in lpFileTime
1607 HRESULT WINAPI CoFileTimeNow(
1608 FILETIME *lpFileTime /* [out] the current time */
1610 DOSFS_UnixTimeToFileTime(time(NULL), lpFileTime, 0);
1611 return S_OK;
1614 /***********************************************************************
1615 * CoTaskMemAlloc (OLE32.43)
1616 * RETURNS
1617 * pointer to newly allocated block
1619 LPVOID WINAPI CoTaskMemAlloc(
1620 ULONG size /* [in] size of memoryblock to be allocated */
1622 LPMALLOC lpmalloc;
1623 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1625 if (FAILED(ret))
1626 return NULL;
1628 return IMalloc_Alloc(lpmalloc,size);
1630 /***********************************************************************
1631 * CoTaskMemFree (OLE32.44)
1633 VOID WINAPI CoTaskMemFree(
1634 LPVOID ptr /* [in] pointer to be freed */
1636 LPMALLOC lpmalloc;
1637 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1639 if (FAILED(ret))
1640 return;
1642 IMalloc_Free(lpmalloc, ptr);
1645 /***********************************************************************
1646 * CoTaskMemRealloc (OLE32.45)
1647 * RETURNS
1648 * pointer to newly allocated block
1650 LPVOID WINAPI CoTaskMemRealloc(
1651 LPVOID pvOld,
1652 ULONG size) /* [in] size of memoryblock to be allocated */
1654 LPMALLOC lpmalloc;
1655 HRESULT ret = CoGetMalloc(0,&lpmalloc);
1657 if (FAILED(ret))
1658 return NULL;
1660 return IMalloc_Realloc(lpmalloc, pvOld, size);
1663 /***********************************************************************
1664 * CoLoadLibrary (OLE32.30)
1666 HINSTANCE WINAPI CoLoadLibrary(LPOLESTR16 lpszLibName, BOOL bAutoFree)
1668 HINSTANCE hLibrary;
1669 OpenDll *ptr;
1670 OpenDll *tmp;
1672 TRACE(ole,"CoLoadLibrary(%p, %d\n", lpszLibName, bAutoFree);
1674 hLibrary = LoadLibraryA(lpszLibName);
1676 if (!bAutoFree)
1677 return hLibrary;
1679 if (openDllList == NULL) {
1680 /* empty list -- add first node */
1681 openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
1682 openDllList->DllName = HEAP_strdupA(GetProcessHeap(), 0, lpszLibName);
1683 openDllList->hLibrary = hLibrary;
1684 openDllList->next = NULL;
1685 } else {
1686 /* search for this dll */
1687 int found = FALSE;
1688 for (ptr = openDllList; ptr->next != NULL; ptr=ptr->next) {
1689 if (ptr->hLibrary == hLibrary) {
1690 found = TRUE;
1691 break;
1694 if (!found) {
1695 /* dll not found, add it */
1696 tmp = openDllList;
1697 openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
1698 openDllList->DllName = HEAP_strdupA(GetProcessHeap(), 0, lpszLibName);
1699 openDllList->hLibrary = hLibrary;
1700 openDllList->next = tmp;
1704 return hLibrary;
1707 /***********************************************************************
1708 * CoInitializeWOW (OLE32.27)
1710 HRESULT WINAPI CoInitializeWOW(DWORD x,DWORD y) {
1711 FIXME(ole,"(0x%08lx,0x%08lx),stub!\n",x,y);
1712 return 0;
1715 /******************************************************************************
1716 * CoLockObjectExternal16 [COMPOBJ.63]
1718 HRESULT WINAPI CoLockObjectExternal16(
1719 LPUNKNOWN pUnk, /* [in] object to be locked */
1720 BOOL16 fLock, /* [in] do lock */
1721 BOOL16 fLastUnlockReleases /* [in] ? */
1723 FIXME(ole,"(%p,%d,%d),stub!\n",pUnk,fLock,fLastUnlockReleases);
1724 return S_OK;
1727 /******************************************************************************
1728 * CoLockObjectExternal32 [OLE32.31]
1730 HRESULT WINAPI CoLockObjectExternal(
1731 LPUNKNOWN pUnk, /* [in] object to be locked */
1732 BOOL fLock, /* [in] do lock */
1733 BOOL fLastUnlockReleases) /* [in] unlock all */
1736 if (fLock)
1739 * Increment the external lock coutner, COM_ExternalLockAddRef also
1740 * increment the object's internal lock counter.
1742 COM_ExternalLockAddRef( pUnk);
1744 else
1747 * Decrement the external lock coutner, COM_ExternalLockRelease also
1748 * decrement the object's internal lock counter.
1750 COM_ExternalLockRelease( pUnk, fLastUnlockReleases);
1753 return S_OK;
1756 /***********************************************************************
1757 * CoGetState16 [COMPOBJ.115]
1759 HRESULT WINAPI CoGetState16(LPDWORD state)
1761 FIXME(ole, "(%p),stub!\n", state);
1762 *state = 0;
1763 return S_OK;
1765 /***********************************************************************
1766 * CoSetState32 [COM32.42]
1768 HRESULT WINAPI CoSetState(LPDWORD state)
1770 FIXME(ole, "(%p),stub!\n", state);
1771 if (state) *state = 0;
1772 return S_OK;
1775 /***
1776 * COM_RevokeAllClasses
1778 * This method is called when the COM libraries are uninitialized to
1779 * release all the references to the class objects registered with
1780 * the library
1782 static void COM_RevokeAllClasses()
1784 while (firstRegisteredClass!=0)
1786 CoRevokeClassObject(firstRegisteredClass->dwCookie);
1790 /****************************************************************************
1791 * COM External Lock methods implementation
1794 /****************************************************************************
1795 * Public - Method that increments the count for a IUnknown* in the linked
1796 * list. The item is inserted if not already in the list.
1798 static void COM_ExternalLockAddRef(
1799 IUnknown *pUnk)
1801 COM_ExternalLock *externalLock = COM_ExternalLockFind(pUnk);
1804 * Add an external lock to the object. If it was already externally
1805 * locked, just increase the reference count. If it was not.
1806 * add the item to the list.
1808 if ( externalLock == EL_NOT_FOUND )
1809 COM_ExternalLockInsert(pUnk);
1810 else
1811 externalLock->uRefCount++;
1814 * Add an internal lock to the object
1816 IUnknown_AddRef(pUnk);
1819 /****************************************************************************
1820 * Public - Method that decrements the count for a IUnknown* in the linked
1821 * list. The item is removed from the list if its count end up at zero or if
1822 * bRelAll is TRUE.
1824 static void COM_ExternalLockRelease(
1825 IUnknown *pUnk,
1826 BOOL bRelAll)
1828 COM_ExternalLock *externalLock = COM_ExternalLockFind(pUnk);
1830 if ( externalLock != EL_NOT_FOUND )
1834 externalLock->uRefCount--; /* release external locks */
1835 IUnknown_Release(pUnk); /* release local locks as well */
1837 if ( bRelAll == FALSE )
1838 break; /* perform single release */
1840 } while ( externalLock->uRefCount > 0 );
1842 if ( externalLock->uRefCount == 0 ) /* get rid of the list entry */
1843 COM_ExternalLockDelete(externalLock);
1846 /****************************************************************************
1847 * Public - Method that frees the content of the list.
1849 static void COM_ExternalLockFreeList()
1851 COM_ExternalLock *head;
1853 head = elList.head; /* grab it by the head */
1854 while ( head != EL_END_OF_LIST )
1856 COM_ExternalLockDelete(head); /* get rid of the head stuff */
1858 head = elList.head; /* get the new head... */
1862 /****************************************************************************
1863 * Public - Method that dump the content of the list.
1865 void COM_ExternalLockDump()
1867 COM_ExternalLock *current = elList.head;
1869 printf("\nExternal lock list contains:\n");
1871 while ( current != EL_END_OF_LIST )
1873 printf( "\t%p with %lu references count.\n",
1874 current->pUnk,
1875 current->uRefCount);
1877 /* Skip to the next item */
1878 current = current->next;
1883 /****************************************************************************
1884 * Internal - Find a IUnknown* in the linked list
1886 static COM_ExternalLock* COM_ExternalLockFind(
1887 IUnknown *pUnk)
1889 return COM_ExternalLockLocate(elList.head, pUnk);
1892 /****************************************************************************
1893 * Internal - Recursivity agent for IUnknownExternalLockList_Find
1895 static COM_ExternalLock* COM_ExternalLockLocate(
1896 COM_ExternalLock *element,
1897 IUnknown *pUnk)
1899 if ( element == EL_END_OF_LIST )
1900 return EL_NOT_FOUND;
1902 else if ( element->pUnk == pUnk ) /* We found it */
1903 return element;
1905 else /* Not the right guy, keep on looking */
1906 return COM_ExternalLockLocate( element->next, pUnk);
1909 /****************************************************************************
1910 * Internal - Insert a new IUnknown* to the linked list
1912 static BOOL COM_ExternalLockInsert(
1913 IUnknown *pUnk)
1915 COM_ExternalLock *newLock = NULL;
1916 COM_ExternalLock *previousHead = NULL;
1919 * Allocate space for the new storage object
1921 newLock = HeapAlloc(GetProcessHeap(), 0, sizeof(COM_ExternalLock));
1923 if (newLock!=NULL)
1925 if ( elList.head == EL_END_OF_LIST )
1927 elList.head = newLock; /* The list is empty */
1929 else
1932 * insert does it at the head
1934 previousHead = elList.head;
1935 elList.head = newLock;
1939 * Set new list item data member
1941 newLock->pUnk = pUnk;
1942 newLock->uRefCount = 1;
1943 newLock->next = previousHead;
1945 return TRUE;
1947 else
1948 return FALSE;
1951 /****************************************************************************
1952 * Internal - Method that removes an item from the linked list.
1954 static void COM_ExternalLockDelete(
1955 COM_ExternalLock *itemList)
1957 COM_ExternalLock *current = elList.head;
1959 if ( current == itemList )
1962 * this section handles the deletion of the first node
1964 elList.head = itemList->next;
1965 HeapFree( GetProcessHeap(), 0, itemList);
1967 else
1971 if ( current->next == itemList ) /* We found the item to free */
1973 current->next = itemList->next; /* readjust the list pointers */
1975 HeapFree( GetProcessHeap(), 0, itemList);
1976 break;
1979 /* Skip to the next item */
1980 current = current->next;
1982 } while ( current != EL_END_OF_LIST );