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