Implemented ExtractIconEx16, added some consts to ExtractIconEx*.
[wine/gsoc_dplay.git] / ole / compobj.c
bloba5349947d2efaf18dfcde5c3cfa19c8a89928a5f
1 /*
2 * COMPOBJ library
4 * Copyright 1995 Martin von Loewis
5 * Copyright 1998 Justin Bradford
6 */
8 #include "config.h"
9 #ifdef HAVE_UNISTD_H
10 # include <unistd.h>
11 #endif
12 #include <fcntl.h>
13 #include <sys/types.h>
14 #include <sys/time.h>
15 #include <sys/stat.h>
16 #ifdef HAVE_SYS_FILE_H
17 # include <sys/file.h>
18 #endif
19 #include <sys/ioctl.h>
20 #include <sys/socket.h>
21 #ifdef HAVE_SYS_SOCKIO_H
22 # include <sys/sockio.h>
23 #endif
24 #ifdef HAVE_NET_IF_H
25 # include <net/if.h>
26 #endif
27 #ifdef HAVE_NETINET_IN_H
28 # include <netinet/in.h>
29 #endif
30 #include <ctype.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <time.h>
34 #include "windows.h"
35 #include "winerror.h"
36 #include "ole2.h"
37 #include "debug.h"
38 #include "file.h"
39 #include "heap.h"
40 #include "ldt.h"
41 #include "winreg.h"
43 #define INITGUID
45 /* FIXME: we include all the header files containing GUIDs
46 * so that the corresponding variables get defined. But they
47 * don't even all belong to the same DLL !!!
49 #include "objbase.h"
50 #include "servprov.h"
51 #include "shlobj.h"
52 #include "ddraw.h"
53 #include "d3d.h"
54 #include "dinput.h"
55 #include "dsound.h"
56 #include "dplay.h"
58 #include "objbase.h"
60 LPMALLOC16 currentMalloc16=NULL;
61 LPMALLOC32 currentMalloc32=NULL;
63 HTASK16 hETask = 0;
64 WORD Table_ETask[62];
67 /* this open DLL table belongs in a per process table, but my guess is that
68 * it shouldn't live in the kernel, so I'll put them out here in DLL
69 * space assuming that there is one OLE32 per process.
71 typedef struct tagOpenDll {
72 char *DllName; /* really only needed for debugging */
73 HINSTANCE32 hLibrary;
74 struct tagOpenDll *next;
75 } OpenDll;
77 static OpenDll *openDllList = NULL; /* linked list of open dlls */
80 /******************************************************************************
81 * CoBuildVersion [COMPOBJ.1]
83 * RETURNS
84 * Current built version, hiword is majornumber, loword is minornumber
86 DWORD WINAPI CoBuildVersion(void)
88 TRACE(ole,"(void)\n");
89 return (rmm<<16)+rup;
92 /******************************************************************************
93 * CoInitialize16 [COMPOBJ.2]
94 * Set the win16 IMalloc used for memory management
96 HRESULT WINAPI CoInitialize16(
97 LPVOID lpReserved /* [in] pointer to win16 malloc interface */
98 ) {
99 currentMalloc16 = (LPMALLOC16)lpReserved;
100 return S_OK;
103 /******************************************************************************
104 * CoInitialize32 [OLE32.26]
106 * Set the win32 IMalloc used for memorymanagement
108 * RETURNS
109 * S_OK if successful, S_FALSE otherwise, RPC_E_CHANGED_MODE if a previous
110 * call to CoInitializeEx specified another threading model.
112 * BUGS
113 * Only the single threaded model is supported. As a result RPC_E_CHANGED_MODE
114 * is never returned.
116 HRESULT WINAPI CoInitialize32(
117 LPVOID lpReserved /* [in] pointer to win32 malloc interface */
119 /* FIXME: there really should be something here that incrememts a refcount
120 * but I'm supposing that it is a real COM object, so I won't bother
121 * creating one here. (Decrement done in CoUnitialize()) */
122 currentMalloc32 = (LPMALLOC32)lpReserved;
123 return S_OK;
126 /******************************************************************************
127 * CoInitializeEx32 [OLE32.163]
129 * Set the win32 IMalloc used for memory management
131 * RETURNS
132 * S_OK if successful, S_FALSE otherwise, RPC_E_CHANGED_MODE if a previous
133 * call to CoInitializeEx specified another threading model.
135 * BUGS
136 * Only the single threaded model is supported. As a result RPC_E_CHANGED_MODE
137 * is never returned.
139 HRESULT WINAPI CoInitializeEx32(
140 LPVOID lpReserved, /* [in] pointer to win32 malloc interface */
141 DWORD dwCoInit /* [in] A value from COINIT specifies the threading model */
143 if (dwCoInit!=COINIT_APARTMENTTHREADED) {
144 FIXME(ole, ":(%p,%x): unsupported flag %x\n", lpReserved, (int)dwCoInit, (int)dwCoInit);
145 /* Hope for the best and continue anyway */
147 return CoInitialize32(lpReserved);
150 /***********************************************************************
151 * CoUnitialize [COMPOBJ.3]
152 * Don't know what it does.
153 * 3-Nov-98 -- this was originally misspelled, I changed it to what I
154 * believe is the correct spelling
156 void WINAPI CoUninitialize(void)
158 TRACE(ole,"(void)\n");
159 CoFreeAllLibraries();
162 /***********************************************************************
163 * CoGetMalloc16 [COMPOBJ.4]
164 * RETURNS
165 * The current win16 IMalloc
167 HRESULT WINAPI CoGetMalloc16(
168 DWORD dwMemContext, /* [in] unknown */
169 LPMALLOC16 * lpMalloc /* [out] current win16 malloc interface */
171 if(!currentMalloc16)
172 currentMalloc16 = IMalloc16_Constructor();
173 *lpMalloc = currentMalloc16;
174 return S_OK;
177 /******************************************************************************
178 * CoGetMalloc32 [OLE32.20]
180 * RETURNS
181 * The current win32 IMalloc
183 HRESULT WINAPI CoGetMalloc32(
184 DWORD dwMemContext, /* [in] unknown */
185 LPMALLOC32 *lpMalloc /* [out] current win32 malloc interface */
187 if(!currentMalloc32)
188 currentMalloc32 = IMalloc32_Constructor();
189 *lpMalloc = currentMalloc32;
190 return S_OK;
193 /***********************************************************************
194 * CoCreateStandardMalloc16 [COMPOBJ.71]
196 HRESULT WINAPI CoCreateStandardMalloc16(DWORD dwMemContext,
197 LPMALLOC16 *lpMalloc)
199 /* FIXME: docu says we shouldn't return the same allocator as in
200 * CoGetMalloc16 */
201 *lpMalloc = IMalloc16_Constructor();
202 return S_OK;
205 /******************************************************************************
206 * CoDisconnectObject [COMPOBJ.15]
208 HRESULT WINAPI CoDisconnectObject( LPUNKNOWN lpUnk, DWORD reserved )
210 TRACE(ole,"%p %lx\n",lpUnk,reserved);
211 return S_OK;
214 /***********************************************************************
215 * IsEqualGUID16 [COMPOBJ.18]
217 * Compares two Unique Identifiers.
219 * RETURNS
220 * TRUE if equal
222 BOOL16 WINAPI IsEqualGUID16(
223 GUID* g1, /* [in] unique id 1 */
224 GUID* g2 /**/
226 return !memcmp( g1, g2, sizeof(GUID) );
229 /***********************************************************************
230 * IsEqualGUID32 [OLE32.76]
232 * Compares two Unique Identifiers.
234 * RETURNS
235 * TRUE if equal
237 BOOL32 WINAPI IsEqualGUID32(
238 REFGUID rguid1, /* [in] unique id 1 */
239 REFGUID rguid2 /* [in] unique id 2 */
242 return !memcmp(rguid1,rguid2,sizeof(GUID));
245 /******************************************************************************
246 * CLSIDFromString16 [COMPOBJ.20]
247 * Converts a unique identifier from it's string representation into
248 * the GUID struct.
250 * Class id: DWORD-WORD-WORD-BYTES[2]-BYTES[6]
252 * RETURNS
253 * the converted GUID
255 HRESULT WINAPI CLSIDFromString16(
256 LPCOLESTR16 idstr, /* [in] string representation of guid */
257 CLSID *id /* [out] GUID converted from string */
259 BYTE *s = (BYTE *) idstr;
260 BYTE *p;
261 int i;
262 BYTE table[256];
264 TRACE(ole,"%s -> %p\n", idstr, id);
266 /* quick lookup table */
267 memset(table, 0, 256);
269 for (i = 0; i < 10; i++) {
270 table['0' + i] = i;
272 for (i = 0; i < 6; i++) {
273 table['A' + i] = i+10;
274 table['a' + i] = i+10;
277 /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
279 if (strlen(idstr) != 38)
280 return OLE_ERROR_OBJECT;
282 p = (BYTE *) id;
284 s++; /* skip leading brace */
285 for (i = 0; i < 4; i++) {
286 p[3 - i] = table[*s]<<4 | table[*(s+1)];
287 s += 2;
289 p += 4;
290 s++; /* skip - */
292 for (i = 0; i < 2; i++) {
293 p[1-i] = table[*s]<<4 | table[*(s+1)];
294 s += 2;
296 p += 2;
297 s++; /* skip - */
299 for (i = 0; i < 2; i++) {
300 p[1-i] = table[*s]<<4 | table[*(s+1)];
301 s += 2;
303 p += 2;
304 s++; /* skip - */
306 /* these are just sequential bytes */
307 for (i = 0; i < 2; i++) {
308 *p++ = table[*s]<<4 | table[*(s+1)];
309 s += 2;
311 s++; /* skip - */
313 for (i = 0; i < 6; i++) {
314 *p++ = table[*s]<<4 | table[*(s+1)];
315 s += 2;
318 return S_OK;
321 /******************************************************************************
322 * CoCreateGuid[OLE32.6]
324 * Creates a 128bit GUID.
325 * Implemented according the DCE specification for UUID generation.
326 * Code is based upon uuid library in e2fsprogs by Theodore Ts'o.
327 * Copyright (C) 1996, 1997 Theodore Ts'o.
329 * RETURNS
331 * S_OK if successful.
333 HRESULT WINAPI CoCreateGuid(
334 GUID *pguid /* [out] points to the GUID to initialize */
336 static char has_init = 0;
337 unsigned char a[6];
338 static int adjustment = 0;
339 static struct timeval last = {0, 0};
340 static UINT16 clock_seq;
341 struct timeval tv;
342 unsigned long long clock_reg;
343 UINT32 clock_high, clock_low;
344 UINT16 temp_clock_seq, temp_clock_mid, temp_clock_hi_and_version;
345 #ifdef HAVE_NET_IF_H
346 int sd;
347 struct ifreq ifr, *ifrp;
348 struct ifconf ifc;
349 char buf[1024];
350 int n, i;
351 #endif
353 /* Have we already tried to get the MAC address? */
354 if (!has_init) {
355 #ifdef HAVE_NET_IF_H
356 /* BSD 4.4 defines the size of an ifreq to be
357 * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
358 * However, under earlier systems, sa_len isn't present, so
359 * the size is just sizeof(struct ifreq)
361 # ifdef HAVE_SA_LEN
362 # ifndef max
363 # define max(a,b) ((a) > (b) ? (a) : (b))
364 # endif
365 # define ifreq_size(i) max(sizeof(struct ifreq),\
366 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
367 # else
368 # define ifreq_size(i) sizeof(struct ifreq)
369 # endif /* HAVE_SA_LEN */
371 sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
372 if (sd < 0) {
373 /* if we can't open a socket, just use random numbers */
374 /* set the multicast bit to prevent conflicts with real cards */
375 a[0] = (rand() & 0xff) | 0x80;
376 a[1] = rand() & 0xff;
377 a[2] = rand() & 0xff;
378 a[3] = rand() & 0xff;
379 a[4] = rand() & 0xff;
380 a[5] = rand() & 0xff;
381 } else {
382 memset(buf, 0, sizeof(buf));
383 ifc.ifc_len = sizeof(buf);
384 ifc.ifc_buf = buf;
385 /* get the ifconf interface */
386 if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) {
387 close(sd);
388 /* no ifconf, so just use random numbers */
389 /* set the multicast bit to prevent conflicts with real cards */
390 a[0] = (rand() & 0xff) | 0x80;
391 a[1] = rand() & 0xff;
392 a[2] = rand() & 0xff;
393 a[3] = rand() & 0xff;
394 a[4] = rand() & 0xff;
395 a[5] = rand() & 0xff;
396 } else {
397 /* loop through the interfaces, looking for a valid one */
398 n = ifc.ifc_len;
399 for (i = 0; i < n; i+= ifreq_size(*ifr) ) {
400 ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i);
401 strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
402 /* try to get the address for this interface */
403 # ifdef SIOCGIFHWADDR
404 if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
405 continue;
406 memcpy(a, (unsigned char *)&ifr.ifr_hwaddr.sa_data, 6);
407 # else
408 # ifdef SIOCGENADDR
409 if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
410 continue;
411 memcpy(a, (unsigned char *) ifr.ifr_enaddr, 6);
412 # else
413 /* XXX we don't have a way of getting the hardware address */
414 close(sd);
415 a[0] = 0;
416 break;
417 # endif /* SIOCGENADDR */
418 # endif /* SIOCGIFHWADDR */
419 /* make sure it's not blank */
420 if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5])
421 continue;
423 goto valid_address;
425 /* if we didn't find a valid address, make a random one */
426 /* once again, set multicast bit to avoid conflicts */
427 a[0] = (rand() & 0xff) | 0x80;
428 a[1] = rand() & 0xff;
429 a[2] = rand() & 0xff;
430 a[3] = rand() & 0xff;
431 a[4] = rand() & 0xff;
432 a[5] = rand() & 0xff;
434 valid_address:
435 close(sd);
438 #else
439 /* no networking info, so generate a random address */
440 a[0] = (rand() & 0xff) | 0x80;
441 a[1] = rand() & 0xff;
442 a[2] = rand() & 0xff;
443 a[3] = rand() & 0xff;
444 a[4] = rand() & 0xff;
445 a[5] = rand() & 0xff;
446 #endif /* HAVE_NET_IF_H */
447 has_init = 1;
450 /* generate time element of GUID */
452 /* Assume that the gettimeofday() has microsecond granularity */
453 #define MAX_ADJUSTMENT 10
455 try_again:
456 gettimeofday(&tv, 0);
457 if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
458 clock_seq = ((rand() & 0xff) << 8) + (rand() & 0xff);
459 clock_seq &= 0x1FFF;
460 last = tv;
461 last.tv_sec--;
463 if ((tv.tv_sec < last.tv_sec) ||
464 ((tv.tv_sec == last.tv_sec) &&
465 (tv.tv_usec < last.tv_usec))) {
466 clock_seq = (clock_seq+1) & 0x1FFF;
467 adjustment = 0;
468 } else if ((tv.tv_sec == last.tv_sec) &&
469 (tv.tv_usec == last.tv_usec)) {
470 if (adjustment >= MAX_ADJUSTMENT)
471 goto try_again;
472 adjustment++;
473 } else
474 adjustment = 0;
476 clock_reg = tv.tv_usec*10 + adjustment;
477 clock_reg += ((unsigned long long) tv.tv_sec)*10000000;
478 clock_reg += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
480 clock_high = clock_reg >> 32;
481 clock_low = clock_reg;
482 temp_clock_seq = clock_seq | 0x8000;
483 temp_clock_mid = (UINT16)clock_high;
484 temp_clock_hi_and_version = (clock_high >> 16) | 0x1000;
486 /* pack the information into the GUID structure */
488 ((unsigned char*)&pguid->Data1)[3] = (unsigned char)clock_low;
489 clock_low >>= 8;
490 ((unsigned char*)&pguid->Data1)[2] = (unsigned char)clock_low;
491 clock_low >>= 8;
492 ((unsigned char*)&pguid->Data1)[1] = (unsigned char)clock_low;
493 clock_low >>= 8;
494 ((unsigned char*)&pguid->Data1)[0] = (unsigned char)clock_low;
496 ((unsigned char*)&pguid->Data2)[1] = (unsigned char)temp_clock_mid;
497 temp_clock_mid >>= 8;
498 ((unsigned char*)&pguid->Data2)[0] = (unsigned char)temp_clock_mid;
500 ((unsigned char*)&pguid->Data3)[1] = (unsigned char)temp_clock_hi_and_version;
501 temp_clock_hi_and_version >>= 8;
502 ((unsigned char*)&pguid->Data3)[0] = (unsigned char)temp_clock_hi_and_version;
504 ((unsigned char*)pguid->Data4)[1] = (unsigned char)temp_clock_seq;
505 temp_clock_seq >>= 8;
506 ((unsigned char*)pguid->Data4)[0] = (unsigned char)temp_clock_seq;
508 ((unsigned char*)pguid->Data4)[2] = a[0];
509 ((unsigned char*)pguid->Data4)[3] = a[1];
510 ((unsigned char*)pguid->Data4)[4] = a[2];
511 ((unsigned char*)pguid->Data4)[5] = a[3];
512 ((unsigned char*)pguid->Data4)[6] = a[4];
513 ((unsigned char*)pguid->Data4)[7] = a[5];
515 TRACE(ole, "%p", pguid);
517 return S_OK;
520 /******************************************************************************
521 * CLSIDFromString32 [OLE32.3]
522 * Converts a unique identifier from it's string representation into
523 * the GUID struct.
524 * RETURNS
525 * the converted GUID
527 HRESULT WINAPI CLSIDFromString32(
528 LPCOLESTR32 idstr, /* [in] string representation of GUID */
529 CLSID *id /* [out] GUID represented by above string */
531 LPOLESTR16 xid = HEAP_strdupWtoA(GetProcessHeap(),0,idstr);
532 OLESTATUS ret = CLSIDFromString16(xid,id);
534 HeapFree(GetProcessHeap(),0,xid);
535 return ret;
538 /******************************************************************************
539 * WINE_StringFromCLSID [???]
540 * Converts a GUID into the respective string representation.
542 * NOTES
544 * RETURNS
545 * the string representation and OLESTATUS
547 HRESULT WINE_StringFromCLSID(
548 const CLSID *id, /* [in] GUID to be converted */
549 LPSTR idstr /* [out] pointer to buffer to contain converted guid */
551 static const char *hex = "0123456789ABCDEF";
552 char *s;
553 int i;
555 if (!id)
556 { ERR(ole,"called with id=Null\n");
557 *idstr = 0x00;
558 return E_FAIL;
561 sprintf(idstr, "{%08lX-%04X-%04X-%02x%02X-",
562 id->Data1, id->Data2, id->Data3,
563 id->Data4[0], id->Data4[1]);
564 s = &idstr[25];
566 /* 6 hex bytes */
567 for (i = 2; i < 8; i++) {
568 *s++ = hex[id->Data4[i]>>4];
569 *s++ = hex[id->Data4[i] & 0xf];
572 *s++ = '}';
573 *s++ = '\0';
575 TRACE(ole,"%p->%s\n", id, idstr);
577 return OLE_OK;
580 /******************************************************************************
581 * StringFromCLSID16 [COMPOBJ.19]
582 * Converts a GUID into the respective string representation.
583 * The target string is allocated using the OLE IMalloc.
584 * RETURNS
585 * the string representation and OLESTATUS
587 HRESULT WINAPI StringFromCLSID16(
588 const CLSID *id, /* [in] the GUID to be converted */
589 LPOLESTR16 *idstr /* [out] a pointer to a to-be-allocated segmented pointer pointing to the resulting string */
592 LPMALLOC16 mllc;
593 OLESTATUS ret;
594 DWORD args[2];
596 ret = CoGetMalloc16(0,&mllc);
597 if (ret) return ret;
599 args[0] = (DWORD)mllc;
600 args[1] = 40;
602 /* No need for a Callback entry, we have WOWCallback16Ex which does
603 * everything we need.
605 if (!WOWCallback16Ex(
606 (FARPROC16)((ICOM_VTABLE(IMalloc16)*)PTR_SEG_TO_LIN(
607 ((LPMALLOC16)PTR_SEG_TO_LIN(mllc))->lpvtbl)
608 )->fnAlloc,
609 WCB16_CDECL,
611 (LPVOID)args,
612 (LPDWORD)idstr
613 )) {
614 WARN(ole,"CallTo16 IMalloc16 failed\n");
615 return E_FAIL;
617 return WINE_StringFromCLSID(id,PTR_SEG_TO_LIN(*idstr));
620 /******************************************************************************
621 * StringFromCLSID32 [OLE32.151]
622 * Converts a GUID into the respective string representation.
623 * The target string is allocated using the OLE IMalloc.
624 * RETURNS
625 * the string representation and OLESTATUS
627 HRESULT WINAPI StringFromCLSID32(
628 const CLSID *id, /* [in] the GUID to be converted */
629 LPOLESTR32 *idstr /* [out] a pointer to a to-be-allocated pointer pointing to the resulting string */
631 char buf[80];
632 OLESTATUS ret;
633 LPMALLOC32 mllc;
635 if ((ret=CoGetMalloc32(0,&mllc)))
636 return ret;
638 ret=WINE_StringFromCLSID(id,buf);
639 if (!ret) {
640 *idstr = mllc->lpvtbl->fnAlloc(mllc,strlen(buf)*2+2);
641 lstrcpyAtoW(*idstr,buf);
643 return ret;
646 /******************************************************************************
647 * StringFromGUID2 [COMPOBJ.76] [OLE32.152]
649 * Converts a global unique identifier into a string of an API-
650 * specified fixed format. (The usual {.....} stuff.)
652 * RETURNS
653 * The (UNICODE) string representation of the GUID in 'str'
654 * The length of the resulting string, 0 if there was any problem.
656 INT32 WINAPI
657 StringFromGUID2(REFGUID id, LPOLESTR32 str, INT32 cmax)
659 char xguid[80];
661 if (WINE_StringFromCLSID(id,xguid))
662 return 0;
663 if (strlen(xguid)>=cmax)
664 return 0;
665 lstrcpyAtoW(str,xguid);
666 return strlen(xguid);
669 /******************************************************************************
670 * CLSIDFromProgID16 [COMPOBJ.61]
671 * Converts a program id into the respective GUID. (By using a registry lookup)
672 * RETURNS
673 * riid associated with the progid
675 HRESULT WINAPI CLSIDFromProgID16(
676 LPCOLESTR16 progid, /* [in] program id as found in registry */
677 LPCLSID riid /* [out] associated CLSID */
679 char *buf,buf2[80];
680 DWORD buf2len;
681 HRESULT err;
682 HKEY xhkey;
684 buf = HeapAlloc(GetProcessHeap(),0,strlen(progid)+8);
685 sprintf(buf,"%s\\CLSID",progid);
686 if ((err=RegOpenKey32A(HKEY_CLASSES_ROOT,buf,&xhkey))) {
687 HeapFree(GetProcessHeap(),0,buf);
688 return OLE_ERROR_GENERIC;
690 HeapFree(GetProcessHeap(),0,buf);
691 buf2len = sizeof(buf2);
692 if ((err=RegQueryValue32A(xhkey,NULL,buf2,&buf2len))) {
693 RegCloseKey(xhkey);
694 return OLE_ERROR_GENERIC;
696 RegCloseKey(xhkey);
697 return CLSIDFromString16(buf2,riid);
700 /******************************************************************************
701 * CLSIDFromProgID32 [OLE32.2]
702 * Converts a program id into the respective GUID. (By using a registry lookup)
703 * RETURNS
704 * riid associated with the progid
706 HRESULT WINAPI CLSIDFromProgID32(
707 LPCOLESTR32 progid, /* [in] program id as found in registry */
708 LPCLSID riid /* [out] associated CLSID */
710 LPOLESTR16 pid = HEAP_strdupWtoA(GetProcessHeap(),0,progid);
711 OLESTATUS ret = CLSIDFromProgID16(pid,riid);
713 HeapFree(GetProcessHeap(),0,pid);
714 return ret;
717 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
718 /***********************************************************************
719 * LookupETask (COMPOBJ.94)
721 OLESTATUS WINAPI LookupETask(HTASK16 *hTask,LPVOID p) {
722 FIXME(ole,"(%p,%p),stub!\n",hTask,p);
723 if ((*hTask = GetCurrentTask()) == hETask) {
724 memcpy(p, Table_ETask, sizeof(Table_ETask));
726 return 0;
729 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
730 /***********************************************************************
731 * SetETask (COMPOBJ.95)
733 OLESTATUS WINAPI SetETask(HTASK16 hTask, LPVOID p) {
734 FIXME(ole,"(%04x,%p),stub!\n",hTask,p);
735 hETask = hTask;
736 return 0;
739 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
740 /***********************************************************************
741 * CallObjectInWOW (COMPOBJ.201)
743 OLESTATUS WINAPI CallObjectInWOW(LPVOID p1,LPVOID p2) {
744 FIXME(ole,"(%p,%p),stub!\n",p1,p2);
745 return 0;
748 /******************************************************************************
749 * CoRegisterClassObject16 [COMPOBJ.5]
751 * Don't know where it registers it ...
753 HRESULT WINAPI CoRegisterClassObject16(
754 REFCLSID rclsid,
755 LPUNKNOWN pUnk,
756 DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
757 DWORD flags, /* [in] REGCLS flags indicating how connections are made */
758 LPDWORD lpdwRegister
760 char buf[80];
762 WINE_StringFromCLSID(rclsid,buf);
764 FIXME(ole,"(%s,%p,0x%08lx,0x%08lx,%p),stub\n",
765 buf,pUnk,dwClsContext,flags,lpdwRegister
767 return 0;
770 /******************************************************************************
771 * CoRegisterClassObject32 [OLE32.36]
773 * Don't know where it registers it ...
775 HRESULT WINAPI CoRegisterClassObject32(
776 REFCLSID rclsid,
777 LPUNKNOWN pUnk,
778 DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
779 DWORD flags, /* [in] REGCLS flags indicating how connections are made */
780 LPDWORD lpdwRegister
782 char buf[80];
784 WINE_StringFromCLSID(rclsid,buf);
786 FIXME(ole,"(%s,%p,0x%08lx,0x%08lx,%p),stub\n",
787 buf,pUnk,dwClsContext,flags,lpdwRegister
789 return 0;
792 /***********************************************************************
793 * CoRevokeClassObject [OLE32.40]
795 HRESULT WINAPI CoRevokeClassObject(DWORD dwRegister) {
796 FIXME(ole,"(%08lx),stub!\n",dwRegister);
797 return S_OK;
800 /***********************************************************************
801 * CoGetClassObject [COMPOBJ.7]
803 HRESULT WINAPI CoGetClassObject(REFCLSID rclsid, DWORD dwClsContext,
804 LPVOID pvReserved, const REFIID iid, LPVOID *ppv)
806 char xclsid[50],xiid[50];
807 HRESULT hres = E_UNEXPECTED;
809 char dllName[MAX_PATH+1];
810 DWORD dllNameLen = sizeof(dllName);
811 HINSTANCE32 hLibrary;
812 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid,
813 REFIID iid, LPVOID *ppv);
814 DllGetClassObjectFunc DllGetClassObject;
816 WINE_StringFromCLSID((LPCLSID)rclsid,xclsid);
817 WINE_StringFromCLSID((LPCLSID)iid,xiid);
818 TRACE(ole,"\n\tCLSID:\t%s,\n\tIID:\t%s\n",xclsid,xiid);
820 /* out of process and remote servers not supported yet */
821 if ((CLSCTX_LOCAL_SERVER|CLSCTX_REMOTE_SERVER) & dwClsContext) {
822 FIXME(ole, "CLSCTX_LOCAL_SERVER and CLSCTX_REMOTE_SERVER not supported!\n");
823 return E_ACCESSDENIED;
826 if ((CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER) & dwClsContext) {
827 HKEY CLSIDkey,key;
829 /* lookup CLSID in registry key HKCR/CLSID */
830 hres = RegOpenKeyEx32A(HKEY_CLASSES_ROOT, "CLSID", 0,
831 KEY_READ, &CLSIDkey);
833 if (hres != ERROR_SUCCESS)
834 return REGDB_E_READREGDB;
835 hres = RegOpenKeyEx32A(CLSIDkey,xclsid,0,KEY_QUERY_VALUE,&key);
836 if (hres != ERROR_SUCCESS) {
837 RegCloseKey(CLSIDkey);
838 return REGDB_E_CLASSNOTREG;
840 hres = RegQueryValue32A(key, "InprocServer32", dllName, &dllNameLen);
841 RegCloseKey(key);
842 RegCloseKey(CLSIDkey);
843 if (hres != ERROR_SUCCESS)
844 return REGDB_E_READREGDB;
845 TRACE(ole,"found InprocServer32 dll %s\n", dllName);
847 /* open dll, call DllGetClassFactory */
848 hLibrary = CoLoadLibrary(dllName, TRUE);
849 if (hLibrary == 0) {
850 TRACE(ole,"couldn't load InprocServer32 dll %s\n", dllName);
851 return E_ACCESSDENIED; /* or should this be CO_E_DLLNOTFOUND? */
853 DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress32(hLibrary, "DllGetClassObject");
854 if (!DllGetClassObject) {
855 /* not sure if this should be called here CoFreeLibrary(hLibrary);*/
856 TRACE(ole,"couldn't find function DllGetClassObject in %s\n", dllName);
857 return E_ACCESSDENIED;
859 /* FIXME: Shouldn't we get a classfactory and use this to create
860 * the required object?
862 return DllGetClassObject(rclsid, iid, ppv);
864 return hres;
867 /******************************************************************************
868 * CoRegisterMessageFilter16 [COMPOBJ.27]
870 HRESULT WINAPI CoRegisterMessageFilter16(
871 LPMESSAGEFILTER lpMessageFilter,
872 LPMESSAGEFILTER *lplpMessageFilter
874 FIXME(ole,"(%p,%p),stub!\n",lpMessageFilter,lplpMessageFilter);
875 return 0;
878 /***********************************************************************
879 * CoCreateInstance [COMPOBJ.13, OLE32.7]
881 HRESULT WINAPI CoCreateInstance(
882 REFCLSID rclsid,
883 LPUNKNOWN pUnkOuter,
884 DWORD dwClsContext,
885 REFIID iid,
886 LPVOID *ppv
888 #if 0
889 char buf[80],xbuf[80];
891 if (rclsid)
892 WINE_StringFromCLSID(rclsid,buf);
893 else
894 sprintf(buf,"<rclsid-0x%08lx>",(DWORD)rclsid);
895 if (iid)
896 WINE_StringFromCLSID(iid,xbuf);
897 else
898 sprintf(xbuf,"<iid-0x%08lx>",(DWORD)iid);
900 FIXME(ole,"(%s,%p,0x%08lx,%s,%p): stub !\n",buf,pUnkOuter,dwClsContext,xbuf,ppv);
901 *ppv = NULL;
902 #else
903 HRESULT hres;
904 LPCLASSFACTORY lpclf = 0;
906 hres = CoGetClassObject(rclsid, dwClsContext, NULL, (const REFIID) &IID_IClassFactory, (LPVOID)&lpclf);
907 if (!SUCCEEDED(hres)) return hres;
908 hres = IClassFactory_CreateInstance(lpclf, pUnkOuter, iid, ppv);
909 IClassFactory_Release(lpclf);
910 return hres;
911 #endif
916 /***********************************************************************
917 * CoFreeLibrary [COMPOBJ.13]
919 void WINAPI CoFreeLibrary(HINSTANCE32 hLibrary)
921 OpenDll *ptr, *prev;
922 OpenDll *tmp;
924 /* lookup library in linked list */
925 prev = NULL;
926 for (ptr = openDllList; ptr != NULL; ptr=ptr->next) {
927 if (ptr->hLibrary == hLibrary) {
928 break;
930 prev = ptr;
933 if (ptr == NULL) {
934 /* shouldn't happen if user passed in a valid hLibrary */
935 return;
937 /* assert: ptr points to the library entry to free */
939 /* free library and remove node from list */
940 FreeLibrary32(hLibrary);
941 if (ptr == openDllList) {
942 tmp = openDllList->next;
943 HeapFree(GetProcessHeap(), 0, openDllList->DllName);
944 HeapFree(GetProcessHeap(), 0, openDllList);
945 openDllList = tmp;
946 } else {
947 tmp = ptr->next;
948 HeapFree(GetProcessHeap(), 0, ptr->DllName);
949 HeapFree(GetProcessHeap(), 0, ptr);
950 prev->next = tmp;
956 /***********************************************************************
957 * CoFreeAllLibraries [COMPOBJ.12]
959 void WINAPI CoFreeAllLibraries(void)
961 OpenDll *ptr, *tmp;
963 for (ptr = openDllList; ptr != NULL; ) {
964 tmp=ptr->next;
965 CoFreeLibrary(ptr->hLibrary);
966 ptr = tmp;
972 /***********************************************************************
973 * CoFreeUnusedLibraries [COMPOBJ.17]
975 void WINAPI CoFreeUnusedLibraries(void)
977 OpenDll *ptr, *tmp;
978 typedef HRESULT(*DllCanUnloadNowFunc)(void);
979 DllCanUnloadNowFunc DllCanUnloadNow;
981 for (ptr = openDllList; ptr != NULL; ) {
982 DllCanUnloadNow = (DllCanUnloadNowFunc)
983 GetProcAddress32(ptr->hLibrary, "DllCanUnloadNow");
985 if (DllCanUnloadNow() == S_OK) {
986 tmp=ptr->next;
987 CoFreeLibrary(ptr->hLibrary);
988 ptr = tmp;
989 } else {
990 ptr=ptr->next;
995 /***********************************************************************
996 * CoFileTimeNow [COMPOBJ.82, OLE32.10]
997 * RETURNS
998 * the current system time in lpFileTime
1000 HRESULT WINAPI CoFileTimeNow(
1001 FILETIME *lpFileTime /* [out] the current time */
1003 DOSFS_UnixTimeToFileTime(time(NULL), lpFileTime, 0);
1004 return S_OK;
1007 /***********************************************************************
1008 * CoTaskMemAlloc (OLE32.43)
1009 * RETURNS
1010 * pointer to newly allocated block
1012 LPVOID WINAPI CoTaskMemAlloc(
1013 ULONG size /* [in] size of memoryblock to be allocated */
1015 LPMALLOC32 lpmalloc;
1016 HRESULT ret = CoGetMalloc32(0,&lpmalloc);
1018 if (ret)
1019 return NULL;
1020 return lpmalloc->lpvtbl->fnAlloc(lpmalloc,size);
1023 /***********************************************************************
1024 * CoTaskMemFree (OLE32.44)
1026 VOID WINAPI CoTaskMemFree(
1027 LPVOID ptr /* [in] pointer to be freed */
1029 LPMALLOC32 lpmalloc;
1030 HRESULT ret = CoGetMalloc32(0,&lpmalloc);
1032 if (ret) return;
1033 lpmalloc->lpvtbl->fnFree(lpmalloc,ptr);
1036 /***********************************************************************
1037 * CoLoadLibrary (OLE32.30)
1039 HINSTANCE32 WINAPI CoLoadLibrary(LPOLESTR16 lpszLibName, BOOL32 bAutoFree)
1041 HINSTANCE32 hLibrary;
1042 OpenDll *ptr;
1043 OpenDll *tmp;
1045 TRACE(ole,"CoLoadLibrary(%p, %d\n", lpszLibName, bAutoFree);
1047 hLibrary = LoadLibrary32A(lpszLibName);
1049 if (!bAutoFree)
1050 return hLibrary;
1052 if (openDllList == NULL) {
1053 /* empty list -- add first node */
1054 openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
1055 openDllList->DllName = HEAP_strdupA(GetProcessHeap(), 0, lpszLibName);
1056 openDllList->hLibrary = hLibrary;
1057 openDllList->next = NULL;
1058 } else {
1059 /* search for this dll */
1060 int found = FALSE;
1061 for (ptr = openDllList; ptr->next != NULL; ptr=ptr->next) {
1062 if (ptr->hLibrary == hLibrary) {
1063 found = TRUE;
1064 break;
1067 if (!found) {
1068 /* dll not found, add it */
1069 tmp = openDllList;
1070 openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
1071 openDllList->DllName = HEAP_strdupA(GetProcessHeap(), 0, lpszLibName);
1072 openDllList->hLibrary = hLibrary;
1073 openDllList->next = tmp;
1077 return hLibrary;
1080 /***********************************************************************
1081 * CoInitializeWOW (OLE32.27)
1083 HRESULT WINAPI CoInitializeWOW(DWORD x,DWORD y) {
1084 FIXME(ole,"(0x%08lx,0x%08lx),stub!\n",x,y);
1085 return 0;
1088 /******************************************************************************
1089 * CoLockObjectExternal16 [COMPOBJ.63]
1091 HRESULT WINAPI CoLockObjectExternal16(
1092 LPUNKNOWN pUnk, /* [in] object to be locked */
1093 BOOL16 fLock, /* [in] do lock */
1094 BOOL16 fLastUnlockReleases /* [in] ? */
1096 FIXME(ole,"(%p,%d,%d),stub!\n",pUnk,fLock,fLastUnlockReleases);
1097 return S_OK;
1100 /******************************************************************************
1101 * CoLockObjectExternal32 [OLE32.31]
1103 HRESULT WINAPI CoLockObjectExternal32(
1104 LPUNKNOWN pUnk, /* [in] object to be locked */
1105 BOOL32 fLock, /* [in] do lock */
1106 BOOL32 fLastUnlockReleases /* [in] ? */
1108 FIXME(ole,"(%p,%d,%d),stub!\n",pUnk,fLock,fLastUnlockReleases);
1109 return S_OK;
1112 /***********************************************************************
1113 * CoGetState16 [COMPOBJ.115]
1115 HRESULT WINAPI CoGetState16(LPDWORD state)
1117 FIXME(ole, "(%p),stub!\n", state);
1118 *state = 0;
1119 return S_OK;
1121 /***********************************************************************
1122 * CoSetState32 [COM32.42]
1124 HRESULT WINAPI CoSetState32(LPDWORD state)
1126 FIXME(ole, "(%p),stub!\n", state);
1127 *state = 0;
1128 return S_OK;