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 "oleaut32_oaidl.h"
40 #include "wine/debug.h"
41 #include "wine/unicode.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(ole);
45 static BOOL BSTR_bCache = TRUE; /* Cache allocations to minimise alloc calls? */
47 /******************************************************************************
51 * BSTR is a simple typedef for a wide-character string used as the principle
52 * string type in ole automation. When encapsulated in a Variant type they are
53 * automatically copied and destroyed as the variant is processed.
55 * The low level BSTR Api allows manipulation of these strings and is used by
56 * higher level Api calls to manage the strings transparently to the caller.
58 * Internally the BSTR type is allocated with space for a DWORD byte count before
59 * the string data begins. This is undocumented and non-system code should not
60 * access the count directly. Use SysStringLen() or SysStringByteLen()
61 * instead. Note that the byte count does not include the terminating NUL.
63 * To create a new BSTR, use SysAllocString(), SysAllocStringLen() or
64 * SysAllocStringByteLen(). To change the size of an existing BSTR, use SysReAllocString()
65 * or SysReAllocStringLen(). Finally to destroy a string use SysFreeString().
67 * BSTR's are cached by Ole Automation by default. To override this behaviour
68 * either set the environment variable 'OANOCACHE', or call SetOaNoCache().
71 * 'Inside OLE, second edition' by Kraig Brockshmidt.
74 /******************************************************************************
75 * SysStringLen [OLEAUT32.7]
77 * Get the allocated length of a BSTR in wide characters.
80 * str [I] BSTR to find the length of
83 * The allocated length of str, or 0 if str is NULL.
87 * The returned length may be different from the length of the string as
88 * calculated by lstrlenW(), since it returns the length that was used to
89 * allocate the string by SysAllocStringLen().
91 UINT WINAPI SysStringLen(BSTR str)
97 * The length of the string (in bytes) is contained in a DWORD placed
98 * just before the BSTR pointer
100 bufferPointer = (DWORD*)str;
104 return (int)(*bufferPointer/sizeof(WCHAR));
107 /******************************************************************************
108 * SysStringByteLen [OLEAUT32.149]
110 * Get the allocated length of a BSTR in bytes.
113 * str [I] BSTR to find the length of
116 * The allocated length of str, or 0 if str is NULL.
119 * See SysStringLen(), BSTR().
121 UINT WINAPI SysStringByteLen(BSTR str)
123 DWORD* bufferPointer;
127 * The length of the string (in bytes) is contained in a DWORD placed
128 * just before the BSTR pointer
130 bufferPointer = (DWORD*)str;
134 return (int)(*bufferPointer);
137 /******************************************************************************
138 * SysAllocString [OLEAUT32.2]
140 * Create a BSTR from an OLESTR.
143 * str [I] Source to create BSTR from
146 * Success: A BSTR allocated with SysAllocStringLen().
147 * Failure: NULL, if oleStr is NULL.
151 * MSDN (October 2001) incorrectly states that NULL is returned if oleStr has
152 * a length of 0. Native Win32 and this implementation both return a valid
153 * empty BSTR in this case.
155 BSTR WINAPI SysAllocString(LPCOLESTR str)
159 /* Delegate this to the SysAllocStringLen32 method. */
160 return SysAllocStringLen(str, lstrlenW(str));
163 /******************************************************************************
164 * SysFreeString [OLEAUT32.6]
169 * str [I] BSTR to free.
176 * str may be NULL, in which case this function does nothing.
178 void WINAPI SysFreeString(BSTR str)
180 DWORD* bufferPointer;
182 /* NULL is a valid parameter */
186 * We have to be careful when we free a BSTR pointer, it points to
187 * the beginning of the string but it skips the byte count contained
190 bufferPointer = (DWORD*)str;
195 * Free the memory from its "real" origin.
197 HeapFree(GetProcessHeap(), 0, bufferPointer);
200 /******************************************************************************
201 * SysAllocStringLen [OLEAUT32.4]
203 * Create a BSTR from an OLESTR of a given wide character length.
206 * str [I] Source to create BSTR from
207 * len [I] Length of oleStr in wide characters
210 * Success: A newly allocated BSTR from SysAllocStringByteLen()
211 * Failure: NULL, if len is >= 0x80000000, or memory allocation fails.
214 * See BSTR(), SysAllocStringByteLen().
216 BSTR WINAPI SysAllocStringLen(const OLECHAR *str, unsigned int len)
222 /* Detect integer overflow. */
223 if (len >= ((UINT_MAX-sizeof(WCHAR)-sizeof(DWORD))/sizeof(WCHAR)))
226 * Find the length of the buffer passed-in, in bytes.
228 bufferSize = len * sizeof (WCHAR);
231 * Allocate a new buffer to hold the string.
232 * don't forget to keep an empty spot at the beginning of the
233 * buffer for the character count and an extra character at the
236 newBuffer = HeapAlloc(GetProcessHeap(), 0,
237 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
240 * If the memory allocation failed, return a null pointer.
246 * Copy the length of the string in the placeholder.
248 *newBuffer = bufferSize;
251 * Skip the byte count.
256 * Copy the information in the buffer.
257 * Since it is valid to pass a NULL pointer here, we'll initialize the
258 * buffer to nul if it is the case.
261 memcpy(newBuffer, str, bufferSize);
263 memset(newBuffer, 0, bufferSize);
266 * Make sure that there is a nul character at the end of the
269 stringBuffer = (WCHAR*)newBuffer;
270 stringBuffer[len] = '\0';
275 /******************************************************************************
276 * SysReAllocStringLen [OLEAUT32.5]
278 * Change the length of a previously created BSTR.
281 * old [O] BSTR to change the length of
282 * str [I] New source for pbstr
283 * len [I] Length of oleStr in wide characters
286 * Success: 1. The size of pbstr is updated.
287 * Failure: 0, if len >= 0x80000000 or memory allocation fails.
290 * See BSTR(), SysAllocStringByteLen().
291 * *old may be changed by this function.
293 int WINAPI SysReAllocStringLen(BSTR* old, const OLECHAR* str, unsigned int len)
295 /* Detect integer overflow. */
296 if (len >= ((UINT_MAX-sizeof(WCHAR)-sizeof(DWORD))/sizeof(WCHAR)))
300 BSTR old_copy = *old;
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);
305 /* Subtle hidden feature: The old string data is still there
307 * Some Microsoft program needs it.
309 if (str && old_copy!=str) memmove(*old, str, newbytelen);
313 * Allocate the new string
315 *old = SysAllocStringLen(str, len);
321 /******************************************************************************
322 * SysAllocStringByteLen [OLEAUT32.150]
324 * Create a BSTR from an OLESTR of a given byte length.
327 * str [I] Source to create BSTR from
328 * len [I] Length of oleStr in bytes
331 * Success: A newly allocated BSTR
332 * Failure: NULL, if len is >= 0x80000000, or memory allocation fails.
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.
341 BSTR WINAPI SysAllocStringByteLen(LPCSTR str, UINT len)
346 /* Detect integer overflow. */
347 if (len >= (UINT_MAX-sizeof(WCHAR)-sizeof(DWORD)))
351 * Allocate a new buffer to hold the string.
352 * don't forget to keep an empty spot at the beginning of the
353 * buffer for the character count and an extra character at the
356 newBuffer = HeapAlloc(GetProcessHeap(), 0,
357 len + sizeof(WCHAR) + sizeof(DWORD));
360 * If the memory allocation failed, return a null pointer.
366 * Copy the length of the string in the placeholder.
371 * Skip the byte count.
376 * Copy the information in the buffer.
377 * Since it is valid to pass a NULL pointer here, we'll initialize the
378 * buffer to nul if it is the case.
381 memcpy(newBuffer, str, len);
384 * Make sure that there is a nul character at the end of the
387 stringBuffer = (char *)newBuffer;
388 stringBuffer[len] = 0;
389 stringBuffer[len+1] = 0;
391 return (LPWSTR)stringBuffer;
394 /******************************************************************************
395 * SysReAllocString [OLEAUT32.3]
397 * Change the length of a previously created BSTR.
400 * old [I/O] BSTR to change the length of
401 * str [I] New source for pbstr
408 * See BSTR(), SysAllocStringStringLen().
410 INT WINAPI SysReAllocString(LPBSTR old,LPCOLESTR str)
419 * 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 const WCHAR _delimiter[] = {'!',0}; /* default delimiter apparently */
451 static const 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;
475 DWORD rot_flags = ROTFLAGS_REGISTRATIONKEEPSALIVE; /* default registration is strong */
477 StringFromGUID2(rcid,guidbuf,39);
478 ret = CreateItemMoniker(pdelimiter,guidbuf,&moniker);
481 ret = GetRunningObjectTable(0,&runobtable);
483 IMoniker_Release(moniker);
486 if(dwFlags == ACTIVEOBJECT_WEAK)
488 ret = IRunningObjectTable_Register(runobtable,rot_flags,punk,moniker,pdwRegister);
489 IRunningObjectTable_Release(runobtable);
490 IMoniker_Release(moniker);
494 /***********************************************************************
495 * RevokeActiveObject (OLEAUT32.34)
497 * Revokes an object from the global item table.
500 * xregister [I] Registration cookie.
501 * reserved [I] Reserved. Set to NULL.
505 * Failure: HRESULT code.
507 HRESULT WINAPI RevokeActiveObject(DWORD xregister,LPVOID reserved)
509 LPRUNNINGOBJECTTABLE runobtable;
512 ret = GetRunningObjectTable(0,&runobtable);
513 if (FAILED(ret)) return ret;
514 ret = IRunningObjectTable_Revoke(runobtable,xregister);
515 if (SUCCEEDED(ret)) ret = S_OK;
516 IRunningObjectTable_Release(runobtable);
520 /***********************************************************************
521 * GetActiveObject (OLEAUT32.35)
523 * Gets an object from the global item table.
526 * rcid [I] CLSID of the object.
527 * preserved [I] Reserved. Set to NULL.
528 * ppunk [O] Address to store object into.
532 * Failure: HRESULT code.
534 HRESULT WINAPI GetActiveObject(REFCLSID rcid,LPVOID preserved,LPUNKNOWN *ppunk)
538 LPRUNNINGOBJECTTABLE runobtable;
541 StringFromGUID2(rcid,guidbuf,39);
542 ret = CreateItemMoniker(pdelimiter,guidbuf,&moniker);
545 ret = GetRunningObjectTable(0,&runobtable);
547 IMoniker_Release(moniker);
550 ret = IRunningObjectTable_GetObject(runobtable,moniker,ppunk);
551 IRunningObjectTable_Release(runobtable);
552 IMoniker_Release(moniker);
557 /***********************************************************************
558 * OaBuildVersion [OLEAUT32.170]
560 * Get the Ole Automation build version.
569 * Known oleaut32.dll versions:
570 *| OLE Ver. Comments Date Build Ver.
571 *| -------- ------------------------- ---- ---------
572 *| OLE 2.1 NT 1993-95 10 3023
574 *| Win32s Ver 1.1e 20 4049
575 *| OLE 2.20 W95/NT 1993-96 20 4112
576 *| OLE 2.20 W95/NT 1993-96 20 4118
577 *| OLE 2.20 W95/NT 1993-96 20 4122
578 *| OLE 2.30 W95/NT 1993-98 30 4265
579 *| OLE 2.40 NT?? 1993-98 40 4267
580 *| OLE 2.40 W98 SE orig. file 1993-98 40 4275
581 *| OLE 2.40 W2K orig. file 1993-XX 40 4514
583 * Currently the versions returned are 2.20 for Win3.1, 2.30 for Win95 & NT 3.51,
584 * and 2.40 for all later versions. The build number is maximum, i.e. 0xffff.
586 ULONG WINAPI OaBuildVersion(void)
588 switch(GetVersion() & 0x8000ffff) /* mask off build number */
590 case 0x80000a03: /* WIN31 */
591 return MAKELONG(0xffff, 20);
592 case 0x00003303: /* NT351 */
593 return MAKELONG(0xffff, 30);
594 case 0x80000004: /* WIN95; I'd like to use the "standard" w95 minor
595 version here (30), but as we still use w95
596 as default winver (which is good IMHO), I better
597 play safe and use the latest value for w95 for now.
598 Change this as soon as default winver gets changed
599 to something more recent */
600 case 0x80000a04: /* WIN98 */
601 case 0x00000004: /* NT40 */
602 case 0x00000005: /* W2K */
603 return MAKELONG(0xffff, 40);
604 case 0x00000105: /* WinXP */
605 case 0x00000006: /* Vista */
606 case 0x00000106: /* Win7 */
607 return MAKELONG(0xffff, 50);
609 FIXME("Version value not known yet. Please investigate it !\n");
610 return MAKELONG(0xffff, 40); /* for now return the same value as for w2k */
614 /******************************************************************************
615 * OleTranslateColor [OLEAUT32.421]
617 * Convert an OLE_COLOR to a COLORREF.
620 * clr [I] Color to convert
621 * hpal [I] Handle to a palette for the conversion
622 * pColorRef [O] Destination for converted color, or NULL to test if the conversion is ok
625 * Success: S_OK. The conversion is ok, and pColorRef contains the converted color if non-NULL.
626 * Failure: E_INVALIDARG, if any argument is invalid.
629 * Document the conversion rules.
631 HRESULT WINAPI OleTranslateColor(
637 BYTE b = HIBYTE(HIWORD(clr));
639 TRACE("(%08x, %p, %p)\n", clr, hpal, pColorRef);
642 * In case pColorRef is NULL, provide our own to simplify the code.
644 if (pColorRef == NULL)
645 pColorRef = &colorref;
652 *pColorRef = PALETTERGB(GetRValue(clr),
667 * Validate the palette index.
669 if (GetPaletteEntries(hpal, LOWORD(clr), 1, &pe) == 0)
684 int index = LOBYTE(LOWORD(clr));
687 * Validate GetSysColor index.
689 if ((index < COLOR_SCROLLBAR) || (index > COLOR_MENUBAR))
692 *pColorRef = GetSysColor(index);
704 extern HRESULT WINAPI OLEAUTPS_DllGetClassObject(REFCLSID, REFIID, LPVOID *) DECLSPEC_HIDDEN;
705 extern BOOL WINAPI OLEAUTPS_DllMain(HINSTANCE, DWORD, LPVOID) DECLSPEC_HIDDEN;
706 extern HRESULT WINAPI OLEAUTPS_DllRegisterServer(void) DECLSPEC_HIDDEN;
707 extern HRESULT WINAPI OLEAUTPS_DllUnregisterServer(void) DECLSPEC_HIDDEN;
708 extern GUID const CLSID_PSFactoryBuffer DECLSPEC_HIDDEN;
710 extern void _get_STDFONT_CF(LPVOID *);
711 extern void _get_STDPIC_CF(LPVOID *);
713 static HRESULT WINAPI PSDispatchFacBuf_QueryInterface(IPSFactoryBuffer *iface, REFIID riid, void **ppv)
715 if (IsEqualIID(riid, &IID_IUnknown) ||
716 IsEqualIID(riid, &IID_IPSFactoryBuffer))
718 IUnknown_AddRef(iface);
722 return E_NOINTERFACE;
725 static ULONG WINAPI PSDispatchFacBuf_AddRef(IPSFactoryBuffer *iface)
730 static ULONG WINAPI PSDispatchFacBuf_Release(IPSFactoryBuffer *iface)
735 static HRESULT WINAPI PSDispatchFacBuf_CreateProxy(IPSFactoryBuffer *iface, IUnknown *pUnkOuter, REFIID riid, IRpcProxyBuffer **ppProxy, void **ppv)
737 IPSFactoryBuffer *pPSFB;
740 if (IsEqualIID(riid, &IID_IDispatch))
741 hr = OLEAUTPS_DllGetClassObject(&CLSID_PSFactoryBuffer, &IID_IPSFactoryBuffer, (void **)&pPSFB);
743 hr = TMARSHAL_DllGetClassObject(&CLSID_PSOAInterface, &IID_IPSFactoryBuffer, (void **)&pPSFB);
745 if (FAILED(hr)) return hr;
747 hr = IPSFactoryBuffer_CreateProxy(pPSFB, pUnkOuter, riid, ppProxy, ppv);
749 IPSFactoryBuffer_Release(pPSFB);
753 static HRESULT WINAPI PSDispatchFacBuf_CreateStub(IPSFactoryBuffer *iface, REFIID riid, IUnknown *pUnkOuter, IRpcStubBuffer **ppStub)
755 IPSFactoryBuffer *pPSFB;
758 if (IsEqualIID(riid, &IID_IDispatch))
759 hr = OLEAUTPS_DllGetClassObject(&CLSID_PSFactoryBuffer, &IID_IPSFactoryBuffer, (void **)&pPSFB);
761 hr = TMARSHAL_DllGetClassObject(&CLSID_PSOAInterface, &IID_IPSFactoryBuffer, (void **)&pPSFB);
763 if (FAILED(hr)) return hr;
765 hr = IPSFactoryBuffer_CreateStub(pPSFB, riid, pUnkOuter, ppStub);
767 IPSFactoryBuffer_Release(pPSFB);
771 static const IPSFactoryBufferVtbl PSDispatchFacBuf_Vtbl =
773 PSDispatchFacBuf_QueryInterface,
774 PSDispatchFacBuf_AddRef,
775 PSDispatchFacBuf_Release,
776 PSDispatchFacBuf_CreateProxy,
777 PSDispatchFacBuf_CreateStub
780 /* This is the whole PSFactoryBuffer object, just the vtableptr */
781 static const IPSFactoryBufferVtbl *pPSDispatchFacBuf = &PSDispatchFacBuf_Vtbl;
783 /***********************************************************************
784 * DllGetClassObject (OLEAUT32.@)
786 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
789 if (IsEqualGUID(rclsid,&CLSID_StdFont)) {
790 if (IsEqualGUID(iid,&IID_IClassFactory)) {
791 _get_STDFONT_CF(ppv);
792 IClassFactory_AddRef((IClassFactory*)*ppv);
796 if (IsEqualGUID(rclsid,&CLSID_StdPicture)) {
797 if (IsEqualGUID(iid,&IID_IClassFactory)) {
799 IClassFactory_AddRef((IClassFactory*)*ppv);
803 if (IsEqualCLSID(rclsid, &CLSID_PSDispatch) && IsEqualIID(iid, &IID_IPSFactoryBuffer)) {
804 *ppv = &pPSDispatchFacBuf;
805 IPSFactoryBuffer_AddRef((IPSFactoryBuffer *)*ppv);
808 if (IsEqualGUID(rclsid,&CLSID_PSOAInterface)) {
809 if (S_OK==TMARSHAL_DllGetClassObject(rclsid,iid,ppv))
813 if (IsEqualCLSID(rclsid, &CLSID_PSTypeInfo) ||
814 IsEqualCLSID(rclsid, &CLSID_PSTypeLib) ||
815 IsEqualCLSID(rclsid, &CLSID_PSDispatch) ||
816 IsEqualCLSID(rclsid, &CLSID_PSEnumVariant))
817 return OLEAUTPS_DllGetClassObject(&CLSID_PSFactoryBuffer, iid, ppv);
819 return OLEAUTPS_DllGetClassObject(rclsid, iid, ppv);
822 /***********************************************************************
823 * DllCanUnloadNow (OLEAUT32.@)
825 * Determine if this dll can be unloaded from the callers address space.
831 * Always returns S_FALSE. This dll cannot be unloaded.
833 HRESULT WINAPI DllCanUnloadNow(void)
838 /*****************************************************************************
839 * DllMain [OLEAUT32.@]
841 BOOL WINAPI DllMain(HINSTANCE hInstDll, DWORD fdwReason, LPVOID lpvReserved)
843 return OLEAUTPS_DllMain( hInstDll, fdwReason, lpvReserved );
846 /***********************************************************************
847 * DllRegisterServer (OLEAUT32.@)
849 HRESULT WINAPI DllRegisterServer(void)
851 return OLEAUTPS_DllRegisterServer();
854 /***********************************************************************
855 * DllUnregisterServer (OLEAUT32.@)
857 HRESULT WINAPI DllUnregisterServer(void)
859 return OLEAUTPS_DllUnregisterServer();
862 /***********************************************************************
863 * OleIconToCursor (OLEAUT32.415)
865 HCURSOR WINAPI OleIconToCursor( HINSTANCE hinstExe, HICON hIcon)
867 FIXME("(%p,%p), partially implemented.\n",hinstExe,hIcon);
868 /* FIXME: make a extended conversation from HICON to HCURSOR */
869 return CopyCursor(hIcon);