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
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 extern const GUID CLSID_PSDispatch;
46 extern const GUID CLSID_PSEnumVariant;
47 extern const GUID CLSID_PSTypeInfo;
48 extern const GUID CLSID_PSTypeLib;
49 extern const GUID CLSID_PSTypeComp;
51 static BOOL BSTR_bCache = TRUE; /* Cache allocations to minimise alloc calls? */
53 HMODULE OLEAUT32_hModule = NULL;
55 /******************************************************************************
59 * BSTR is a simple typedef for a wide-character string used as the principle
60 * string type in ole automation. When encapsulated in a Variant type they are
61 * automatically copied and destroyed as the variant is processed.
63 * The low level BSTR Api allows manipulation of these strings and is used by
64 * higher level Api calls to manage the strings transparently to the caller.
66 * Internally the BSTR type is allocated with space for a DWORD byte count before
67 * the string data begins. This is undocumented and non-system code should not
68 * access the count directly. Use SysStringLen() or SysStringByteLen()
69 * instead. Note that the byte count does not include the terminating NUL.
71 * To create a new BSTR, use SysAllocString(), SysAllocStringLen() or
72 * SysAllocStringByteLen(). To change the size of an existing BSTR, use SysReAllocString()
73 * or SysReAllocStringLen(). Finally to destroy a string use SysFreeString().
75 * BSTR's are cached by Ole Automation by default. To override this behaviour
76 * either set the environment variable 'OANOCACHE', or call SetOaNoCache().
79 * 'Inside OLE, second edition' by Kraig Brockshmidt.
82 /******************************************************************************
83 * SysStringLen [OLEAUT32.7]
85 * Get the allocated length of a BSTR in wide characters.
88 * str [I] BSTR to find the length of
91 * The allocated length of str, or 0 if str is NULL.
95 * The returned length may be different from the length of the string as
96 * calculated by lstrlenW(), since it returns the length that was used to
97 * allocate the string by SysAllocStringLen().
99 UINT WINAPI SysStringLen(BSTR str)
101 DWORD* bufferPointer;
105 * The length of the string (in bytes) is contained in a DWORD placed
106 * just before the BSTR pointer
108 bufferPointer = (DWORD*)str;
112 return (int)(*bufferPointer/sizeof(WCHAR));
115 /******************************************************************************
116 * SysStringByteLen [OLEAUT32.149]
118 * Get the allocated length of a BSTR in bytes.
121 * str [I] BSTR to find the length of
124 * The allocated length of str, or 0 if str is NULL.
127 * See SysStringLen(), BSTR().
129 UINT WINAPI SysStringByteLen(BSTR str)
131 DWORD* bufferPointer;
135 * The length of the string (in bytes) is contained in a DWORD placed
136 * just before the BSTR pointer
138 bufferPointer = (DWORD*)str;
142 return (int)(*bufferPointer);
145 /******************************************************************************
146 * SysAllocString [OLEAUT32.2]
148 * Create a BSTR from an OLESTR.
151 * str [I] Source to create BSTR from
154 * Success: A BSTR allocated with SysAllocStringLen().
155 * Failure: NULL, if oleStr is NULL.
159 * MSDN (October 2001) incorrectly states that NULL is returned if oleStr has
160 * a length of 0. Native Win32 and this implementation both return a valid
161 * empty BSTR in this case.
163 BSTR WINAPI SysAllocString(LPCOLESTR str)
167 /* Delegate this to the SysAllocStringLen32 method. */
168 return SysAllocStringLen(str, lstrlenW(str));
171 /******************************************************************************
172 * SysFreeString [OLEAUT32.6]
177 * str [I] BSTR to free.
184 * str may be NULL, in which case this function does nothing.
186 void WINAPI SysFreeString(BSTR str)
188 DWORD* bufferPointer;
190 /* NULL is a valid parameter */
194 * We have to be careful when we free a BSTR pointer, it points to
195 * the beginning of the string but it skips the byte count contained
198 bufferPointer = (DWORD*)str;
203 * Free the memory from its "real" origin.
205 HeapFree(GetProcessHeap(), 0, bufferPointer);
208 /******************************************************************************
209 * SysAllocStringLen [OLEAUT32.4]
211 * Create a BSTR from an OLESTR of a given wide character length.
214 * str [I] Source to create BSTR from
215 * len [I] Length of oleStr in wide characters
218 * Success: A newly allocated BSTR from SysAllocStringByteLen()
219 * Failure: NULL, if len is >= 0x80000000, or memory allocation fails.
222 * See BSTR(), SysAllocStringByteLen().
224 BSTR WINAPI SysAllocStringLen(const OLECHAR *str, unsigned int len)
231 * Find the length of the buffer passed-in in bytes.
233 bufferSize = len * sizeof (WCHAR);
236 * Allocate a new buffer to hold the string.
237 * don't forget to keep an empty spot at the beginning of the
238 * buffer for the character count and an extra character at the
241 newBuffer = HeapAlloc(GetProcessHeap(), 0,
242 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
245 * If the memory allocation failed, return a null pointer.
251 * Copy the length of the string in the placeholder.
253 *newBuffer = bufferSize;
256 * Skip the byte count.
261 * Copy the information in the buffer.
262 * Since it is valid to pass a NULL pointer here, we'll initialize the
263 * buffer to nul if it is the case.
266 memcpy(newBuffer, str, bufferSize);
268 memset(newBuffer, 0, bufferSize);
271 * Make sure that there is a nul character at the end of the
274 stringBuffer = (WCHAR*)newBuffer;
275 stringBuffer[len] = L'\0';
277 return (LPWSTR)stringBuffer;
280 /******************************************************************************
281 * SysReAllocStringLen [OLEAUT32.5]
283 * Change the length of a previously created BSTR.
286 * old [O] BSTR to change the length of
287 * str [I] New source for pbstr
288 * len [I] Length of oleStr in wide characters
291 * Success: 1. The size of pbstr is updated.
292 * Failure: 0, if len >= 0x80000000 or memory allocation fails.
295 * See BSTR(), SysAllocStringByteLen().
296 * *pbstr may be changed by this function.
298 int WINAPI SysReAllocStringLen(BSTR* old, const OLECHAR* str, unsigned int len)
301 DWORD newbytelen = len*sizeof(WCHAR);
302 DWORD *ptr = HeapReAlloc(GetProcessHeap(),0,((DWORD*)*old)-1,newbytelen+sizeof(WCHAR)+sizeof(DWORD));
303 *old = (BSTR)(ptr+1);
306 memcpy(*old, str, newbytelen);
309 /* Subtle hidden feature: The old string data is still there
311 * Some Microsoft program needs it.
316 * Allocate the new string
318 *old = SysAllocStringLen(str, len);
324 /******************************************************************************
325 * SysAllocStringByteLen [OLEAUT32.150]
327 * Create a BSTR from an OLESTR of a given byte length.
330 * str [I] Source to create BSTR from
331 * len [I] Length of oleStr in bytes
334 * Success: A newly allocated BSTR
335 * Failure: NULL, if len is >= 0x80000000, or memory allocation fails.
338 * -If len is 0 or oleStr is NULL the resulting string is empty ("").
339 * -This function always NUL terminates the resulting BSTR.
340 * -oleStr may be either an LPCSTR or LPCOLESTR, since it is copied
341 * without checking for a terminating NUL.
344 BSTR WINAPI SysAllocStringByteLen(LPCSTR str, UINT len)
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
355 newBuffer = HeapAlloc(GetProcessHeap(), 0,
356 len + sizeof(WCHAR) + sizeof(DWORD));
359 * If the memory allocation failed, return a null pointer.
365 * Copy the length of the string in the placeholder.
370 * Skip the byte count.
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.
380 memcpy(newBuffer, str, len);
383 * Make sure that there is a nul character at the end of the
386 stringBuffer = (char *)newBuffer;
387 stringBuffer[len] = 0;
388 stringBuffer[len+1] = 0;
390 return (LPWSTR)stringBuffer;
393 /******************************************************************************
394 * SysReAllocString [OLEAUT32.3]
396 * Change the length of a previously created BSTR.
399 * old [I/O] BSTR to change the length of
400 * str [I] New source for pbstr
407 * See BSTR(), SysAllocStringStringLen().
409 INT WINAPI SysReAllocString(LPBSTR old,LPCOLESTR str)
418 * Make sure we free the old string.
424 * Allocate the new string
426 *old = SysAllocString(str);
431 /******************************************************************************
432 * SetOaNoCache (OLEAUT32.327)
434 * Instruct Ole Automation not to cache BSTR allocations.
445 void WINAPI SetOaNoCache(void)
450 static WCHAR _delimiter[2] = {'!',0}; /* default delimiter apparently */
451 static WCHAR *pdelimiter = &_delimiter[0];
453 /***********************************************************************
454 * RegisterActiveObject (OLEAUT32.33)
456 * Registers an object in the global item table.
459 * punk [I] Object to register.
460 * rcid [I] CLSID of the object.
462 * pdwRegister [O] Address to store cookie of object registration in.
466 * Failure: HRESULT code.
468 HRESULT WINAPI RegisterActiveObject(
469 LPUNKNOWN punk,REFCLSID rcid,DWORD dwFlags,LPDWORD pdwRegister
473 LPRUNNINGOBJECTTABLE runobtable;
476 StringFromGUID2(rcid,guidbuf,39);
477 ret = CreateItemMoniker(pdelimiter,guidbuf,&moniker);
480 ret = GetRunningObjectTable(0,&runobtable);
482 IMoniker_Release(moniker);
485 ret = IRunningObjectTable_Register(runobtable,dwFlags,punk,moniker,pdwRegister);
486 IRunningObjectTable_Release(runobtable);
487 IMoniker_Release(moniker);
491 /***********************************************************************
492 * RevokeActiveObject (OLEAUT32.34)
494 * Revokes an object from the global item table.
497 * xregister [I] Registration cookie.
498 * reserved [I] Reserved. Set to NULL.
502 * Failure: HRESULT code.
504 HRESULT WINAPI RevokeActiveObject(DWORD xregister,LPVOID reserved)
506 LPRUNNINGOBJECTTABLE runobtable;
509 ret = GetRunningObjectTable(0,&runobtable);
510 if (FAILED(ret)) return ret;
511 ret = IRunningObjectTable_Revoke(runobtable,xregister);
512 if (SUCCEEDED(ret)) ret = S_OK;
513 IRunningObjectTable_Release(runobtable);
517 /***********************************************************************
518 * GetActiveObject (OLEAUT32.35)
520 * Gets an object from the global item table.
523 * rcid [I] CLSID of the object.
524 * preserved [I] Reserved. Set to NULL.
525 * ppunk [O] Address to store object into.
529 * Failure: HRESULT code.
531 HRESULT WINAPI GetActiveObject(REFCLSID rcid,LPVOID preserved,LPUNKNOWN *ppunk)
535 LPRUNNINGOBJECTTABLE runobtable;
538 StringFromGUID2(rcid,guidbuf,39);
539 ret = CreateItemMoniker(pdelimiter,guidbuf,&moniker);
542 ret = GetRunningObjectTable(0,&runobtable);
544 IMoniker_Release(moniker);
547 ret = IRunningObjectTable_GetObject(runobtable,moniker,ppunk);
548 IRunningObjectTable_Release(runobtable);
549 IMoniker_Release(moniker);
554 /***********************************************************************
555 * OaBuildVersion [OLEAUT32.170]
557 * Get the Ole Automation build version.
566 * Known oleaut32.dll versions:
567 *| OLE Ver. Comments Date Build Ver.
568 *| -------- ------------------------- ---- ---------
569 *| OLE 2.1 NT 1993-95 10 3023
571 *| Win32s Ver 1.1e 20 4049
572 *| OLE 2.20 W95/NT 1993-96 20 4112
573 *| OLE 2.20 W95/NT 1993-96 20 4118
574 *| OLE 2.20 W95/NT 1993-96 20 4122
575 *| OLE 2.30 W95/NT 1993-98 30 4265
576 *| OLE 2.40 NT?? 1993-98 40 4267
577 *| OLE 2.40 W98 SE orig. file 1993-98 40 4275
578 *| OLE 2.40 W2K orig. file 1993-XX 40 4514
580 * Currently the versions returned are 2.20 for Win3.1, 2.30 for Win95 & NT 3.51,
581 * and 2.40 for all later versions. The build number is maximum, i.e. 0xffff.
583 ULONG WINAPI OaBuildVersion()
585 switch(GetVersion() & 0x8000ffff) /* mask off build number */
587 case 0x80000a03: /* WIN31 */
588 return MAKELONG(0xffff, 20);
589 case 0x00003303: /* NT351 */
590 return MAKELONG(0xffff, 30);
591 case 0x80000004: /* WIN95; I'd like to use the "standard" w95 minor
592 version here (30), but as we still use w95
593 as default winver (which is good IMHO), I better
594 play safe and use the latest value for w95 for now.
595 Change this as soon as default winver gets changed
596 to something more recent */
597 case 0x80000a04: /* WIN98 */
598 case 0x00000004: /* NT40 */
599 case 0x00000005: /* W2K */
600 case 0x00000105: /* WinXP */
601 return MAKELONG(0xffff, 40);
603 FIXME("Version value not known yet. Please investigate it !\n");
604 return MAKELONG(0xffff, 40); /* for now return the same value as for w2k */
608 /******************************************************************************
609 * OleTranslateColor [OLEAUT32.421]
611 * Convert an OLE_COLOR to a COLORREF.
614 * clr [I] Color to convert
615 * hpal [I] Handle to a palette for the conversion
616 * pColorRef [O] Destination for converted color, or NULL to test if the conversion is ok
619 * Success: S_OK. The conversion is ok, and pColorRef contains the converted color if non-NULL.
620 * Failure: E_INVALIDARG, if any argument is invalid.
623 * Document the conversion rules.
625 HRESULT WINAPI OleTranslateColor(
631 BYTE b = HIBYTE(HIWORD(clr));
633 TRACE("(%08lx, %p, %p):stub\n", clr, hpal, pColorRef);
636 * In case pColorRef is NULL, provide our own to simplify the code.
638 if (pColorRef == NULL)
639 pColorRef = &colorref;
646 *pColorRef = PALETTERGB(GetRValue(clr),
661 * Validate the palette index.
663 if (GetPaletteEntries(hpal, LOWORD(clr), 1, &pe) == 0)
678 int index = LOBYTE(LOWORD(clr));
681 * Validate GetSysColor index.
683 if ((index < COLOR_SCROLLBAR) || (index > COLOR_MENUBAR))
686 *pColorRef = GetSysColor(index);
698 extern HRESULT OLEAUTPS_DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv);
700 extern void _get_STDFONT_CF(LPVOID);
701 extern void _get_STDPIC_CF(LPVOID);
703 /***********************************************************************
704 * DllGetClassObject (OLEAUT32.1)
706 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
709 if (IsEqualGUID(rclsid,&CLSID_StdFont)) {
710 if (IsEqualGUID(iid,&IID_IClassFactory)) {
711 _get_STDFONT_CF(ppv);
712 IClassFactory_AddRef((IClassFactory*)*ppv);
716 if (IsEqualGUID(rclsid,&CLSID_StdPicture)) {
717 if (IsEqualGUID(iid,&IID_IClassFactory)) {
719 IClassFactory_AddRef((IClassFactory*)*ppv);
723 if (IsEqualCLSID(rclsid, &CLSID_PSDispatch) ||
724 IsEqualCLSID(rclsid, &CLSID_PSTypeInfo) ||
725 IsEqualCLSID(rclsid, &CLSID_PSTypeLib) ||
726 IsEqualCLSID(rclsid, &CLSID_PSEnumVariant)) {
727 return OLEAUTPS_DllGetClassObject(&CLSID_PSDispatch, iid, ppv);
729 if (IsEqualGUID(rclsid,&CLSID_PSOAInterface)) {
730 if (S_OK==TypeLibFac_DllGetClassObject(rclsid,iid,ppv))
734 FIXME("\n\tCLSID:\t%s,\n\tIID:\t%s\n",debugstr_guid(rclsid),debugstr_guid(iid));
735 return CLASS_E_CLASSNOTAVAILABLE;
738 /***********************************************************************
739 * DllCanUnloadNow (OLEAUT32.410)
741 * Determine if this dll can be unloaded from the callers address space.
747 * Always returns S_FALSE. This dll cannot be unloaded.
749 HRESULT WINAPI DllCanUnloadNow(void)
754 /*****************************************************************************
755 * DllMain [OLEAUT32.@]
757 BOOL WINAPI DllMain(HINSTANCE hInstDll, DWORD fdwReason, LPVOID lpvReserved)
759 TRACE("(%p,%lu,%p)\n", hInstDll, fdwReason, lpvReserved);
762 case DLL_PROCESS_ATTACH:
763 DisableThreadLibraryCalls(hInstDll);
764 OLEAUT32_hModule = (HMODULE)hInstDll;
766 case DLL_PROCESS_DETACH: