Moved the instance creation of GUID's to a new file.
[wine.git] / ole / compobj.c
bloba4f1ef523d866c94a0817ad7a79847c6695708f9
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 <stdlib.h>
31 #include <string.h>
32 #include <time.h>
33 #include "windows.h"
34 #include "winerror.h"
35 #include "ole.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 #include "objbase.h"
45 LPMALLOC16 currentMalloc16=NULL;
46 LPMALLOC32 currentMalloc32=NULL;
48 HTASK16 hETask = 0;
49 WORD Table_ETask[62];
52 /* this open DLL table belongs in a per process table, but my guess is that
53 * it shouldn't live in the kernel, so I'll put them out here in DLL
54 * space assuming that there is one OLE32 per process.
56 typedef struct tagOpenDll {
57 char *DllName; /* really only needed for debugging */
58 HINSTANCE32 hLibrary;
59 struct tagOpenDll *next;
60 } OpenDll;
62 static OpenDll *openDllList = NULL; /* linked list of open dlls */
65 /******************************************************************************
66 * CoBuildVersion [COMPOBJ.1]
68 * RETURNS
69 * Current built version, hiword is majornumber, loword is minornumber
71 DWORD WINAPI CoBuildVersion(void)
73 TRACE(ole,"(void)\n");
74 return (rmm<<16)+rup;
77 /******************************************************************************
78 * CoInitialize16 [COMPOBJ.2]
79 * Set the win16 IMalloc used for memory management
81 HRESULT WINAPI CoInitialize16(
82 LPVOID lpReserved /* [in] pointer to win16 malloc interface */
83 ) {
84 currentMalloc16 = (LPMALLOC16)lpReserved;
85 return S_OK;
88 /******************************************************************************
89 * CoInitialize32 [OLE32.26]
91 * Set the win32 IMalloc used for memorymanagement
93 * RETURNS
94 * S_OK if successful, S_FALSE otherwise, RPC_E_CHANGED_MODE if a previous
95 * call to CoInitializeEx specified another threading model.
97 * BUGS
98 * Only the single threaded model is supported. As a result RPC_E_CHANGED_MODE
99 * is never returned.
101 HRESULT WINAPI CoInitialize32(
102 LPVOID lpReserved /* [in] pointer to win32 malloc interface */
104 /* FIXME: there really should be something here that incrememts a refcount
105 * but I'm supposing that it is a real COM object, so I won't bother
106 * creating one here. (Decrement done in CoUnitialize()) */
107 currentMalloc32 = (LPMALLOC32)lpReserved;
108 return S_OK;
111 /******************************************************************************
112 * CoInitializeEx32 [OLE32.163]
114 * Set the win32 IMalloc used for memory management
116 * RETURNS
117 * S_OK if successful, S_FALSE otherwise, RPC_E_CHANGED_MODE if a previous
118 * call to CoInitializeEx specified another threading model.
120 * BUGS
121 * Only the single threaded model is supported. As a result RPC_E_CHANGED_MODE
122 * is never returned.
124 HRESULT WINAPI CoInitializeEx32(
125 LPVOID lpReserved, /* [in] pointer to win32 malloc interface */
126 DWORD dwCoInit /* [in] A value from COINIT specifies the threading model */
128 if (dwCoInit!=COINIT_APARTMENTTHREADED) {
129 FIXME(ole, ":(%p,%x): unsupported flag %x\n", lpReserved, (int)dwCoInit, (int)dwCoInit);
130 /* Hope for the best and continue anyway */
132 return CoInitialize32(lpReserved);
135 /***********************************************************************
136 * CoUnitialize [COMPOBJ.3]
137 * Don't know what it does.
138 * 3-Nov-98 -- this was originally misspelled, I changed it to what I
139 * believe is the correct spelling
141 void WINAPI CoUninitialize(void)
143 TRACE(ole,"(void)\n");
144 CoFreeAllLibraries();
147 /***********************************************************************
148 * CoGetMalloc16 [COMPOBJ.4]
149 * RETURNS
150 * The current win16 IMalloc
152 HRESULT WINAPI CoGetMalloc16(
153 DWORD dwMemContext, /* [in] unknown */
154 LPMALLOC16 * lpMalloc /* [out] current win16 malloc interface */
156 if(!currentMalloc16)
157 currentMalloc16 = IMalloc16_Constructor();
158 *lpMalloc = currentMalloc16;
159 return S_OK;
162 /******************************************************************************
163 * CoGetMalloc32 [OLE32.20]
165 * RETURNS
166 * The current win32 IMalloc
168 HRESULT WINAPI CoGetMalloc32(
169 DWORD dwMemContext, /* [in] unknown */
170 LPMALLOC32 *lpMalloc /* [out] current win32 malloc interface */
172 if(!currentMalloc32)
173 currentMalloc32 = IMalloc32_Constructor();
174 *lpMalloc = currentMalloc32;
175 return S_OK;
178 /***********************************************************************
179 * CoCreateStandardMalloc16 [COMPOBJ.71]
181 HRESULT WINAPI CoCreateStandardMalloc16(DWORD dwMemContext,
182 LPMALLOC16 *lpMalloc)
184 /* FIXME: docu says we shouldn't return the same allocator as in
185 * CoGetMalloc16 */
186 *lpMalloc = IMalloc16_Constructor();
187 return S_OK;
190 /******************************************************************************
191 * CoDisconnectObject [COMPOBJ.15]
193 HRESULT WINAPI CoDisconnectObject( LPUNKNOWN lpUnk, DWORD reserved )
195 TRACE(ole,"%p %lx\n",lpUnk,reserved);
196 return S_OK;
199 /***********************************************************************
200 * IsEqualGUID16 [COMPOBJ.18]
202 * Compares two Unique Identifiers.
204 * RETURNS
205 * TRUE if equal
207 BOOL16 WINAPI IsEqualGUID16(
208 GUID* g1, /* [in] unique id 1 */
209 GUID* g2 /**/
211 return !memcmp( g1, g2, sizeof(GUID) );
214 /***********************************************************************
215 * IsEqualGUID32 [OLE32.76]
217 * Compares two Unique Identifiers.
219 * RETURNS
220 * TRUE if equal
222 BOOL32 WINAPI IsEqualGUID32(
223 REFGUID rguid1, /* [in] unique id 1 */
224 REFGUID rguid2 /* [in] unique id 2 */
227 return !memcmp(rguid1,rguid2,sizeof(GUID));
230 /******************************************************************************
231 * CLSIDFromString16 [COMPOBJ.20]
232 * Converts a unique identifier from it's string representation into
233 * the GUID struct.
235 * Class id: DWORD-WORD-WORD-BYTES[2]-BYTES[6]
237 * RETURNS
238 * the converted GUID
240 HRESULT WINAPI CLSIDFromString16(
241 LPCOLESTR16 idstr, /* [in] string representation of guid */
242 CLSID *id /* [out] GUID converted from string */
244 BYTE *s = (BYTE *) idstr;
245 BYTE *p;
246 int i;
247 BYTE table[256];
249 TRACE(ole,"%s -> %p\n", idstr, id);
251 /* quick lookup table */
252 memset(table, 0, 256);
254 for (i = 0; i < 10; i++) {
255 table['0' + i] = i;
257 for (i = 0; i < 6; i++) {
258 table['A' + i] = i+10;
259 table['a' + i] = i+10;
262 /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
264 if (strlen(idstr) != 38)
265 return OLE_ERROR_OBJECT;
267 p = (BYTE *) id;
269 s++; /* skip leading brace */
270 for (i = 0; i < 4; i++) {
271 p[3 - i] = table[*s]<<4 | table[*(s+1)];
272 s += 2;
274 p += 4;
275 s++; /* skip - */
277 for (i = 0; i < 2; i++) {
278 p[1-i] = table[*s]<<4 | table[*(s+1)];
279 s += 2;
281 p += 2;
282 s++; /* skip - */
284 for (i = 0; i < 2; i++) {
285 p[1-i] = table[*s]<<4 | table[*(s+1)];
286 s += 2;
288 p += 2;
289 s++; /* skip - */
291 /* these are just sequential bytes */
292 for (i = 0; i < 2; i++) {
293 *p++ = table[*s]<<4 | table[*(s+1)];
294 s += 2;
296 s++; /* skip - */
298 for (i = 0; i < 6; i++) {
299 *p++ = table[*s]<<4 | table[*(s+1)];
300 s += 2;
303 return S_OK;
306 /******************************************************************************
307 * CoCreateGuid[OLE32.6]
309 * Creates a 128bit GUID.
310 * Implemented according the DCE specification for UUID generation.
311 * Code is based upon uuid library in e2fsprogs by Theodore Ts'o.
312 * Copyright (C) 1996, 1997 Theodore Ts'o.
314 * RETURNS
316 * S_OK if successful.
318 HRESULT WINAPI CoCreateGuid(
319 GUID *pguid /* [out] points to the GUID to initialize */
321 static char has_init = 0;
322 unsigned char a[6];
323 static int adjustment = 0;
324 static struct timeval last = {0, 0};
325 static UINT16 clock_seq;
326 struct timeval tv;
327 unsigned long long clock_reg;
328 UINT32 clock_high, clock_low;
329 UINT16 temp_clock_seq, temp_clock_mid, temp_clock_hi_and_version;
330 #ifdef HAVE_NET_IF_H
331 int sd;
332 struct ifreq ifr, *ifrp;
333 struct ifconf ifc;
334 char buf[1024];
335 int n, i;
336 #endif
338 /* Have we already tried to get the MAC address? */
339 if (!has_init) {
340 #ifdef HAVE_NET_IF_H
341 /* BSD 4.4 defines the size of an ifreq to be
342 * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
343 * However, under earlier systems, sa_len isn't present, so
344 * the size is just sizeof(struct ifreq)
346 # ifdef HAVE_SA_LEN
347 # ifndef max
348 # define max(a,b) ((a) > (b) ? (a) : (b))
349 # endif
350 # define ifreq_size(i) max(sizeof(struct ifreq),\
351 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
352 # else
353 # define ifreq_size(i) sizeof(struct ifreq)
354 # endif /* HAVE_SA_LEN */
356 sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
357 if (sd < 0) {
358 /* if we can't open a socket, just use random numbers */
359 /* set the multicast bit to prevent conflicts with real cards */
360 a[0] = (rand() & 0xff) | 0x80;
361 a[1] = rand() & 0xff;
362 a[2] = rand() & 0xff;
363 a[3] = rand() & 0xff;
364 a[4] = rand() & 0xff;
365 a[5] = rand() & 0xff;
366 } else {
367 memset(buf, 0, sizeof(buf));
368 ifc.ifc_len = sizeof(buf);
369 ifc.ifc_buf = buf;
370 /* get the ifconf interface */
371 if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) {
372 close(sd);
373 /* no ifconf, so 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 /* loop through the interfaces, looking for a valid one */
383 n = ifc.ifc_len;
384 for (i = 0; i < n; i+= ifreq_size(*ifr) ) {
385 ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i);
386 strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
387 /* try to get the address for this interface */
388 # ifdef SIOCGIFHWADDR
389 if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
390 continue;
391 memcpy(a, (unsigned char *)&ifr.ifr_hwaddr.sa_data, 6);
392 # else
393 # ifdef SIOCGENADDR
394 if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
395 continue;
396 memcpy(a, (unsigned char *) ifr.ifr_enaddr, 6);
397 # else
398 /* XXX we don't have a way of getting the hardware address */
399 close(sd);
400 a[0] = 0;
401 break;
402 # endif /* SIOCGENADDR */
403 # endif /* SIOCGIFHWADDR */
404 /* make sure it's not blank */
405 if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5])
406 continue;
408 goto valid_address;
410 /* if we didn't find a valid address, make a random one */
411 /* once again, set multicast bit to avoid conflicts */
412 a[0] = (rand() & 0xff) | 0x80;
413 a[1] = rand() & 0xff;
414 a[2] = rand() & 0xff;
415 a[3] = rand() & 0xff;
416 a[4] = rand() & 0xff;
417 a[5] = rand() & 0xff;
419 valid_address:
420 close(sd);
423 #else
424 /* no networking info, so generate a random address */
425 a[0] = (rand() & 0xff) | 0x80;
426 a[1] = rand() & 0xff;
427 a[2] = rand() & 0xff;
428 a[3] = rand() & 0xff;
429 a[4] = rand() & 0xff;
430 a[5] = rand() & 0xff;
431 #endif /* HAVE_NET_IF_H */
432 has_init = 1;
435 /* generate time element of GUID */
437 /* Assume that the gettimeofday() has microsecond granularity */
438 #define MAX_ADJUSTMENT 10
440 try_again:
441 gettimeofday(&tv, 0);
442 if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
443 clock_seq = ((rand() & 0xff) << 8) + (rand() & 0xff);
444 clock_seq &= 0x1FFF;
445 last = tv;
446 last.tv_sec--;
448 if ((tv.tv_sec < last.tv_sec) ||
449 ((tv.tv_sec == last.tv_sec) &&
450 (tv.tv_usec < last.tv_usec))) {
451 clock_seq = (clock_seq+1) & 0x1FFF;
452 adjustment = 0;
453 } else if ((tv.tv_sec == last.tv_sec) &&
454 (tv.tv_usec == last.tv_usec)) {
455 if (adjustment >= MAX_ADJUSTMENT)
456 goto try_again;
457 adjustment++;
458 } else
459 adjustment = 0;
461 clock_reg = tv.tv_usec*10 + adjustment;
462 clock_reg += ((unsigned long long) tv.tv_sec)*10000000;
463 clock_reg += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
465 clock_high = clock_reg >> 32;
466 clock_low = clock_reg;
467 temp_clock_seq = clock_seq | 0x8000;
468 temp_clock_mid = (UINT16)clock_high;
469 temp_clock_hi_and_version = (clock_high >> 16) | 0x1000;
471 /* pack the information into the GUID structure */
473 ((unsigned char*)&pguid->Data1)[3] = (unsigned char)clock_low;
474 clock_low >>= 8;
475 ((unsigned char*)&pguid->Data1)[2] = (unsigned char)clock_low;
476 clock_low >>= 8;
477 ((unsigned char*)&pguid->Data1)[1] = (unsigned char)clock_low;
478 clock_low >>= 8;
479 ((unsigned char*)&pguid->Data1)[0] = (unsigned char)clock_low;
481 ((unsigned char*)&pguid->Data2)[1] = (unsigned char)temp_clock_mid;
482 temp_clock_mid >>= 8;
483 ((unsigned char*)&pguid->Data2)[0] = (unsigned char)temp_clock_mid;
485 ((unsigned char*)&pguid->Data3)[1] = (unsigned char)temp_clock_hi_and_version;
486 temp_clock_hi_and_version >>= 8;
487 ((unsigned char*)&pguid->Data3)[0] = (unsigned char)temp_clock_hi_and_version;
489 ((unsigned char*)pguid->Data4)[1] = (unsigned char)temp_clock_seq;
490 temp_clock_seq >>= 8;
491 ((unsigned char*)pguid->Data4)[0] = (unsigned char)temp_clock_seq;
493 ((unsigned char*)pguid->Data4)[2] = a[0];
494 ((unsigned char*)pguid->Data4)[3] = a[1];
495 ((unsigned char*)pguid->Data4)[4] = a[2];
496 ((unsigned char*)pguid->Data4)[5] = a[3];
497 ((unsigned char*)pguid->Data4)[6] = a[4];
498 ((unsigned char*)pguid->Data4)[7] = a[5];
500 TRACE(ole, "%p", pguid);
502 return S_OK;
505 /******************************************************************************
506 * CLSIDFromString32 [OLE32.3]
507 * Converts a unique identifier from it's string representation into
508 * the GUID struct.
509 * RETURNS
510 * the converted GUID
512 HRESULT WINAPI CLSIDFromString32(
513 LPCOLESTR32 idstr, /* [in] string representation of GUID */
514 CLSID *id /* [out] GUID represented by above string */
516 LPOLESTR16 xid = HEAP_strdupWtoA(GetProcessHeap(),0,idstr);
517 OLESTATUS ret = CLSIDFromString16(xid,id);
519 HeapFree(GetProcessHeap(),0,xid);
520 return ret;
523 /******************************************************************************
524 * WINE_StringFromCLSID [???]
525 * Converts a GUID into the respective string representation.
527 * NOTES
529 * RETURNS
530 * the string representation and OLESTATUS
532 HRESULT WINE_StringFromCLSID(
533 const CLSID *id, /* [in] GUID to be converted */
534 LPSTR idstr /* [out] pointer to buffer to contain converted guid */
536 static const char *hex = "0123456789ABCDEF";
537 char *s;
538 int i;
540 if (!id)
541 { ERR(ole,"called with id=Null\n");
542 *idstr = 0x00;
543 return E_FAIL;
546 sprintf(idstr, "{%08lX-%04X-%04X-%02x%02X-",
547 id->Data1, id->Data2, id->Data3,
548 id->Data4[0], id->Data4[1]);
549 s = &idstr[25];
551 /* 6 hex bytes */
552 for (i = 2; i < 8; i++) {
553 *s++ = hex[id->Data4[i]>>4];
554 *s++ = hex[id->Data4[i] & 0xf];
557 *s++ = '}';
558 *s++ = '\0';
560 TRACE(ole,"%p->%s\n", id, idstr);
562 return OLE_OK;
565 /******************************************************************************
566 * StringFromCLSID16 [COMPOBJ.19]
567 * Converts a GUID into the respective string representation.
568 * The target string is allocated using the OLE IMalloc.
569 * RETURNS
570 * the string representation and OLESTATUS
572 HRESULT WINAPI StringFromCLSID16(
573 const CLSID *id, /* [in] the GUID to be converted */
574 LPOLESTR16 *idstr /* [out] a pointer to a to-be-allocated segmented pointer pointing to the resulting string */
577 LPMALLOC16 mllc;
578 OLESTATUS ret;
579 DWORD args[2];
581 ret = CoGetMalloc16(0,&mllc);
582 if (ret) return ret;
584 args[0] = (DWORD)mllc;
585 args[1] = 40;
587 /* No need for a Callback entry, we have WOWCallback16Ex which does
588 * everything we need.
590 if (!WOWCallback16Ex(
591 (FARPROC16)((ICOM_VTABLE(IMalloc16)*)PTR_SEG_TO_LIN(
592 ((LPMALLOC16)PTR_SEG_TO_LIN(mllc))->lpvtbl)
593 )->fnAlloc,
594 WCB16_CDECL,
596 (LPVOID)args,
597 (LPDWORD)idstr
598 )) {
599 WARN(ole,"CallTo16 IMalloc16 failed\n");
600 return E_FAIL;
602 return WINE_StringFromCLSID(id,PTR_SEG_TO_LIN(*idstr));
605 /******************************************************************************
606 * StringFromCLSID32 [OLE32.151]
607 * Converts a GUID into the respective string representation.
608 * The target string is allocated using the OLE IMalloc.
609 * RETURNS
610 * the string representation and OLESTATUS
612 HRESULT WINAPI StringFromCLSID32(
613 const CLSID *id, /* [in] the GUID to be converted */
614 LPOLESTR32 *idstr /* [out] a pointer to a to-be-allocated pointer pointing to the resulting string */
616 char buf[80];
617 OLESTATUS ret;
618 LPMALLOC32 mllc;
620 if ((ret=CoGetMalloc32(0,&mllc)))
621 return ret;
623 ret=WINE_StringFromCLSID(id,buf);
624 if (!ret) {
625 *idstr = mllc->lpvtbl->fnAlloc(mllc,strlen(buf)*2+2);
626 lstrcpyAtoW(*idstr,buf);
628 return ret;
631 /******************************************************************************
632 * StringFromGUID2 [COMPOBJ.76] [OLE32.152]
634 * Converts a global unique identifier into a string of an API-
635 * specified fixed format. (The usual {.....} stuff.)
637 * RETURNS
638 * The (UNICODE) string representation of the GUID in 'str'
639 * The length of the resulting string, 0 if there was any problem.
641 INT32 WINAPI
642 StringFromGUID2(REFGUID id, LPOLESTR32 str, INT32 cmax)
644 char xguid[80];
646 if (WINE_StringFromCLSID(id,xguid))
647 return 0;
648 if (strlen(xguid)>=cmax)
649 return 0;
650 lstrcpyAtoW(str,xguid);
651 return strlen(xguid);
654 /******************************************************************************
655 * CLSIDFromProgID16 [COMPOBJ.61]
656 * Converts a program id into the respective GUID. (By using a registry lookup)
657 * RETURNS
658 * riid associated with the progid
660 HRESULT WINAPI CLSIDFromProgID16(
661 LPCOLESTR16 progid, /* [in] program id as found in registry */
662 LPCLSID riid /* [out] associated CLSID */
664 char *buf,buf2[80];
665 DWORD buf2len;
666 HRESULT err;
667 HKEY xhkey;
669 buf = HeapAlloc(GetProcessHeap(),0,strlen(progid)+8);
670 sprintf(buf,"%s\\CLSID",progid);
671 if ((err=RegOpenKey32A(HKEY_CLASSES_ROOT,buf,&xhkey))) {
672 HeapFree(GetProcessHeap(),0,buf);
673 return OLE_ERROR_GENERIC;
675 HeapFree(GetProcessHeap(),0,buf);
676 buf2len = sizeof(buf2);
677 if ((err=RegQueryValue32A(xhkey,NULL,buf2,&buf2len))) {
678 RegCloseKey(xhkey);
679 return OLE_ERROR_GENERIC;
681 RegCloseKey(xhkey);
682 return CLSIDFromString16(buf2,riid);
685 /******************************************************************************
686 * CLSIDFromProgID32 [OLE32.2]
687 * Converts a program id into the respective GUID. (By using a registry lookup)
688 * RETURNS
689 * riid associated with the progid
691 HRESULT WINAPI CLSIDFromProgID32(
692 LPCOLESTR32 progid, /* [in] program id as found in registry */
693 LPCLSID riid /* [out] associated CLSID */
695 LPOLESTR16 pid = HEAP_strdupWtoA(GetProcessHeap(),0,progid);
696 OLESTATUS ret = CLSIDFromProgID16(pid,riid);
698 HeapFree(GetProcessHeap(),0,pid);
699 return ret;
702 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
703 /***********************************************************************
704 * LookupETask (COMPOBJ.94)
706 OLESTATUS WINAPI LookupETask(HTASK16 *hTask,LPVOID p) {
707 FIXME(ole,"(%p,%p),stub!\n",hTask,p);
708 if ((*hTask = GetCurrentTask()) == hETask) {
709 memcpy(p, Table_ETask, sizeof(Table_ETask));
711 return 0;
714 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
715 /***********************************************************************
716 * SetETask (COMPOBJ.95)
718 OLESTATUS WINAPI SetETask(HTASK16 hTask, LPVOID p) {
719 FIXME(ole,"(%04x,%p),stub!\n",hTask,p);
720 hETask = hTask;
721 return 0;
724 /* FIXME: this function is not declared in the WINELIB headers. But where should it go ? */
725 /***********************************************************************
726 * CallObjectInWOW (COMPOBJ.201)
728 OLESTATUS WINAPI CallObjectInWOW(LPVOID p1,LPVOID p2) {
729 FIXME(ole,"(%p,%p),stub!\n",p1,p2);
730 return 0;
733 /******************************************************************************
734 * CoRegisterClassObject16 [COMPOBJ.5]
736 * Don't know where it registers it ...
738 HRESULT WINAPI CoRegisterClassObject16(
739 REFCLSID rclsid,
740 LPUNKNOWN pUnk,
741 DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
742 DWORD flags, /* [in] REGCLS flags indicating how connections are made */
743 LPDWORD lpdwRegister
745 char buf[80];
747 WINE_StringFromCLSID(rclsid,buf);
749 FIXME(ole,"(%s,%p,0x%08lx,0x%08lx,%p),stub\n",
750 buf,pUnk,dwClsContext,flags,lpdwRegister
752 return 0;
755 /******************************************************************************
756 * CoRegisterClassObject32 [OLE32.36]
758 * Don't know where it registers it ...
760 HRESULT WINAPI CoRegisterClassObject32(
761 REFCLSID rclsid,
762 LPUNKNOWN pUnk,
763 DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
764 DWORD flags, /* [in] REGCLS flags indicating how connections are made */
765 LPDWORD lpdwRegister
767 char buf[80];
769 WINE_StringFromCLSID(rclsid,buf);
771 FIXME(ole,"(%s,%p,0x%08lx,0x%08lx,%p),stub\n",
772 buf,pUnk,dwClsContext,flags,lpdwRegister
774 return 0;
777 /***********************************************************************
778 * CoRevokeClassObject [OLE32.40]
780 HRESULT WINAPI CoRevokeClassObject(DWORD dwRegister) {
781 FIXME(ole,"(%08lx),stub!\n",dwRegister);
782 return S_OK;
785 /***********************************************************************
786 * CoGetClassObject [COMPOBJ.7]
788 HRESULT WINAPI CoGetClassObject(REFCLSID rclsid, DWORD dwClsContext,
789 LPVOID pvReserved, const REFIID iid, LPVOID *ppv)
791 char xclsid[50],xiid[50];
792 HRESULT hres = E_UNEXPECTED;
794 char dllName[MAX_PATH+1];
795 DWORD dllNameLen = sizeof(dllName);
796 HINSTANCE32 hLibrary;
797 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid,
798 REFIID iid, LPVOID *ppv);
799 DllGetClassObjectFunc DllGetClassObject;
801 WINE_StringFromCLSID((LPCLSID)rclsid,xclsid);
802 WINE_StringFromCLSID((LPCLSID)iid,xiid);
803 TRACE(ole,"\n\tCLSID:\t%s,\n\tIID:\t%s\n",xclsid,xiid);
805 /* out of process and remote servers not supported yet */
806 if ((CLSCTX_LOCAL_SERVER|CLSCTX_REMOTE_SERVER) & dwClsContext) {
807 FIXME(ole, "CLSCTX_LOCAL_SERVER and CLSCTX_REMOTE_SERVER not supported!\n");
808 return E_ACCESSDENIED;
811 if ((CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER) & dwClsContext) {
812 HKEY CLSIDkey,key;
814 /* lookup CLSID in registry key HKCR/CLSID */
815 hres = RegOpenKeyEx32A(HKEY_CLASSES_ROOT, "CLSID", 0,
816 KEY_READ, &CLSIDkey);
818 if (hres != ERROR_SUCCESS)
819 return REGDB_E_READREGDB;
820 hres = RegOpenKeyEx32A(CLSIDkey,xclsid,0,KEY_QUERY_VALUE,&key);
821 if (hres != ERROR_SUCCESS) {
822 RegCloseKey(CLSIDkey);
823 return REGDB_E_CLASSNOTREG;
825 hres = RegQueryValue32A(key, "InprocServer32", dllName, &dllNameLen);
826 RegCloseKey(key);
827 RegCloseKey(CLSIDkey);
828 if (hres != ERROR_SUCCESS)
829 return REGDB_E_READREGDB;
830 TRACE(ole,"found InprocServer32 dll %s\n", dllName);
832 /* open dll, call DllGetClassFactory */
833 hLibrary = CoLoadLibrary(dllName, TRUE);
834 if (hLibrary == 0) {
835 TRACE(ole,"couldn't load InprocServer32 dll %s\n", dllName);
836 return E_ACCESSDENIED; /* or should this be CO_E_DLLNOTFOUND? */
838 DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress32(hLibrary, "DllGetClassObject");
839 if (!DllGetClassObject) {
840 /* not sure if this should be called here CoFreeLibrary(hLibrary);*/
841 TRACE(ole,"couldn't find function DllGetClassObject in %s\n", dllName);
842 return E_ACCESSDENIED;
844 /* FIXME: Shouldn't we get a classfactory and use this to create
845 * the required object?
847 return DllGetClassObject(rclsid, iid, ppv);
849 return hres;
852 /******************************************************************************
853 * CoRegisterMessageFilter16 [COMPOBJ.27]
855 HRESULT WINAPI CoRegisterMessageFilter16(
856 LPMESSAGEFILTER lpMessageFilter,
857 LPMESSAGEFILTER *lplpMessageFilter
859 FIXME(ole,"(%p,%p),stub!\n",lpMessageFilter,lplpMessageFilter);
860 return 0;
863 /***********************************************************************
864 * CoCreateInstance [COMPOBJ.13, OLE32.7]
866 HRESULT WINAPI CoCreateInstance(
867 REFCLSID rclsid,
868 LPUNKNOWN pUnkOuter,
869 DWORD dwClsContext,
870 REFIID iid,
871 LPVOID *ppv
873 #if 0
874 char buf[80],xbuf[80];
876 if (rclsid)
877 WINE_StringFromCLSID(rclsid,buf);
878 else
879 sprintf(buf,"<rclsid-0x%08lx>",(DWORD)rclsid);
880 if (iid)
881 WINE_StringFromCLSID(iid,xbuf);
882 else
883 sprintf(xbuf,"<iid-0x%08lx>",(DWORD)iid);
885 FIXME(ole,"(%s,%p,0x%08lx,%s,%p): stub !\n",buf,pUnkOuter,dwClsContext,xbuf,ppv);
886 *ppv = NULL;
887 #else
888 HRESULT hres;
889 LPCLASSFACTORY lpclf = 0;
891 hres = CoGetClassObject(rclsid, dwClsContext, NULL, (const REFIID) &IID_IClassFactory, (LPVOID)&lpclf);
892 if (!SUCCEEDED(hres)) return hres;
893 hres = IClassFactory_CreateInstance(lpclf, pUnkOuter, iid, ppv);
894 IClassFactory_Release(lpclf);
895 return hres;
896 #endif
901 /***********************************************************************
902 * CoFreeLibrary [COMPOBJ.13]
904 void WINAPI CoFreeLibrary(HINSTANCE32 hLibrary)
906 OpenDll *ptr, *prev;
907 OpenDll *tmp;
909 /* lookup library in linked list */
910 prev = NULL;
911 for (ptr = openDllList; ptr != NULL; ptr=ptr->next) {
912 if (ptr->hLibrary == hLibrary) {
913 break;
915 prev = ptr;
918 if (ptr == NULL) {
919 /* shouldn't happen if user passed in a valid hLibrary */
920 return;
922 /* assert: ptr points to the library entry to free */
924 /* free library and remove node from list */
925 FreeLibrary32(hLibrary);
926 if (ptr == openDllList) {
927 tmp = openDllList->next;
928 HeapFree(GetProcessHeap(), 0, openDllList->DllName);
929 HeapFree(GetProcessHeap(), 0, openDllList);
930 openDllList = tmp;
931 } else {
932 tmp = ptr->next;
933 HeapFree(GetProcessHeap(), 0, ptr->DllName);
934 HeapFree(GetProcessHeap(), 0, ptr);
935 prev->next = tmp;
941 /***********************************************************************
942 * CoFreeAllLibraries [COMPOBJ.12]
944 void WINAPI CoFreeAllLibraries(void)
946 OpenDll *ptr, *tmp;
948 for (ptr = openDllList; ptr != NULL; ) {
949 tmp=ptr->next;
950 CoFreeLibrary(ptr->hLibrary);
951 ptr = tmp;
957 /***********************************************************************
958 * CoFreeUnusedLibraries [COMPOBJ.17]
960 void WINAPI CoFreeUnusedLibraries(void)
962 OpenDll *ptr, *tmp;
963 typedef HRESULT(*DllCanUnloadNowFunc)(void);
964 DllCanUnloadNowFunc DllCanUnloadNow;
966 for (ptr = openDllList; ptr != NULL; ) {
967 DllCanUnloadNow = (DllCanUnloadNowFunc)
968 GetProcAddress32(ptr->hLibrary, "DllCanUnloadNow");
970 if (DllCanUnloadNow() == S_OK) {
971 tmp=ptr->next;
972 CoFreeLibrary(ptr->hLibrary);
973 ptr = tmp;
974 } else {
975 ptr=ptr->next;
980 /***********************************************************************
981 * CoFileTimeNow [COMPOBJ.82, OLE32.10]
982 * RETURNS
983 * the current system time in lpFileTime
985 HRESULT WINAPI CoFileTimeNow(
986 FILETIME *lpFileTime /* [out] the current time */
988 DOSFS_UnixTimeToFileTime(time(NULL), lpFileTime, 0);
989 return S_OK;
992 /***********************************************************************
993 * CoTaskMemAlloc (OLE32.43)
994 * RETURNS
995 * pointer to newly allocated block
997 LPVOID WINAPI CoTaskMemAlloc(
998 ULONG size /* [in] size of memoryblock to be allocated */
1000 LPMALLOC32 lpmalloc;
1001 HRESULT ret = CoGetMalloc32(0,&lpmalloc);
1003 if (ret)
1004 return NULL;
1005 return lpmalloc->lpvtbl->fnAlloc(lpmalloc,size);
1008 /***********************************************************************
1009 * CoTaskMemFree (OLE32.44)
1011 VOID WINAPI CoTaskMemFree(
1012 LPVOID ptr /* [in] pointer to be freed */
1014 LPMALLOC32 lpmalloc;
1015 HRESULT ret = CoGetMalloc32(0,&lpmalloc);
1017 if (ret) return;
1018 lpmalloc->lpvtbl->fnFree(lpmalloc,ptr);
1021 /***********************************************************************
1022 * CoLoadLibrary (OLE32.30)
1024 HINSTANCE32 WINAPI CoLoadLibrary(LPOLESTR16 lpszLibName, BOOL32 bAutoFree)
1026 HINSTANCE32 hLibrary;
1027 OpenDll *ptr;
1028 OpenDll *tmp;
1030 TRACE(ole,"CoLoadLibrary(%p, %d\n", lpszLibName, bAutoFree);
1032 hLibrary = LoadLibrary32A(lpszLibName);
1034 if (!bAutoFree)
1035 return hLibrary;
1037 if (openDllList == NULL) {
1038 /* empty list -- add first node */
1039 openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
1040 openDllList->DllName = HEAP_strdupA(GetProcessHeap(), 0, lpszLibName);
1041 openDllList->hLibrary = hLibrary;
1042 openDllList->next = NULL;
1043 } else {
1044 /* search for this dll */
1045 int found = FALSE;
1046 for (ptr = openDllList; ptr->next != NULL; ptr=ptr->next) {
1047 if (ptr->hLibrary == hLibrary) {
1048 found = TRUE;
1049 break;
1052 if (!found) {
1053 /* dll not found, add it */
1054 tmp = openDllList;
1055 openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
1056 openDllList->DllName = HEAP_strdupA(GetProcessHeap(), 0, lpszLibName);
1057 openDllList->hLibrary = hLibrary;
1058 openDllList->next = tmp;
1062 return hLibrary;
1065 /***********************************************************************
1066 * CoInitializeWOW (OLE32.27)
1068 HRESULT WINAPI CoInitializeWOW(DWORD x,DWORD y) {
1069 FIXME(ole,"(0x%08lx,0x%08lx),stub!\n",x,y);
1070 return 0;
1073 /******************************************************************************
1074 * CoLockObjectExternal16 [COMPOBJ.63]
1076 HRESULT WINAPI CoLockObjectExternal16(
1077 LPUNKNOWN pUnk, /* [in] object to be locked */
1078 BOOL16 fLock, /* [in] do lock */
1079 BOOL16 fLastUnlockReleases /* [in] ? */
1081 FIXME(ole,"(%p,%d,%d),stub!\n",pUnk,fLock,fLastUnlockReleases);
1082 return S_OK;
1085 /******************************************************************************
1086 * CoLockObjectExternal32 [OLE32.31]
1088 HRESULT WINAPI CoLockObjectExternal32(
1089 LPUNKNOWN pUnk, /* [in] object to be locked */
1090 BOOL32 fLock, /* [in] do lock */
1091 BOOL32 fLastUnlockReleases /* [in] ? */
1093 FIXME(ole,"(%p,%d,%d),stub!\n",pUnk,fLock,fLastUnlockReleases);
1094 return S_OK;
1097 /***********************************************************************
1098 * CoGetState16 [COMPOBJ.115]
1100 HRESULT WINAPI CoGetState16(LPDWORD state)
1102 FIXME(ole, "(%p),stub!\n", state);
1103 *state = 0;
1104 return S_OK;
1106 /***********************************************************************
1107 * CoSetState32 [COM32.42]
1109 HRESULT WINAPI CoSetState32(LPDWORD state)
1111 FIXME(ole, "(%p),stub!\n", state);
1112 *state = 0;
1113 return S_OK;