Correct errors with move to kernel time functions.
[wine/multimedia.git] / dlls / oleaut32 / oleaut.c
blobf58660e9c632e63b02066104e012e8c8084d2bdc
1 /*
2 * OLEAUT32
4 * Copyright 1999, 2000 Marcus Meissner
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.
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.
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
21 #include <stdarg.h>
22 #include <string.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "winuser.h"
28 #include "winerror.h"
30 #include "ole2.h"
31 #include "olectl.h"
32 #include "oleauto.h"
34 #include "tmarshal.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(ole);
40 /* The OLE Automation ProxyStub Interface Class (aka Typelib Marshaler) */
41 extern const GUID CLSID_PSOAInterface;
43 /* IDispatch marshaler */
44 extern const GUID CLSID_PSDispatch;
46 static BOOL BSTR_bCache = TRUE; /* Cache allocations to minimise alloc calls? */
48 HMODULE OLEAUT32_hModule = NULL;
50 /******************************************************************************
51 * BSTR {OLEAUT32}
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.
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.
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.
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().
70 * BSTR's are cached by Ole Automation by default. To override this behaviour
71 * either set the environment variable 'OANOCACHE', or call SetOaNoCache().
73 * SEE ALSO
74 * 'Inside OLE, second edition' by Kraig Brockshmidt.
77 /******************************************************************************
78 * SysStringLen [OLEAUT32.7]
80 * Get the allocated length of a BSTR in wide characters.
82 * PARAMS
83 * str [I] BSTR to find the length of
85 * RETURNS
86 * The allocated length of str, or 0 if str is NULL.
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().
94 UINT WINAPI SysStringLen(BSTR str)
96 DWORD* bufferPointer;
98 if (!str) return 0;
100 * The length of the string (in bytes) is contained in a DWORD placed
101 * just before the BSTR pointer
103 bufferPointer = (DWORD*)str;
105 bufferPointer--;
107 return (int)(*bufferPointer/sizeof(WCHAR));
110 /******************************************************************************
111 * SysStringByteLen [OLEAUT32.149]
113 * Get the allocated length of a BSTR in bytes.
115 * PARAMS
116 * str [I] BSTR to find the length of
118 * RETURNS
119 * The allocated length of str, or 0 if str is NULL.
121 * NOTES
122 * See SysStringLen(), BSTR().
124 UINT WINAPI SysStringByteLen(BSTR str)
126 DWORD* bufferPointer;
128 if (!str) return 0;
130 * The length of the string (in bytes) is contained in a DWORD placed
131 * just before the BSTR pointer
133 bufferPointer = (DWORD*)str;
135 bufferPointer--;
137 return (int)(*bufferPointer);
140 /******************************************************************************
141 * SysAllocString [OLEAUT32.2]
143 * Create a BSTR from an OLESTR.
145 * PARAMS
146 * str [I] Source to create BSTR from
148 * RETURNS
149 * Success: A BSTR allocated with SysAllocStringLen().
150 * Failure: NULL, if oleStr is NULL.
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.
158 BSTR WINAPI SysAllocString(LPCOLESTR str)
160 if (!str) return 0;
162 /* Delegate this to the SysAllocStringLen32 method. */
163 return SysAllocStringLen(str, lstrlenW(str));
166 /******************************************************************************
167 * SysFreeString [OLEAUT32.6]
169 * Free a BSTR.
171 * PARAMS
172 * str [I] BSTR to free.
174 * RETURNS
175 * Nothing.
177 * NOTES
178 * See BSTR.
179 * str may be NULL, in which case this function does nothing.
181 void WINAPI SysFreeString(BSTR str)
183 DWORD* bufferPointer;
185 /* NULL is a valid parameter */
186 if(!str) return;
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.
193 bufferPointer = (DWORD*)str;
195 bufferPointer--;
198 * Free the memory from its "real" origin.
200 HeapFree(GetProcessHeap(), 0, bufferPointer);
203 /******************************************************************************
204 * SysAllocStringLen [OLEAUT32.4]
206 * Create a BSTR from an OLESTR of a given wide character length.
208 * PARAMS
209 * str [I] Source to create BSTR from
210 * len [I] Length of oleStr in wide characters
212 * RETURNS
213 * Success: A newly allocated BSTR from SysAllocStringByteLen()
214 * Failure: NULL, if len is >= 0x80000000, or memory allocation fails.
216 * NOTES
217 * See BSTR(), SysAllocStringByteLen().
219 BSTR WINAPI SysAllocStringLen(const OLECHAR *str, unsigned int len)
221 DWORD bufferSize;
222 DWORD* newBuffer;
223 WCHAR* stringBuffer;
226 * Find the length of the buffer passed-in in bytes.
228 bufferSize = len * sizeof (WCHAR);
231 * Allocate a new buffer to hold the string.
232 * don'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.
236 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
238 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
241 * If the memory allocation failed, return a null pointer.
243 if (newBuffer==0)
244 return 0;
247 * Copy the length of the string in the placeholder.
249 *newBuffer = bufferSize;
252 * Skip the byte count.
254 newBuffer++;
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.
261 if (str != 0)
262 memcpy(newBuffer, str, bufferSize);
263 else
264 memset(newBuffer, 0, bufferSize);
267 * Make sure that there is a nul character at the end of the
268 * string.
270 stringBuffer = (WCHAR*)newBuffer;
271 stringBuffer[len] = L'\0';
273 return (LPWSTR)stringBuffer;
276 /******************************************************************************
277 * SysReAllocStringLen [OLEAUT32.5]
279 * Change the length of a previously created BSTR.
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
286 * RETURNS
287 * Success: 1. The size of pbstr is updated.
288 * Failure: 0, if len >= 0x80000000 or memory allocation fails.
290 * NOTES
291 * See BSTR(), SysAllocStringByteLen().
292 * *pbstr may be changed by this function.
294 int WINAPI SysReAllocStringLen(BSTR* old, const OLECHAR* str, unsigned int len)
296 if (*old!=NULL) {
297 DWORD newbytelen = len*sizeof(WCHAR);
298 DWORD *ptr = HeapReAlloc(GetProcessHeap(),0,((DWORD*)*old)-1,newbytelen+sizeof(WCHAR)+sizeof(DWORD));
299 *old = (BSTR)(ptr+1);
300 *ptr = newbytelen;
301 if (str) {
302 memcpy(*old, str, newbytelen);
303 (*old)[len] = 0;
304 } else {
305 /* Subtle hidden feature: The old string data is still there
306 * when 'in' is NULL!
307 * Some Microsoft program needs it.
310 } else {
312 * Allocate the new string
314 *old = SysAllocStringLen(str, len);
317 return 1;
320 /******************************************************************************
321 * SysAllocStringByteLen [OLEAUT32.150]
323 * Create a BSTR from an OLESTR of a given byte length.
325 * PARAMS
326 * str [I] Source to create BSTR from
327 * len [I] Length of oleStr in bytes
329 * RETURNS
330 * Success: A newly allocated BSTR
331 * Failure: NULL, if len is >= 0x80000000, or memory allocation fails.
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.
340 BSTR WINAPI SysAllocStringByteLen(LPCSTR str, UINT len)
342 DWORD* newBuffer;
343 char* stringBuffer;
346 * Allocate a new buffer to hold the string.
347 * don't forget to keep an empty spot at the beginning of the
348 * buffer for the character count and an extra character at the
349 * end for the NULL.
351 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
353 len + sizeof(WCHAR) + sizeof(DWORD));
356 * If the memory allocation failed, return a null pointer.
358 if (newBuffer==0)
359 return 0;
362 * Copy the length of the string in the placeholder.
364 *newBuffer = len;
367 * Skip the byte count.
369 newBuffer++;
372 * Copy the information in the buffer.
373 * Since it is valid to pass a NULL pointer here, we'll initialize the
374 * buffer to nul if it is the case.
376 if (str != 0)
377 memcpy(newBuffer, str, len);
380 * Make sure that there is a nul character at the end of the
381 * string.
383 stringBuffer = (char *)newBuffer;
384 stringBuffer[len] = 0;
385 stringBuffer[len+1] = 0;
387 return (LPWSTR)stringBuffer;
390 /******************************************************************************
391 * SysReAllocString [OLEAUT32.3]
393 * Change the length of a previously created BSTR.
395 * PARAMS
396 * old [I/O] BSTR to change the length of
397 * str [I] New source for pbstr
399 * RETURNS
400 * Success: 1
401 * Failure: 0.
403 * NOTES
404 * See BSTR(), SysAllocStringStringLen().
406 INT WINAPI SysReAllocString(LPBSTR old,LPCOLESTR str)
409 * Sanity check
411 if (old==NULL)
412 return 0;
415 * Make sure we free the old string.
417 if (*old!=NULL)
418 SysFreeString(*old);
421 * Allocate the new string
423 *old = SysAllocString(str);
425 return 1;
428 /******************************************************************************
429 * SetOaNoCache (OLEAUT32.327)
431 * Instruct Ole Automation not to cache BSTR allocations.
433 * PARAMS
434 * None.
436 * RETURNS
437 * Nothing.
439 * NOTES
440 * See BSTR.
442 void WINAPI SetOaNoCache(void)
444 BSTR_bCache = FALSE;
447 static WCHAR _delimiter[2] = {'!',0}; /* default delimiter apparently */
448 static WCHAR *pdelimiter = &_delimiter[0];
450 /***********************************************************************
451 * RegisterActiveObject (OLEAUT32.33)
453 HRESULT WINAPI RegisterActiveObject(
454 LPUNKNOWN punk,REFCLSID rcid,DWORD dwFlags,LPDWORD pdwRegister
456 WCHAR guidbuf[80];
457 HRESULT ret;
458 LPRUNNINGOBJECTTABLE runobtable;
459 LPMONIKER moniker;
461 StringFromGUID2(rcid,guidbuf,39);
462 ret = CreateItemMoniker(pdelimiter,guidbuf,&moniker);
463 if (FAILED(ret))
464 return ret;
465 ret = GetRunningObjectTable(0,&runobtable);
466 if (FAILED(ret)) {
467 IMoniker_Release(moniker);
468 return ret;
470 ret = IRunningObjectTable_Register(runobtable,dwFlags,punk,moniker,pdwRegister);
471 IRunningObjectTable_Release(runobtable);
472 IMoniker_Release(moniker);
473 return ret;
476 /***********************************************************************
477 * RevokeActiveObject (OLEAUT32.34)
479 HRESULT WINAPI RevokeActiveObject(DWORD xregister,LPVOID reserved)
481 LPRUNNINGOBJECTTABLE runobtable;
482 HRESULT ret;
484 ret = GetRunningObjectTable(0,&runobtable);
485 if (FAILED(ret)) return ret;
486 ret = IRunningObjectTable_Revoke(runobtable,xregister);
487 if (SUCCEEDED(ret)) ret = S_OK;
488 IRunningObjectTable_Release(runobtable);
489 return ret;
492 /***********************************************************************
493 * GetActiveObject (OLEAUT32.35)
495 HRESULT WINAPI GetActiveObject(REFCLSID rcid,LPVOID preserved,LPUNKNOWN *ppunk)
497 WCHAR guidbuf[80];
498 HRESULT ret;
499 LPRUNNINGOBJECTTABLE runobtable;
500 LPMONIKER moniker;
502 StringFromGUID2(rcid,guidbuf,39);
503 ret = CreateItemMoniker(pdelimiter,guidbuf,&moniker);
504 if (FAILED(ret))
505 return ret;
506 ret = GetRunningObjectTable(0,&runobtable);
507 if (FAILED(ret)) {
508 IMoniker_Release(moniker);
509 return ret;
511 ret = IRunningObjectTable_GetObject(runobtable,moniker,ppunk);
512 IRunningObjectTable_Release(runobtable);
513 IMoniker_Release(moniker);
514 return ret;
518 /***********************************************************************
519 * OaBuildVersion [OLEAUT32.170]
521 * Get the Ole Automation build version.
523 * PARAMS
524 * None
526 * RETURNS
527 * The build version.
529 * NOTES
530 * Known oleaut32.dll versions:
531 *| OLE Ver. Comments Date Build Ver.
532 *| -------- ------------------------- ---- ---------
533 *| OLE 2.1 NT 1993-95 10 3023
534 *| OLE 2.1 10 3027
535 *| Win32s Ver 1.1e 20 4049
536 *| OLE 2.20 W95/NT 1993-96 20 4112
537 *| OLE 2.20 W95/NT 1993-96 20 4118
538 *| OLE 2.20 W95/NT 1993-96 20 4122
539 *| OLE 2.30 W95/NT 1993-98 30 4265
540 *| OLE 2.40 NT?? 1993-98 40 4267
541 *| OLE 2.40 W98 SE orig. file 1993-98 40 4275
542 *| OLE 2.40 W2K orig. file 1993-XX 40 4514
544 * Currently the versions returned are 2.20 for Win3.1, 2.30 for Win95 & NT 3.51,
545 * and 2.40 for all later versions. The build number is maximum, i.e. 0xffff.
547 ULONG WINAPI OaBuildVersion()
549 switch(GetVersion() & 0x8000ffff) /* mask off build number */
551 case 0x80000a03: /* WIN31 */
552 return MAKELONG(0xffff, 20);
553 case 0x00003303: /* NT351 */
554 return MAKELONG(0xffff, 30);
555 case 0x80000004: /* WIN95; I'd like to use the "standard" w95 minor
556 version here (30), but as we still use w95
557 as default winver (which is good IMHO), I better
558 play safe and use the latest value for w95 for now.
559 Change this as soon as default winver gets changed
560 to something more recent */
561 case 0x80000a04: /* WIN98 */
562 case 0x00000004: /* NT40 */
563 case 0x00000005: /* W2K */
564 return MAKELONG(0xffff, 40);
565 default:
566 ERR("Version value not known yet. Please investigate it !\n");
567 return 0x0;
571 /******************************************************************************
572 * OleTranslateColor [OLEAUT32.421]
574 * Convert an OLE_COLOR to a COLORREF.
576 * PARAMS
577 * clr [I] Color to convert
578 * hpal [I] Handle to a palette for the conversion
579 * pColorRef [O] Destination for converted color, or NULL to test if the conversion is ok
581 * RETURNS
582 * Success: S_OK. The conversion is ok, and pColorRef contains the converted color if non-NULL.
583 * Failure: E_INVALIDARG, if any argument is invalid.
585 * FIXME
586 * Document the conversion rules.
588 HRESULT WINAPI OleTranslateColor(
589 OLE_COLOR clr,
590 HPALETTE hpal,
591 COLORREF* pColorRef)
593 COLORREF colorref;
594 BYTE b = HIBYTE(HIWORD(clr));
596 TRACE("(%08lx, %p, %p):stub\n", clr, hpal, pColorRef);
599 * In case pColorRef is NULL, provide our own to simplify the code.
601 if (pColorRef == NULL)
602 pColorRef = &colorref;
604 switch (b)
606 case 0x00:
608 if (hpal != 0)
609 *pColorRef = PALETTERGB(GetRValue(clr),
610 GetGValue(clr),
611 GetBValue(clr));
612 else
613 *pColorRef = clr;
615 break;
618 case 0x01:
620 if (hpal != 0)
622 PALETTEENTRY pe;
624 * Validate the palette index.
626 if (GetPaletteEntries(hpal, LOWORD(clr), 1, &pe) == 0)
627 return E_INVALIDARG;
630 *pColorRef = clr;
632 break;
635 case 0x02:
636 *pColorRef = clr;
637 break;
639 case 0x80:
641 int index = LOBYTE(LOWORD(clr));
644 * Validate GetSysColor index.
646 if ((index < COLOR_SCROLLBAR) || (index > COLOR_GRADIENTINACTIVECAPTION))
647 return E_INVALIDARG;
649 *pColorRef = GetSysColor(index);
651 break;
654 default:
655 return E_INVALIDARG;
658 return S_OK;
661 extern HRESULT OLEAUTPS_DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv);
663 extern void _get_STDFONT_CF(LPVOID);
664 extern void _get_STDPIC_CF(LPVOID);
666 /***********************************************************************
667 * DllGetClassObject (OLEAUT32.1)
669 HRESULT WINAPI OLEAUT32_DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
671 *ppv = NULL;
672 if (IsEqualGUID(rclsid,&CLSID_StdFont)) {
673 if (IsEqualGUID(iid,&IID_IClassFactory)) {
674 _get_STDFONT_CF(ppv);
675 IClassFactory_AddRef((IClassFactory*)*ppv);
676 return S_OK;
679 if (IsEqualGUID(rclsid,&CLSID_StdPicture)) {
680 if (IsEqualGUID(iid,&IID_IClassFactory)) {
681 _get_STDPIC_CF(ppv);
682 IClassFactory_AddRef((IClassFactory*)*ppv);
683 return S_OK;
686 if (IsEqualGUID(rclsid,&CLSID_PSDispatch)) {
687 return OLEAUTPS_DllGetClassObject(rclsid,iid,ppv);
689 if (IsEqualGUID(rclsid,&CLSID_PSOAInterface)) {
690 if (S_OK==TypeLibFac_DllGetClassObject(rclsid,iid,ppv))
691 return S_OK;
692 /*FALLTHROUGH*/
694 FIXME("\n\tCLSID:\t%s,\n\tIID:\t%s\n",debugstr_guid(rclsid),debugstr_guid(iid));
695 return CLASS_E_CLASSNOTAVAILABLE;
698 /***********************************************************************
699 * DllCanUnloadNow (OLEAUT32.410)
701 * Determine if this dll can be unloaded from the callers address space.
703 * PARAMS
704 * None.
706 * RETURNS
707 * Always returns S_FALSE. This dll cannot be unloaded.
709 HRESULT WINAPI OLEAUT32_DllCanUnloadNow()
711 return S_FALSE;
714 /*****************************************************************************
715 * DllMain [OLEAUT32.@]
717 BOOL WINAPI DllMain(HINSTANCE hInstDll, DWORD fdwReason, LPVOID lpvReserved)
719 TRACE("(%p,%lu,%p)\n", hInstDll, fdwReason, lpvReserved);
721 switch (fdwReason) {
722 case DLL_PROCESS_ATTACH:
723 DisableThreadLibraryCalls(hInstDll);
724 OLEAUT32_hModule = (HMODULE)hInstDll;
725 break;
726 case DLL_PROCESS_DETACH:
727 break;
730 return TRUE;