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
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
42 static BOOL BSTR_bCache
= TRUE
; /* Cache allocations to minimise alloc calls? */
44 HMODULE OLEAUT32_hModule
= NULL
;
46 /******************************************************************************
50 * BSTR is a simple typedef for a wide-character string used as the principle
51 * string type in ole automation. When encapsulated in a Variant type they are
52 * automatically copied and destroyed as the variant is processed.
54 * The low level BSTR Api allows manipulation of these strings and is used by
55 * higher level Api calls to manage the strings transparently to the caller.
57 * Internally the BSTR type is allocated with space for a DWORD byte count before
58 * the string data begins. This is undocumented and non-system code should not
59 * access the count directly. Use SysStringLen() or SysStringByteLen()
60 * instead. Note that the byte count does not include the terminating NUL.
62 * To create a new BSTR, use SysAllocString(), SysAllocStringLen() or
63 * SysAllocStringByteLen(). To change the size of an existing BSTR, use SysReAllocString()
64 * or SysReAllocStringLen(). Finally to destroy a string use SysFreeString().
66 * BSTR's are cached by Ole Automation by default. To override this behaviour
67 * either set the environment variable 'OANOCACHE', or call SetOaNoCache().
70 * 'Inside OLE, second edition' by Kraig Brockshmidt.
73 /******************************************************************************
74 * SysStringLen [OLEAUT32.7]
76 * Get the allocated length of a BSTR in wide characters.
79 * str [I] BSTR to find the length of
82 * The allocated length of str, or 0 if str is NULL.
86 * The returned length may be different from the length of the string as
87 * calculated by lstrlenW(), since it returns the length that was used to
88 * allocate the string by SysAllocStringLen().
90 UINT WINAPI
SysStringLen(BSTR str
)
96 * The length of the string (in bytes) is contained in a DWORD placed
97 * just before the BSTR pointer
99 bufferPointer
= (DWORD
*)str
;
103 return (int)(*bufferPointer
/sizeof(WCHAR
));
106 /******************************************************************************
107 * SysStringByteLen [OLEAUT32.149]
109 * Get the allocated length of a BSTR in bytes.
112 * str [I] BSTR to find the length of
115 * The allocated length of str, or 0 if str is NULL.
118 * See SysStringLen(), BSTR().
120 UINT WINAPI
SysStringByteLen(BSTR str
)
122 DWORD
* bufferPointer
;
126 * The length of the string (in bytes) is contained in a DWORD placed
127 * just before the BSTR pointer
129 bufferPointer
= (DWORD
*)str
;
133 return (int)(*bufferPointer
);
136 /******************************************************************************
137 * SysAllocString [OLEAUT32.2]
139 * Create a BSTR from an OLESTR.
142 * str [I] Source to create BSTR from
145 * Success: A BSTR allocated with SysAllocStringLen().
146 * Failure: NULL, if oleStr is NULL.
150 * MSDN (October 2001) incorrectly states that NULL is returned if oleStr has
151 * a length of 0. Native Win32 and this implementation both return a valid
152 * empty BSTR in this case.
154 BSTR WINAPI
SysAllocString(LPCOLESTR str
)
158 /* Delegate this to the SysAllocStringLen32 method. */
159 return SysAllocStringLen(str
, lstrlenW(str
));
162 /******************************************************************************
163 * SysFreeString [OLEAUT32.6]
168 * str [I] BSTR to free.
175 * str may be NULL, in which case this function does nothing.
177 void WINAPI
SysFreeString(BSTR str
)
179 DWORD
* bufferPointer
;
181 /* NULL is a valid parameter */
185 * We have to be careful when we free a BSTR pointer, it points to
186 * the beginning of the string but it skips the byte count contained
189 bufferPointer
= (DWORD
*)str
;
194 * Free the memory from its "real" origin.
196 HeapFree(GetProcessHeap(), 0, bufferPointer
);
199 /******************************************************************************
200 * SysAllocStringLen [OLEAUT32.4]
202 * Create a BSTR from an OLESTR of a given wide character length.
205 * str [I] Source to create BSTR from
206 * len [I] Length of oleStr in wide characters
209 * Success: A newly allocated BSTR from SysAllocStringByteLen()
210 * Failure: NULL, if len is >= 0x80000000, or memory allocation fails.
213 * See BSTR(), SysAllocStringByteLen().
215 BSTR WINAPI
SysAllocStringLen(const OLECHAR
*str
, unsigned int len
)
221 /* Detect integer overflow. */
222 if (len
>= ((UINT_MAX
-sizeof(WCHAR
)-sizeof(DWORD
))/sizeof(WCHAR
)))
225 * Find the length of the buffer passed-in, in bytes.
227 bufferSize
= len
* sizeof (WCHAR
);
230 * Allocate a new buffer to hold the string.
231 * don't forget to keep an empty spot at the beginning of the
232 * buffer for the character count and an extra character at the
235 newBuffer
= HeapAlloc(GetProcessHeap(), 0,
236 bufferSize
+ sizeof(WCHAR
) + sizeof(DWORD
));
239 * If the memory allocation failed, return a null pointer.
245 * Copy the length of the string in the placeholder.
247 *newBuffer
= bufferSize
;
250 * Skip the byte count.
255 * Copy the information in the buffer.
256 * Since it is valid to pass a NULL pointer here, we'll initialize the
257 * buffer to nul if it is the case.
260 memcpy(newBuffer
, str
, bufferSize
);
262 memset(newBuffer
, 0, bufferSize
);
265 * Make sure that there is a nul character at the end of the
268 stringBuffer
= (WCHAR
*)newBuffer
;
269 stringBuffer
[len
] = '\0';
271 return (LPWSTR
)stringBuffer
;
274 /******************************************************************************
275 * SysReAllocStringLen [OLEAUT32.5]
277 * Change the length of a previously created BSTR.
280 * old [O] BSTR to change the length of
281 * str [I] New source for pbstr
282 * len [I] Length of oleStr in wide characters
285 * Success: 1. The size of pbstr is updated.
286 * Failure: 0, if len >= 0x80000000 or memory allocation fails.
289 * See BSTR(), SysAllocStringByteLen().
290 * *pbstr may be changed by this function.
292 int WINAPI
SysReAllocStringLen(BSTR
* old
, const OLECHAR
* str
, unsigned int len
)
294 /* Detect integer overflow. */
295 if (len
>= ((UINT_MAX
-sizeof(WCHAR
)-sizeof(DWORD
))/sizeof(WCHAR
)))
299 DWORD newbytelen
= len
*sizeof(WCHAR
);
300 DWORD
*ptr
= HeapReAlloc(GetProcessHeap(),0,((DWORD
*)*old
)-1,newbytelen
+sizeof(WCHAR
)+sizeof(DWORD
));
301 *old
= (BSTR
)(ptr
+1);
304 memcpy(*old
, str
, newbytelen
);
307 /* Subtle hidden feature: The old string data is still there
309 * Some Microsoft program needs it.
314 * Allocate the new string
316 *old
= SysAllocStringLen(str
, len
);
322 /******************************************************************************
323 * SysAllocStringByteLen [OLEAUT32.150]
325 * Create a BSTR from an OLESTR of a given byte length.
328 * str [I] Source to create BSTR from
329 * len [I] Length of oleStr in bytes
332 * Success: A newly allocated BSTR
333 * Failure: NULL, if len is >= 0x80000000, or memory allocation fails.
336 * -If len is 0 or oleStr is NULL the resulting string is empty ("").
337 * -This function always NUL terminates the resulting BSTR.
338 * -oleStr may be either an LPCSTR or LPCOLESTR, since it is copied
339 * without checking for a terminating NUL.
342 BSTR WINAPI
SysAllocStringByteLen(LPCSTR str
, UINT len
)
347 /* Detect integer overflow. */
348 if (len
>= (UINT_MAX
-sizeof(WCHAR
)-sizeof(DWORD
)))
352 * Allocate a new buffer to hold the string.
353 * don't forget to keep an empty spot at the beginning of the
354 * buffer for the character count and an extra character at the
357 newBuffer
= HeapAlloc(GetProcessHeap(), 0,
358 len
+ sizeof(WCHAR
) + sizeof(DWORD
));
361 * If the memory allocation failed, return a null pointer.
367 * Copy the length of the string in the placeholder.
372 * Skip the byte count.
377 * Copy the information in the buffer.
378 * Since it is valid to pass a NULL pointer here, we'll initialize the
379 * buffer to nul if it is the case.
382 memcpy(newBuffer
, str
, len
);
385 * Make sure that there is a nul character at the end of the
388 stringBuffer
= (char *)newBuffer
;
389 stringBuffer
[len
] = 0;
390 stringBuffer
[len
+1] = 0;
392 return (LPWSTR
)stringBuffer
;
395 /******************************************************************************
396 * SysReAllocString [OLEAUT32.3]
398 * Change the length of a previously created BSTR.
401 * old [I/O] BSTR to change the length of
402 * str [I] New source for pbstr
409 * See BSTR(), SysAllocStringStringLen().
411 INT WINAPI
SysReAllocString(LPBSTR old
,LPCOLESTR str
)
420 * Make sure we free the old string.
426 * Allocate the new string
428 *old
= SysAllocString(str
);
433 /******************************************************************************
434 * SetOaNoCache (OLEAUT32.327)
436 * Instruct Ole Automation not to cache BSTR allocations.
447 void WINAPI
SetOaNoCache(void)
452 static const WCHAR _delimiter
[2] = {'!',0}; /* default delimiter apparently */
453 static const WCHAR
*pdelimiter
= &_delimiter
[0];
455 /***********************************************************************
456 * RegisterActiveObject (OLEAUT32.33)
458 * Registers an object in the global item table.
461 * punk [I] Object to register.
462 * rcid [I] CLSID of the object.
464 * pdwRegister [O] Address to store cookie of object registration in.
468 * Failure: HRESULT code.
470 HRESULT WINAPI
RegisterActiveObject(
471 LPUNKNOWN punk
,REFCLSID rcid
,DWORD dwFlags
,LPDWORD pdwRegister
475 LPRUNNINGOBJECTTABLE runobtable
;
478 StringFromGUID2(rcid
,guidbuf
,39);
479 ret
= CreateItemMoniker(pdelimiter
,guidbuf
,&moniker
);
482 ret
= GetRunningObjectTable(0,&runobtable
);
484 IMoniker_Release(moniker
);
487 ret
= IRunningObjectTable_Register(runobtable
,dwFlags
,punk
,moniker
,pdwRegister
);
488 IRunningObjectTable_Release(runobtable
);
489 IMoniker_Release(moniker
);
493 /***********************************************************************
494 * RevokeActiveObject (OLEAUT32.34)
496 * Revokes an object from the global item table.
499 * xregister [I] Registration cookie.
500 * reserved [I] Reserved. Set to NULL.
504 * Failure: HRESULT code.
506 HRESULT WINAPI
RevokeActiveObject(DWORD xregister
,LPVOID reserved
)
508 LPRUNNINGOBJECTTABLE runobtable
;
511 ret
= GetRunningObjectTable(0,&runobtable
);
512 if (FAILED(ret
)) return ret
;
513 ret
= IRunningObjectTable_Revoke(runobtable
,xregister
);
514 if (SUCCEEDED(ret
)) ret
= S_OK
;
515 IRunningObjectTable_Release(runobtable
);
519 /***********************************************************************
520 * GetActiveObject (OLEAUT32.35)
522 * Gets an object from the global item table.
525 * rcid [I] CLSID of the object.
526 * preserved [I] Reserved. Set to NULL.
527 * ppunk [O] Address to store object into.
531 * Failure: HRESULT code.
533 HRESULT WINAPI
GetActiveObject(REFCLSID rcid
,LPVOID preserved
,LPUNKNOWN
*ppunk
)
537 LPRUNNINGOBJECTTABLE runobtable
;
540 StringFromGUID2(rcid
,guidbuf
,39);
541 ret
= CreateItemMoniker(pdelimiter
,guidbuf
,&moniker
);
544 ret
= GetRunningObjectTable(0,&runobtable
);
546 IMoniker_Release(moniker
);
549 ret
= IRunningObjectTable_GetObject(runobtable
,moniker
,ppunk
);
550 IRunningObjectTable_Release(runobtable
);
551 IMoniker_Release(moniker
);
556 /***********************************************************************
557 * OaBuildVersion [OLEAUT32.170]
559 * Get the Ole Automation build version.
568 * Known oleaut32.dll versions:
569 *| OLE Ver. Comments Date Build Ver.
570 *| -------- ------------------------- ---- ---------
571 *| OLE 2.1 NT 1993-95 10 3023
573 *| Win32s Ver 1.1e 20 4049
574 *| OLE 2.20 W95/NT 1993-96 20 4112
575 *| OLE 2.20 W95/NT 1993-96 20 4118
576 *| OLE 2.20 W95/NT 1993-96 20 4122
577 *| OLE 2.30 W95/NT 1993-98 30 4265
578 *| OLE 2.40 NT?? 1993-98 40 4267
579 *| OLE 2.40 W98 SE orig. file 1993-98 40 4275
580 *| OLE 2.40 W2K orig. file 1993-XX 40 4514
582 * Currently the versions returned are 2.20 for Win3.1, 2.30 for Win95 & NT 3.51,
583 * and 2.40 for all later versions. The build number is maximum, i.e. 0xffff.
585 ULONG WINAPI
OaBuildVersion(void)
587 switch(GetVersion() & 0x8000ffff) /* mask off build number */
589 case 0x80000a03: /* WIN31 */
590 return MAKELONG(0xffff, 20);
591 case 0x00003303: /* NT351 */
592 return MAKELONG(0xffff, 30);
593 case 0x80000004: /* WIN95; I'd like to use the "standard" w95 minor
594 version here (30), but as we still use w95
595 as default winver (which is good IMHO), I better
596 play safe and use the latest value for w95 for now.
597 Change this as soon as default winver gets changed
598 to something more recent */
599 case 0x80000a04: /* WIN98 */
600 case 0x00000004: /* NT40 */
601 case 0x00000005: /* W2K */
602 case 0x00000105: /* WinXP */
603 return MAKELONG(0xffff, 40);
605 FIXME("Version value not known yet. Please investigate it !\n");
606 return MAKELONG(0xffff, 40); /* for now return the same value as for w2k */
610 /******************************************************************************
611 * OleTranslateColor [OLEAUT32.421]
613 * Convert an OLE_COLOR to a COLORREF.
616 * clr [I] Color to convert
617 * hpal [I] Handle to a palette for the conversion
618 * pColorRef [O] Destination for converted color, or NULL to test if the conversion is ok
621 * Success: S_OK. The conversion is ok, and pColorRef contains the converted color if non-NULL.
622 * Failure: E_INVALIDARG, if any argument is invalid.
625 * Document the conversion rules.
627 HRESULT WINAPI
OleTranslateColor(
633 BYTE b
= HIBYTE(HIWORD(clr
));
635 TRACE("(%08x, %p, %p)\n", clr
, hpal
, pColorRef
);
638 * In case pColorRef is NULL, provide our own to simplify the code.
640 if (pColorRef
== NULL
)
641 pColorRef
= &colorref
;
648 *pColorRef
= PALETTERGB(GetRValue(clr
),
663 * Validate the palette index.
665 if (GetPaletteEntries(hpal
, LOWORD(clr
), 1, &pe
) == 0)
680 int index
= LOBYTE(LOWORD(clr
));
683 * Validate GetSysColor index.
685 if ((index
< COLOR_SCROLLBAR
) || (index
> COLOR_MENUBAR
))
688 *pColorRef
= GetSysColor(index
);
700 extern HRESULT
OLEAUTPS_DllGetClassObject(REFCLSID rclsid
, REFIID riid
, LPVOID
*ppv
);
702 extern void _get_STDFONT_CF(LPVOID
*);
703 extern void _get_STDPIC_CF(LPVOID
*);
705 static HRESULT WINAPI
PSDispatchFacBuf_QueryInterface(IPSFactoryBuffer
*iface
, REFIID riid
, void **ppv
)
707 if (IsEqualIID(riid
, &IID_IUnknown
) ||
708 IsEqualIID(riid
, &IID_IPSFactoryBuffer
))
710 IUnknown_AddRef(iface
);
711 *ppv
= (void *)iface
;
714 return E_NOINTERFACE
;
717 static ULONG WINAPI
PSDispatchFacBuf_AddRef(IPSFactoryBuffer
*iface
)
722 static ULONG WINAPI
PSDispatchFacBuf_Release(IPSFactoryBuffer
*iface
)
727 static HRESULT WINAPI
PSDispatchFacBuf_CreateProxy(IPSFactoryBuffer
*iface
, IUnknown
*pUnkOuter
, REFIID riid
, IRpcProxyBuffer
**ppProxy
, void **ppv
)
729 IPSFactoryBuffer
*pPSFB
;
732 if (IsEqualIID(riid
, &IID_IDispatch
))
733 hr
= OLEAUTPS_DllGetClassObject(&CLSID_PSDispatch
, &IID_IPSFactoryBuffer
, (void **)&pPSFB
);
735 hr
= TMARSHAL_DllGetClassObject(&CLSID_PSOAInterface
, &IID_IPSFactoryBuffer
, (void **)&pPSFB
);
737 if (FAILED(hr
)) return hr
;
739 hr
= IPSFactoryBuffer_CreateProxy(pPSFB
, pUnkOuter
, riid
, ppProxy
, ppv
);
741 IPSFactoryBuffer_Release(pPSFB
);
745 static HRESULT WINAPI
PSDispatchFacBuf_CreateStub(IPSFactoryBuffer
*iface
, REFIID riid
, IUnknown
*pUnkOuter
, IRpcStubBuffer
**ppStub
)
747 IPSFactoryBuffer
*pPSFB
;
750 if (IsEqualIID(riid
, &IID_IDispatch
))
751 hr
= OLEAUTPS_DllGetClassObject(&CLSID_PSDispatch
, &IID_IPSFactoryBuffer
, (void **)&pPSFB
);
753 hr
= TMARSHAL_DllGetClassObject(&CLSID_PSOAInterface
, &IID_IPSFactoryBuffer
, (void **)&pPSFB
);
755 if (FAILED(hr
)) return hr
;
757 hr
= IPSFactoryBuffer_CreateStub(pPSFB
, riid
, pUnkOuter
, ppStub
);
759 IPSFactoryBuffer_Release(pPSFB
);
763 static const IPSFactoryBufferVtbl PSDispatchFacBuf_Vtbl
=
765 PSDispatchFacBuf_QueryInterface
,
766 PSDispatchFacBuf_AddRef
,
767 PSDispatchFacBuf_Release
,
768 PSDispatchFacBuf_CreateProxy
,
769 PSDispatchFacBuf_CreateStub
772 /* This is the whole PSFactoryBuffer object, just the vtableptr */
773 static const IPSFactoryBufferVtbl
*pPSDispatchFacBuf
= &PSDispatchFacBuf_Vtbl
;
775 /***********************************************************************
776 * DllGetClassObject (OLEAUT32.@)
778 HRESULT WINAPI
DllGetClassObject(REFCLSID rclsid
, REFIID iid
, LPVOID
*ppv
)
781 if (IsEqualGUID(rclsid
,&CLSID_StdFont
)) {
782 if (IsEqualGUID(iid
,&IID_IClassFactory
)) {
783 _get_STDFONT_CF(ppv
);
784 IClassFactory_AddRef((IClassFactory
*)*ppv
);
788 if (IsEqualGUID(rclsid
,&CLSID_StdPicture
)) {
789 if (IsEqualGUID(iid
,&IID_IClassFactory
)) {
791 IClassFactory_AddRef((IClassFactory
*)*ppv
);
795 if (IsEqualCLSID(rclsid
, &CLSID_PSTypeInfo
) ||
796 IsEqualCLSID(rclsid
, &CLSID_PSTypeLib
) ||
797 IsEqualCLSID(rclsid
, &CLSID_PSEnumVariant
)) {
798 return OLEAUTPS_DllGetClassObject(&CLSID_PSDispatch
, iid
, ppv
);
800 if (IsEqualCLSID(rclsid
, &CLSID_PSDispatch
) && IsEqualIID(iid
, &IID_IPSFactoryBuffer
)) {
801 *ppv
= &pPSDispatchFacBuf
;
802 IPSFactoryBuffer_AddRef((IPSFactoryBuffer
*)*ppv
);
805 if (IsEqualGUID(rclsid
,&CLSID_PSOAInterface
)) {
806 if (S_OK
==TMARSHAL_DllGetClassObject(rclsid
,iid
,ppv
))
810 FIXME("\n\tCLSID:\t%s,\n\tIID:\t%s\n",debugstr_guid(rclsid
),debugstr_guid(iid
));
811 return CLASS_E_CLASSNOTAVAILABLE
;
814 /***********************************************************************
815 * DllCanUnloadNow (OLEAUT32.@)
817 * Determine if this dll can be unloaded from the callers address space.
823 * Always returns S_FALSE. This dll cannot be unloaded.
825 HRESULT WINAPI
DllCanUnloadNow(void)
830 /*****************************************************************************
831 * DllMain [OLEAUT32.@]
833 BOOL WINAPI
DllMain(HINSTANCE hInstDll
, DWORD fdwReason
, LPVOID lpvReserved
)
835 TRACE("(%p,%d,%p)\n", hInstDll
, fdwReason
, lpvReserved
);
838 case DLL_PROCESS_ATTACH
:
839 DisableThreadLibraryCalls(hInstDll
);
840 OLEAUT32_hModule
= hInstDll
;
842 case DLL_PROCESS_DETACH
:
849 /***********************************************************************
850 * OleIconToCursor (OLEAUT32.415)
852 HCURSOR WINAPI
OleIconToCursor( HINSTANCE hinstExe
, HICON hIcon
)
854 FIXME("(%p,%p), partially implemented.\n",hinstExe
,hIcon
);
855 /* FIXME: make a extended conversation from HICON to HCURSOR */
856 return CopyCursor(hIcon
);