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