4 * Copyright 1995 Martin von Loewis
5 * Copyright 1998 Justin Bradford
6 * Copyright 1999 Francis Beaudet
7 * Copyright 1999 Sylvain St-Germain
15 #include <sys/types.h>
18 #ifdef HAVE_SYS_FILE_H
19 # include <sys/file.h>
21 #include <sys/ioctl.h>
22 #include <sys/socket.h>
23 #ifdef HAVE_SYS_SOCKIO_H
24 # include <sys/sockio.h>
29 #ifdef HAVE_NETINET_IN_H
30 # include <netinet/in.h>
36 #include "wine/winbase16.h"
37 #include "wine/winestring.h"
47 #include "wine/obj_base.h"
48 #include "wine/obj_misc.h"
49 #include "wine/obj_clientserver.h"
53 /****************************************************************************
54 * COM External Lock structures and methods declaration
56 * This api provides a linked list to managed external references to
59 * The public interface consists of three calls:
60 * COM_ExternalLockAddRef
61 * COM_ExternalLockRelease
62 * COM_ExternalLockFreeList
65 #define EL_END_OF_LIST 0
66 #define EL_NOT_FOUND 0
69 * Declaration of the static structure that manage the
70 * external lock to COM objects.
72 typedef struct COM_ExternalLock COM_ExternalLock;
73 typedef struct COM_ExternalLockList COM_ExternalLockList;
75 struct COM_ExternalLock
77 IUnknown *pUnk; /* IUnknown referenced */
78 ULONG uRefCount; /* external lock counter to IUnknown object*/
79 COM_ExternalLock *next; /* Pointer to next element in list */
82 struct COM_ExternalLockList
84 COM_ExternalLock *head; /* head of list */
88 * Declaration and initialization of the static structure that manages
89 * the external lock to COM objects.
91 static COM_ExternalLockList elList = { EL_END_OF_LIST };
94 * Public Interface to the external lock list
96 static void COM_ExternalLockFreeList();
97 static void COM_ExternalLockAddRef(IUnknown *pUnk);
98 static void COM_ExternalLockRelease(IUnknown *pUnk, BOOL32 bRelAll);
99 void COM_ExternalLockDump(); /* testing purposes, not static to avoid warning */
102 * Private methods used to managed the linked list
104 static BOOL32 COM_ExternalLockInsert(
107 static void COM_ExternalLockDelete(
108 COM_ExternalLock *element);
110 static COM_ExternalLock* COM_ExternalLockFind(
113 static COM_ExternalLock* COM_ExternalLockLocate(
114 COM_ExternalLock *element,
117 /****************************************************************************
118 * This section defines variables internal to the COM module.
120 * TODO: Most of these things will have to be made thread-safe.
122 LPMALLOC16 currentMalloc16=NULL;
123 LPMALLOC32 currentMalloc32=NULL;
126 WORD Table_ETask[62];
129 * This lock count counts the number of times CoInitialize is called. It is
130 * decreased every time CoUninitialize is called. When it hits 0, the COM
131 * libraries are freed
133 static ULONG s_COMLockCount = 0;
136 * This linked list contains the list of registered class objects. These
137 * are mostly used to register the factories for out-of-proc servers of OLE
140 * TODO: Make this data structure aware of inter-process communication. This
141 * means that parts of this will be exported to the Wine Server.
143 typedef struct tagRegisteredClass
145 CLSID classIdentifier;
146 LPUNKNOWN classObject;
150 struct tagRegisteredClass* nextClass;
153 static RegisteredClass* firstRegisteredClass = NULL;
155 /* this open DLL table belongs in a per process table, but my guess is that
156 * it shouldn't live in the kernel, so I'll put them out here in DLL
157 * space assuming that there is one OLE32 per process.
159 typedef struct tagOpenDll {
160 char *DllName; /* really only needed for debugging */
161 HINSTANCE32 hLibrary;
162 struct tagOpenDll *next;
165 static OpenDll *openDllList = NULL; /* linked list of open dlls */
167 /*****************************************************************************
168 * This section contains prototypes to internal methods for this
171 static HRESULT COM_GetRegisteredClassObject(REFCLSID rclsid,
175 static void COM_RevokeAllClasses();
178 /******************************************************************************
179 * CoBuildVersion [COMPOBJ.1]
182 * Current built version, hiword is majornumber, loword is minornumber
184 DWORD WINAPI CoBuildVersion(void)
186 TRACE(ole,"(void)\n");
187 return (rmm<<16)+rup;
190 /******************************************************************************
191 * CoInitialize16 [COMPOBJ.2]
192 * Set the win16 IMalloc used for memory management
194 HRESULT WINAPI CoInitialize16(
195 LPVOID lpReserved /* [in] pointer to win16 malloc interface */
197 currentMalloc16 = (LPMALLOC16)lpReserved;
201 /******************************************************************************
202 * CoInitialize32 [OLE32.26]
204 * Initializes the COM libraries.
206 * See CoInitializeEx32
208 HRESULT WINAPI CoInitialize32(
209 LPVOID lpReserved /* [in] pointer to win32 malloc interface
210 (obsolete, should be NULL) */
214 * Just delegate to the newer method.
216 return CoInitializeEx32(lpReserved, COINIT_APARTMENTTHREADED);
219 /******************************************************************************
220 * CoInitializeEx32 [OLE32.163]
222 * Initializes the COM libraries. The behavior used to set the win32 IMalloc
223 * used for memory management is obsolete.
226 * S_OK if successful,
227 * S_FALSE if this function was called already.
228 * RPC_E_CHANGED_MODE if a previous call to CoInitialize specified another
232 * Only the single threaded model is supported. As a result RPC_E_CHANGED_MODE
235 * See the windows documentation for more details.
237 HRESULT WINAPI CoInitializeEx32(
238 LPVOID lpReserved, /* [in] pointer to win32 malloc interface
239 (obsolete, should be NULL) */
240 DWORD dwCoInit /* [in] A value from COINIT specifies the threading model */
245 TRACE(ole, "(%p, %x)\n", lpReserved, (int)dwCoInit);
247 if (lpReserved!=NULL)
249 ERR(ole,"(%p, %x) - Bad parameter passed-in %p, must be an old Windows Application\n", lpReserved, (int)dwCoInit, lpReserved);
253 * Check for unsupported features.
255 if (dwCoInit!=COINIT_APARTMENTTHREADED)
257 FIXME(ole, ":(%p,%x): unsupported flag %x\n", lpReserved, (int)dwCoInit, (int)dwCoInit);
258 /* Hope for the best and continue anyway */
262 * Check the lock count. If this is the first time going through the initialize
263 * process, we have to initialize the libraries.
265 if (s_COMLockCount==0)
268 * Initialize the various COM libraries and data structures.
270 TRACE(ole, "() - Initializing the COM libraries\n");
278 * Crank-up that lock count.
285 /***********************************************************************
286 * CoUninitialize16 [COMPOBJ.3]
287 * Don't know what it does.
288 * 3-Nov-98 -- this was originally misspelled, I changed it to what I
289 * believe is the correct spelling
291 void WINAPI CoUninitialize16(void)
294 CoFreeAllLibraries();
297 /***********************************************************************
298 * CoUninitialize32 [OLE32.47]
300 * This method will release the COM libraries.
302 * See the windows documentation for more details.
304 void WINAPI CoUninitialize32(void)
309 * Decrease the reference count.
314 * If we are back to 0 locks on the COM library, make sure we free
315 * all the associated data structures.
317 if (s_COMLockCount==0)
320 * Release the various COM libraries and data structures.
322 TRACE(ole, "() - Releasing the COM libraries\n");
325 * Release the references to the registered class objects.
327 COM_RevokeAllClasses();
330 * This will free the loaded COM Dlls.
332 CoFreeAllLibraries();
335 * This will free list of external references to COM objects.
337 COM_ExternalLockFreeList();
341 /***********************************************************************
342 * CoGetMalloc16 [COMPOBJ.4]
344 * The current win16 IMalloc
346 HRESULT WINAPI CoGetMalloc16(
347 DWORD dwMemContext, /* [in] unknown */
348 LPMALLOC16 * lpMalloc /* [out] current win16 malloc interface */
351 currentMalloc16 = IMalloc16_Constructor();
352 *lpMalloc = currentMalloc16;
356 /******************************************************************************
357 * CoGetMalloc32 [OLE32.20]
360 * The current win32 IMalloc
362 HRESULT WINAPI CoGetMalloc32(
363 DWORD dwMemContext, /* [in] unknown */
364 LPMALLOC32 *lpMalloc /* [out] current win32 malloc interface */
367 currentMalloc32 = IMalloc32_Constructor();
368 *lpMalloc = currentMalloc32;
372 /***********************************************************************
373 * CoCreateStandardMalloc16 [COMPOBJ.71]
375 HRESULT WINAPI CoCreateStandardMalloc16(DWORD dwMemContext,
376 LPMALLOC16 *lpMalloc)
378 /* FIXME: docu says we shouldn't return the same allocator as in
380 *lpMalloc = IMalloc16_Constructor();
384 /******************************************************************************
385 * CoDisconnectObject [COMPOBJ.15]
387 HRESULT WINAPI CoDisconnectObject( LPUNKNOWN lpUnk, DWORD reserved )
389 TRACE(ole,"%p %lx\n",lpUnk,reserved);
393 /***********************************************************************
394 * IsEqualGUID16 [COMPOBJ.18]
396 * Compares two Unique Identifiers.
401 BOOL16 WINAPI IsEqualGUID16(
402 GUID* g1, /* [in] unique id 1 */
405 return !memcmp( g1, g2, sizeof(GUID) );
408 /***********************************************************************
409 * IsEqualGUID32 [OLE32.76]
411 * Compares two Unique Identifiers.
416 BOOL32 WINAPI IsEqualGUID32(
417 REFGUID rguid1, /* [in] unique id 1 */
418 REFGUID rguid2 /* [in] unique id 2 */
421 return !memcmp(rguid1,rguid2,sizeof(GUID));
424 /******************************************************************************
425 * CLSIDFromString16 [COMPOBJ.20]
426 * Converts a unique identifier from it's string representation into
429 * Class id: DWORD-WORD-WORD-BYTES[2]-BYTES[6]
434 HRESULT WINAPI CLSIDFromString16(
435 LPCOLESTR16 idstr, /* [in] string representation of guid */
436 CLSID *id /* [out] GUID converted from string */
438 BYTE *s = (BYTE *) idstr;
443 TRACE(ole,"%s -> %p\n", idstr, id);
445 /* quick lookup table */
446 memset(table, 0, 256);
448 for (i = 0; i < 10; i++) {
451 for (i = 0; i < 6; i++) {
452 table['A' + i] = i+10;
453 table['a' + i] = i+10;
456 /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
458 if (strlen(idstr) != 38)
459 return OLE_ERROR_OBJECT;
463 s++; /* skip leading brace */
464 for (i = 0; i < 4; i++) {
465 p[3 - i] = table[*s]<<4 | table[*(s+1)];
471 for (i = 0; i < 2; i++) {
472 p[1-i] = table[*s]<<4 | table[*(s+1)];
478 for (i = 0; i < 2; i++) {
479 p[1-i] = table[*s]<<4 | table[*(s+1)];
485 /* these are just sequential bytes */
486 for (i = 0; i < 2; i++) {
487 *p++ = table[*s]<<4 | table[*(s+1)];
492 for (i = 0; i < 6; i++) {
493 *p++ = table[*s]<<4 | table[*(s+1)];
500 /******************************************************************************
501 * CoCreateGuid[OLE32.6]
503 * Creates a 128bit GUID.
504 * Implemented according the DCE specification for UUID generation.
505 * Code is based upon uuid library in e2fsprogs by Theodore Ts'o.
506 * Copyright (C) 1996, 1997 Theodore Ts'o.
510 * S_OK if successful.
512 HRESULT WINAPI CoCreateGuid(
513 GUID *pguid /* [out] points to the GUID to initialize */
515 static char has_init = 0;
517 static int adjustment = 0;
518 static struct timeval last = {0, 0};
519 static UINT16 clock_seq;
521 unsigned long long clock_reg;
522 UINT32 clock_high, clock_low;
523 UINT16 temp_clock_seq, temp_clock_mid, temp_clock_hi_and_version;
526 struct ifreq ifr, *ifrp;
532 /* Have we already tried to get the MAC address? */
535 /* BSD 4.4 defines the size of an ifreq to be
536 * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
537 * However, under earlier systems, sa_len isn't present, so
538 * the size is just sizeof(struct ifreq)
542 # define max(a,b) ((a) > (b) ? (a) : (b))
544 # define ifreq_size(i) max(sizeof(struct ifreq),\
545 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
547 # define ifreq_size(i) sizeof(struct ifreq)
548 # endif /* HAVE_SA_LEN */
550 sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
552 /* if we can't open a socket, just use random numbers */
553 /* set the multicast bit to prevent conflicts with real cards */
554 a[0] = (rand() & 0xff) | 0x80;
555 a[1] = rand() & 0xff;
556 a[2] = rand() & 0xff;
557 a[3] = rand() & 0xff;
558 a[4] = rand() & 0xff;
559 a[5] = rand() & 0xff;
561 memset(buf, 0, sizeof(buf));
562 ifc.ifc_len = sizeof(buf);
564 /* get the ifconf interface */
565 if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) {
567 /* no ifconf, so just use random numbers */
568 /* set the multicast bit to prevent conflicts with real cards */
569 a[0] = (rand() & 0xff) | 0x80;
570 a[1] = rand() & 0xff;
571 a[2] = rand() & 0xff;
572 a[3] = rand() & 0xff;
573 a[4] = rand() & 0xff;
574 a[5] = rand() & 0xff;
576 /* loop through the interfaces, looking for a valid one */
578 for (i = 0; i < n; i+= ifreq_size(*ifr) ) {
579 ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i);
580 strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
581 /* try to get the address for this interface */
582 # ifdef SIOCGIFHWADDR
583 if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
585 memcpy(a, (unsigned char *)&ifr.ifr_hwaddr.sa_data, 6);
588 if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
590 memcpy(a, (unsigned char *) ifr.ifr_enaddr, 6);
592 /* XXX we don't have a way of getting the hardware address */
596 # endif /* SIOCGENADDR */
597 # endif /* SIOCGIFHWADDR */
598 /* make sure it's not blank */
599 if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5])
604 /* if we didn't find a valid address, make a random one */
605 /* once again, set multicast bit to avoid conflicts */
606 a[0] = (rand() & 0xff) | 0x80;
607 a[1] = rand() & 0xff;
608 a[2] = rand() & 0xff;
609 a[3] = rand() & 0xff;
610 a[4] = rand() & 0xff;
611 a[5] = rand() & 0xff;
618 /* no networking info, so generate a random address */
619 a[0] = (rand() & 0xff) | 0x80;
620 a[1] = rand() & 0xff;
621 a[2] = rand() & 0xff;
622 a[3] = rand() & 0xff;
623 a[4] = rand() & 0xff;
624 a[5] = rand() & 0xff;
625 #endif /* HAVE_NET_IF_H */
629 /* generate time element of GUID */
631 /* Assume that the gettimeofday() has microsecond granularity */
632 #define MAX_ADJUSTMENT 10
635 gettimeofday(&tv, 0);
636 if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
637 clock_seq = ((rand() & 0xff) << 8) + (rand() & 0xff);
642 if ((tv.tv_sec < last.tv_sec) ||
643 ((tv.tv_sec == last.tv_sec) &&
644 (tv.tv_usec < last.tv_usec))) {
645 clock_seq = (clock_seq+1) & 0x1FFF;
647 } else if ((tv.tv_sec == last.tv_sec) &&
648 (tv.tv_usec == last.tv_usec)) {
649 if (adjustment >= MAX_ADJUSTMENT)
655 clock_reg = tv.tv_usec*10 + adjustment;
656 clock_reg += ((unsigned long long) tv.tv_sec)*10000000;
657 clock_reg += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
659 clock_high = clock_reg >> 32;
660 clock_low = clock_reg;
661 temp_clock_seq = clock_seq | 0x8000;
662 temp_clock_mid = (UINT16)clock_high;
663 temp_clock_hi_and_version = (clock_high >> 16) | 0x1000;
665 /* pack the information into the GUID structure */
667 ((unsigned char*)&pguid->Data1)[3] = (unsigned char)clock_low;
669 ((unsigned char*)&pguid->Data1)[2] = (unsigned char)clock_low;
671 ((unsigned char*)&pguid->Data1)[1] = (unsigned char)clock_low;
673 ((unsigned char*)&pguid->Data1)[0] = (unsigned char)clock_low;
675 ((unsigned char*)&pguid->Data2)[1] = (unsigned char)temp_clock_mid;
676 temp_clock_mid >>= 8;
677 ((unsigned char*)&pguid->Data2)[0] = (unsigned char)temp_clock_mid;
679 ((unsigned char*)&pguid->Data3)[1] = (unsigned char)temp_clock_hi_and_version;
680 temp_clock_hi_and_version >>= 8;
681 ((unsigned char*)&pguid->Data3)[0] = (unsigned char)temp_clock_hi_and_version;
683 ((unsigned char*)pguid->Data4)[1] = (unsigned char)temp_clock_seq;
684 temp_clock_seq >>= 8;
685 ((unsigned char*)pguid->Data4)[0] = (unsigned char)temp_clock_seq;
687 ((unsigned char*)pguid->Data4)[2] = a[0];
688 ((unsigned char*)pguid->Data4)[3] = a[1];
689 ((unsigned char*)pguid->Data4)[4] = a[2];
690 ((unsigned char*)pguid->Data4)[5] = a[3];
691 ((unsigned char*)pguid->Data4)[6] = a[4];
692 ((unsigned char*)pguid->Data4)[7] = a[5];
694 TRACE(ole, "%p", pguid);
699 /******************************************************************************
700 * CLSIDFromString32 [OLE32.3]
701 * Converts a unique identifier from it's string representation into
706 HRESULT WINAPI CLSIDFromString32(
707 LPCOLESTR32 idstr, /* [in] string representation of GUID */
708 CLSID *id /* [out] GUID represented by above string */
710 LPOLESTR16 xid = HEAP_strdupWtoA(GetProcessHeap(),0,idstr);
711 OLESTATUS ret = CLSIDFromString16(xid,id);
713 HeapFree(GetProcessHeap(),0,xid);
717 /******************************************************************************
718 * WINE_StringFromCLSID [Internal]
719 * Converts a GUID into the respective string representation.
724 * the string representation and OLESTATUS
726 HRESULT WINE_StringFromCLSID(
727 const CLSID *id, /* [in] GUID to be converted */
728 LPSTR idstr /* [out] pointer to buffer to contain converted guid */
730 static const char *hex = "0123456789ABCDEF";
735 { ERR(ole,"called with id=Null\n");
740 sprintf(idstr, "{%08lX-%04X-%04X-%02x%02X-",
741 id->Data1, id->Data2, id->Data3,
742 id->Data4[0], id->Data4[1]);
746 for (i = 2; i < 8; i++) {
747 *s++ = hex[id->Data4[i]>>4];
748 *s++ = hex[id->Data4[i] & 0xf];
754 TRACE(ole,"%p->%s\n", id, idstr);
759 /******************************************************************************
760 * StringFromCLSID16 [COMPOBJ.19]
761 * Converts a GUID into the respective string representation.
762 * The target string is allocated using the OLE IMalloc.
764 * the string representation and OLESTATUS
766 HRESULT WINAPI StringFromCLSID16(
767 REFCLSID id, /* [in] the GUID to be converted */
768 LPOLESTR16 *idstr /* [out] a pointer to a to-be-allocated segmented pointer pointing to the resulting string */
775 ret = CoGetMalloc16(0,&mllc);
778 args[0] = (DWORD)mllc;
781 /* No need for a Callback entry, we have WOWCallback16Ex which does
782 * everything we need.
784 if (!WOWCallback16Ex(
785 (FARPROC16)((ICOM_VTABLE(IMalloc16)*)PTR_SEG_TO_LIN(
786 ((LPMALLOC16)PTR_SEG_TO_LIN(mllc))->lpvtbl)
793 WARN(ole,"CallTo16 IMalloc16 failed\n");
796 return WINE_StringFromCLSID(id,PTR_SEG_TO_LIN(*idstr));
799 /******************************************************************************
800 * StringFromCLSID32 [OLE32.151]
801 * Converts a GUID into the respective string representation.
802 * The target string is allocated using the OLE IMalloc.
804 * the string representation and OLESTATUS
806 HRESULT WINAPI StringFromCLSID32(
807 REFCLSID id, /* [in] the GUID to be converted */
808 LPOLESTR32 *idstr /* [out] a pointer to a to-be-allocated pointer pointing to the resulting string */
814 if ((ret=CoGetMalloc32(0,&mllc)))
817 ret=WINE_StringFromCLSID(id,buf);
819 *idstr = mllc->lpvtbl->fnAlloc(mllc,strlen(buf)*2+2);
820 lstrcpyAtoW(*idstr,buf);
825 /******************************************************************************
826 * StringFromGUID2 [COMPOBJ.76] [OLE32.152]
828 * Converts a global unique identifier into a string of an API-
829 * specified fixed format. (The usual {.....} stuff.)
832 * The (UNICODE) string representation of the GUID in 'str'
833 * The length of the resulting string, 0 if there was any problem.
836 StringFromGUID2(REFGUID id, LPOLESTR32 str, INT32 cmax)
840 if (WINE_StringFromCLSID(id,xguid))
842 if (strlen(xguid)>=cmax)
844 lstrcpyAtoW(str,xguid);
845 return strlen(xguid);
848 /******************************************************************************
849 * CLSIDFromProgID16 [COMPOBJ.61]
850 * Converts a program id into the respective GUID. (By using a registry lookup)
852 * riid associated with the progid
854 HRESULT WINAPI CLSIDFromProgID16(
855 LPCOLESTR16 progid, /* [in] program id as found in registry */
856 LPCLSID riid /* [out] associated CLSID */
863 buf = HeapAlloc(GetProcessHeap(),0,strlen(progid)+8);
864 sprintf(buf,"%s\\CLSID",progid);
865 if ((err=RegOpenKey32A(HKEY_CLASSES_ROOT,buf,&xhkey))) {
866 HeapFree(GetProcessHeap(),0,buf);
867 return OLE_ERROR_GENERIC;
869 HeapFree(GetProcessHeap(),0,buf);
870 buf2len = sizeof(buf2);
871 if ((err=RegQueryValue32A(xhkey,NULL,buf2,&buf2len))) {
873 return OLE_ERROR_GENERIC;
876 return CLSIDFromString16(buf2,riid);
879 /******************************************************************************
880 * CLSIDFromProgID32 [OLE32.2]
881 * Converts a program id into the respective GUID. (By using a registry lookup)
883 * riid associated with the progid
885 HRESULT WINAPI CLSIDFromProgID32(
886 LPCOLESTR32 progid, /* [in] program id as found in registry */
887 LPCLSID riid /* [out] associated CLSID */
889 LPOLESTR16 pid = HEAP_strdupWtoA(GetProcessHeap(),0,progid);
890 OLESTATUS ret = CLSIDFromProgID16(pid,riid);
892 HeapFree(GetProcessHeap(),0,pid);
896 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
897 /***********************************************************************
898 * LookupETask (COMPOBJ.94)
900 OLESTATUS WINAPI LookupETask(HTASK16 *hTask,LPVOID p) {
901 FIXME(ole,"(%p,%p),stub!\n",hTask,p);
902 if ((*hTask = GetCurrentTask()) == hETask) {
903 memcpy(p, Table_ETask, sizeof(Table_ETask));
908 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
909 /***********************************************************************
910 * SetETask (COMPOBJ.95)
912 OLESTATUS WINAPI SetETask(HTASK16 hTask, LPVOID p) {
913 FIXME(ole,"(%04x,%p),stub!\n",hTask,p);
918 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
919 /***********************************************************************
920 * CallObjectInWOW (COMPOBJ.201)
922 OLESTATUS WINAPI CallObjectInWOW(LPVOID p1,LPVOID p2) {
923 FIXME(ole,"(%p,%p),stub!\n",p1,p2);
927 /******************************************************************************
928 * CoRegisterClassObject16 [COMPOBJ.5]
930 * Don't know where it registers it ...
932 HRESULT WINAPI CoRegisterClassObject16(
935 DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
936 DWORD flags, /* [in] REGCLS flags indicating how connections are made */
941 WINE_StringFromCLSID(rclsid,buf);
943 FIXME(ole,"(%s,%p,0x%08lx,0x%08lx,%p),stub\n",
944 buf,pUnk,dwClsContext,flags,lpdwRegister
950 * COM_GetRegisteredClassObject
952 * This internal method is used to scan the registered class list to
953 * find a class object.
956 * rclsid Class ID of the class to find.
957 * dwClsContext Class context to match.
958 * ppv [out] returns a pointer to the class object. Complying
959 * to normal COM usage, this method will increase the
960 * reference count on this object.
962 static HRESULT COM_GetRegisteredClassObject(
967 RegisteredClass* curClass;
975 * Iterate through the whole list and try to match the class ID.
977 curClass = firstRegisteredClass;
979 while (curClass != 0)
982 * Check if we have a match on the class ID.
984 if (IsEqualGUID32(&(curClass->classIdentifier), rclsid))
987 * Since we don't do out-of process or DCOM just right away, let's ignore the
992 * We have a match, return the pointer to the class object.
994 *ppUnk = curClass->classObject;
996 IUnknown_AddRef(curClass->classObject);
1002 * Step to the next class in the list.
1004 curClass = curClass->nextClass;
1008 * If we get to here, we haven't found our class.
1013 /******************************************************************************
1014 * CoRegisterClassObject32 [OLE32.36]
1016 * This method will register the class object for a given class ID.
1018 * See the Windows documentation for more details.
1020 HRESULT WINAPI CoRegisterClassObject32(
1023 DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
1024 DWORD flags, /* [in] REGCLS flags indicating how connections are made */
1025 LPDWORD lpdwRegister
1028 RegisteredClass* newClass;
1029 LPUNKNOWN foundObject;
1033 WINE_StringFromCLSID(rclsid,buf);
1035 TRACE(ole,"(%s,%p,0x%08lx,0x%08lx,%p)\n",
1036 buf,pUnk,dwClsContext,flags,lpdwRegister);
1039 * Perform a sanity check on the parameters
1041 if ( (lpdwRegister==0) || (pUnk==0) )
1043 return E_INVALIDARG;
1047 * Initialize the cookie (out parameter)
1052 * First, check if the class is already registered.
1053 * If it is, this should cause an error.
1055 hr = COM_GetRegisteredClassObject(rclsid, dwClsContext, &foundObject);
1060 * The COM_GetRegisteredClassObject increased the reference count on the
1061 * object so it has to be released.
1063 IUnknown_Release(foundObject);
1065 return CO_E_OBJISREG;
1069 * If it is not registered, we must create a new entry for this class and
1070 * append it to the registered class list.
1071 * We use the address of the chain node as the cookie since we are sure it's
1074 newClass = HeapAlloc(GetProcessHeap(), 0, sizeof(RegisteredClass));
1077 * Initialize the node.
1079 newClass->classIdentifier = *rclsid;
1080 newClass->runContext = dwClsContext;
1081 newClass->connectFlags = flags;
1082 newClass->dwCookie = (DWORD)newClass;
1083 newClass->nextClass = firstRegisteredClass;
1086 * Since we're making a copy of the object pointer, we have to increase it's
1089 newClass->classObject = pUnk;
1090 IUnknown_AddRef(newClass->classObject);
1092 firstRegisteredClass = newClass;
1095 * We're successfyl Yippee!
1100 /***********************************************************************
1101 * CoRevokeClassObject32 [OLE32.40]
1103 * This method will remove a class object from the class registry
1105 * See the Windows documentation for more details.
1107 HRESULT WINAPI CoRevokeClassObject32(
1110 RegisteredClass** prevClassLink;
1111 RegisteredClass* curClass;
1113 TRACE(ole,"(%08lx)\n",dwRegister);
1116 * Iterate through the whole list and try to match the cookie.
1118 curClass = firstRegisteredClass;
1119 prevClassLink = &firstRegisteredClass;
1121 while (curClass != 0)
1124 * Check if we have a match on the cookie.
1126 if (curClass->dwCookie == dwRegister)
1129 * Remove the class from the chain.
1131 *prevClassLink = curClass->nextClass;
1134 * Release the reference to the class object.
1136 IUnknown_Release(curClass->classObject);
1139 * Free the memory used by the chain node.
1141 HeapFree(GetProcessHeap(), 0, curClass);
1147 * Step to the next class in the list.
1149 prevClassLink = &(curClass->nextClass);
1150 curClass = curClass->nextClass;
1154 * If we get to here, we haven't found our class.
1156 return E_INVALIDARG;
1159 /***********************************************************************
1160 * CoGetClassObject [COMPOBJ.7]
1162 HRESULT WINAPI CoGetClassObject(REFCLSID rclsid, DWORD dwClsContext,
1163 LPVOID pvReserved, REFIID iid, LPVOID *ppv)
1165 LPUNKNOWN regClassObject;
1166 char xclsid[50],xiid[50];
1167 HRESULT hres = E_UNEXPECTED;
1169 char dllName[MAX_PATH+1];
1170 DWORD dllNameLen = sizeof(dllName);
1171 HINSTANCE32 hLibrary;
1172 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid,
1173 REFIID iid, LPVOID *ppv);
1174 DllGetClassObjectFunc DllGetClassObject;
1176 WINE_StringFromCLSID((LPCLSID)rclsid,xclsid);
1177 WINE_StringFromCLSID((LPCLSID)iid,xiid);
1178 TRACE(ole,"\n\tCLSID:\t%s,\n\tIID:\t%s\n",xclsid,xiid);
1181 * First, try and see if we can't match the class ID with one of the
1182 * registered classes.
1184 if (S_OK == COM_GetRegisteredClassObject(rclsid, dwClsContext, ®ClassObject))
1187 * Get the required interface from the retrieved pointer.
1189 hres = IUnknown_QueryInterface(regClassObject, iid, ppv);
1192 * Since QI got another reference on the pointer, we want to release the
1193 * one we already have. If QI was unsuccessful, this will release the object. This
1194 * is good since we are not returning it in the "out" parameter.
1196 IUnknown_Release(regClassObject);
1201 /* out of process and remote servers not supported yet */
1202 if ((CLSCTX_LOCAL_SERVER|CLSCTX_REMOTE_SERVER) & dwClsContext) {
1203 FIXME(ole, "CLSCTX_LOCAL_SERVER and CLSCTX_REMOTE_SERVER not supported!\n");
1204 return E_ACCESSDENIED;
1207 if ((CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER) & dwClsContext) {
1210 /* lookup CLSID in registry key HKCR/CLSID */
1211 hres = RegOpenKeyEx32A(HKEY_CLASSES_ROOT, "CLSID", 0,
1212 KEY_READ, &CLSIDkey);
1214 if (hres != ERROR_SUCCESS)
1215 return REGDB_E_READREGDB;
1216 hres = RegOpenKeyEx32A(CLSIDkey,xclsid,0,KEY_QUERY_VALUE,&key);
1217 if (hres != ERROR_SUCCESS) {
1218 RegCloseKey(CLSIDkey);
1219 return REGDB_E_CLASSNOTREG;
1221 hres = RegQueryValue32A(key, "InprocServer32", dllName, &dllNameLen);
1223 RegCloseKey(CLSIDkey);
1224 if (hres != ERROR_SUCCESS)
1225 return REGDB_E_READREGDB;
1226 TRACE(ole,"found InprocServer32 dll %s\n", dllName);
1228 /* open dll, call DllGetClassFactory */
1229 hLibrary = CoLoadLibrary(dllName, TRUE);
1230 if (hLibrary == 0) {
1231 TRACE(ole,"couldn't load InprocServer32 dll %s\n", dllName);
1232 return E_ACCESSDENIED; /* or should this be CO_E_DLLNOTFOUND? */
1234 DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress32(hLibrary, "DllGetClassObject");
1235 if (!DllGetClassObject) {
1236 /* not sure if this should be called here CoFreeLibrary(hLibrary);*/
1237 TRACE(ole,"couldn't find function DllGetClassObject in %s\n", dllName);
1238 return E_ACCESSDENIED;
1242 * Ask the DLL for it's class object. (there was a note here about class
1243 * factories but this is good.
1245 return DllGetClassObject(rclsid, iid, ppv);
1250 /******************************************************************************
1251 * CoRegisterMessageFilter16 [COMPOBJ.27]
1253 HRESULT WINAPI CoRegisterMessageFilter16(
1254 LPMESSAGEFILTER lpMessageFilter,
1255 LPMESSAGEFILTER *lplpMessageFilter
1257 FIXME(ole,"(%p,%p),stub!\n",lpMessageFilter,lplpMessageFilter);
1261 /***********************************************************************
1262 * CoCreateInstance [COMPOBJ.13, OLE32.7]
1264 HRESULT WINAPI CoCreateInstance(
1266 LPUNKNOWN pUnkOuter,
1272 LPCLASSFACTORY lpclf = 0;
1281 * Initialize the "out" parameter
1286 * Get a class factory to construct the object we want.
1288 hres = CoGetClassObject(rclsid,
1298 * Create the object and don't forget to release the factory
1300 hres = IClassFactory_CreateInstance(lpclf, pUnkOuter, iid, ppv);
1301 IClassFactory_Release(lpclf);
1308 /***********************************************************************
1309 * CoFreeLibrary [COMPOBJ.13]
1311 void WINAPI CoFreeLibrary(HINSTANCE32 hLibrary)
1313 OpenDll *ptr, *prev;
1316 /* lookup library in linked list */
1318 for (ptr = openDllList; ptr != NULL; ptr=ptr->next) {
1319 if (ptr->hLibrary == hLibrary) {
1326 /* shouldn't happen if user passed in a valid hLibrary */
1329 /* assert: ptr points to the library entry to free */
1331 /* free library and remove node from list */
1332 FreeLibrary32(hLibrary);
1333 if (ptr == openDllList) {
1334 tmp = openDllList->next;
1335 HeapFree(GetProcessHeap(), 0, openDllList->DllName);
1336 HeapFree(GetProcessHeap(), 0, openDllList);
1340 HeapFree(GetProcessHeap(), 0, ptr->DllName);
1341 HeapFree(GetProcessHeap(), 0, ptr);
1348 /***********************************************************************
1349 * CoFreeAllLibraries [COMPOBJ.12]
1351 void WINAPI CoFreeAllLibraries(void)
1355 for (ptr = openDllList; ptr != NULL; ) {
1357 CoFreeLibrary(ptr->hLibrary);
1364 /***********************************************************************
1365 * CoFreeUnusedLibraries [COMPOBJ.17]
1367 void WINAPI CoFreeUnusedLibraries(void)
1370 typedef HRESULT(*DllCanUnloadNowFunc)(void);
1371 DllCanUnloadNowFunc DllCanUnloadNow;
1373 for (ptr = openDllList; ptr != NULL; ) {
1374 DllCanUnloadNow = (DllCanUnloadNowFunc)
1375 GetProcAddress32(ptr->hLibrary, "DllCanUnloadNow");
1377 if (DllCanUnloadNow() == S_OK) {
1379 CoFreeLibrary(ptr->hLibrary);
1387 /***********************************************************************
1388 * CoFileTimeNow [COMPOBJ.82, OLE32.10]
1390 * the current system time in lpFileTime
1392 HRESULT WINAPI CoFileTimeNow(
1393 FILETIME *lpFileTime /* [out] the current time */
1395 DOSFS_UnixTimeToFileTime(time(NULL), lpFileTime, 0);
1399 /***********************************************************************
1400 * CoTaskMemAlloc (OLE32.43)
1402 * pointer to newly allocated block
1404 LPVOID WINAPI CoTaskMemAlloc(
1405 ULONG size /* [in] size of memoryblock to be allocated */
1407 LPMALLOC32 lpmalloc;
1408 HRESULT ret = CoGetMalloc32(0,&lpmalloc);
1412 return lpmalloc->lpvtbl->fnAlloc(lpmalloc,size);
1415 /***********************************************************************
1416 * CoTaskMemFree (OLE32.44)
1418 VOID WINAPI CoTaskMemFree(
1419 LPVOID ptr /* [in] pointer to be freed */
1421 LPMALLOC32 lpmalloc;
1422 HRESULT ret = CoGetMalloc32(0,&lpmalloc);
1425 lpmalloc->lpvtbl->fnFree(lpmalloc,ptr);
1428 /***********************************************************************
1429 * CoLoadLibrary (OLE32.30)
1431 HINSTANCE32 WINAPI CoLoadLibrary(LPOLESTR16 lpszLibName, BOOL32 bAutoFree)
1433 HINSTANCE32 hLibrary;
1437 TRACE(ole,"CoLoadLibrary(%p, %d\n", lpszLibName, bAutoFree);
1439 hLibrary = LoadLibrary32A(lpszLibName);
1444 if (openDllList == NULL) {
1445 /* empty list -- add first node */
1446 openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
1447 openDllList->DllName = HEAP_strdupA(GetProcessHeap(), 0, lpszLibName);
1448 openDllList->hLibrary = hLibrary;
1449 openDllList->next = NULL;
1451 /* search for this dll */
1453 for (ptr = openDllList; ptr->next != NULL; ptr=ptr->next) {
1454 if (ptr->hLibrary == hLibrary) {
1460 /* dll not found, add it */
1462 openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
1463 openDllList->DllName = HEAP_strdupA(GetProcessHeap(), 0, lpszLibName);
1464 openDllList->hLibrary = hLibrary;
1465 openDllList->next = tmp;
1472 /***********************************************************************
1473 * CoInitializeWOW (OLE32.27)
1475 HRESULT WINAPI CoInitializeWOW(DWORD x,DWORD y) {
1476 FIXME(ole,"(0x%08lx,0x%08lx),stub!\n",x,y);
1480 /******************************************************************************
1481 * CoLockObjectExternal16 [COMPOBJ.63]
1483 HRESULT WINAPI CoLockObjectExternal16(
1484 LPUNKNOWN pUnk, /* [in] object to be locked */
1485 BOOL16 fLock, /* [in] do lock */
1486 BOOL16 fLastUnlockReleases /* [in] ? */
1488 FIXME(ole,"(%p,%d,%d),stub!\n",pUnk,fLock,fLastUnlockReleases);
1492 /******************************************************************************
1493 * CoLockObjectExternal32 [OLE32.31]
1495 HRESULT WINAPI CoLockObjectExternal32(
1496 LPUNKNOWN pUnk, /* [in] object to be locked */
1497 BOOL32 fLock, /* [in] do lock */
1498 BOOL32 fLastUnlockReleases) /* [in] unlock all */
1504 * Increment the external lock coutner, COM_ExternalLockAddRef also
1505 * increment the object's internal lock counter.
1507 COM_ExternalLockAddRef( pUnk);
1512 * Decrement the external lock coutner, COM_ExternalLockRelease also
1513 * decrement the object's internal lock counter.
1515 COM_ExternalLockRelease( pUnk, fLastUnlockReleases);
1521 /***********************************************************************
1522 * CoGetState16 [COMPOBJ.115]
1524 HRESULT WINAPI CoGetState16(LPDWORD state)
1526 FIXME(ole, "(%p),stub!\n", state);
1530 /***********************************************************************
1531 * CoSetState32 [COM32.42]
1533 HRESULT WINAPI CoSetState32(LPDWORD state)
1535 FIXME(ole, "(%p),stub!\n", state);
1541 * COM_RevokeAllClasses
1543 * This method is called when the COM libraries are uninitialized to
1544 * release all the references to the class objects registered with
1547 static void COM_RevokeAllClasses()
1549 while (firstRegisteredClass!=0)
1551 CoRevokeClassObject32(firstRegisteredClass->dwCookie);
1555 /****************************************************************************
1556 * COM External Lock methods implementation
1559 /****************************************************************************
1560 * Public - Method that increments the count for a IUnknown* in the linked
1561 * list. The item is inserted if not already in the list.
1563 static void COM_ExternalLockAddRef(
1566 COM_ExternalLock *externalLock = COM_ExternalLockFind(pUnk);
1568 if ( externalLock == EL_NOT_FOUND )
1569 COM_ExternalLockInsert(pUnk);
1572 externalLock->uRefCount++; /* add an external lock */
1573 IUnknown_AddRef(pUnk); /* add a local lock as well */
1577 /****************************************************************************
1578 * Public - Method that decrements the count for a IUnknown* in the linked
1579 * list. The item is removed from the list if its count end up at zero or if
1582 static void COM_ExternalLockRelease(
1586 COM_ExternalLock *externalLock = COM_ExternalLockFind(pUnk);
1588 if ( externalLock != EL_NOT_FOUND )
1592 externalLock->uRefCount--; /* release external locks */
1593 IUnknown_Release(pUnk); /* release local locks as well */
1595 if ( bRelAll == FALSE )
1596 break; /* perform single release */
1598 } while ( externalLock->uRefCount > 0 );
1600 if ( externalLock->uRefCount == 0 ) /* get rid of the list entry */
1601 COM_ExternalLockDelete(externalLock);
1604 /****************************************************************************
1605 * Public - Method that frees the content of the list.
1607 static void COM_ExternalLockFreeList()
1609 COM_ExternalLock *head;
1611 head = elList.head; /* grab it by the head */
1612 while ( head != EL_END_OF_LIST )
1614 COM_ExternalLockDelete(head); /* get rid of the head stuff */
1616 head = elList.head; /* get the new head... */
1620 /****************************************************************************
1621 * Public - Method that dump the content of the list.
1623 void COM_ExternalLockDump()
1625 COM_ExternalLock *current = elList.head;
1627 printf("\nExternal lock list contains:\n");
1629 while ( current != EL_END_OF_LIST )
1631 printf( "\t%p with %lu references count.\n",
1633 current->uRefCount);
1635 /* Skip to the next item */
1636 current = current->next;
1641 /****************************************************************************
1642 * Internal - Find a IUnknown* in the linked list
1644 static COM_ExternalLock* COM_ExternalLockFind(
1647 return COM_ExternalLockLocate(elList.head, pUnk);
1650 /****************************************************************************
1651 * Internal - Recursivity agent for IUnknownExternalLockList_Find
1653 static COM_ExternalLock* COM_ExternalLockLocate(
1654 COM_ExternalLock *element,
1657 if ( element == EL_END_OF_LIST )
1658 return EL_NOT_FOUND;
1660 else if ( element->pUnk == pUnk ) /* We found it */
1663 else /* Not the right guy, keep on looking */
1664 return COM_ExternalLockLocate( element->next, pUnk);
1667 /****************************************************************************
1668 * Internal - Insert a new IUnknown* to the linked list
1670 static BOOL32 COM_ExternalLockInsert(
1673 COM_ExternalLock *newLock = NULL;
1674 COM_ExternalLock *previousHead = NULL;
1677 * Allocate space for the new storage object
1679 newLock = HeapAlloc(GetProcessHeap(), 0, sizeof(COM_ExternalLock));
1683 if ( elList.head == EL_END_OF_LIST )
1685 elList.head = newLock; /* The list is empty */
1690 * insert does it at the head
1692 previousHead = elList.head;
1693 elList.head = newLock;
1697 * Set new list item data member
1699 newLock->pUnk = pUnk;
1700 newLock->uRefCount = 1;
1701 newLock->next = previousHead;
1709 /****************************************************************************
1710 * Internal - Method that removes an item from the linked list.
1712 static void COM_ExternalLockDelete(
1713 COM_ExternalLock *itemList)
1715 COM_ExternalLock *current = elList.head;
1717 if ( current == itemList )
1720 * this section handles the deletion of the first node
1722 elList.head = itemList->next;
1723 HeapFree( GetProcessHeap(), 0, itemList);
1729 if ( current->next == itemList ) /* We found the item to free */
1731 current->next = itemList->next; /* readjust the list pointers */
1733 HeapFree( GetProcessHeap(), 0, itemList);
1737 /* Skip to the next item */
1738 current = current->next;
1740 } while ( current != EL_END_OF_LIST );