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
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(ole);
41 /* The OLE Automation ProxyStub Interface Class (aka Typelib Marshaler) */
42 extern const GUID CLSID_PSOAInterface;
44 extern const GUID CLSID_PSDispatch;
45 extern const GUID CLSID_PSEnumVariant;
46 extern const GUID CLSID_PSTypeInfo;
47 extern const GUID CLSID_PSTypeLib;
48 extern const GUID CLSID_PSTypeComp;
50 static BOOL BSTR_bCache = TRUE; /* Cache allocations to minimise alloc calls? */
52 HMODULE OLEAUT32_hModule = NULL;
54 /******************************************************************************
58 * BSTR is a simple typedef for a wide-character string used as the principle
59 * string type in ole automation. When encapsulated in a Variant type they are
60 * automatically copied and destroyed as the variant is processed.
62 * The low level BSTR Api allows manipulation of these strings and is used by
63 * higher level Api calls to manage the strings transparently to the caller.
65 * Internally the BSTR type is allocated with space for a DWORD byte count before
66 * the string data begins. This is undocumented and non-system code should not
67 * access the count directly. Use SysStringLen() or SysStringByteLen()
68 * instead. Note that the byte count does not include the terminating NUL.
70 * To create a new BSTR, use SysAllocString(), SysAllocStringLen() or
71 * SysAllocStringByteLen(). To change the size of an existing BSTR, use SysReAllocString()
72 * or SysReAllocStringLen(). Finally to destroy a string use SysFreeString().
74 * BSTR's are cached by Ole Automation by default. To override this behaviour
75 * either set the environment variable 'OANOCACHE', or call SetOaNoCache().
78 * 'Inside OLE, second edition' by Kraig Brockshmidt.
81 /******************************************************************************
82 * SysStringLen [OLEAUT32.7]
84 * Get the allocated length of a BSTR in wide characters.
87 * str [I] BSTR to find the length of
90 * The allocated length of str, or 0 if str is NULL.
94 * The returned length may be different from the length of the string as
95 * calculated by lstrlenW(), since it returns the length that was used to
96 * allocate the string by SysAllocStringLen().
98 UINT WINAPI SysStringLen(BSTR str)
100 DWORD* bufferPointer;
104 * The length of the string (in bytes) is contained in a DWORD placed
105 * just before the BSTR pointer
107 bufferPointer = (DWORD*)str;
111 return (int)(*bufferPointer/sizeof(WCHAR));
114 /******************************************************************************
115 * SysStringByteLen [OLEAUT32.149]
117 * Get the allocated length of a BSTR in bytes.
120 * str [I] BSTR to find the length of
123 * The allocated length of str, or 0 if str is NULL.
126 * See SysStringLen(), BSTR().
128 UINT WINAPI SysStringByteLen(BSTR str)
130 DWORD* bufferPointer;
134 * The length of the string (in bytes) is contained in a DWORD placed
135 * just before the BSTR pointer
137 bufferPointer = (DWORD*)str;
141 return (int)(*bufferPointer);
144 /******************************************************************************
145 * SysAllocString [OLEAUT32.2]
147 * Create a BSTR from an OLESTR.
150 * str [I] Source to create BSTR from
153 * Success: A BSTR allocated with SysAllocStringLen().
154 * Failure: NULL, if oleStr is NULL.
158 * MSDN (October 2001) incorrectly states that NULL is returned if oleStr has
159 * a length of 0. Native Win32 and this implementation both return a valid
160 * empty BSTR in this case.
162 BSTR WINAPI SysAllocString(LPCOLESTR str)
166 /* Delegate this to the SysAllocStringLen32 method. */
167 return SysAllocStringLen(str, lstrlenW(str));
170 /******************************************************************************
171 * SysFreeString [OLEAUT32.6]
176 * str [I] BSTR to free.
183 * str may be NULL, in which case this function does nothing.
185 void WINAPI SysFreeString(BSTR str)
187 DWORD* bufferPointer;
189 /* NULL is a valid parameter */
193 * We have to be careful when we free a BSTR pointer, it points to
194 * the beginning of the string but it skips the byte count contained
197 bufferPointer = (DWORD*)str;
202 * Free the memory from its "real" origin.
204 HeapFree(GetProcessHeap(), 0, bufferPointer);
207 /******************************************************************************
208 * SysAllocStringLen [OLEAUT32.4]
210 * Create a BSTR from an OLESTR of a given wide character length.
213 * str [I] Source to create BSTR from
214 * len [I] Length of oleStr in wide characters
217 * Success: A newly allocated BSTR from SysAllocStringByteLen()
218 * Failure: NULL, if len is >= 0x80000000, or memory allocation fails.
221 * See BSTR(), SysAllocStringByteLen().
223 BSTR WINAPI SysAllocStringLen(const OLECHAR *str, unsigned int len)
230 * Find the length of the buffer passed-in in bytes.
232 bufferSize = len * sizeof (WCHAR);
235 * Allocate a new buffer to hold the string.
236 * don't forget to keep an empty spot at the beginning of the
237 * buffer for the character count and an extra character at the
240 newBuffer = HeapAlloc(GetProcessHeap(), 0,
241 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
244 * If the memory allocation failed, return a null pointer.
250 * Copy the length of the string in the placeholder.
252 *newBuffer = bufferSize;
255 * Skip the byte count.
260 * Copy the information in the buffer.
261 * Since it is valid to pass a NULL pointer here, we'll initialize the
262 * buffer to nul if it is the case.
265 memcpy(newBuffer, str, bufferSize);
267 memset(newBuffer, 0, bufferSize);
270 * Make sure that there is a nul character at the end of the
273 stringBuffer = (WCHAR*)newBuffer;
274 stringBuffer[len] = L'\0';
276 return (LPWSTR)stringBuffer;
279 /******************************************************************************
280 * SysReAllocStringLen [OLEAUT32.5]
282 * Change the length of a previously created BSTR.
285 * old [O] BSTR to change the length of
286 * str [I] New source for pbstr
287 * len [I] Length of oleStr in wide characters
290 * Success: 1. The size of pbstr is updated.
291 * Failure: 0, if len >= 0x80000000 or memory allocation fails.
294 * See BSTR(), SysAllocStringByteLen().
295 * *pbstr may be changed by this function.
297 int WINAPI SysReAllocStringLen(BSTR* old, const OLECHAR* str, unsigned int len)
300 DWORD newbytelen = len*sizeof(WCHAR);
301 DWORD *ptr = HeapReAlloc(GetProcessHeap(),0,((DWORD*)*old)-1,newbytelen+sizeof(WCHAR)+sizeof(DWORD));
302 *old = (BSTR)(ptr+1);
305 memcpy(*old, str, newbytelen);
308 /* Subtle hidden feature: The old string data is still there
310 * Some Microsoft program needs it.
315 * Allocate the new string
317 *old = SysAllocStringLen(str, len);
323 /******************************************************************************
324 * SysAllocStringByteLen [OLEAUT32.150]
326 * Create a BSTR from an OLESTR of a given byte length.
329 * str [I] Source to create BSTR from
330 * len [I] Length of oleStr in bytes
333 * Success: A newly allocated BSTR
334 * Failure: NULL, if len is >= 0x80000000, or memory allocation fails.
337 * -If len is 0 or oleStr is NULL the resulting string is empty ("").
338 * -This function always NUL terminates the resulting BSTR.
339 * -oleStr may be either an LPCSTR or LPCOLESTR, since it is copied
340 * without checking for a terminating NUL.
343 BSTR WINAPI SysAllocStringByteLen(LPCSTR str, UINT len)
349 * Allocate a new buffer to hold the string.
350 * don't forget to keep an empty spot at the beginning of the
351 * buffer for the character count and an extra character at the
354 newBuffer = HeapAlloc(GetProcessHeap(), 0,
355 len + sizeof(WCHAR) + sizeof(DWORD));
358 * If the memory allocation failed, return a null pointer.
364 * Copy the length of the string in the placeholder.
369 * Skip the byte count.
374 * Copy the information in the buffer.
375 * Since it is valid to pass a NULL pointer here, we'll initialize the
376 * buffer to nul if it is the case.
379 memcpy(newBuffer, str, len);
382 * Make sure that there is a nul character at the end of the
385 stringBuffer = (char *)newBuffer;
386 stringBuffer[len] = 0;
387 stringBuffer[len+1] = 0;
389 return (LPWSTR)stringBuffer;
392 /******************************************************************************
393 * SysReAllocString [OLEAUT32.3]
395 * Change the length of a previously created BSTR.
398 * old [I/O] BSTR to change the length of
399 * str [I] New source for pbstr
406 * See BSTR(), SysAllocStringStringLen().
408 INT WINAPI SysReAllocString(LPBSTR old,LPCOLESTR str)
417 * Make sure we free the old string.
423 * Allocate the new string
425 *old = SysAllocString(str);
430 /******************************************************************************
431 * SetOaNoCache (OLEAUT32.327)
433 * Instruct Ole Automation not to cache BSTR allocations.
444 void WINAPI SetOaNoCache(void)
449 static WCHAR _delimiter[2] = {'!',0}; /* default delimiter apparently */
450 static WCHAR *pdelimiter = &_delimiter[0];
452 /***********************************************************************
453 * RegisterActiveObject (OLEAUT32.33)
455 * Registers an object in the global item table.
458 * punk [I] Object to register.
459 * rcid [I] CLSID of the object.
461 * pdwRegister [O] Address to store cookie of object registration in.
465 * Failure: HRESULT code.
467 HRESULT WINAPI RegisterActiveObject(
468 LPUNKNOWN punk,REFCLSID rcid,DWORD dwFlags,LPDWORD pdwRegister
472 LPRUNNINGOBJECTTABLE runobtable;
475 StringFromGUID2(rcid,guidbuf,39);
476 ret = CreateItemMoniker(pdelimiter,guidbuf,&moniker);
479 ret = GetRunningObjectTable(0,&runobtable);
481 IMoniker_Release(moniker);
484 ret = IRunningObjectTable_Register(runobtable,dwFlags,punk,moniker,pdwRegister);
485 IRunningObjectTable_Release(runobtable);
486 IMoniker_Release(moniker);
490 /***********************************************************************
491 * RevokeActiveObject (OLEAUT32.34)
493 * Revokes an object from the global item table.
496 * xregister [I] Registration cookie.
497 * reserved [I] Reserved. Set to NULL.
501 * Failure: HRESULT code.
503 HRESULT WINAPI RevokeActiveObject(DWORD xregister,LPVOID reserved)
505 LPRUNNINGOBJECTTABLE runobtable;
508 ret = GetRunningObjectTable(0,&runobtable);
509 if (FAILED(ret)) return ret;
510 ret = IRunningObjectTable_Revoke(runobtable,xregister);
511 if (SUCCEEDED(ret)) ret = S_OK;
512 IRunningObjectTable_Release(runobtable);
516 /***********************************************************************
517 * GetActiveObject (OLEAUT32.35)
519 * Gets an object from the global item table.
522 * rcid [I] CLSID of the object.
523 * preserved [I] Reserved. Set to NULL.
524 * ppunk [O] Address to store object into.
528 * Failure: HRESULT code.
530 HRESULT WINAPI GetActiveObject(REFCLSID rcid,LPVOID preserved,LPUNKNOWN *ppunk)
534 LPRUNNINGOBJECTTABLE runobtable;
537 StringFromGUID2(rcid,guidbuf,39);
538 ret = CreateItemMoniker(pdelimiter,guidbuf,&moniker);
541 ret = GetRunningObjectTable(0,&runobtable);
543 IMoniker_Release(moniker);
546 ret = IRunningObjectTable_GetObject(runobtable,moniker,ppunk);
547 IRunningObjectTable_Release(runobtable);
548 IMoniker_Release(moniker);
553 /***********************************************************************
554 * OaBuildVersion [OLEAUT32.170]
556 * Get the Ole Automation build version.
565 * Known oleaut32.dll versions:
566 *| OLE Ver. Comments Date Build Ver.
567 *| -------- ------------------------- ---- ---------
568 *| OLE 2.1 NT 1993-95 10 3023
570 *| Win32s Ver 1.1e 20 4049
571 *| OLE 2.20 W95/NT 1993-96 20 4112
572 *| OLE 2.20 W95/NT 1993-96 20 4118
573 *| OLE 2.20 W95/NT 1993-96 20 4122
574 *| OLE 2.30 W95/NT 1993-98 30 4265
575 *| OLE 2.40 NT?? 1993-98 40 4267
576 *| OLE 2.40 W98 SE orig. file 1993-98 40 4275
577 *| OLE 2.40 W2K orig. file 1993-XX 40 4514
579 * Currently the versions returned are 2.20 for Win3.1, 2.30 for Win95 & NT 3.51,
580 * and 2.40 for all later versions. The build number is maximum, i.e. 0xffff.
582 ULONG WINAPI OaBuildVersion()
584 switch(GetVersion() & 0x8000ffff) /* mask off build number */
586 case 0x80000a03: /* WIN31 */
587 return MAKELONG(0xffff, 20);
588 case 0x00003303: /* NT351 */
589 return MAKELONG(0xffff, 30);
590 case 0x80000004: /* WIN95; I'd like to use the "standard" w95 minor
591 version here (30), but as we still use w95
592 as default winver (which is good IMHO), I better
593 play safe and use the latest value for w95 for now.
594 Change this as soon as default winver gets changed
595 to something more recent */
596 case 0x80000a04: /* WIN98 */
597 case 0x00000004: /* NT40 */
598 case 0x00000005: /* W2K */
599 case 0x00000105: /* WinXP */
600 return MAKELONG(0xffff, 40);
602 FIXME("Version value not known yet. Please investigate it !\n");
603 return MAKELONG(0xffff, 40); /* for now return the same value as for w2k */
607 /******************************************************************************
608 * OleTranslateColor [OLEAUT32.421]
610 * Convert an OLE_COLOR to a COLORREF.
613 * clr [I] Color to convert
614 * hpal [I] Handle to a palette for the conversion
615 * pColorRef [O] Destination for converted color, or NULL to test if the conversion is ok
618 * Success: S_OK. The conversion is ok, and pColorRef contains the converted color if non-NULL.
619 * Failure: E_INVALIDARG, if any argument is invalid.
622 * Document the conversion rules.
624 HRESULT WINAPI OleTranslateColor(
630 BYTE b = HIBYTE(HIWORD(clr));
632 TRACE("(%08lx, %p, %p):stub\n", clr, hpal, pColorRef);
635 * In case pColorRef is NULL, provide our own to simplify the code.
637 if (pColorRef == NULL)
638 pColorRef = &colorref;
645 *pColorRef = PALETTERGB(GetRValue(clr),
660 * Validate the palette index.
662 if (GetPaletteEntries(hpal, LOWORD(clr), 1, &pe) == 0)
677 int index = LOBYTE(LOWORD(clr));
680 * Validate GetSysColor index.
682 if ((index < COLOR_SCROLLBAR) || (index > COLOR_MENUBAR))
685 *pColorRef = GetSysColor(index);
697 extern HRESULT OLEAUTPS_DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv);
699 extern void _get_STDFONT_CF(LPVOID);
700 extern void _get_STDPIC_CF(LPVOID);
702 static HRESULT WINAPI PSDispatchFacBuf_QueryInterface(IPSFactoryBuffer *iface, REFIID riid, void **ppv)
704 if (IsEqualIID(riid, &IID_IUnknown) ||
705 IsEqualIID(riid, &IID_IPSFactoryBuffer))
707 IUnknown_AddRef(iface);
708 *ppv = (void *)iface;
711 return E_NOINTERFACE;
714 static ULONG WINAPI PSDispatchFacBuf_AddRef(IPSFactoryBuffer *iface)
719 static ULONG WINAPI PSDispatchFacBuf_Release(IPSFactoryBuffer *iface)
724 static HRESULT WINAPI PSDispatchFacBuf_CreateProxy(IPSFactoryBuffer *iface, IUnknown *pUnkOuter, REFIID riid, IRpcProxyBuffer **ppProxy, void **ppv)
726 IPSFactoryBuffer *pPSFB;
729 if (IsEqualIID(riid, &IID_IDispatch))
730 hr = OLEAUTPS_DllGetClassObject(&CLSID_PSDispatch, &IID_IPSFactoryBuffer, (void **)&pPSFB);
732 hr = TMARSHAL_DllGetClassObject(&CLSID_PSOAInterface, &IID_IPSFactoryBuffer, (void **)&pPSFB);
734 if (FAILED(hr)) return hr;
736 hr = IPSFactoryBuffer_CreateProxy(pPSFB, pUnkOuter, riid, ppProxy, ppv);
738 IPSFactoryBuffer_Release(pPSFB);
742 static HRESULT WINAPI PSDispatchFacBuf_CreateStub(IPSFactoryBuffer *iface, REFIID riid, IUnknown *pUnkOuter, IRpcStubBuffer **ppStub)
744 IPSFactoryBuffer *pPSFB;
747 if (IsEqualIID(riid, &IID_IDispatch))
748 hr = OLEAUTPS_DllGetClassObject(&CLSID_PSDispatch, &IID_IPSFactoryBuffer, (void **)&pPSFB);
750 hr = TMARSHAL_DllGetClassObject(&CLSID_PSOAInterface, &IID_IPSFactoryBuffer, (void **)&pPSFB);
752 if (FAILED(hr)) return hr;
754 hr = IPSFactoryBuffer_CreateStub(pPSFB, riid, pUnkOuter, ppStub);
756 IPSFactoryBuffer_Release(pPSFB);
760 static const IPSFactoryBufferVtbl PSDispatchFacBuf_Vtbl =
762 PSDispatchFacBuf_QueryInterface,
763 PSDispatchFacBuf_AddRef,
764 PSDispatchFacBuf_Release,
765 PSDispatchFacBuf_CreateProxy,
766 PSDispatchFacBuf_CreateStub
769 /* This is the whole PSFactoryBuffer object, just the vtableptr */
770 static const IPSFactoryBufferVtbl *pPSDispatchFacBuf = &PSDispatchFacBuf_Vtbl;
772 /***********************************************************************
773 * DllGetClassObject (OLEAUT32.@)
775 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
778 if (IsEqualGUID(rclsid,&CLSID_StdFont)) {
779 if (IsEqualGUID(iid,&IID_IClassFactory)) {
780 _get_STDFONT_CF(ppv);
781 IClassFactory_AddRef((IClassFactory*)*ppv);
785 if (IsEqualGUID(rclsid,&CLSID_StdPicture)) {
786 if (IsEqualGUID(iid,&IID_IClassFactory)) {
788 IClassFactory_AddRef((IClassFactory*)*ppv);
792 if (IsEqualCLSID(rclsid, &CLSID_PSTypeInfo) ||
793 IsEqualCLSID(rclsid, &CLSID_PSTypeLib) ||
794 IsEqualCLSID(rclsid, &CLSID_PSEnumVariant)) {
795 return OLEAUTPS_DllGetClassObject(&CLSID_PSDispatch, iid, ppv);
797 if (IsEqualCLSID(rclsid, &CLSID_PSDispatch) && IsEqualIID(iid, &IID_IPSFactoryBuffer)) {
798 *ppv = &pPSDispatchFacBuf;
799 IPSFactoryBuffer_AddRef((IPSFactoryBuffer *)*ppv);
802 if (IsEqualGUID(rclsid,&CLSID_PSOAInterface)) {
803 if (S_OK==TMARSHAL_DllGetClassObject(rclsid,iid,ppv))
807 FIXME("\n\tCLSID:\t%s,\n\tIID:\t%s\n",debugstr_guid(rclsid),debugstr_guid(iid));
808 return CLASS_E_CLASSNOTAVAILABLE;
811 /***********************************************************************
812 * DllCanUnloadNow (OLEAUT32.@)
814 * Determine if this dll can be unloaded from the callers address space.
820 * Always returns S_FALSE. This dll cannot be unloaded.
822 HRESULT WINAPI DllCanUnloadNow(void)
827 /*****************************************************************************
828 * DllMain [OLEAUT32.@]
830 BOOL WINAPI DllMain(HINSTANCE hInstDll, DWORD fdwReason, LPVOID lpvReserved)
832 TRACE("(%p,%ld,%p)\n", hInstDll, fdwReason, lpvReserved);
835 case DLL_PROCESS_ATTACH:
836 DisableThreadLibraryCalls(hInstDll);
837 OLEAUT32_hModule = (HMODULE)hInstDll;
839 case DLL_PROCESS_DETACH: