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