Partially complete GetTextCharsetInfo and corrects return values for
[wine/multimedia.git] / ole / compobj.c
blobafc5b385ca898b93e9475a2f2eba3f0fa63063c4
1 /*
2 * COMPOBJ library
4 * Copyright 1995 Martin von Loewis
5 * Copyright 1998 Justin Bradford
6 */
8 #define INITGUID
10 #include "config.h"
11 #ifdef HAVE_UNISTD_H
12 #include <unistd.h>
13 #endif
14 #include <fcntl.h>
15 #include <sys/types.h>
16 #include <sys/time.h>
17 #include <sys/stat.h>
18 #include <sys/file.h>
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 "ole.h"
35 #include "ole2.h"
36 #include "winerror.h"
37 #include "debug.h"
38 #include "file.h"
39 #include "compobj.h"
40 #include "heap.h"
41 #include "ldt.h"
42 #include "interfaces.h"
43 #include "shlobj.h"
44 #include "ddraw.h"
45 #include "dsound.h"
46 #include "dinput.h"
47 #include "d3d.h"
48 #include "dplay.h"
49 #include "windows.h"
52 LPMALLOC16 currentMalloc16=NULL;
53 LPMALLOC32 currentMalloc32=NULL;
55 HTASK16 hETask = 0;
56 WORD Table_ETask[62];
59 /* this open DLL table belongs in a per process table, but my guess is that
60 * it shouldn't live in the kernel, so I'll put them out here in DLL
61 * space assuming that there is one OLE32 per process.
63 typedef struct tagOpenDll {
64 char *DllName; /* really only needed for debugging */
65 HINSTANCE32 hLibrary;
66 struct tagOpenDll *next;
67 } OpenDll;
69 static OpenDll *openDllList = NULL; /* linked list of open dlls */
72 /******************************************************************************
73 * CoBuildVersion [COMPOBJ.1]
75 * RETURNS
76 * Current built version, hiword is majornumber, loword is minornumber
78 DWORD WINAPI CoBuildVersion(void)
80 TRACE(ole,"(void)\n");
81 return (rmm<<16)+rup;
84 /******************************************************************************
85 * CoInitialize16 [COMPOBJ.2]
86 * Set the win16 IMalloc used for memory management
88 HRESULT WINAPI CoInitialize16(
89 LPMALLOC16 lpReserved /* [in] pointer to win16 malloc interface */
90 ) {
91 currentMalloc16 = lpReserved;
92 return S_OK;
95 /******************************************************************************
96 * CoInitialize32 [OLE32.26]
97 * Set the win32 IMalloc used for memorymanagement
99 HRESULT WINAPI CoInitialize32(
100 LPMALLOC32 lpReserved /* [in] pointer to win32 malloc interface */
102 /* FIXME: there really should be something here that incrememts a refcount
103 * but I'm supposing that it is a real COM object, so I won't bother
104 * creating one here. (Decrement done in CoUnitialize()) */
105 currentMalloc32 = lpReserved;
106 return S_OK;
109 /***********************************************************************
110 * CoUnitialize [COMPOBJ.3]
111 * Don't know what it does.
112 * 3-Nov-98 -- this was originally misspelled, I changed it to what I
113 * believe is the correct spelling
115 void WINAPI CoUninitialize(void)
117 TRACE(ole,"(void)\n");
118 CoFreeAllLibraries();
121 /***********************************************************************
122 * CoGetMalloc16 [COMPOBJ.4]
123 * RETURNS
124 * The current win16 IMalloc
126 HRESULT WINAPI CoGetMalloc16(
127 DWORD dwMemContext, /* [in] unknown */
128 LPMALLOC16 * lpMalloc /* [out] current win16 malloc interface */
130 if(!currentMalloc16)
131 currentMalloc16 = IMalloc16_Constructor();
132 *lpMalloc = currentMalloc16;
133 return S_OK;
136 /******************************************************************************
137 * CoGetMalloc32 [OLE32.20]
139 * RETURNS
140 * The current win32 IMalloc
142 HRESULT WINAPI CoGetMalloc32(
143 DWORD dwMemContext, /* [in] unknown */
144 LPMALLOC32 *lpMalloc /* [out] current win32 malloc interface */
146 if(!currentMalloc32)
147 currentMalloc32 = IMalloc32_Constructor();
148 *lpMalloc = currentMalloc32;
149 return S_OK;
152 /***********************************************************************
153 * CoCreateStandardMalloc16 [COMPOBJ.71]
155 OLESTATUS WINAPI CoCreateStandardMalloc16(DWORD dwMemContext,
156 LPMALLOC16 *lpMalloc)
158 /* FIXME: docu says we shouldn't return the same allocator as in
159 * CoGetMalloc16 */
160 *lpMalloc = IMalloc16_Constructor();
161 return S_OK;
164 /******************************************************************************
165 * CoDisconnectObject [COMPOBJ.15]
167 OLESTATUS WINAPI CoDisconnectObject( LPUNKNOWN lpUnk, DWORD reserved )
169 TRACE(ole,"%p %lx\n",lpUnk,reserved);
170 return S_OK;
173 /***********************************************************************
174 * IsEqualGUID [COMPOBJ.18]
175 * Compares two Unique Identifiers
176 * RETURNS
177 * TRUE if equal
179 BOOL16 WINAPI IsEqualGUID(
180 GUID* g1, /* [in] unique id 1 */
181 GUID* g2 /* [in] unique id 2 */
183 return !memcmp( g1, g2, sizeof(GUID) );
186 /******************************************************************************
187 * CLSIDFromString16 [COMPOBJ.20]
188 * Converts a unique identifier from it's string representation into
189 * the GUID struct.
191 * Class id: DWORD-WORD-WORD-BYTES[2]-BYTES[6]
193 * RETURNS
194 * the converted GUID
196 OLESTATUS WINAPI CLSIDFromString16(
197 LPCOLESTR16 idstr, /* [in] string representation of guid */
198 CLSID *id /* [out] GUID converted from string */
200 BYTE *s = (BYTE *) idstr;
201 BYTE *p;
202 int i;
203 BYTE table[256];
205 TRACE(ole,"%s -> %p\n", idstr, id);
207 /* quick lookup table */
208 memset(table, 0, 256);
210 for (i = 0; i < 10; i++) {
211 table['0' + i] = i;
213 for (i = 0; i < 6; i++) {
214 table['A' + i] = i+10;
215 table['a' + i] = i+10;
218 /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
220 if (strlen(idstr) != 38)
221 return OLE_ERROR_OBJECT;
223 p = (BYTE *) id;
225 s++; /* skip leading brace */
226 for (i = 0; i < 4; i++) {
227 p[3 - i] = table[*s]<<4 | table[*(s+1)];
228 s += 2;
230 p += 4;
231 s++; /* skip - */
233 for (i = 0; i < 2; i++) {
234 p[1-i] = table[*s]<<4 | table[*(s+1)];
235 s += 2;
237 p += 2;
238 s++; /* skip - */
240 for (i = 0; i < 2; i++) {
241 p[1-i] = table[*s]<<4 | table[*(s+1)];
242 s += 2;
244 p += 2;
245 s++; /* skip - */
247 /* these are just sequential bytes */
248 for (i = 0; i < 2; i++) {
249 *p++ = table[*s]<<4 | table[*(s+1)];
250 s += 2;
252 s++; /* skip - */
254 for (i = 0; i < 6; i++) {
255 *p++ = table[*s]<<4 | table[*(s+1)];
256 s += 2;
259 return S_OK;
262 /******************************************************************************
263 * CoCreateGuid[OLE32.6]
264 * Implemented according the DCE specification for UUID generation.
265 * Code is based upon uuid library in e2fsprogs by Theodore Ts'o.
266 * Copyright (C) 1996, 1997 Theodore Ts'o.
268 int CoCreateGuid(GUID *pguid) {
269 static char has_init = 0;
270 unsigned char a[6];
271 static int adjustment = 0;
272 static struct timeval last = {0, 0};
273 static UINT16 clock_seq;
274 struct timeval tv;
275 unsigned long long clock_reg;
276 UINT32 clock_high, clock_low;
277 UINT16 temp_clock_seq, temp_clock_mid, temp_clock_hi_and_version;
278 #ifdef HAVE_NET_IF_H
279 int sd;
280 struct ifreq ifr, *ifrp;
281 struct ifconf ifc;
282 char buf[1024];
283 int n, i;
284 #endif
286 /* Have we already tried to get the MAC address? */
287 if (!has_init) {
288 #ifdef HAVE_NET_IF_H
289 /* BSD 4.4 defines the size of an ifreq to be
290 * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
291 * However, under earlier systems, sa_len isn't present, so
292 * the size is just sizeof(struct ifreq)
294 # ifdef HAVE_SA_LEN
295 # ifndef max
296 # define max(a,b) ((a) > (b) ? (a) : (b))
297 # endif
298 # define ifreq_size(i) max(sizeof(struct ifreq),\
299 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
300 # else
301 # define ifreq_size(i) sizeof(struct ifreq)
302 # endif /* HAVE_SA_LEN */
304 sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
305 if (sd < 0) {
306 /* if we can't open a socket, just use random numbers */
307 /* set the multicast bit to prevent conflicts with real cards */
308 a[0] = (rand() & 0xff) | 0x80;
309 a[1] = rand() & 0xff;
310 a[2] = rand() & 0xff;
311 a[3] = rand() & 0xff;
312 a[4] = rand() & 0xff;
313 a[5] = rand() & 0xff;
314 } else {
315 memset(buf, 0, sizeof(buf));
316 ifc.ifc_len = sizeof(buf);
317 ifc.ifc_buf = buf;
318 /* get the ifconf interface */
319 if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) {
320 close(sd);
321 /* no ifconf, so just use random numbers */
322 /* set the multicast bit to prevent conflicts with real cards */
323 a[0] = (rand() & 0xff) | 0x80;
324 a[1] = rand() & 0xff;
325 a[2] = rand() & 0xff;
326 a[3] = rand() & 0xff;
327 a[4] = rand() & 0xff;
328 a[5] = rand() & 0xff;
329 } else {
330 /* loop through the interfaces, looking for a valid one */
331 n = ifc.ifc_len;
332 for (i = 0; i < n; i+= ifreq_size(*ifr) ) {
333 ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i);
334 strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
335 /* try to get the address for this interface */
336 # ifdef SIOCGIFHWADDR
337 if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
338 continue;
339 memcpy(a, (unsigned char *)&ifr.ifr_hwaddr.sa_data, 6);
340 # else
341 # ifdef SIOCGENADDR
342 if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
343 continue;
344 memcpy(a, (unsigned char *) ifr.ifr_enaddr, 6);
345 # else
346 /* XXX we don't have a way of getting the hardware address */
347 close(sd);
348 a[0] = 0;
349 break;
350 # endif /* SIOCGENADDR */
351 # endif /* SIOCGIFHWADDR */
352 /* make sure it's not blank */
353 if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5])
354 continue;
356 goto valid_address;
358 /* if we didn't find a valid address, make a random one */
359 /* once again, set multicast bit to avoid conflicts */
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;
367 valid_address:
368 close(sd);
371 #else
372 /* no networking info, so generate a random address */
373 a[0] = (rand() & 0xff) | 0x80;
374 a[1] = rand() & 0xff;
375 a[2] = rand() & 0xff;
376 a[3] = rand() & 0xff;
377 a[4] = rand() & 0xff;
378 a[5] = rand() & 0xff;
379 #endif /* HAVE_NET_IF_H */
380 has_init = 1;
383 /* generate time element of GUID */
385 /* Assume that the gettimeofday() has microsecond granularity */
386 #define MAX_ADJUSTMENT 10
388 try_again:
389 gettimeofday(&tv, 0);
390 if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
391 clock_seq = ((rand() & 0xff) << 8) + (rand() & 0xff);
392 clock_seq &= 0x1FFF;
393 last = tv;
394 last.tv_sec--;
396 if ((tv.tv_sec < last.tv_sec) ||
397 ((tv.tv_sec == last.tv_sec) &&
398 (tv.tv_usec < last.tv_usec))) {
399 clock_seq = (clock_seq+1) & 0x1FFF;
400 adjustment = 0;
401 } else if ((tv.tv_sec == last.tv_sec) &&
402 (tv.tv_usec == last.tv_usec)) {
403 if (adjustment >= MAX_ADJUSTMENT)
404 goto try_again;
405 adjustment++;
406 } else
407 adjustment = 0;
409 clock_reg = tv.tv_usec*10 + adjustment;
410 clock_reg += ((unsigned long long) tv.tv_sec)*10000000;
411 clock_reg += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
413 clock_high = clock_reg >> 32;
414 clock_low = clock_reg;
415 temp_clock_seq = clock_seq | 0x8000;
416 temp_clock_mid = (UINT16)clock_high;
417 temp_clock_hi_and_version = (clock_high >> 16) | 0x1000;
419 /* pack the information into the GUID structure */
421 ((unsigned char*)&pguid->Data1)[3] = (unsigned char)clock_low;
422 clock_low >>= 8;
423 ((unsigned char*)&pguid->Data1)[2] = (unsigned char)clock_low;
424 clock_low >>= 8;
425 ((unsigned char*)&pguid->Data1)[1] = (unsigned char)clock_low;
426 clock_low >>= 8;
427 ((unsigned char*)&pguid->Data1)[0] = (unsigned char)clock_low;
429 ((unsigned char*)&pguid->Data2)[1] = (unsigned char)temp_clock_mid;
430 temp_clock_mid >>= 8;
431 ((unsigned char*)&pguid->Data2)[0] = (unsigned char)temp_clock_mid;
433 ((unsigned char*)&pguid->Data3)[1] = (unsigned char)temp_clock_hi_and_version;
434 temp_clock_hi_and_version >>= 8;
435 ((unsigned char*)&pguid->Data3)[0] = (unsigned char)temp_clock_hi_and_version;
437 ((unsigned char*)pguid->Data4)[1] = (unsigned char)temp_clock_seq;
438 temp_clock_seq >>= 8;
439 ((unsigned char*)pguid->Data4)[0] = (unsigned char)temp_clock_seq;
441 ((unsigned char*)pguid->Data4)[2] = a[0];
442 ((unsigned char*)pguid->Data4)[3] = a[1];
443 ((unsigned char*)pguid->Data4)[4] = a[2];
444 ((unsigned char*)pguid->Data4)[5] = a[3];
445 ((unsigned char*)pguid->Data4)[6] = a[4];
446 ((unsigned char*)pguid->Data4)[7] = a[5];
448 TRACE(ole, "%p", pguid);
450 return 1;
453 /******************************************************************************
454 * CLSIDFromString32 [OLE32.3]
455 * Converts a unique identifier from it's string representation into
456 * the GUID struct.
457 * RETURNS
458 * the converted GUID
460 OLESTATUS WINAPI CLSIDFromString32(
461 LPCOLESTR32 idstr, /* [in] string representation of GUID */
462 CLSID *id /* [out] GUID represented by above string */
464 LPOLESTR16 xid = HEAP_strdupWtoA(GetProcessHeap(),0,idstr);
465 OLESTATUS ret = CLSIDFromString16(xid,id);
467 HeapFree(GetProcessHeap(),0,xid);
468 return ret;
471 /******************************************************************************
472 * WINE_StringFromCLSID [???]
473 * Converts a GUID into the respective string representation.
475 * NOTES
476 * Why is this WINAPI?
478 * RETURNS
479 * the string representation and OLESTATUS
481 OLESTATUS WINAPI WINE_StringFromCLSID(
482 const CLSID *id, /* [in] GUID to be converted */
483 LPSTR idstr /* [out] pointer to buffer to contain converted guid */
485 static const char *hex = "0123456789ABCDEF";
486 char *s;
487 int i;
489 if (!id)
490 { ERR(ole,"called with id=Null\n");
491 *idstr = 0x00;
492 return E_FAIL;
495 sprintf(idstr, "{%08lX-%04X-%04X-%02x%02X-",
496 id->Data1, id->Data2, id->Data3,
497 id->Data4[0], id->Data4[1]);
498 s = &idstr[25];
500 /* 6 hex bytes */
501 for (i = 2; i < 8; i++) {
502 *s++ = hex[id->Data4[i]>>4];
503 *s++ = hex[id->Data4[i] & 0xf];
506 *s++ = '}';
507 *s++ = '\0';
509 TRACE(ole,"%p->%s\n", id, idstr);
511 return OLE_OK;
514 /******************************************************************************
515 * StringFromCLSID16 [COMPOBJ.19]
516 * Converts a GUID into the respective string representation.
517 * The target string is allocated using the OLE IMalloc.
518 * RETURNS
519 * the string representation and OLESTATUS
521 OLESTATUS WINAPI StringFromCLSID16(
522 const CLSID *id, /* [in] the GUID to be converted */
523 LPOLESTR16 *idstr /* [out] a pointer to a to-be-allocated segmented pointer pointing to the resulting string */
526 LPMALLOC16 mllc;
527 OLESTATUS ret;
528 DWORD args[2];
530 ret = CoGetMalloc16(0,&mllc);
531 if (ret) return ret;
533 args[0] = (DWORD)mllc;
534 args[1] = 40;
536 /* No need for a Callback entry, we have WOWCallback16Ex which does
537 * everything we need.
539 if (!WOWCallback16Ex(
540 (FARPROC16)((LPMALLOC16_VTABLE)PTR_SEG_TO_LIN(
541 ((LPMALLOC16)PTR_SEG_TO_LIN(mllc))->lpvtbl)
542 )->fnAlloc,
543 WCB16_CDECL,
545 (LPVOID)args,
546 (LPDWORD)idstr
547 )) {
548 WARN(ole,"CallTo16 IMalloc16 failed\n");
549 return E_FAIL;
551 return WINE_StringFromCLSID(id,PTR_SEG_TO_LIN(*idstr));
554 /******************************************************************************
555 * StringFromCLSID32 [OLE32.151]
556 * Converts a GUID into the respective string representation.
557 * The target string is allocated using the OLE IMalloc.
558 * RETURNS
559 * the string representation and OLESTATUS
561 OLESTATUS WINAPI StringFromCLSID32(
562 const CLSID *id, /* [in] the GUID to be converted */
563 LPOLESTR32 *idstr /* [out] a pointer to a to-be-allocated pointer pointing to the resulting string */
565 char buf[80];
566 OLESTATUS ret;
567 LPMALLOC32 mllc;
569 if ((ret=CoGetMalloc32(0,&mllc)))
570 return ret;
572 ret=WINE_StringFromCLSID(id,buf);
573 if (!ret) {
574 *idstr = mllc->lpvtbl->fnAlloc(mllc,strlen(buf)*2+2);
575 lstrcpyAtoW(*idstr,buf);
577 return ret;
580 /******************************************************************************
581 * StringFromGUID2 [COMPOBJ.76] [OLE32.152]
583 * Converts a global unique identifier into a string of an API-
584 * specified fixed format. (The usual {.....} stuff.)
586 * RETURNS
587 * The (UNICODE) string representation of the GUID in 'str'
588 * The length of the resulting string, 0 if there was any problem.
590 INT32 WINAPI
591 StringFromGUID2(REFGUID id, LPOLESTR32 str, INT32 cmax)
593 char xguid[80];
595 if (WINE_StringFromCLSID(id,xguid))
596 return 0;
597 if (strlen(xguid)>=cmax)
598 return 0;
599 lstrcpyAtoW(str,xguid);
600 return strlen(xguid);
603 /******************************************************************************
604 * CLSIDFromProgID16 [COMPOBJ.61]
605 * Converts a program id into the respective GUID. (By using a registry lookup)
606 * RETURNS
607 * riid associated with the progid
609 OLESTATUS WINAPI CLSIDFromProgID16(
610 LPCOLESTR16 progid, /* [in] program id as found in registry */
611 LPCLSID riid /* [out] associated CLSID */
613 char *buf,buf2[80];
614 DWORD buf2len;
615 HRESULT err;
616 HKEY xhkey;
618 buf = HeapAlloc(GetProcessHeap(),0,strlen(progid)+8);
619 sprintf(buf,"%s\\CLSID",progid);
620 if ((err=RegOpenKey32A(HKEY_CLASSES_ROOT,buf,&xhkey))) {
621 HeapFree(GetProcessHeap(),0,buf);
622 return OLE_ERROR_GENERIC;
624 HeapFree(GetProcessHeap(),0,buf);
625 buf2len = sizeof(buf2);
626 if ((err=RegQueryValue32A(xhkey,NULL,buf2,&buf2len))) {
627 RegCloseKey(xhkey);
628 return OLE_ERROR_GENERIC;
630 RegCloseKey(xhkey);
631 return CLSIDFromString16(buf2,riid);
634 /******************************************************************************
635 * CLSIDFromProgID32 [OLE32.2]
636 * Converts a program id into the respective GUID. (By using a registry lookup)
637 * RETURNS
638 * riid associated with the progid
640 OLESTATUS WINAPI CLSIDFromProgID32(
641 LPCOLESTR32 progid, /* [in] program id as found in registry */
642 LPCLSID riid /* [out] associated CLSID */
644 LPOLESTR16 pid = HEAP_strdupWtoA(GetProcessHeap(),0,progid);
645 OLESTATUS ret = CLSIDFromProgID16(pid,riid);
647 HeapFree(GetProcessHeap(),0,pid);
648 return ret;
651 /***********************************************************************
652 * LookupETask (COMPOBJ.94)
654 OLESTATUS WINAPI LookupETask(HTASK16 *hTask,LPVOID p) {
655 FIXME(ole,"(%p,%p),stub!\n",hTask,p);
656 if ((*hTask = GetCurrentTask()) == hETask) {
657 memcpy(p, Table_ETask, sizeof(Table_ETask));
659 return 0;
662 /***********************************************************************
663 * SetETask (COMPOBJ.95)
665 OLESTATUS WINAPI SetETask(HTASK16 hTask, LPVOID p) {
666 FIXME(ole,"(%04x,%p),stub!\n",hTask,p);
667 hETask = hTask;
668 return 0;
671 /***********************************************************************
672 * CallObjectInWOW (COMPOBJ.201)
674 OLESTATUS WINAPI CallObjectInWOW(LPVOID p1,LPVOID p2) {
675 FIXME(ole,"(%p,%p),stub!\n",p1,p2);
676 return 0;
679 /******************************************************************************
680 * CoRegisterClassObject16 [COMPOBJ.5]
682 * Don't know where it registers it ...
684 OLESTATUS WINAPI CoRegisterClassObject16(
685 REFCLSID rclsid,
686 LPUNKNOWN pUnk,
687 DWORD dwClsContext,
688 DWORD flags,
689 LPDWORD lpdwRegister
691 char buf[80];
693 WINE_StringFromCLSID(rclsid,buf);
695 FIXME(ole,"(%s,%p,0x%08lx,0x%08lx,%p),stub\n",
696 buf,pUnk,dwClsContext,flags,lpdwRegister
698 return 0;
701 /******************************************************************************
702 * CoRegisterClassObject32 [OLE32.36]
704 * Don't know where it registers it ...
706 OLESTATUS WINAPI CoRegisterClassObject32(
707 REFCLSID rclsid,
708 LPUNKNOWN pUnk,
709 DWORD dwClsContext,
710 DWORD flags,
711 LPDWORD lpdwRegister
713 char buf[80];
715 WINE_StringFromCLSID(rclsid,buf);
717 FIXME(ole,"(%s,%p,0x%08lx,0x%08lx,%p),stub\n",
718 buf,pUnk,dwClsContext,flags,lpdwRegister
720 return 0;
723 /***********************************************************************
724 * CoRevokeClassObject [OLE32.40]
726 HRESULT WINAPI CoRevokeClassObject(DWORD dwRegister) {
727 FIXME(ole,"(%08lx),stub!\n",dwRegister);
728 return S_OK;
731 /***********************************************************************
732 * CoGetClassObject [COMPOBJ.7]
734 HRESULT WINAPI CoGetClassObject(REFCLSID rclsid, DWORD dwClsContext,
735 LPVOID pvReserved, const REFIID iid, LPVOID *ppv)
737 char xclsid[50],xiid[50];
738 HRESULT hres = E_UNEXPECTED;
740 char dllName[MAX_PATH+1];
741 LONG dllNameLen = sizeof(dllName);
742 HINSTANCE32 hLibrary;
743 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid,
744 REFIID iid, LPVOID *ppv);
745 DllGetClassObjectFunc DllGetClassObject;
747 WINE_StringFromCLSID((LPCLSID)rclsid,xclsid);
748 WINE_StringFromCLSID((LPCLSID)iid,xiid);
749 TRACE(ole,"\n\tCLSID:\t%s,\n\tIID:\t%s\n",xclsid,xiid);
751 /* out of process and remote servers not supported yet */
752 if ((CLSCTX_LOCAL_SERVER|CLSCTX_REMOTE_SERVER) & dwClsContext) {
753 FIXME(ole, "CLSCTX_LOCAL_SERVER and CLSCTX_REMOTE_SERVER not supported!\n");
754 return E_ACCESSDENIED;
757 if ((CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER) & dwClsContext) {
758 HKEY CLSIDkey,key;
760 /* lookup CLSID in registry key HKCR/CLSID */
761 hres = RegOpenKeyEx32A(HKEY_CLASSES_ROOT, "CLSID", 0,
762 KEY_READ, &CLSIDkey);
764 if (hres != ERROR_SUCCESS)
765 return REGDB_E_READREGDB;
766 hres = RegOpenKeyEx32A(HKEY_CLASSES_ROOT,xclsid,0,KEY_QUERY_VALUE,
767 &key);
768 if (hres != ERROR_SUCCESS) {
769 RegCloseKey(CLSIDkey);
770 return REGDB_E_CLASSNOTREG;
772 hres = RegQueryValue32A(key, "InprocServer32", dllName, &dllNameLen);
773 RegCloseKey(key);
774 RegCloseKey(CLSIDkey);
775 if (hres != ERROR_SUCCESS)
776 return REGDB_E_READREGDB;
777 TRACE(ole,"found InprocServer32 dll %s\n", dllName);
779 /* open dll, call DllGetClassFactory */
780 hLibrary = CoLoadLibrary(dllName, TRUE);
781 if (hLibrary == 0) {
782 TRACE(ole,"couldn't load InprocServer32 dll %s\n", dllName);
783 return E_ACCESSDENIED; /* or should this be CO_E_DLLNOTFOUND? */
785 DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress32(hLibrary, "DllGetClassObject");
786 if (!DllGetClassObject) {
787 /* not sure if this should be called here CoFreeLibrary(hLibrary);*/
788 TRACE(ole,"couldn't find function DllGetClassObject in %s\n", dllName);
789 return E_ACCESSDENIED;
791 return DllGetClassObject(rclsid, iid, ppv);
793 return hres;
796 /******************************************************************************
797 * CoRegisterMessageFilter16 [COMPOBJ.27]
799 OLESTATUS WINAPI CoRegisterMessageFilter16(
800 LPMESSAGEFILTER lpMessageFilter,
801 LPMESSAGEFILTER *lplpMessageFilter
803 FIXME(ole,"(%p,%p),stub!\n",lpMessageFilter,lplpMessageFilter);
804 return 0;
807 /***********************************************************************
808 * CoCreateInstance [COMPOBJ.13, OLE32.7]
810 HRESULT WINAPI CoCreateInstance(
811 REFCLSID rclsid,
812 LPUNKNOWN pUnkOuter,
813 DWORD dwClsContext,
814 REFIID iid,
815 LPVOID *ppv
817 #if 0
818 char buf[80],xbuf[80];
820 if (rclsid)
821 WINE_StringFromCLSID(rclsid,buf);
822 else
823 sprintf(buf,"<rclsid-0x%08lx>",(DWORD)rclsid);
824 if (iid)
825 WINE_StringFromCLSID(iid,xbuf);
826 else
827 sprintf(xbuf,"<iid-0x%08lx>",(DWORD)iid);
829 FIXME(ole,"(%s,%p,0x%08lx,%s,%p): stub !\n",buf,pUnkOuter,dwClsContext,xbuf,ppv);
830 *ppv = NULL;
831 #else
832 HRESULT hres;
833 LPCLASSFACTORY lpclf = 0;
835 hres = CoGetClassObject(rclsid, dwClsContext, NULL, (const REFIID) &IID_IClassFactory, (LPVOID)&lpclf);
836 if (!SUCCEEDED(hres)) return hres;
837 hres = lpclf->lpvtbl->fnCreateInstance(lpclf, pUnkOuter, iid, ppv);
838 lpclf->lpvtbl->fnRelease(lpclf);
839 return hres;
840 #endif
845 /***********************************************************************
846 * CoFreeLibrary [COMPOBJ.13]
848 void WINAPI CoFreeLibrary(HINSTANCE32 hLibrary)
850 OpenDll *ptr, *prev;
851 OpenDll *tmp;
853 /* lookup library in linked list */
854 prev = NULL;
855 for (ptr = openDllList; ptr != NULL; ptr=ptr->next) {
856 if (ptr->hLibrary == hLibrary) {
857 break;
859 prev = ptr;
862 if (ptr == NULL) {
863 /* shouldn't happen if user passed in a valid hLibrary */
864 return;
866 /* assert: ptr points to the library entry to free */
868 /* free library and remove node from list */
869 FreeLibrary32(hLibrary);
870 if (ptr == openDllList) {
871 tmp = openDllList->next;
872 HeapFree(GetProcessHeap(), 0, openDllList->DllName);
873 HeapFree(GetProcessHeap(), 0, openDllList);
874 openDllList = tmp;
875 } else {
876 tmp = ptr->next;
877 HeapFree(GetProcessHeap(), 0, ptr->DllName);
878 HeapFree(GetProcessHeap(), 0, ptr);
879 prev->next = tmp;
885 /***********************************************************************
886 * CoFreeAllLibraries [COMPOBJ.12]
888 void WINAPI CoFreeAllLibraries(void)
890 OpenDll *ptr, *tmp;
892 for (ptr = openDllList; ptr != NULL; ) {
893 tmp=ptr->next;
894 CoFreeLibrary(ptr->hLibrary);
895 ptr = tmp;
901 /***********************************************************************
902 * CoFreeUnusedLibraries [COMPOBJ.17]
904 void WINAPI CoFreeUnusedLibraries(void)
906 OpenDll *ptr, *tmp;
907 typedef HRESULT(*DllCanUnloadNowFunc)(void);
908 DllCanUnloadNowFunc DllCanUnloadNow;
910 for (ptr = openDllList; ptr != NULL; ) {
911 DllCanUnloadNow = (DllCanUnloadNowFunc)
912 GetProcAddress32(ptr->hLibrary, "DllCanUnloadNow");
914 if (DllCanUnloadNow() == S_OK) {
915 tmp=ptr->next;
916 CoFreeLibrary(ptr->hLibrary);
917 ptr = tmp;
918 } else {
919 ptr=ptr->next;
924 /***********************************************************************
925 * CoFileTimeNow [COMPOBJ.82, OLE32.10]
926 * RETURNS
927 * the current system time in lpFileTime
929 HRESULT WINAPI CoFileTimeNow(
930 FILETIME *lpFileTime /* [out] the current time */
932 DOSFS_UnixTimeToFileTime(time(NULL), lpFileTime, 0);
933 return S_OK;
936 /***********************************************************************
937 * CoTaskMemAlloc (OLE32.43)
938 * RETURNS
939 * pointer to newly allocated block
941 LPVOID WINAPI CoTaskMemAlloc(
942 ULONG size /* [in] size of memoryblock to be allocated */
944 LPMALLOC32 lpmalloc;
945 HRESULT ret = CoGetMalloc32(0,&lpmalloc);
947 if (ret)
948 return NULL;
949 return lpmalloc->lpvtbl->fnAlloc(lpmalloc,size);
952 /***********************************************************************
953 * CoTaskMemFree (OLE32.44)
955 VOID WINAPI CoTaskMemFree(
956 LPVOID ptr /* [in] pointer to be freed */
958 LPMALLOC32 lpmalloc;
959 HRESULT ret = CoGetMalloc32(0,&lpmalloc);
961 if (ret) return;
962 return lpmalloc->lpvtbl->fnFree(lpmalloc,ptr);
965 /***********************************************************************
966 * CoLoadLibrary (OLE32.30)
968 HINSTANCE32 WINAPI CoLoadLibrary(LPSTR lpszLibName, BOOL32 bAutoFree)
970 HINSTANCE32 hLibrary;
971 OpenDll *ptr;
972 OpenDll *tmp;
974 TRACE(ole,"CoLoadLibrary(%p, %d\n", lpszLibName, bAutoFree);
976 hLibrary = LoadLibrary32A(lpszLibName);
978 if (!bAutoFree)
979 return hLibrary;
981 if (openDllList == NULL) {
982 /* empty list -- add first node */
983 openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
984 openDllList->DllName = HEAP_strdupA(GetProcessHeap(), 0, lpszLibName);
985 openDllList->hLibrary = hLibrary;
986 openDllList->next = NULL;
987 } else {
988 /* search for this dll */
989 int found = FALSE;
990 for (ptr = openDllList; ptr->next != NULL; ptr=ptr->next) {
991 if (ptr->hLibrary == hLibrary) {
992 found = TRUE;
993 break;
996 if (!found) {
997 /* dll not found, add it */
998 tmp = openDllList;
999 openDllList = (OpenDll*)HeapAlloc(GetProcessHeap(),0, sizeof(OpenDll));
1000 openDllList->DllName = HEAP_strdupA(GetProcessHeap(), 0, lpszLibName);
1001 openDllList->hLibrary = hLibrary;
1002 openDllList->next = tmp;
1006 return hLibrary;
1009 /***********************************************************************
1010 * CoInitializeWOW (OLE32.27)
1012 HRESULT WINAPI CoInitializeWOW(DWORD x,DWORD y) {
1013 FIXME(ole,"(0x%08lx,0x%08lx),stub!\n",x,y);
1014 return 0;
1017 /******************************************************************************
1018 * CoLockObjectExternal16 [COMPOBJ.63]
1020 HRESULT WINAPI CoLockObjectExternal16(
1021 LPUNKNOWN pUnk, /* [in] object to be locked */
1022 BOOL16 fLock, /* [in] do lock */
1023 BOOL16 fLastUnlockReleases /* [in] ? */
1025 FIXME(ole,"(%p,%d,%d),stub!\n",pUnk,fLock,fLastUnlockReleases);
1026 return S_OK;
1029 /******************************************************************************
1030 * CoLockObjectExternal32 [OLE32.31]
1032 HRESULT WINAPI CoLockObjectExternal32(
1033 LPUNKNOWN pUnk, /* [in] object to be locked */
1034 BOOL32 fLock, /* [in] do lock */
1035 BOOL32 fLastUnlockReleases /* [in] ? */
1037 FIXME(ole,"(%p,%d,%d),stub!\n",pUnk,fLock,fLastUnlockReleases);
1038 return S_OK;
1041 /***********************************************************************
1042 * CoGetState16 [COMPOBJ.115]
1044 HRESULT WINAPI CoGetState16(LPDWORD state)
1046 FIXME(ole, "(%p),stub!\n", state);
1047 *state = 0;
1048 return S_OK;
1050 /***********************************************************************
1051 * CoSetState32 [COM32.42]
1053 HRESULT WINAPI CoSetState32(LPDWORD state)
1055 FIXME(ole, "(%p),stub!\n", state);
1056 *state = 0;
1057 return S_OK;