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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(ole);
41 static BOOL BSTR_bCache = TRUE; /* Cache allocations to minimise alloc calls? */
43 HMODULE OLEAUT32_hModule = NULL;
45 /******************************************************************************
49 * BSTR is a simple typedef for a wide-character string used as the principle
50 * string type in ole automation. When encapsulated in a Variant type they are
51 * automatically copied and destroyed as the variant is processed.
53 * The low level BSTR Api allows manipulation of these strings and is used by
54 * higher level Api calls to manage the strings transparently to the caller.
56 * Internally the BSTR type is allocated with space for a DWORD byte count before
57 * the string data begins. This is undocumented and non-system code should not
58 * access the count directly. Use SysStringLen() or SysStringByteLen()
59 * instead. Note that the byte count does not include the terminating NUL.
61 * To create a new BSTR, use SysAllocString(), SysAllocStringLen() or
62 * SysAllocStringByteLen(). To change the size of an existing BSTR, use SysReAllocString()
63 * or SysReAllocStringLen(). Finally to destroy a string use SysFreeString().
65 * BSTR's are cached by Ole Automation by default. To override this behaviour
66 * either set the environment variable 'OANOCACHE', or call SetOaNoCache().
69 * 'Inside OLE, second edition' by Kraig Brockshmidt.
72 /******************************************************************************
73 * SysStringLen [OLEAUT32.7]
75 * Get the allocated length of a BSTR in wide characters.
78 * str [I] BSTR to find the length of
81 * The allocated length of str, or 0 if str is NULL.
85 * The returned length may be different from the length of the string as
86 * calculated by lstrlenW(), since it returns the length that was used to
87 * allocate the string by SysAllocStringLen().
89 UINT WINAPI SysStringLen(BSTR str)
95 * The length of the string (in bytes) is contained in a DWORD placed
96 * just before the BSTR pointer
98 bufferPointer = (DWORD*)str;
102 return (int)(*bufferPointer/sizeof(WCHAR));
105 /******************************************************************************
106 * SysStringByteLen [OLEAUT32.149]
108 * Get the allocated length of a BSTR in bytes.
111 * str [I] BSTR to find the length of
114 * The allocated length of str, or 0 if str is NULL.
117 * See SysStringLen(), BSTR().
119 UINT WINAPI SysStringByteLen(BSTR str)
121 DWORD* bufferPointer;
125 * The length of the string (in bytes) is contained in a DWORD placed
126 * just before the BSTR pointer
128 bufferPointer = (DWORD*)str;
132 return (int)(*bufferPointer);
135 /******************************************************************************
136 * SysAllocString [OLEAUT32.2]
138 * Create a BSTR from an OLESTR.
141 * str [I] Source to create BSTR from
144 * Success: A BSTR allocated with SysAllocStringLen().
145 * Failure: NULL, if oleStr is NULL.
149 * MSDN (October 2001) incorrectly states that NULL is returned if oleStr has
150 * a length of 0. Native Win32 and this implementation both return a valid
151 * empty BSTR in this case.
153 BSTR WINAPI SysAllocString(LPCOLESTR str)
157 /* Delegate this to the SysAllocStringLen32 method. */
158 return SysAllocStringLen(str, lstrlenW(str));
161 /******************************************************************************
162 * SysFreeString [OLEAUT32.6]
167 * str [I] BSTR to free.
174 * str may be NULL, in which case this function does nothing.
176 void WINAPI SysFreeString(BSTR str)
178 DWORD* bufferPointer;
180 /* NULL is a valid parameter */
184 * We have to be careful when we free a BSTR pointer, it points to
185 * the beginning of the string but it skips the byte count contained
188 bufferPointer = (DWORD*)str;
193 * Free the memory from its "real" origin.
195 HeapFree(GetProcessHeap(), 0, bufferPointer);
198 /******************************************************************************
199 * SysAllocStringLen [OLEAUT32.4]
201 * Create a BSTR from an OLESTR of a given wide character length.
204 * str [I] Source to create BSTR from
205 * len [I] Length of oleStr in wide characters
208 * Success: A newly allocated BSTR from SysAllocStringByteLen()
209 * Failure: NULL, if len is >= 0x80000000, or memory allocation fails.
212 * See BSTR(), SysAllocStringByteLen().
214 BSTR WINAPI SysAllocStringLen(const OLECHAR *str, unsigned int len)
221 * Find the length of the buffer passed-in in bytes.
223 bufferSize = len * sizeof (WCHAR);
226 * Allocate a new buffer to hold the string.
227 * don't forget to keep an empty spot at the beginning of the
228 * buffer for the character count and an extra character at the
231 newBuffer = HeapAlloc(GetProcessHeap(), 0,
232 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
235 * If the memory allocation failed, return a null pointer.
241 * Copy the length of the string in the placeholder.
243 *newBuffer = bufferSize;
246 * Skip the byte count.
251 * Copy the information in the buffer.
252 * Since it is valid to pass a NULL pointer here, we'll initialize the
253 * buffer to nul if it is the case.
256 memcpy(newBuffer, str, bufferSize);
258 memset(newBuffer, 0, bufferSize);
261 * Make sure that there is a nul character at the end of the
264 stringBuffer = (WCHAR*)newBuffer;
265 stringBuffer[len] = L'\0';
267 return (LPWSTR)stringBuffer;
270 /******************************************************************************
271 * SysReAllocStringLen [OLEAUT32.5]
273 * Change the length of a previously created BSTR.
276 * old [O] BSTR to change the length of
277 * str [I] New source for pbstr
278 * len [I] Length of oleStr in wide characters
281 * Success: 1. The size of pbstr is updated.
282 * Failure: 0, if len >= 0x80000000 or memory allocation fails.
285 * See BSTR(), SysAllocStringByteLen().
286 * *pbstr may be changed by this function.
288 int WINAPI SysReAllocStringLen(BSTR* old, const OLECHAR* str, unsigned int len)
291 DWORD newbytelen = len*sizeof(WCHAR);
292 DWORD *ptr = HeapReAlloc(GetProcessHeap(),0,((DWORD*)*old)-1,newbytelen+sizeof(WCHAR)+sizeof(DWORD));
293 *old = (BSTR)(ptr+1);
296 memcpy(*old, str, newbytelen);
299 /* Subtle hidden feature: The old string data is still there
301 * Some Microsoft program needs it.
306 * Allocate the new string
308 *old = SysAllocStringLen(str, len);
314 /******************************************************************************
315 * SysAllocStringByteLen [OLEAUT32.150]
317 * Create a BSTR from an OLESTR of a given byte length.
320 * str [I] Source to create BSTR from
321 * len [I] Length of oleStr in bytes
324 * Success: A newly allocated BSTR
325 * Failure: NULL, if len is >= 0x80000000, or memory allocation fails.
328 * -If len is 0 or oleStr is NULL the resulting string is empty ("").
329 * -This function always NUL terminates the resulting BSTR.
330 * -oleStr may be either an LPCSTR or LPCOLESTR, since it is copied
331 * without checking for a terminating NUL.
334 BSTR WINAPI SysAllocStringByteLen(LPCSTR str, UINT len)
340 * Allocate a new buffer to hold the string.
341 * don't forget to keep an empty spot at the beginning of the
342 * buffer for the character count and an extra character at the
345 newBuffer = HeapAlloc(GetProcessHeap(), 0,
346 len + sizeof(WCHAR) + sizeof(DWORD));
349 * If the memory allocation failed, return a null pointer.
355 * Copy the length of the string in the placeholder.
360 * Skip the byte count.
365 * Copy the information in the buffer.
366 * Since it is valid to pass a NULL pointer here, we'll initialize the
367 * buffer to nul if it is the case.
370 memcpy(newBuffer, str, len);
373 * Make sure that there is a nul character at the end of the
376 stringBuffer = (char *)newBuffer;
377 stringBuffer[len] = 0;
378 stringBuffer[len+1] = 0;
380 return (LPWSTR)stringBuffer;
383 /******************************************************************************
384 * SysReAllocString [OLEAUT32.3]
386 * Change the length of a previously created BSTR.
389 * old [I/O] BSTR to change the length of
390 * str [I] New source for pbstr
397 * See BSTR(), SysAllocStringStringLen().
399 INT WINAPI SysReAllocString(LPBSTR old,LPCOLESTR str)
408 * Make sure we free the old string.
414 * Allocate the new string
416 *old = SysAllocString(str);
421 /******************************************************************************
422 * SetOaNoCache (OLEAUT32.327)
424 * Instruct Ole Automation not to cache BSTR allocations.
435 void WINAPI SetOaNoCache(void)
440 static const WCHAR _delimiter[2] = {'!',0}; /* default delimiter apparently */
441 static const WCHAR *pdelimiter = &_delimiter[0];
443 /***********************************************************************
444 * RegisterActiveObject (OLEAUT32.33)
446 * Registers an object in the global item table.
449 * punk [I] Object to register.
450 * rcid [I] CLSID of the object.
452 * pdwRegister [O] Address to store cookie of object registration in.
456 * Failure: HRESULT code.
458 HRESULT WINAPI RegisterActiveObject(
459 LPUNKNOWN punk,REFCLSID rcid,DWORD dwFlags,LPDWORD pdwRegister
463 LPRUNNINGOBJECTTABLE runobtable;
466 StringFromGUID2(rcid,guidbuf,39);
467 ret = CreateItemMoniker(pdelimiter,guidbuf,&moniker);
470 ret = GetRunningObjectTable(0,&runobtable);
472 IMoniker_Release(moniker);
475 ret = IRunningObjectTable_Register(runobtable,dwFlags,punk,moniker,pdwRegister);
476 IRunningObjectTable_Release(runobtable);
477 IMoniker_Release(moniker);
481 /***********************************************************************
482 * RevokeActiveObject (OLEAUT32.34)
484 * Revokes an object from the global item table.
487 * xregister [I] Registration cookie.
488 * reserved [I] Reserved. Set to NULL.
492 * Failure: HRESULT code.
494 HRESULT WINAPI RevokeActiveObject(DWORD xregister,LPVOID reserved)
496 LPRUNNINGOBJECTTABLE runobtable;
499 ret = GetRunningObjectTable(0,&runobtable);
500 if (FAILED(ret)) return ret;
501 ret = IRunningObjectTable_Revoke(runobtable,xregister);
502 if (SUCCEEDED(ret)) ret = S_OK;
503 IRunningObjectTable_Release(runobtable);
507 /***********************************************************************
508 * GetActiveObject (OLEAUT32.35)
510 * Gets an object from the global item table.
513 * rcid [I] CLSID of the object.
514 * preserved [I] Reserved. Set to NULL.
515 * ppunk [O] Address to store object into.
519 * Failure: HRESULT code.
521 HRESULT WINAPI GetActiveObject(REFCLSID rcid,LPVOID preserved,LPUNKNOWN *ppunk)
525 LPRUNNINGOBJECTTABLE runobtable;
528 StringFromGUID2(rcid,guidbuf,39);
529 ret = CreateItemMoniker(pdelimiter,guidbuf,&moniker);
532 ret = GetRunningObjectTable(0,&runobtable);
534 IMoniker_Release(moniker);
537 ret = IRunningObjectTable_GetObject(runobtable,moniker,ppunk);
538 IRunningObjectTable_Release(runobtable);
539 IMoniker_Release(moniker);
544 /***********************************************************************
545 * OaBuildVersion [OLEAUT32.170]
547 * Get the Ole Automation build version.
556 * Known oleaut32.dll versions:
557 *| OLE Ver. Comments Date Build Ver.
558 *| -------- ------------------------- ---- ---------
559 *| OLE 2.1 NT 1993-95 10 3023
561 *| Win32s Ver 1.1e 20 4049
562 *| OLE 2.20 W95/NT 1993-96 20 4112
563 *| OLE 2.20 W95/NT 1993-96 20 4118
564 *| OLE 2.20 W95/NT 1993-96 20 4122
565 *| OLE 2.30 W95/NT 1993-98 30 4265
566 *| OLE 2.40 NT?? 1993-98 40 4267
567 *| OLE 2.40 W98 SE orig. file 1993-98 40 4275
568 *| OLE 2.40 W2K orig. file 1993-XX 40 4514
570 * Currently the versions returned are 2.20 for Win3.1, 2.30 for Win95 & NT 3.51,
571 * and 2.40 for all later versions. The build number is maximum, i.e. 0xffff.
573 ULONG WINAPI OaBuildVersion()
575 switch(GetVersion() & 0x8000ffff) /* mask off build number */
577 case 0x80000a03: /* WIN31 */
578 return MAKELONG(0xffff, 20);
579 case 0x00003303: /* NT351 */
580 return MAKELONG(0xffff, 30);
581 case 0x80000004: /* WIN95; I'd like to use the "standard" w95 minor
582 version here (30), but as we still use w95
583 as default winver (which is good IMHO), I better
584 play safe and use the latest value for w95 for now.
585 Change this as soon as default winver gets changed
586 to something more recent */
587 case 0x80000a04: /* WIN98 */
588 case 0x00000004: /* NT40 */
589 case 0x00000005: /* W2K */
590 case 0x00000105: /* WinXP */
591 return MAKELONG(0xffff, 40);
593 FIXME("Version value not known yet. Please investigate it !\n");
594 return MAKELONG(0xffff, 40); /* for now return the same value as for w2k */
598 /******************************************************************************
599 * OleTranslateColor [OLEAUT32.421]
601 * Convert an OLE_COLOR to a COLORREF.
604 * clr [I] Color to convert
605 * hpal [I] Handle to a palette for the conversion
606 * pColorRef [O] Destination for converted color, or NULL to test if the conversion is ok
609 * Success: S_OK. The conversion is ok, and pColorRef contains the converted color if non-NULL.
610 * Failure: E_INVALIDARG, if any argument is invalid.
613 * Document the conversion rules.
615 HRESULT WINAPI OleTranslateColor(
621 BYTE b = HIBYTE(HIWORD(clr));
623 TRACE("(%08lx, %p, %p)\n", clr, hpal, pColorRef);
626 * In case pColorRef is NULL, provide our own to simplify the code.
628 if (pColorRef == NULL)
629 pColorRef = &colorref;
636 *pColorRef = PALETTERGB(GetRValue(clr),
651 * Validate the palette index.
653 if (GetPaletteEntries(hpal, LOWORD(clr), 1, &pe) == 0)
668 int index = LOBYTE(LOWORD(clr));
671 * Validate GetSysColor index.
673 if ((index < COLOR_SCROLLBAR) || (index > COLOR_MENUBAR))
676 *pColorRef = GetSysColor(index);
688 extern HRESULT OLEAUTPS_DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv);
690 extern void _get_STDFONT_CF(LPVOID);
691 extern void _get_STDPIC_CF(LPVOID);
693 static HRESULT WINAPI PSDispatchFacBuf_QueryInterface(IPSFactoryBuffer *iface, REFIID riid, void **ppv)
695 if (IsEqualIID(riid, &IID_IUnknown) ||
696 IsEqualIID(riid, &IID_IPSFactoryBuffer))
698 IUnknown_AddRef(iface);
699 *ppv = (void *)iface;
702 return E_NOINTERFACE;
705 static ULONG WINAPI PSDispatchFacBuf_AddRef(IPSFactoryBuffer *iface)
710 static ULONG WINAPI PSDispatchFacBuf_Release(IPSFactoryBuffer *iface)
715 static HRESULT WINAPI PSDispatchFacBuf_CreateProxy(IPSFactoryBuffer *iface, IUnknown *pUnkOuter, REFIID riid, IRpcProxyBuffer **ppProxy, void **ppv)
717 IPSFactoryBuffer *pPSFB;
720 if (IsEqualIID(riid, &IID_IDispatch))
721 hr = OLEAUTPS_DllGetClassObject(&CLSID_PSDispatch, &IID_IPSFactoryBuffer, (void **)&pPSFB);
723 hr = TMARSHAL_DllGetClassObject(&CLSID_PSOAInterface, &IID_IPSFactoryBuffer, (void **)&pPSFB);
725 if (FAILED(hr)) return hr;
727 hr = IPSFactoryBuffer_CreateProxy(pPSFB, pUnkOuter, riid, ppProxy, ppv);
729 IPSFactoryBuffer_Release(pPSFB);
733 static HRESULT WINAPI PSDispatchFacBuf_CreateStub(IPSFactoryBuffer *iface, REFIID riid, IUnknown *pUnkOuter, IRpcStubBuffer **ppStub)
735 IPSFactoryBuffer *pPSFB;
738 if (IsEqualIID(riid, &IID_IDispatch))
739 hr = OLEAUTPS_DllGetClassObject(&CLSID_PSDispatch, &IID_IPSFactoryBuffer, (void **)&pPSFB);
741 hr = TMARSHAL_DllGetClassObject(&CLSID_PSOAInterface, &IID_IPSFactoryBuffer, (void **)&pPSFB);
743 if (FAILED(hr)) return hr;
745 hr = IPSFactoryBuffer_CreateStub(pPSFB, riid, pUnkOuter, ppStub);
747 IPSFactoryBuffer_Release(pPSFB);
751 static const IPSFactoryBufferVtbl PSDispatchFacBuf_Vtbl =
753 PSDispatchFacBuf_QueryInterface,
754 PSDispatchFacBuf_AddRef,
755 PSDispatchFacBuf_Release,
756 PSDispatchFacBuf_CreateProxy,
757 PSDispatchFacBuf_CreateStub
760 /* This is the whole PSFactoryBuffer object, just the vtableptr */
761 static const IPSFactoryBufferVtbl *pPSDispatchFacBuf = &PSDispatchFacBuf_Vtbl;
763 /***********************************************************************
764 * DllGetClassObject (OLEAUT32.@)
766 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
769 if (IsEqualGUID(rclsid,&CLSID_StdFont)) {
770 if (IsEqualGUID(iid,&IID_IClassFactory)) {
771 _get_STDFONT_CF(ppv);
772 IClassFactory_AddRef((IClassFactory*)*ppv);
776 if (IsEqualGUID(rclsid,&CLSID_StdPicture)) {
777 if (IsEqualGUID(iid,&IID_IClassFactory)) {
779 IClassFactory_AddRef((IClassFactory*)*ppv);
783 if (IsEqualCLSID(rclsid, &CLSID_PSTypeInfo) ||
784 IsEqualCLSID(rclsid, &CLSID_PSTypeLib) ||
785 IsEqualCLSID(rclsid, &CLSID_PSEnumVariant)) {
786 return OLEAUTPS_DllGetClassObject(&CLSID_PSDispatch, iid, ppv);
788 if (IsEqualCLSID(rclsid, &CLSID_PSDispatch) && IsEqualIID(iid, &IID_IPSFactoryBuffer)) {
789 *ppv = &pPSDispatchFacBuf;
790 IPSFactoryBuffer_AddRef((IPSFactoryBuffer *)*ppv);
793 if (IsEqualGUID(rclsid,&CLSID_PSOAInterface)) {
794 if (S_OK==TMARSHAL_DllGetClassObject(rclsid,iid,ppv))
798 FIXME("\n\tCLSID:\t%s,\n\tIID:\t%s\n",debugstr_guid(rclsid),debugstr_guid(iid));
799 return CLASS_E_CLASSNOTAVAILABLE;
802 /***********************************************************************
803 * DllCanUnloadNow (OLEAUT32.@)
805 * Determine if this dll can be unloaded from the callers address space.
811 * Always returns S_FALSE. This dll cannot be unloaded.
813 HRESULT WINAPI DllCanUnloadNow(void)
818 /*****************************************************************************
819 * DllMain [OLEAUT32.@]
821 BOOL WINAPI DllMain(HINSTANCE hInstDll, DWORD fdwReason, LPVOID lpvReserved)
823 TRACE("(%p,%ld,%p)\n", hInstDll, fdwReason, lpvReserved);
826 case DLL_PROCESS_ATTACH:
827 DisableThreadLibraryCalls(hInstDll);
828 OLEAUT32_hModule = (HMODULE)hInstDll;
830 case DLL_PROCESS_DETACH: