dmsynth: Dump data passed to Download method.
[wine] / dlls / oleaut32 / oleaut.c
1 /*
2  *      OLEAUT32
3  *
4  * Copyright 1999, 2000 Marcus Meissner
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22 #include <string.h>
23 #include <limits.h>
24
25 #define COBJMACROS
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "winerror.h"
32
33 #include "ole2.h"
34 #include "olectl.h"
35 #include "oleauto.h"
36 #include "initguid.h"
37 #include "typelib.h"
38 #include "oleaut32_oaidl.h"
39
40 #include "wine/debug.h"
41 #include "wine/unicode.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(ole);
44 WINE_DECLARE_DEBUG_CHANNEL(heap);
45
46 /******************************************************************************
47  * BSTR  {OLEAUT32}
48  *
49  * NOTES
50  *  BSTR is a simple typedef for a wide-character string used as the principle
51  *  string type in ole automation. When encapsulated in a Variant type they are
52  *  automatically copied and destroyed as the variant is processed.
53  *
54  *  The low level BSTR API allows manipulation of these strings and is used by
55  *  higher level API calls to manage the strings transparently to the caller.
56  *
57  *  Internally the BSTR type is allocated with space for a DWORD byte count before
58  *  the string data begins. This is undocumented and non-system code should not
59  *  access the count directly. Use SysStringLen() or SysStringByteLen()
60  *  instead. Note that the byte count does not include the terminating NUL.
61  *
62  *  To create a new BSTR, use SysAllocString(), SysAllocStringLen() or
63  *  SysAllocStringByteLen(). To change the size of an existing BSTR, use SysReAllocString()
64  *  or SysReAllocStringLen(). Finally to destroy a string use SysFreeString().
65  *
66  *  BSTR's are cached by Ole Automation by default. To override this behaviour
67  *  either set the environment variable 'OANOCACHE', or call SetOaNoCache().
68  *
69  * SEE ALSO
70  *  'Inside OLE, second edition' by Kraig Brockshmidt.
71  */
72
73 static BOOL bstr_cache_enabled;
74
75 static CRITICAL_SECTION cs_bstr_cache;
76 static CRITICAL_SECTION_DEBUG cs_bstr_cache_dbg =
77 {
78     0, 0, &cs_bstr_cache,
79     { &cs_bstr_cache_dbg.ProcessLocksList, &cs_bstr_cache_dbg.ProcessLocksList },
80       0, 0, { (DWORD_PTR)(__FILE__ ": bstr_cache") }
81 };
82 static CRITICAL_SECTION cs_bstr_cache = { &cs_bstr_cache_dbg, -1, 0, 0, 0, 0 };
83
84 typedef struct {
85     DWORD size;
86     union {
87         char ptr[1];
88         WCHAR str[1];
89         DWORD dwptr[1];
90     } u;
91 } bstr_t;
92
93 typedef struct {
94     unsigned short head;
95     unsigned short cnt;
96     bstr_t *buf[6];
97 } bstr_cache_entry_t;
98
99 #define BUCKET_SIZE 16
100
101 #define ARENA_INUSE_FILLER     0x55
102 #define ARENA_TAIL_FILLER      0xab
103 #define ARENA_FREE_FILLER      0xfeeefeee
104
105 static bstr_cache_entry_t bstr_cache[0x10000/BUCKET_SIZE];
106
107 static inline size_t bstr_alloc_size(size_t size)
108 {
109     return (FIELD_OFFSET(bstr_t, u.ptr[size]) + sizeof(WCHAR) + BUCKET_SIZE-1) & ~(BUCKET_SIZE-1);
110 }
111
112 static inline bstr_t *bstr_from_str(BSTR str)
113 {
114     return CONTAINING_RECORD(str, bstr_t, u.str);
115 }
116
117 static inline bstr_cache_entry_t *get_cache_entry(size_t size)
118 {
119     unsigned cache_idx = FIELD_OFFSET(bstr_t, u.ptr[size-1])/BUCKET_SIZE;
120     return bstr_cache_enabled && cache_idx < sizeof(bstr_cache)/sizeof(*bstr_cache)
121         ? bstr_cache + cache_idx
122         : NULL;
123 }
124
125 static bstr_t *alloc_bstr(size_t size)
126 {
127     bstr_cache_entry_t *cache_entry = get_cache_entry(size+sizeof(WCHAR));
128     bstr_t *ret;
129
130     if(cache_entry) {
131         EnterCriticalSection(&cs_bstr_cache);
132
133         if(!cache_entry->cnt) {
134             cache_entry = get_cache_entry(size+sizeof(WCHAR)+BUCKET_SIZE);
135             if(cache_entry && !cache_entry->cnt)
136                 cache_entry = NULL;
137         }
138
139         if(cache_entry) {
140             ret = cache_entry->buf[cache_entry->head++];
141             cache_entry->head %= sizeof(cache_entry->buf)/sizeof(*cache_entry->buf);
142             cache_entry->cnt--;
143         }
144
145         LeaveCriticalSection(&cs_bstr_cache);
146
147         if(cache_entry) {
148             if(WARN_ON(heap)) {
149                 size_t tail;
150
151                 memset(ret, ARENA_INUSE_FILLER, FIELD_OFFSET(bstr_t, u.ptr[size+sizeof(WCHAR)]));
152                 tail = bstr_alloc_size(size) - FIELD_OFFSET(bstr_t, u.ptr[size+sizeof(WCHAR)]);
153                 if(tail)
154                     memset(ret->u.ptr+size+sizeof(WCHAR), ARENA_TAIL_FILLER, tail);
155             }
156             ret->size = size;
157             return ret;
158         }
159     }
160
161     ret = HeapAlloc(GetProcessHeap(), 0, bstr_alloc_size(size));
162     if(ret)
163         ret->size = size;
164     return ret;
165 }
166
167 /******************************************************************************
168  *             SysStringLen  [OLEAUT32.7]
169  *
170  * Get the allocated length of a BSTR in wide characters.
171  *
172  * PARAMS
173  *  str [I] BSTR to find the length of
174  *
175  * RETURNS
176  *  The allocated length of str, or 0 if str is NULL.
177  *
178  * NOTES
179  *  See BSTR.
180  *  The returned length may be different from the length of the string as
181  *  calculated by lstrlenW(), since it returns the length that was used to
182  *  allocate the string by SysAllocStringLen().
183  */
184 UINT WINAPI SysStringLen(BSTR str)
185 {
186     return str ? bstr_from_str(str)->size/sizeof(WCHAR) : 0;
187 }
188
189 /******************************************************************************
190  *             SysStringByteLen  [OLEAUT32.149]
191  *
192  * Get the allocated length of a BSTR in bytes.
193  *
194  * PARAMS
195  *  str [I] BSTR to find the length of
196  *
197  * RETURNS
198  *  The allocated length of str, or 0 if str is NULL.
199  *
200  * NOTES
201  *  See SysStringLen(), BSTR().
202  */
203 UINT WINAPI SysStringByteLen(BSTR str)
204 {
205     return str ? bstr_from_str(str)->size : 0;
206 }
207
208 /******************************************************************************
209  *              SysAllocString  [OLEAUT32.2]
210  *
211  * Create a BSTR from an OLESTR.
212  *
213  * PARAMS
214  *  str [I] Source to create BSTR from
215  *
216  * RETURNS
217  *  Success: A BSTR allocated with SysAllocStringLen().
218  *  Failure: NULL, if oleStr is NULL.
219  *
220  * NOTES
221  *  See BSTR.
222  *  MSDN (October 2001) incorrectly states that NULL is returned if oleStr has
223  *  a length of 0. Native Win32 and this implementation both return a valid
224  *  empty BSTR in this case.
225  */
226 BSTR WINAPI SysAllocString(LPCOLESTR str)
227 {
228     if (!str) return 0;
229
230     /* Delegate this to the SysAllocStringLen32 method. */
231     return SysAllocStringLen(str, lstrlenW(str));
232 }
233
234 /******************************************************************************
235  *              SysFreeString   [OLEAUT32.6]
236  *
237  * Free a BSTR.
238  *
239  * PARAMS
240  *  str [I] BSTR to free.
241  *
242  * RETURNS
243  *  Nothing.
244  *
245  * NOTES
246  *  See BSTR.
247  *  str may be NULL, in which case this function does nothing.
248  */
249 void WINAPI SysFreeString(BSTR str)
250 {
251     bstr_cache_entry_t *cache_entry;
252     bstr_t *bstr;
253
254     if(!str)
255         return;
256
257     bstr = bstr_from_str(str);
258     cache_entry = get_cache_entry(bstr->size+sizeof(WCHAR));
259     if(cache_entry) {
260         EnterCriticalSection(&cs_bstr_cache);
261
262         if(cache_entry->cnt < sizeof(cache_entry->buf)/sizeof(*cache_entry->buf)) {
263             cache_entry->buf[(cache_entry->head+cache_entry->cnt)%((sizeof(cache_entry->buf)/sizeof(*cache_entry->buf)))] = bstr;
264             cache_entry->cnt++;
265
266             if(WARN_ON(heap)) {
267                 unsigned i, n = bstr_alloc_size(bstr->size) / sizeof(DWORD) - 1;
268                 bstr->size = ARENA_FREE_FILLER;
269                 for(i=0; i<n; i++)
270                     bstr->u.dwptr[i] = ARENA_FREE_FILLER;
271             }
272
273             LeaveCriticalSection(&cs_bstr_cache);
274             return;
275         }
276
277         LeaveCriticalSection(&cs_bstr_cache);
278     }
279
280     HeapFree(GetProcessHeap(), 0, bstr);
281 }
282
283 /******************************************************************************
284  *             SysAllocStringLen     [OLEAUT32.4]
285  *
286  * Create a BSTR from an OLESTR of a given wide character length.
287  *
288  * PARAMS
289  *  str [I] Source to create BSTR from
290  *  len [I] Length of oleStr in wide characters
291  *
292  * RETURNS
293  *  Success: A newly allocated BSTR from SysAllocStringByteLen()
294  *  Failure: NULL, if len is >= 0x80000000, or memory allocation fails.
295  *
296  * NOTES
297  *  See BSTR(), SysAllocStringByteLen().
298  */
299 BSTR WINAPI SysAllocStringLen(const OLECHAR *str, unsigned int len)
300 {
301     bstr_t *bstr;
302     DWORD size;
303
304     /* Detect integer overflow. */
305     if (len >= ((UINT_MAX-sizeof(WCHAR)-sizeof(DWORD))/sizeof(WCHAR)))
306         return NULL;
307
308     TRACE("%s\n", debugstr_wn(str, len));
309
310     size = len*sizeof(WCHAR);
311     bstr = alloc_bstr(size);
312     if(!bstr)
313         return NULL;
314
315     if(str) {
316         memcpy(bstr->u.str, str, size);
317         bstr->u.str[len] = 0;
318     }else {
319         memset(bstr->u.str, 0, size+sizeof(WCHAR));
320     }
321
322     return bstr->u.str;
323 }
324
325 /******************************************************************************
326  *             SysReAllocStringLen   [OLEAUT32.5]
327  *
328  * Change the length of a previously created BSTR.
329  *
330  * PARAMS
331  *  old [O] BSTR to change the length of
332  *  str [I] New source for pbstr
333  *  len [I] Length of oleStr in wide characters
334  *
335  * RETURNS
336  *  Success: 1. The size of pbstr is updated.
337  *  Failure: 0, if len >= 0x80000000 or memory allocation fails.
338  *
339  * NOTES
340  *  See BSTR(), SysAllocStringByteLen().
341  *  *old may be changed by this function.
342  */
343 int WINAPI SysReAllocStringLen(BSTR* old, const OLECHAR* str, unsigned int len)
344 {
345     /* Detect integer overflow. */
346     if (len >= ((UINT_MAX-sizeof(WCHAR)-sizeof(DWORD))/sizeof(WCHAR)))
347         return 0;
348
349     if (*old!=NULL) {
350       BSTR old_copy = *old;
351       DWORD newbytelen = len*sizeof(WCHAR);
352       bstr_t *bstr = HeapReAlloc(GetProcessHeap(),0,((DWORD*)*old)-1,bstr_alloc_size(newbytelen));
353       *old = bstr->u.str;
354       bstr->size = newbytelen;
355       /* Subtle hidden feature: The old string data is still there
356        * when 'in' is NULL!
357        * Some Microsoft program needs it.
358        * FIXME: Is it a sideeffect of BSTR caching?
359        */
360       if (str && old_copy!=str) memmove(*old, str, newbytelen);
361       (*old)[len] = 0;
362     } else {
363       /*
364        * Allocate the new string
365        */
366       *old = SysAllocStringLen(str, len);
367     }
368
369     return 1;
370 }
371
372 /******************************************************************************
373  *             SysAllocStringByteLen     [OLEAUT32.150]
374  *
375  * Create a BSTR from an OLESTR of a given byte length.
376  *
377  * PARAMS
378  *  str [I] Source to create BSTR from
379  *  len [I] Length of oleStr in bytes
380  *
381  * RETURNS
382  *  Success: A newly allocated BSTR
383  *  Failure: NULL, if len is >= 0x80000000, or memory allocation fails.
384  *
385  * NOTES
386  *  -If len is 0 or oleStr is NULL the resulting string is empty ("").
387  *  -This function always NUL terminates the resulting BSTR.
388  *  -oleStr may be either an LPCSTR or LPCOLESTR, since it is copied
389  *  without checking for a terminating NUL.
390  *  See BSTR.
391  */
392 BSTR WINAPI SysAllocStringByteLen(LPCSTR str, UINT len)
393 {
394     bstr_t *bstr;
395
396     /* Detect integer overflow. */
397     if (len >= (UINT_MAX-sizeof(WCHAR)-sizeof(DWORD)))
398         return NULL;
399
400     bstr = alloc_bstr(len);
401     if(!bstr)
402         return NULL;
403
404     if(str) {
405         memcpy(bstr->u.ptr, str, len);
406         bstr->u.ptr[len] = bstr->u.ptr[len+1] = 0;
407     }else {
408         memset(bstr->u.ptr, 0, len+sizeof(WCHAR));
409     }
410
411     return bstr->u.str;
412 }
413
414 /******************************************************************************
415  *              SysReAllocString        [OLEAUT32.3]
416  *
417  * Change the length of a previously created BSTR.
418  *
419  * PARAMS
420  *  old [I/O] BSTR to change the length of
421  *  str [I]   New source for pbstr
422  *
423  * RETURNS
424  *  Success: 1
425  *  Failure: 0.
426  *
427  * NOTES
428  *  See BSTR(), SysAllocStringStringLen().
429  */
430 INT WINAPI SysReAllocString(LPBSTR old,LPCOLESTR str)
431 {
432     /*
433      * Sanity check
434      */
435     if (old==NULL)
436       return 0;
437
438     /*
439      * Make sure we free the old string.
440      */
441     SysFreeString(*old);
442
443     /*
444      * Allocate the new string
445      */
446     *old = SysAllocString(str);
447
448      return 1;
449 }
450
451 /******************************************************************************
452  *              SetOaNoCache (OLEAUT32.327)
453  *
454  * Instruct Ole Automation not to cache BSTR allocations.
455  *
456  * PARAMS
457  *  None.
458  *
459  * RETURNS
460  *  Nothing.
461  *
462  * NOTES
463  *  SetOaNoCache does not release cached strings, so it leaks by design.
464  */
465 void WINAPI SetOaNoCache(void)
466 {
467     TRACE("\n");
468     bstr_cache_enabled = FALSE;
469 }
470
471 static const WCHAR      _delimiter[] = {'!',0}; /* default delimiter apparently */
472 static const WCHAR      *pdelimiter = &_delimiter[0];
473
474 /***********************************************************************
475  *              RegisterActiveObject (OLEAUT32.33)
476  *
477  * Registers an object in the global item table.
478  *
479  * PARAMS
480  *  punk        [I] Object to register.
481  *  rcid        [I] CLSID of the object.
482  *  dwFlags     [I] Flags.
483  *  pdwRegister [O] Address to store cookie of object registration in.
484  *
485  * RETURNS
486  *  Success: S_OK.
487  *  Failure: HRESULT code.
488  */
489 HRESULT WINAPI RegisterActiveObject(
490         LPUNKNOWN punk,REFCLSID rcid,DWORD dwFlags,LPDWORD pdwRegister
491 ) {
492         WCHAR                   guidbuf[80];
493         HRESULT                 ret;
494         LPRUNNINGOBJECTTABLE    runobtable;
495         LPMONIKER               moniker;
496         DWORD                   rot_flags = ROTFLAGS_REGISTRATIONKEEPSALIVE; /* default registration is strong */
497
498         StringFromGUID2(rcid,guidbuf,39);
499         ret = CreateItemMoniker(pdelimiter,guidbuf,&moniker);
500         if (FAILED(ret))
501                 return ret;
502         ret = GetRunningObjectTable(0,&runobtable);
503         if (FAILED(ret)) {
504                 IMoniker_Release(moniker);
505                 return ret;
506         }
507         if(dwFlags == ACTIVEOBJECT_WEAK)
508           rot_flags = 0;
509         ret = IRunningObjectTable_Register(runobtable,rot_flags,punk,moniker,pdwRegister);
510         IRunningObjectTable_Release(runobtable);
511         IMoniker_Release(moniker);
512         return ret;
513 }
514
515 /***********************************************************************
516  *              RevokeActiveObject (OLEAUT32.34)
517  *
518  * Revokes an object from the global item table.
519  *
520  * PARAMS
521  *  xregister [I] Registration cookie.
522  *  reserved  [I] Reserved. Set to NULL.
523  *
524  * RETURNS
525  *  Success: S_OK.
526  *  Failure: HRESULT code.
527  */
528 HRESULT WINAPI RevokeActiveObject(DWORD xregister,LPVOID reserved)
529 {
530         LPRUNNINGOBJECTTABLE    runobtable;
531         HRESULT                 ret;
532
533         ret = GetRunningObjectTable(0,&runobtable);
534         if (FAILED(ret)) return ret;
535         ret = IRunningObjectTable_Revoke(runobtable,xregister);
536         if (SUCCEEDED(ret)) ret = S_OK;
537         IRunningObjectTable_Release(runobtable);
538         return ret;
539 }
540
541 /***********************************************************************
542  *              GetActiveObject (OLEAUT32.35)
543  *
544  * Gets an object from the global item table.
545  *
546  * PARAMS
547  *  rcid        [I] CLSID of the object.
548  *  preserved   [I] Reserved. Set to NULL.
549  *  ppunk       [O] Address to store object into.
550  *
551  * RETURNS
552  *  Success: S_OK.
553  *  Failure: HRESULT code.
554  */
555 HRESULT WINAPI GetActiveObject(REFCLSID rcid,LPVOID preserved,LPUNKNOWN *ppunk)
556 {
557         WCHAR                   guidbuf[80];
558         HRESULT                 ret;
559         LPRUNNINGOBJECTTABLE    runobtable;
560         LPMONIKER               moniker;
561
562         StringFromGUID2(rcid,guidbuf,39);
563         ret = CreateItemMoniker(pdelimiter,guidbuf,&moniker);
564         if (FAILED(ret))
565                 return ret;
566         ret = GetRunningObjectTable(0,&runobtable);
567         if (FAILED(ret)) {
568                 IMoniker_Release(moniker);
569                 return ret;
570         }
571         ret = IRunningObjectTable_GetObject(runobtable,moniker,ppunk);
572         IRunningObjectTable_Release(runobtable);
573         IMoniker_Release(moniker);
574         return ret;
575 }
576
577
578 /***********************************************************************
579  *           OaBuildVersion           [OLEAUT32.170]
580  *
581  * Get the Ole Automation build version.
582  *
583  * PARAMS
584  *  None
585  *
586  * RETURNS
587  *  The build version.
588  *
589  * NOTES
590  *  Known oleaut32.dll versions:
591  *| OLE Ver.  Comments                   Date     Build Ver.
592  *| --------  -------------------------  ----     ---------
593  *| OLE 2.1   NT                         1993-95  10 3023
594  *| OLE 2.1                                       10 3027
595  *| Win32s    Ver 1.1e                            20 4049
596  *| OLE 2.20  W95/NT                     1993-96  20 4112
597  *| OLE 2.20  W95/NT                     1993-96  20 4118
598  *| OLE 2.20  W95/NT                     1993-96  20 4122
599  *| OLE 2.30  W95/NT                     1993-98  30 4265
600  *| OLE 2.40  NT??                       1993-98  40 4267
601  *| OLE 2.40  W98 SE orig. file          1993-98  40 4275
602  *| OLE 2.40  W2K orig. file             1993-XX  40 4514
603  *
604  * Currently the versions returned are 2.20 for Win3.1, 2.30 for Win95 & NT 3.51,
605  * and 2.40 for all later versions. The build number is maximum, i.e. 0xffff.
606  */
607 ULONG WINAPI OaBuildVersion(void)
608 {
609     switch(GetVersion() & 0x8000ffff)  /* mask off build number */
610     {
611     case 0x80000a03:  /* WIN31 */
612                 return MAKELONG(0xffff, 20);
613     case 0x00003303:  /* NT351 */
614                 return MAKELONG(0xffff, 30);
615     case 0x80000004:  /* WIN95; I'd like to use the "standard" w95 minor
616                          version here (30), but as we still use w95
617                          as default winver (which is good IMHO), I better
618                          play safe and use the latest value for w95 for now.
619                          Change this as soon as default winver gets changed
620                          to something more recent */
621     case 0x80000a04:  /* WIN98 */
622     case 0x00000004:  /* NT40 */
623     case 0x00000005:  /* W2K */
624                 return MAKELONG(0xffff, 40);
625     case 0x00000105:  /* WinXP */
626     case 0x00000006:  /* Vista */
627     case 0x00000106:  /* Win7 */
628                 return MAKELONG(0xffff, 50);
629     default:
630                 FIXME("Version value not known yet. Please investigate it !\n");
631                 return MAKELONG(0xffff, 40);  /* for now return the same value as for w2k */
632     }
633 }
634
635 /******************************************************************************
636  *              OleTranslateColor       [OLEAUT32.421]
637  *
638  * Convert an OLE_COLOR to a COLORREF.
639  *
640  * PARAMS
641  *  clr       [I] Color to convert
642  *  hpal      [I] Handle to a palette for the conversion
643  *  pColorRef [O] Destination for converted color, or NULL to test if the conversion is ok
644  *
645  * RETURNS
646  *  Success: S_OK. The conversion is ok, and pColorRef contains the converted color if non-NULL.
647  *  Failure: E_INVALIDARG, if any argument is invalid.
648  *
649  * FIXME
650  *  Document the conversion rules.
651  */
652 HRESULT WINAPI OleTranslateColor(
653   OLE_COLOR clr,
654   HPALETTE  hpal,
655   COLORREF* pColorRef)
656 {
657   COLORREF colorref;
658   BYTE b = HIBYTE(HIWORD(clr));
659
660   TRACE("(%08x, %p, %p)\n", clr, hpal, pColorRef);
661
662   /*
663    * In case pColorRef is NULL, provide our own to simplify the code.
664    */
665   if (pColorRef == NULL)
666     pColorRef = &colorref;
667
668   switch (b)
669   {
670     case 0x00:
671     {
672       if (hpal != 0)
673         *pColorRef =  PALETTERGB(GetRValue(clr),
674                                  GetGValue(clr),
675                                  GetBValue(clr));
676       else
677         *pColorRef = clr;
678
679       break;
680     }
681
682     case 0x01:
683     {
684       if (hpal != 0)
685       {
686         PALETTEENTRY pe;
687         /*
688          * Validate the palette index.
689          */
690         if (GetPaletteEntries(hpal, LOWORD(clr), 1, &pe) == 0)
691           return E_INVALIDARG;
692       }
693
694       *pColorRef = clr;
695
696       break;
697     }
698
699     case 0x02:
700       *pColorRef = clr;
701       break;
702
703     case 0x80:
704     {
705       int index = LOBYTE(LOWORD(clr));
706
707       /*
708        * Validate GetSysColor index.
709        */
710       if ((index < COLOR_SCROLLBAR) || (index > COLOR_MENUBAR))
711         return E_INVALIDARG;
712
713       *pColorRef =  GetSysColor(index);
714
715       break;
716     }
717
718     default:
719       return E_INVALIDARG;
720   }
721
722   return S_OK;
723 }
724
725 extern HRESULT WINAPI OLEAUTPS_DllGetClassObject(REFCLSID, REFIID, LPVOID *) DECLSPEC_HIDDEN;
726 extern BOOL WINAPI OLEAUTPS_DllMain(HINSTANCE, DWORD, LPVOID) DECLSPEC_HIDDEN;
727 extern HRESULT WINAPI OLEAUTPS_DllRegisterServer(void) DECLSPEC_HIDDEN;
728 extern HRESULT WINAPI OLEAUTPS_DllUnregisterServer(void) DECLSPEC_HIDDEN;
729 extern GUID const CLSID_PSFactoryBuffer DECLSPEC_HIDDEN;
730
731 extern void _get_STDFONT_CF(LPVOID *);
732 extern void _get_STDPIC_CF(LPVOID *);
733
734 static HRESULT WINAPI PSDispatchFacBuf_QueryInterface(IPSFactoryBuffer *iface, REFIID riid, void **ppv)
735 {
736     if (IsEqualIID(riid, &IID_IUnknown) ||
737         IsEqualIID(riid, &IID_IPSFactoryBuffer))
738     {
739         IPSFactoryBuffer_AddRef(iface);
740         *ppv = iface;
741         return S_OK;
742     }
743     return E_NOINTERFACE;
744 }
745
746 static ULONG WINAPI PSDispatchFacBuf_AddRef(IPSFactoryBuffer *iface)
747 {
748     return 2;
749 }
750
751 static ULONG WINAPI PSDispatchFacBuf_Release(IPSFactoryBuffer *iface)
752 {
753     return 1;
754 }
755
756 static HRESULT WINAPI PSDispatchFacBuf_CreateProxy(IPSFactoryBuffer *iface, IUnknown *pUnkOuter, REFIID riid, IRpcProxyBuffer **ppProxy, void **ppv)
757 {
758     IPSFactoryBuffer *pPSFB;
759     HRESULT hr;
760
761     if (IsEqualIID(riid, &IID_IDispatch))
762         hr = OLEAUTPS_DllGetClassObject(&CLSID_PSFactoryBuffer, &IID_IPSFactoryBuffer, (void **)&pPSFB);
763     else
764         hr = TMARSHAL_DllGetClassObject(&CLSID_PSOAInterface, &IID_IPSFactoryBuffer, (void **)&pPSFB);
765
766     if (FAILED(hr)) return hr;
767
768     hr = IPSFactoryBuffer_CreateProxy(pPSFB, pUnkOuter, riid, ppProxy, ppv);
769
770     IPSFactoryBuffer_Release(pPSFB);
771     return hr;
772 }
773
774 static HRESULT WINAPI PSDispatchFacBuf_CreateStub(IPSFactoryBuffer *iface, REFIID riid, IUnknown *pUnkOuter, IRpcStubBuffer **ppStub)
775 {
776     IPSFactoryBuffer *pPSFB;
777     HRESULT hr;
778
779     if (IsEqualIID(riid, &IID_IDispatch))
780         hr = OLEAUTPS_DllGetClassObject(&CLSID_PSFactoryBuffer, &IID_IPSFactoryBuffer, (void **)&pPSFB);
781     else
782         hr = TMARSHAL_DllGetClassObject(&CLSID_PSOAInterface, &IID_IPSFactoryBuffer, (void **)&pPSFB);
783
784     if (FAILED(hr)) return hr;
785
786     hr = IPSFactoryBuffer_CreateStub(pPSFB, riid, pUnkOuter, ppStub);
787
788     IPSFactoryBuffer_Release(pPSFB);
789     return hr;
790 }
791
792 static const IPSFactoryBufferVtbl PSDispatchFacBuf_Vtbl =
793 {
794     PSDispatchFacBuf_QueryInterface,
795     PSDispatchFacBuf_AddRef,
796     PSDispatchFacBuf_Release,
797     PSDispatchFacBuf_CreateProxy,
798     PSDispatchFacBuf_CreateStub
799 };
800
801 /* This is the whole PSFactoryBuffer object, just the vtableptr */
802 static const IPSFactoryBufferVtbl *pPSDispatchFacBuf = &PSDispatchFacBuf_Vtbl;
803
804 /***********************************************************************
805  *              DllGetClassObject (OLEAUT32.@)
806  */
807 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
808 {
809     *ppv = NULL;
810     if (IsEqualGUID(rclsid,&CLSID_StdFont)) {
811         if (IsEqualGUID(iid,&IID_IClassFactory)) {
812             _get_STDFONT_CF(ppv);
813             IClassFactory_AddRef((IClassFactory*)*ppv);
814             return S_OK;
815         }
816     }
817     if (IsEqualGUID(rclsid,&CLSID_StdPicture)) {
818         if (IsEqualGUID(iid,&IID_IClassFactory)) {
819             _get_STDPIC_CF(ppv);
820             IClassFactory_AddRef((IClassFactory*)*ppv);
821             return S_OK;
822         }
823     }
824     if (IsEqualCLSID(rclsid, &CLSID_PSDispatch) && IsEqualIID(iid, &IID_IPSFactoryBuffer)) {
825         *ppv = &pPSDispatchFacBuf;
826         IPSFactoryBuffer_AddRef((IPSFactoryBuffer *)*ppv);
827         return S_OK;
828     }
829     if (IsEqualGUID(rclsid,&CLSID_PSOAInterface)) {
830         if (S_OK==TMARSHAL_DllGetClassObject(rclsid,iid,ppv))
831             return S_OK;
832         /*FALLTHROUGH*/
833     }
834     if (IsEqualCLSID(rclsid, &CLSID_PSTypeInfo) ||
835         IsEqualCLSID(rclsid, &CLSID_PSTypeLib) ||
836         IsEqualCLSID(rclsid, &CLSID_PSDispatch) ||
837         IsEqualCLSID(rclsid, &CLSID_PSEnumVariant))
838         return OLEAUTPS_DllGetClassObject(&CLSID_PSFactoryBuffer, iid, ppv);
839
840     return OLEAUTPS_DllGetClassObject(rclsid, iid, ppv);
841 }
842
843 /***********************************************************************
844  *              DllCanUnloadNow (OLEAUT32.@)
845  *
846  * Determine if this dll can be unloaded from the callers address space.
847  *
848  * PARAMS
849  *  None.
850  *
851  * RETURNS
852  *  Always returns S_FALSE. This dll cannot be unloaded.
853  */
854 HRESULT WINAPI DllCanUnloadNow(void)
855 {
856     return S_FALSE;
857 }
858
859 /*****************************************************************************
860  *              DllMain         [OLEAUT32.@]
861  */
862 BOOL WINAPI DllMain(HINSTANCE hInstDll, DWORD fdwReason, LPVOID lpvReserved)
863 {
864     static const WCHAR oanocacheW[] = {'o','a','n','o','c','a','c','h','e',0};
865
866     bstr_cache_enabled = !GetEnvironmentVariableW(oanocacheW, NULL, 0);
867
868     return OLEAUTPS_DllMain( hInstDll, fdwReason, lpvReserved );
869 }
870
871 /***********************************************************************
872  *              DllRegisterServer (OLEAUT32.@)
873  */
874 HRESULT WINAPI DllRegisterServer(void)
875 {
876     return OLEAUTPS_DllRegisterServer();
877 }
878
879 /***********************************************************************
880  *              DllUnregisterServer (OLEAUT32.@)
881  */
882 HRESULT WINAPI DllUnregisterServer(void)
883 {
884     return OLEAUTPS_DllUnregisterServer();
885 }
886
887 /***********************************************************************
888  *              OleIconToCursor (OLEAUT32.415)
889  */
890 HCURSOR WINAPI OleIconToCursor( HINSTANCE hinstExe, HICON hIcon)
891 {
892     FIXME("(%p,%p), partially implemented.\n",hinstExe,hIcon);
893     /* FIXME: make a extended conversation from HICON to HCURSOR */
894     return CopyCursor(hIcon);
895 }