Document active object and variant functions.
[wine/multimedia.git] / dlls / oleaut32 / oleaut.c
blob06533a6f3f6c0ace5e84c8aa35ae815204f973ba
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 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "winuser.h"
30 #include "winerror.h"
32 #include "ole2.h"
33 #include "olectl.h"
34 #include "oleauto.h"
36 #include "tmarshal.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(ole);
42 /* The OLE Automation ProxyStub Interface Class (aka Typelib Marshaler) */
43 extern const GUID CLSID_PSOAInterface;
45 /* IDispatch marshaler */
46 extern const GUID CLSID_PSDispatch;
48 static BOOL BSTR_bCache = TRUE; /* Cache allocations to minimise alloc calls? */
50 HMODULE OLEAUT32_hModule = NULL;
52 /******************************************************************************
53 * BSTR {OLEAUT32}
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.
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.
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.
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().
72 * BSTR's are cached by Ole Automation by default. To override this behaviour
73 * either set the environment variable 'OANOCACHE', or call SetOaNoCache().
75 * SEE ALSO
76 * 'Inside OLE, second edition' by Kraig Brockshmidt.
79 /******************************************************************************
80 * SysStringLen [OLEAUT32.7]
82 * Get the allocated length of a BSTR in wide characters.
84 * PARAMS
85 * str [I] BSTR to find the length of
87 * RETURNS
88 * The allocated length of str, or 0 if str is NULL.
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().
96 UINT WINAPI SysStringLen(BSTR str)
98 DWORD* bufferPointer;
100 if (!str) return 0;
102 * The length of the string (in bytes) is contained in a DWORD placed
103 * just before the BSTR pointer
105 bufferPointer = (DWORD*)str;
107 bufferPointer--;
109 return (int)(*bufferPointer/sizeof(WCHAR));
112 /******************************************************************************
113 * SysStringByteLen [OLEAUT32.149]
115 * Get the allocated length of a BSTR in bytes.
117 * PARAMS
118 * str [I] BSTR to find the length of
120 * RETURNS
121 * The allocated length of str, or 0 if str is NULL.
123 * NOTES
124 * See SysStringLen(), BSTR().
126 UINT WINAPI SysStringByteLen(BSTR str)
128 DWORD* bufferPointer;
130 if (!str) return 0;
132 * The length of the string (in bytes) is contained in a DWORD placed
133 * just before the BSTR pointer
135 bufferPointer = (DWORD*)str;
137 bufferPointer--;
139 return (int)(*bufferPointer);
142 /******************************************************************************
143 * SysAllocString [OLEAUT32.2]
145 * Create a BSTR from an OLESTR.
147 * PARAMS
148 * str [I] Source to create BSTR from
150 * RETURNS
151 * Success: A BSTR allocated with SysAllocStringLen().
152 * Failure: NULL, if oleStr is NULL.
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.
160 BSTR WINAPI SysAllocString(LPCOLESTR str)
162 if (!str) return 0;
164 /* Delegate this to the SysAllocStringLen32 method. */
165 return SysAllocStringLen(str, lstrlenW(str));
168 /******************************************************************************
169 * SysFreeString [OLEAUT32.6]
171 * Free a BSTR.
173 * PARAMS
174 * str [I] BSTR to free.
176 * RETURNS
177 * Nothing.
179 * NOTES
180 * See BSTR.
181 * str may be NULL, in which case this function does nothing.
183 void WINAPI SysFreeString(BSTR str)
185 DWORD* bufferPointer;
187 /* NULL is a valid parameter */
188 if(!str) return;
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.
195 bufferPointer = (DWORD*)str;
197 bufferPointer--;
200 * Free the memory from its "real" origin.
202 HeapFree(GetProcessHeap(), 0, bufferPointer);
205 /******************************************************************************
206 * SysAllocStringLen [OLEAUT32.4]
208 * Create a BSTR from an OLESTR of a given wide character length.
210 * PARAMS
211 * str [I] Source to create BSTR from
212 * len [I] Length of oleStr in wide characters
214 * RETURNS
215 * Success: A newly allocated BSTR from SysAllocStringByteLen()
216 * Failure: NULL, if len is >= 0x80000000, or memory allocation fails.
218 * NOTES
219 * See BSTR(), SysAllocStringByteLen().
221 BSTR WINAPI SysAllocStringLen(const OLECHAR *str, unsigned int len)
223 DWORD bufferSize;
224 DWORD* newBuffer;
225 WCHAR* stringBuffer;
228 * Find the length of the buffer passed-in in bytes.
230 bufferSize = len * sizeof (WCHAR);
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.
238 newBuffer = HeapAlloc(GetProcessHeap(), 0,
239 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
242 * If the memory allocation failed, return a null pointer.
244 if (newBuffer==0)
245 return 0;
248 * Copy the length of the string in the placeholder.
250 *newBuffer = bufferSize;
253 * Skip the byte count.
255 newBuffer++;
258 * Copy the information in the buffer.
259 * Since it is valid to pass a NULL pointer here, we'll initialize the
260 * buffer to nul if it is the case.
262 if (str != 0)
263 memcpy(newBuffer, str, bufferSize);
264 else
265 memset(newBuffer, 0, bufferSize);
268 * Make sure that there is a nul character at the end of the
269 * string.
271 stringBuffer = (WCHAR*)newBuffer;
272 stringBuffer[len] = L'\0';
274 return (LPWSTR)stringBuffer;
277 /******************************************************************************
278 * SysReAllocStringLen [OLEAUT32.5]
280 * Change the length of a previously created BSTR.
282 * PARAMS
283 * old [O] BSTR to change the length of
284 * str [I] New source for pbstr
285 * len [I] Length of oleStr in wide characters
287 * RETURNS
288 * Success: 1. The size of pbstr is updated.
289 * Failure: 0, if len >= 0x80000000 or memory allocation fails.
291 * NOTES
292 * See BSTR(), SysAllocStringByteLen().
293 * *pbstr may be changed by this function.
295 int WINAPI SysReAllocStringLen(BSTR* old, const OLECHAR* str, unsigned int len)
297 if (*old!=NULL) {
298 DWORD newbytelen = len*sizeof(WCHAR);
299 DWORD *ptr = HeapReAlloc(GetProcessHeap(),0,((DWORD*)*old)-1,newbytelen+sizeof(WCHAR)+sizeof(DWORD));
300 *old = (BSTR)(ptr+1);
301 *ptr = newbytelen;
302 if (str) {
303 memcpy(*old, str, newbytelen);
304 (*old)[len] = 0;
305 } else {
306 /* Subtle hidden feature: The old string data is still there
307 * when 'in' is NULL!
308 * Some Microsoft program needs it.
311 } else {
313 * Allocate the new string
315 *old = SysAllocStringLen(str, len);
318 return 1;
321 /******************************************************************************
322 * SysAllocStringByteLen [OLEAUT32.150]
324 * Create a BSTR from an OLESTR of a given byte length.
326 * PARAMS
327 * str [I] Source to create BSTR from
328 * len [I] Length of oleStr in bytes
330 * RETURNS
331 * Success: A newly allocated BSTR
332 * Failure: NULL, if len is >= 0x80000000, or memory allocation fails.
334 * NOTES
335 * -If len is 0 or oleStr is NULL the resulting string is empty ("").
336 * -This function always NUL terminates the resulting BSTR.
337 * -oleStr may be either an LPCSTR or LPCOLESTR, since it is copied
338 * without checking for a terminating NUL.
339 * See BSTR.
341 BSTR WINAPI SysAllocStringByteLen(LPCSTR str, UINT len)
343 DWORD* newBuffer;
344 char* stringBuffer;
347 * Allocate a new buffer to hold the string.
348 * don't forget to keep an empty spot at the beginning of the
349 * buffer for the character count and an extra character at the
350 * end for the NULL.
352 newBuffer = HeapAlloc(GetProcessHeap(), 0,
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 * Registers an object in the global item table.
455 * PARAMS
456 * punk [I] Object to register.
457 * rcid [I] CLSID of the object.
458 * dwFlags [I] Flags.
459 * pdwRegister [O] Address to store cookie of object registration in.
461 * RETURNS
462 * Success: S_OK.
463 * Failure: HRESULT code.
465 HRESULT WINAPI RegisterActiveObject(
466 LPUNKNOWN punk,REFCLSID rcid,DWORD dwFlags,LPDWORD pdwRegister
468 WCHAR guidbuf[80];
469 HRESULT ret;
470 LPRUNNINGOBJECTTABLE runobtable;
471 LPMONIKER moniker;
473 StringFromGUID2(rcid,guidbuf,39);
474 ret = CreateItemMoniker(pdelimiter,guidbuf,&moniker);
475 if (FAILED(ret))
476 return ret;
477 ret = GetRunningObjectTable(0,&runobtable);
478 if (FAILED(ret)) {
479 IMoniker_Release(moniker);
480 return ret;
482 ret = IRunningObjectTable_Register(runobtable,dwFlags,punk,moniker,pdwRegister);
483 IRunningObjectTable_Release(runobtable);
484 IMoniker_Release(moniker);
485 return ret;
488 /***********************************************************************
489 * RevokeActiveObject (OLEAUT32.34)
491 * Revokes an object from the global item table.
493 * PARAMS
494 * xregister [I] Registration cookie.
495 * reserved [I] Reserved. Set to NULL.
497 * RETURNS
498 * Success: S_OK.
499 * Failure: HRESULT code.
501 HRESULT WINAPI RevokeActiveObject(DWORD xregister,LPVOID reserved)
503 LPRUNNINGOBJECTTABLE runobtable;
504 HRESULT ret;
506 ret = GetRunningObjectTable(0,&runobtable);
507 if (FAILED(ret)) return ret;
508 ret = IRunningObjectTable_Revoke(runobtable,xregister);
509 if (SUCCEEDED(ret)) ret = S_OK;
510 IRunningObjectTable_Release(runobtable);
511 return ret;
514 /***********************************************************************
515 * GetActiveObject (OLEAUT32.35)
517 * Gets an object from the global item table.
519 * PARAMS
520 * rcid [I] CLSID of the object.
521 * preserved [I] Reserved. Set to NULL.
522 * ppunk [O] Address to store object into.
524 * RETURNS
525 * Success: S_OK.
526 * Failure: HRESULT code.
528 HRESULT WINAPI GetActiveObject(REFCLSID rcid,LPVOID preserved,LPUNKNOWN *ppunk)
530 WCHAR guidbuf[80];
531 HRESULT ret;
532 LPRUNNINGOBJECTTABLE runobtable;
533 LPMONIKER moniker;
535 StringFromGUID2(rcid,guidbuf,39);
536 ret = CreateItemMoniker(pdelimiter,guidbuf,&moniker);
537 if (FAILED(ret))
538 return ret;
539 ret = GetRunningObjectTable(0,&runobtable);
540 if (FAILED(ret)) {
541 IMoniker_Release(moniker);
542 return ret;
544 ret = IRunningObjectTable_GetObject(runobtable,moniker,ppunk);
545 IRunningObjectTable_Release(runobtable);
546 IMoniker_Release(moniker);
547 return ret;
551 /***********************************************************************
552 * OaBuildVersion [OLEAUT32.170]
554 * Get the Ole Automation build version.
556 * PARAMS
557 * None
559 * RETURNS
560 * The build version.
562 * NOTES
563 * Known oleaut32.dll versions:
564 *| OLE Ver. Comments Date Build Ver.
565 *| -------- ------------------------- ---- ---------
566 *| OLE 2.1 NT 1993-95 10 3023
567 *| OLE 2.1 10 3027
568 *| Win32s Ver 1.1e 20 4049
569 *| OLE 2.20 W95/NT 1993-96 20 4112
570 *| OLE 2.20 W95/NT 1993-96 20 4118
571 *| OLE 2.20 W95/NT 1993-96 20 4122
572 *| OLE 2.30 W95/NT 1993-98 30 4265
573 *| OLE 2.40 NT?? 1993-98 40 4267
574 *| OLE 2.40 W98 SE orig. file 1993-98 40 4275
575 *| OLE 2.40 W2K orig. file 1993-XX 40 4514
577 * Currently the versions returned are 2.20 for Win3.1, 2.30 for Win95 & NT 3.51,
578 * and 2.40 for all later versions. The build number is maximum, i.e. 0xffff.
580 ULONG WINAPI OaBuildVersion()
582 switch(GetVersion() & 0x8000ffff) /* mask off build number */
584 case 0x80000a03: /* WIN31 */
585 return MAKELONG(0xffff, 20);
586 case 0x00003303: /* NT351 */
587 return MAKELONG(0xffff, 30);
588 case 0x80000004: /* WIN95; I'd like to use the "standard" w95 minor
589 version here (30), but as we still use w95
590 as default winver (which is good IMHO), I better
591 play safe and use the latest value for w95 for now.
592 Change this as soon as default winver gets changed
593 to something more recent */
594 case 0x80000a04: /* WIN98 */
595 case 0x00000004: /* NT40 */
596 case 0x00000005: /* W2K */
597 case 0x00000105: /* WinXP */
598 return MAKELONG(0xffff, 40);
599 default:
600 FIXME("Version value not known yet. Please investigate it !\n");
601 return MAKELONG(0xffff, 40); /* for now return the same value as for w2k */
605 /******************************************************************************
606 * OleTranslateColor [OLEAUT32.421]
608 * Convert an OLE_COLOR to a COLORREF.
610 * PARAMS
611 * clr [I] Color to convert
612 * hpal [I] Handle to a palette for the conversion
613 * pColorRef [O] Destination for converted color, or NULL to test if the conversion is ok
615 * RETURNS
616 * Success: S_OK. The conversion is ok, and pColorRef contains the converted color if non-NULL.
617 * Failure: E_INVALIDARG, if any argument is invalid.
619 * FIXME
620 * Document the conversion rules.
622 HRESULT WINAPI OleTranslateColor(
623 OLE_COLOR clr,
624 HPALETTE hpal,
625 COLORREF* pColorRef)
627 COLORREF colorref;
628 BYTE b = HIBYTE(HIWORD(clr));
630 TRACE("(%08lx, %p, %p):stub\n", clr, hpal, pColorRef);
633 * In case pColorRef is NULL, provide our own to simplify the code.
635 if (pColorRef == NULL)
636 pColorRef = &colorref;
638 switch (b)
640 case 0x00:
642 if (hpal != 0)
643 *pColorRef = PALETTERGB(GetRValue(clr),
644 GetGValue(clr),
645 GetBValue(clr));
646 else
647 *pColorRef = clr;
649 break;
652 case 0x01:
654 if (hpal != 0)
656 PALETTEENTRY pe;
658 * Validate the palette index.
660 if (GetPaletteEntries(hpal, LOWORD(clr), 1, &pe) == 0)
661 return E_INVALIDARG;
664 *pColorRef = clr;
666 break;
669 case 0x02:
670 *pColorRef = clr;
671 break;
673 case 0x80:
675 int index = LOBYTE(LOWORD(clr));
678 * Validate GetSysColor index.
680 if ((index < COLOR_SCROLLBAR) || (index > COLOR_MENUBAR))
681 return E_INVALIDARG;
683 *pColorRef = GetSysColor(index);
685 break;
688 default:
689 return E_INVALIDARG;
692 return S_OK;
695 extern HRESULT OLEAUTPS_DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv);
697 extern void _get_STDFONT_CF(LPVOID);
698 extern void _get_STDPIC_CF(LPVOID);
700 /***********************************************************************
701 * DllGetClassObject (OLEAUT32.1)
703 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
705 *ppv = NULL;
706 if (IsEqualGUID(rclsid,&CLSID_StdFont)) {
707 if (IsEqualGUID(iid,&IID_IClassFactory)) {
708 _get_STDFONT_CF(ppv);
709 IClassFactory_AddRef((IClassFactory*)*ppv);
710 return S_OK;
713 if (IsEqualGUID(rclsid,&CLSID_StdPicture)) {
714 if (IsEqualGUID(iid,&IID_IClassFactory)) {
715 _get_STDPIC_CF(ppv);
716 IClassFactory_AddRef((IClassFactory*)*ppv);
717 return S_OK;
720 if (IsEqualGUID(rclsid,&CLSID_PSDispatch)) {
721 return OLEAUTPS_DllGetClassObject(rclsid,iid,ppv);
723 if (IsEqualGUID(rclsid,&CLSID_PSOAInterface)) {
724 if (S_OK==TypeLibFac_DllGetClassObject(rclsid,iid,ppv))
725 return S_OK;
726 /*FALLTHROUGH*/
728 FIXME("\n\tCLSID:\t%s,\n\tIID:\t%s\n",debugstr_guid(rclsid),debugstr_guid(iid));
729 return CLASS_E_CLASSNOTAVAILABLE;
732 /***********************************************************************
733 * DllCanUnloadNow (OLEAUT32.410)
735 * Determine if this dll can be unloaded from the callers address space.
737 * PARAMS
738 * None.
740 * RETURNS
741 * Always returns S_FALSE. This dll cannot be unloaded.
743 HRESULT WINAPI DllCanUnloadNow(void)
745 return S_FALSE;
748 /*****************************************************************************
749 * DllMain [OLEAUT32.@]
751 BOOL WINAPI DllMain(HINSTANCE hInstDll, DWORD fdwReason, LPVOID lpvReserved)
753 TRACE("(%p,%lu,%p)\n", hInstDll, fdwReason, lpvReserved);
755 switch (fdwReason) {
756 case DLL_PROCESS_ATTACH:
757 DisableThreadLibraryCalls(hInstDll);
758 OLEAUT32_hModule = (HMODULE)hInstDll;
759 break;
760 case DLL_PROCESS_DETACH:
761 break;
764 return TRUE;