📄 oleaut.c
字号:
/*
* OLEAUT32
*
* Copyright 1999, 2000 Marcus Meissner
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <stdarg.h>
#include <string.h>
#include <limits.h>
#define COBJMACROS
#include "windef.h"
#include "winbase.h"
#include "wingdi.h"
#include "winuser.h"
#include "winerror.h"
#include "ole2.h"
#include "olectl.h"
#include "oleauto.h"
#include "typelib.h"
#include "wine/debug.h"
WINE_DEFAULT_DEBUG_CHANNEL(ole);
static BOOL BSTR_bCache = TRUE; /* Cache allocations to minimise alloc calls? */
HMODULE OLEAUT32_hModule = NULL;
/******************************************************************************
* BSTR {OLEAUT32}
*
* NOTES
* BSTR is a simple typedef for a wide-character string used as the principle
* string type in ole automation. When encapsulated in a Variant type they are
* automatically copied and destroyed as the variant is processed.
*
* The low level BSTR Api allows manipulation of these strings and is used by
* higher level Api calls to manage the strings transparently to the caller.
*
* Internally the BSTR type is allocated with space for a DWORD byte count before
* the string data begins. This is undocumented and non-system code should not
* access the count directly. Use SysStringLen() or SysStringByteLen()
* instead. Note that the byte count does not include the terminating NUL.
*
* To create a new BSTR, use SysAllocString(), SysAllocStringLen() or
* SysAllocStringByteLen(). To change the size of an existing BSTR, use SysReAllocString()
* or SysReAllocStringLen(). Finally to destroy a string use SysFreeString().
*
* BSTR's are cached by Ole Automation by default. To override this behaviour
* either set the environment variable 'OANOCACHE', or call SetOaNoCache().
*
* SEE ALSO
* 'Inside OLE, second edition' by Kraig Brockshmidt.
*/
/******************************************************************************
* SysStringLen [OLEAUT32.7]
*
* Get the allocated length of a BSTR in wide characters.
*
* PARAMS
* str [I] BSTR to find the length of
*
* RETURNS
* The allocated length of str, or 0 if str is NULL.
*
* NOTES
* See BSTR.
* The returned length may be different from the length of the string as
* calculated by lstrlenW(), since it returns the length that was used to
* allocate the string by SysAllocStringLen().
*/
UINT WINAPI SysStringLen(BSTR str)
{
DWORD* bufferPointer;
if (!str) return 0;
/*
* The length of the string (in bytes) is contained in a DWORD placed
* just before the BSTR pointer
*/
bufferPointer = (DWORD*)str;
bufferPointer--;
return (int)(*bufferPointer/sizeof(WCHAR));
}
/******************************************************************************
* SysStringByteLen [OLEAUT32.149]
*
* Get the allocated length of a BSTR in bytes.
*
* PARAMS
* str [I] BSTR to find the length of
*
* RETURNS
* The allocated length of str, or 0 if str is NULL.
*
* NOTES
* See SysStringLen(), BSTR().
*/
UINT WINAPI SysStringByteLen(BSTR str)
{
DWORD* bufferPointer;
if (!str) return 0;
/*
* The length of the string (in bytes) is contained in a DWORD placed
* just before the BSTR pointer
*/
bufferPointer = (DWORD*)str;
bufferPointer--;
return (int)(*bufferPointer);
}
/******************************************************************************
* SysAllocString [OLEAUT32.2]
*
* Create a BSTR from an OLESTR.
*
* PARAMS
* str [I] Source to create BSTR from
*
* RETURNS
* Success: A BSTR allocated with SysAllocStringLen().
* Failure: NULL, if oleStr is NULL.
*
* NOTES
* See BSTR.
* MSDN (October 2001) incorrectly states that NULL is returned if oleStr has
* a length of 0. Native Win32 and this implementation both return a valid
* empty BSTR in this case.
*/
BSTR WINAPI SysAllocString(LPCOLESTR str)
{
if (!str) return 0;
/* Delegate this to the SysAllocStringLen32 method. */
return SysAllocStringLen(str, lstrlenW(str));
}
/******************************************************************************
* SysFreeString [OLEAUT32.6]
*
* Free a BSTR.
*
* PARAMS
* str [I] BSTR to free.
*
* RETURNS
* Nothing.
*
* NOTES
* See BSTR.
* str may be NULL, in which case this function does nothing.
*/
void WINAPI SysFreeString(BSTR str)
{
DWORD* bufferPointer;
/* NULL is a valid parameter */
if(!str) return;
/*
* We have to be careful when we free a BSTR pointer, it points to
* the beginning of the string but it skips the byte count contained
* before the string.
*/
bufferPointer = (DWORD*)str;
bufferPointer--;
/*
* Free the memory from its "real" origin.
*/
HeapFree(GetProcessHeap(), 0, bufferPointer);
}
/******************************************************************************
* SysAllocStringLen [OLEAUT32.4]
*
* Create a BSTR from an OLESTR of a given wide character length.
*
* PARAMS
* str [I] Source to create BSTR from
* len [I] Length of oleStr in wide characters
*
* RETURNS
* Success: A newly allocated BSTR from SysAllocStringByteLen()
* Failure: NULL, if len is >= 0x80000000, or memory allocation fails.
*
* NOTES
* See BSTR(), SysAllocStringByteLen().
*/
BSTR WINAPI SysAllocStringLen(const OLECHAR *str, unsigned int len)
{
DWORD bufferSize;
DWORD* newBuffer;
WCHAR* stringBuffer;
/* Detect integer overflow. */
if (len >= ((UINT_MAX-sizeof(WCHAR)-sizeof(DWORD))/sizeof(WCHAR)))
return NULL;
/*
* Find the length of the buffer passed-in, in bytes.
*/
bufferSize = len * sizeof (WCHAR);
/*
* Allocate a new buffer to hold the string.
* don't forget to keep an empty spot at the beginning of the
* buffer for the character count and an extra character at the
* end for the NULL.
*/
newBuffer = HeapAlloc(GetProcessHeap(), 0,
bufferSize + sizeof(WCHAR) + sizeof(DWORD));
/*
* If the memory allocation failed, return a null pointer.
*/
if (!newBuffer)
return NULL;
/*
* Copy the length of the string in the placeholder.
*/
*newBuffer = bufferSize;
/*
* Skip the byte count.
*/
newBuffer++;
/*
* Copy the information in the buffer.
* Since it is valid to pass a NULL pointer here, we'll initialize the
* buffer to nul if it is the case.
*/
if (str != 0)
memcpy(newBuffer, str, bufferSize);
else
memset(newBuffer, 0, bufferSize);
/*
* Make sure that there is a nul character at the end of the
* string.
*/
stringBuffer = (WCHAR*)newBuffer;
stringBuffer[len] = '\0';
return (LPWSTR)stringBuffer;
}
/******************************************************************************
* SysReAllocStringLen [OLEAUT32.5]
*
* Change the length of a previously created BSTR.
*
* PARAMS
* old [O] BSTR to change the length of
* str [I] New source for pbstr
* len [I] Length of oleStr in wide characters
*
* RETURNS
* Success: 1. The size of pbstr is updated.
* Failure: 0, if len >= 0x80000000 or memory allocation fails.
*
* NOTES
* See BSTR(), SysAllocStringByteLen().
* *pbstr may be changed by this function.
*/
int WINAPI SysReAllocStringLen(BSTR* old, const OLECHAR* str, unsigned int len)
{
if (*old!=NULL) {
DWORD newbytelen = len*sizeof(WCHAR);
DWORD *ptr = HeapReAlloc(GetProcessHeap(),0,((DWORD*)*old)-1,newbytelen+sizeof(WCHAR)+sizeof(DWORD));
*old = (BSTR)(ptr+1);
*ptr = newbytelen;
if (str) {
memcpy(*old, str, newbytelen);
(*old)[len] = 0;
} else {
/* Subtle hidden feature: The old string data is still there
* when 'in' is NULL!
* Some Microsoft program needs it.
*/
}
} else {
/*
* Allocate the new string
*/
*old = SysAllocStringLen(str, len);
}
return 1;
}
/******************************************************************************
* SysAllocStringByteLen [OLEAUT32.150]
*
* Create a BSTR from an OLESTR of a given byte length.
*
* PARAMS
* str [I] Source to create BSTR from
* len [I] Length of oleStr in bytes
*
* RETURNS
* Success: A newly allocated BSTR
* Failure: NULL, if len is >= 0x80000000, or memory allocation fails.
*
* NOTES
* -If len is 0 or oleStr is NULL the resulting string is empty ("").
* -This function always NUL terminates the resulting BSTR.
* -oleStr may be either an LPCSTR or LPCOLESTR, since it is copied
* without checking for a terminating NUL.
* See BSTR.
*/
BSTR WINAPI SysAllocStringByteLen(LPCSTR str, UINT len)
{
DWORD* newBuffer;
char* stringBuffer;
/*
* Allocate a new buffer to hold the string.
* don't forget to keep an empty spot at the beginning of the
* buffer for the character count and an extra character at the
* end for the NULL.
*/
newBuffer = HeapAlloc(GetProcessHeap(), 0,
len + sizeof(WCHAR) + sizeof(DWORD));
/*
* If the memory allocation failed, return a null pointer.
*/
if (newBuffer==0)
return 0;
/*
* Copy the length of the string in the placeholder.
*/
*newBuffer = len;
/*
* Skip the byte count.
*/
newBuffer++;
/*
* Copy the information in the buffer.
* Since it is valid to pass a NULL pointer here, we'll initialize the
* buffer to nul if it is the case.
*/
if (str != 0)
memcpy(newBuffer, str, len);
/*
* Make sure that there is a nul character at the end of the
* string.
*/
stringBuffer = (char *)newBuffer;
stringBuffer[len] = 0;
stringBuffer[len+1] = 0;
return (LPWSTR)stringBuffer;
}
/******************************************************************************
* SysReAllocString [OLEAUT32.3]
*
* Change the length of a previously created BSTR.
*
* PARAMS
* old [I/O] BSTR to change the length of
* str [I] New source for pbstr
*
* RETURNS
* Success: 1
* Failure: 0.
*
* NOTES
* See BSTR(), SysAllocStringStringLen().
*/
INT WINAPI SysReAllocString(LPBSTR old,LPCOLESTR str)
{
/*
* Sanity check
*/
if (old==NULL)
return 0;
/*
* Make sure we free the old string.
*/
if (*old!=NULL)
SysFreeString(*old);
/*
* Allocate the new string
*/
*old = SysAllocString(str);
return 1;
}
/******************************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -