- Implemented GetRecordInfoFromTypeInfo and GetRecordInfoFromGuid.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <stdarg.h>
22 #include <string.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "winuser.h"
30 #include "winerror.h"
31
32 #include "ole2.h"
33 #include "olectl.h"
34 #include "oleauto.h"
35
36 #include "tmarshal.h"
37
38 #include "wine/debug.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(ole);
41
42 /* The OLE Automation ProxyStub Interface Class (aka Typelib Marshaler) */
43 extern const GUID CLSID_PSOAInterface;
44
45 /* IDispatch marshaler */
46 extern const GUID CLSID_PSDispatch;
47
48 static BOOL BSTR_bCache = TRUE; /* Cache allocations to minimise alloc calls? */
49
50 HMODULE OLEAUT32_hModule = NULL;
51
52 /******************************************************************************
53  * BSTR  {OLEAUT32}
54  *
55  * NOTES
56  *  BSTR is a simple typedef for a wide-character string used as the principle
57  *  string type in ole automation. When encapsulated in a Variant type they are
58  *  automatically copied and destroyed as the variant is processed.
59  *
60  *  The low level BSTR Api allows manipulation of these strings and is used by
61  *  higher level Api calls to manage the strings transparently to the caller.
62  *
63  *  Internally the BSTR type is allocated with space for a DWORD byte count before
64  *  the string data begins. This is undocumented and non-system code should not
65  *  access the count directly. Use SysStringLen() or SysStringByteLen()
66  *  instead. Note that the byte count does not include the terminating NUL.
67  *
68  *  To create a new BSTR, use SysAllocString(), SysAllocStringLen() or
69  *  SysAllocStringByteLen(). To change the size of an existing BSTR, use SysReAllocString()
70  *  or SysReAllocStringLen(). Finally to destroy a string use SysFreeString().
71  *
72  *  BSTR's are cached by Ole Automation by default. To override this behaviour
73  *  either set the environment variable 'OANOCACHE', or call SetOaNoCache().
74  *
75  * SEE ALSO
76  *  'Inside OLE, second edition' by Kraig Brockshmidt.
77  */
78
79 /******************************************************************************
80  *             SysStringLen  [OLEAUT32.7]
81  *
82  * Get the allocated length of a BSTR in wide characters.
83  *
84  * PARAMS
85  *  str [I] BSTR to find the length of
86  *
87  * RETURNS
88  *  The allocated length of str, or 0 if str is NULL.
89  *
90  * NOTES
91  *  See BSTR.
92  *  The returned length may be different from the length of the string as
93  *  calculated by lstrlenW(), since it returns the length that was used to
94  *  allocate the string by SysAllocStringLen().
95  */
96 UINT WINAPI SysStringLen(BSTR str)
97 {
98     DWORD* bufferPointer;
99
100      if (!str) return 0;
101     /*
102      * The length of the string (in bytes) is contained in a DWORD placed
103      * just before the BSTR pointer
104      */
105     bufferPointer = (DWORD*)str;
106
107     bufferPointer--;
108
109     return (int)(*bufferPointer/sizeof(WCHAR));
110 }
111
112 /******************************************************************************
113  *             SysStringByteLen  [OLEAUT32.149]
114  *
115  * Get the allocated length of a BSTR in bytes.
116  *
117  * PARAMS
118  *  str [I] BSTR to find the length of
119  *
120  * RETURNS
121  *  The allocated length of str, or 0 if str is NULL.
122  *
123  * NOTES
124  *  See SysStringLen(), BSTR().
125  */
126 UINT WINAPI SysStringByteLen(BSTR str)
127 {
128     DWORD* bufferPointer;
129
130      if (!str) return 0;
131     /*
132      * The length of the string (in bytes) is contained in a DWORD placed
133      * just before the BSTR pointer
134      */
135     bufferPointer = (DWORD*)str;
136
137     bufferPointer--;
138
139     return (int)(*bufferPointer);
140 }
141
142 /******************************************************************************
143  *              SysAllocString  [OLEAUT32.2]
144  *
145  * Create a BSTR from an OLESTR.
146  *
147  * PARAMS
148  *  str [I] Source to create BSTR from
149  *
150  * RETURNS
151  *  Success: A BSTR allocated with SysAllocStringLen().
152  *  Failure: NULL, if oleStr is NULL.
153  *
154  * NOTES
155  *  See BSTR.
156  *  MSDN (October 2001) incorrectly states that NULL is returned if oleStr has
157  *  a length of 0. Native Win32 and this implementation both return a valid
158  *  empty BSTR in this case.
159  */
160 BSTR WINAPI SysAllocString(LPCOLESTR str)
161 {
162     if (!str) return 0;
163
164     /* Delegate this to the SysAllocStringLen32 method. */
165     return SysAllocStringLen(str, lstrlenW(str));
166 }
167
168 /******************************************************************************
169  *              SysFreeString   [OLEAUT32.6]
170  *
171  * Free a BSTR.
172  *
173  * PARAMS
174  *  str [I] BSTR to free.
175  *
176  * RETURNS
177  *  Nothing.
178  *
179  * NOTES
180  *  See BSTR.
181  *  str may be NULL, in which case this function does nothing.
182  */
183 void WINAPI SysFreeString(BSTR str)
184 {
185     DWORD* bufferPointer;
186
187     /* NULL is a valid parameter */
188     if(!str) return;
189
190     /*
191      * We have to be careful when we free a BSTR pointer, it points to
192      * the beginning of the string but it skips the byte count contained
193      * before the string.
194      */
195     bufferPointer = (DWORD*)str;
196
197     bufferPointer--;
198
199     /*
200      * Free the memory from its "real" origin.
201      */
202     HeapFree(GetProcessHeap(), 0, bufferPointer);
203 }
204
205 /******************************************************************************
206  *             SysAllocStringLen     [OLEAUT32.4]
207  *
208  * Create a BSTR from an OLESTR of a given wide character length.
209  *
210  * PARAMS
211  *  str [I] Source to create BSTR from
212  *  len [I] Length of oleStr in wide characters
213  *
214  * RETURNS
215  *  Success: A newly allocated BSTR from SysAllocStringByteLen()
216  *  Failure: NULL, if len is >= 0x80000000, or memory allocation fails.
217  *
218  * NOTES
219  *  See BSTR(), SysAllocStringByteLen().
220  */
221 BSTR WINAPI SysAllocStringLen(const OLECHAR *str, unsigned int len)
222 {
223     DWORD  bufferSize;
224     DWORD* newBuffer;
225     WCHAR* stringBuffer;
226
227     /*
228      * Find the length of the buffer passed-in in bytes.
229      */
230     bufferSize = len * sizeof (WCHAR);
231
232     /*
233      * Allocate a new buffer to hold the string.
234      * don't forget to keep an empty spot at the beginning of the
235      * buffer for the character count and an extra character at the
236      * end for the NULL.
237      */
238     newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
239                                  0,
240                                  bufferSize + sizeof(WCHAR) + sizeof(DWORD));
241
242     /*
243      * If the memory allocation failed, return a null pointer.
244      */
245     if (newBuffer==0)
246       return 0;
247
248     /*
249      * Copy the length of the string in the placeholder.
250      */
251     *newBuffer = bufferSize;
252
253     /*
254      * Skip the byte count.
255      */
256     newBuffer++;
257
258     /*
259      * Copy the information in the buffer.
260      * Since it is valid to pass a NULL pointer here, we'll initialize the
261      * buffer to nul if it is the case.
262      */
263     if (str != 0)
264       memcpy(newBuffer, str, bufferSize);
265     else
266       memset(newBuffer, 0, bufferSize);
267
268     /*
269      * Make sure that there is a nul character at the end of the
270      * string.
271      */
272     stringBuffer = (WCHAR*)newBuffer;
273     stringBuffer[len] = L'\0';
274
275     return (LPWSTR)stringBuffer;
276 }
277
278 /******************************************************************************
279  *             SysReAllocStringLen   [OLEAUT32.5]
280  *
281  * Change the length of a previously created BSTR.
282  *
283  * PARAMS
284  *  old [O] BSTR to change the length of
285  *  str [I] New source for pbstr
286  *  len [I] Length of oleStr in wide characters
287  *
288  * RETURNS
289  *  Success: 1. The size of pbstr is updated.
290  *  Failure: 0, if len >= 0x80000000 or memory allocation fails.
291  *
292  * NOTES
293  *  See BSTR(), SysAllocStringByteLen().
294  *  *pbstr may be changed by this function.
295  */
296 int WINAPI SysReAllocStringLen(BSTR* old, const OLECHAR* str, unsigned int len)
297 {
298     if (*old!=NULL) {
299       DWORD newbytelen = len*sizeof(WCHAR);
300       DWORD *ptr = HeapReAlloc(GetProcessHeap(),0,((DWORD*)*old)-1,newbytelen+sizeof(WCHAR)+sizeof(DWORD));
301       *old = (BSTR)(ptr+1);
302       *ptr = newbytelen;
303       if (str) {
304         memcpy(*old, str, newbytelen);
305         (*old)[len] = 0;
306       } else {
307         /* Subtle hidden feature: The old string data is still there
308          * when 'in' is NULL!
309          * Some Microsoft program needs it.
310          */
311       }
312     } else {
313       /*
314        * Allocate the new string
315        */
316       *old = SysAllocStringLen(str, len);
317     }
318
319     return 1;
320 }
321
322 /******************************************************************************
323  *             SysAllocStringByteLen     [OLEAUT32.150]
324  *
325  * Create a BSTR from an OLESTR of a given byte length.
326  *
327  * PARAMS
328  *  str [I] Source to create BSTR from
329  *  len [I] Length of oleStr in bytes
330  *
331  * RETURNS
332  *  Success: A newly allocated BSTR
333  *  Failure: NULL, if len is >= 0x80000000, or memory allocation fails.
334  *
335  * NOTES
336  *  -If len is 0 or oleStr is NULL the resulting string is empty ("").
337  *  -This function always NUL terminates the resulting BSTR.
338  *  -oleStr may be either an LPCSTR or LPCOLESTR, since it is copied
339  *  without checking for a terminating NUL.
340  *  See BSTR.
341  */
342 BSTR WINAPI SysAllocStringByteLen(LPCSTR str, UINT len)
343 {
344     DWORD* newBuffer;
345     char* stringBuffer;
346
347     /*
348      * Allocate a new buffer to hold the string.
349      * don't forget to keep an empty spot at the beginning of the
350      * buffer for the character count and an extra character at the
351      * end for the NULL.
352      */
353     newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
354                                  0,
355                                  len + sizeof(WCHAR) + sizeof(DWORD));
356
357     /*
358      * If the memory allocation failed, return a null pointer.
359      */
360     if (newBuffer==0)
361       return 0;
362
363     /*
364      * Copy the length of the string in the placeholder.
365      */
366     *newBuffer = len;
367
368     /*
369      * Skip the byte count.
370      */
371     newBuffer++;
372
373     /*
374      * Copy the information in the buffer.
375      * Since it is valid to pass a NULL pointer here, we'll initialize the
376      * buffer to nul if it is the case.
377      */
378     if (str != 0)
379       memcpy(newBuffer, str, len);
380
381     /*
382      * Make sure that there is a nul character at the end of the
383      * string.
384      */
385     stringBuffer = (char *)newBuffer;
386     stringBuffer[len] = 0;
387     stringBuffer[len+1] = 0;
388
389     return (LPWSTR)stringBuffer;
390 }
391
392 /******************************************************************************
393  *              SysReAllocString        [OLEAUT32.3]
394  *
395  * Change the length of a previously created BSTR.
396  *
397  * PARAMS
398  *  old [I/O] BSTR to change the length of
399  *  str [I]   New source for pbstr
400  *
401  * RETURNS
402  *  Success: 1
403  *  Failure: 0.
404  *
405  * NOTES
406  *  See BSTR(), SysAllocStringStringLen().
407  */
408 INT WINAPI SysReAllocString(LPBSTR old,LPCOLESTR str)
409 {
410     /*
411      * Sanity check
412      */
413     if (old==NULL)
414       return 0;
415
416     /*
417      * Make sure we free the old string.
418      */
419     if (*old!=NULL)
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 WCHAR    _delimiter[2] = {'!',0}; /* default delimiter apparently */
450 static WCHAR    *pdelimiter = &_delimiter[0];
451
452 /***********************************************************************
453  *              RegisterActiveObject (OLEAUT32.33)
454  */
455 HRESULT WINAPI RegisterActiveObject(
456         LPUNKNOWN punk,REFCLSID rcid,DWORD dwFlags,LPDWORD pdwRegister
457 ) {
458         WCHAR                   guidbuf[80];
459         HRESULT                 ret;
460         LPRUNNINGOBJECTTABLE    runobtable;
461         LPMONIKER               moniker;
462
463         StringFromGUID2(rcid,guidbuf,39);
464         ret = CreateItemMoniker(pdelimiter,guidbuf,&moniker);
465         if (FAILED(ret))
466                 return ret;
467         ret = GetRunningObjectTable(0,&runobtable);
468         if (FAILED(ret)) {
469                 IMoniker_Release(moniker);
470                 return ret;
471         }
472         ret = IRunningObjectTable_Register(runobtable,dwFlags,punk,moniker,pdwRegister);
473         IRunningObjectTable_Release(runobtable);
474         IMoniker_Release(moniker);
475         return ret;
476 }
477
478 /***********************************************************************
479  *              RevokeActiveObject (OLEAUT32.34)
480  */
481 HRESULT WINAPI RevokeActiveObject(DWORD xregister,LPVOID reserved)
482 {
483         LPRUNNINGOBJECTTABLE    runobtable;
484         HRESULT                 ret;
485
486         ret = GetRunningObjectTable(0,&runobtable);
487         if (FAILED(ret)) return ret;
488         ret = IRunningObjectTable_Revoke(runobtable,xregister);
489         if (SUCCEEDED(ret)) ret = S_OK;
490         IRunningObjectTable_Release(runobtable);
491         return ret;
492 }
493
494 /***********************************************************************
495  *              GetActiveObject (OLEAUT32.35)
496  */
497 HRESULT WINAPI GetActiveObject(REFCLSID rcid,LPVOID preserved,LPUNKNOWN *ppunk)
498 {
499         WCHAR                   guidbuf[80];
500         HRESULT                 ret;
501         LPRUNNINGOBJECTTABLE    runobtable;
502         LPMONIKER               moniker;
503
504         StringFromGUID2(rcid,guidbuf,39);
505         ret = CreateItemMoniker(pdelimiter,guidbuf,&moniker);
506         if (FAILED(ret))
507                 return ret;
508         ret = GetRunningObjectTable(0,&runobtable);
509         if (FAILED(ret)) {
510                 IMoniker_Release(moniker);
511                 return ret;
512         }
513         ret = IRunningObjectTable_GetObject(runobtable,moniker,ppunk);
514         IRunningObjectTable_Release(runobtable);
515         IMoniker_Release(moniker);
516         return ret;
517 }
518
519
520 /***********************************************************************
521  *           OaBuildVersion           [OLEAUT32.170]
522  *
523  * Get the Ole Automation build version.
524  *
525  * PARAMS
526  *  None
527  *
528  * RETURNS
529  *  The build version.
530  *
531  * NOTES
532  *  Known oleaut32.dll versions:
533  *| OLE Ver.  Comments                   Date     Build Ver.
534  *| --------  -------------------------  ----     ---------
535  *| OLE 2.1   NT                         1993-95  10 3023
536  *| OLE 2.1                                       10 3027
537  *| Win32s    Ver 1.1e                            20 4049
538  *| OLE 2.20  W95/NT                     1993-96  20 4112
539  *| OLE 2.20  W95/NT                     1993-96  20 4118
540  *| OLE 2.20  W95/NT                     1993-96  20 4122
541  *| OLE 2.30  W95/NT                     1993-98  30 4265
542  *| OLE 2.40  NT??                       1993-98  40 4267
543  *| OLE 2.40  W98 SE orig. file          1993-98  40 4275
544  *| OLE 2.40  W2K orig. file             1993-XX  40 4514
545  *
546  * Currently the versions returned are 2.20 for Win3.1, 2.30 for Win95 & NT 3.51,
547  * and 2.40 for all later versions. The build number is maximum, i.e. 0xffff.
548  */
549 ULONG WINAPI OaBuildVersion()
550 {
551     switch(GetVersion() & 0x8000ffff)  /* mask off build number */
552     {
553     case 0x80000a03:  /* WIN31 */
554                 return MAKELONG(0xffff, 20);
555     case 0x00003303:  /* NT351 */
556                 return MAKELONG(0xffff, 30);
557     case 0x80000004:  /* WIN95; I'd like to use the "standard" w95 minor
558                          version here (30), but as we still use w95
559                          as default winver (which is good IMHO), I better
560                          play safe and use the latest value for w95 for now.
561                          Change this as soon as default winver gets changed
562                          to something more recent */
563     case 0x80000a04:  /* WIN98 */
564     case 0x00000004:  /* NT40 */
565     case 0x00000005:  /* W2K */
566     case 0x00000105:  /* WinXP */
567                 return MAKELONG(0xffff, 40);
568     default:
569                 FIXME("Version value not known yet. Please investigate it !\n");
570                 return MAKELONG(0xffff, 40);  /* for now return the same value as for w2k */
571     }
572 }
573
574 /******************************************************************************
575  *              OleTranslateColor       [OLEAUT32.421]
576  *
577  * Convert an OLE_COLOR to a COLORREF.
578  *
579  * PARAMS
580  *  clr       [I] Color to convert
581  *  hpal      [I] Handle to a palette for the conversion
582  *  pColorRef [O] Destination for converted color, or NULL to test if the conversion is ok
583  *
584  * RETURNS
585  *  Success: S_OK. The conversion is ok, and pColorRef contains the converted color if non-NULL.
586  *  Failure: E_INVALIDARG, if any argument is invalid.
587  *
588  * FIXME
589  *  Document the conversion rules.
590  */
591 HRESULT WINAPI OleTranslateColor(
592   OLE_COLOR clr,
593   HPALETTE  hpal,
594   COLORREF* pColorRef)
595 {
596   COLORREF colorref;
597   BYTE b = HIBYTE(HIWORD(clr));
598
599   TRACE("(%08lx, %p, %p):stub\n", clr, hpal, pColorRef);
600
601   /*
602    * In case pColorRef is NULL, provide our own to simplify the code.
603    */
604   if (pColorRef == NULL)
605     pColorRef = &colorref;
606
607   switch (b)
608   {
609     case 0x00:
610     {
611       if (hpal != 0)
612         *pColorRef =  PALETTERGB(GetRValue(clr),
613                                  GetGValue(clr),
614                                  GetBValue(clr));
615       else
616         *pColorRef = clr;
617
618       break;
619     }
620
621     case 0x01:
622     {
623       if (hpal != 0)
624       {
625         PALETTEENTRY pe;
626         /*
627          * Validate the palette index.
628          */
629         if (GetPaletteEntries(hpal, LOWORD(clr), 1, &pe) == 0)
630           return E_INVALIDARG;
631       }
632
633       *pColorRef = clr;
634
635       break;
636     }
637
638     case 0x02:
639       *pColorRef = clr;
640       break;
641
642     case 0x80:
643     {
644       int index = LOBYTE(LOWORD(clr));
645
646       /*
647        * Validate GetSysColor index.
648        */
649       if ((index < COLOR_SCROLLBAR) || (index > COLOR_MENUBAR))
650         return E_INVALIDARG;
651
652       *pColorRef =  GetSysColor(index);
653
654       break;
655     }
656
657     default:
658       return E_INVALIDARG;
659   }
660
661   return S_OK;
662 }
663
664 extern HRESULT OLEAUTPS_DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv);
665
666 extern void _get_STDFONT_CF(LPVOID);
667 extern void _get_STDPIC_CF(LPVOID);
668
669 /***********************************************************************
670  *              DllGetClassObject (OLEAUT32.1)
671  */
672 HRESULT WINAPI OLEAUT32_DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
673 {
674     *ppv = NULL;
675     if (IsEqualGUID(rclsid,&CLSID_StdFont)) {
676         if (IsEqualGUID(iid,&IID_IClassFactory)) {
677             _get_STDFONT_CF(ppv);
678             IClassFactory_AddRef((IClassFactory*)*ppv);
679             return S_OK;
680         }
681     }
682     if (IsEqualGUID(rclsid,&CLSID_StdPicture)) {
683         if (IsEqualGUID(iid,&IID_IClassFactory)) {
684             _get_STDPIC_CF(ppv);
685             IClassFactory_AddRef((IClassFactory*)*ppv);
686             return S_OK;
687         }
688     }
689     if (IsEqualGUID(rclsid,&CLSID_PSDispatch)) {
690         return OLEAUTPS_DllGetClassObject(rclsid,iid,ppv);
691     }
692     if (IsEqualGUID(rclsid,&CLSID_PSOAInterface)) {
693         if (S_OK==TypeLibFac_DllGetClassObject(rclsid,iid,ppv))
694             return S_OK;
695         /*FALLTHROUGH*/
696     }
697     FIXME("\n\tCLSID:\t%s,\n\tIID:\t%s\n",debugstr_guid(rclsid),debugstr_guid(iid));
698     return CLASS_E_CLASSNOTAVAILABLE;
699 }
700
701 /***********************************************************************
702  *              DllCanUnloadNow (OLEAUT32.410)
703  *
704  * Determine if this dll can be unloaded from the callers address space.
705  *
706  * PARAMS
707  *  None.
708  *
709  * RETURNS
710  *  Always returns S_FALSE. This dll cannot be unloaded.
711  */
712 HRESULT WINAPI OLEAUT32_DllCanUnloadNow(void)
713 {
714     return S_FALSE;
715 }
716
717 /*****************************************************************************
718  *              DllMain         [OLEAUT32.@]
719  */
720 BOOL WINAPI DllMain(HINSTANCE hInstDll, DWORD fdwReason, LPVOID lpvReserved)
721 {
722   TRACE("(%p,%lu,%p)\n", hInstDll, fdwReason, lpvReserved);
723
724   switch (fdwReason) {
725   case DLL_PROCESS_ATTACH:
726     DisableThreadLibraryCalls(hInstDll);
727     OLEAUT32_hModule = (HMODULE)hInstDll;
728     break;
729   case DLL_PROCESS_DETACH:
730     break;
731   };
732
733   return TRUE;
734 }