Mobile API Reference  MicroStrategy 2019
Msi_ccombstr.h
Go to the documentation of this file.
1 // File: ccombstr.h
2 // Date: 8/20/2001
3 // Author: Yuling Ma
4 //
5 // Copyright (C) 2001 MicroStrategy Incorporated
6 // All rights reserved
7 
8 // This header file is for defining the CComBstr related structures, symbols and functions.
9 // On NT platform, the these structures, symbols and functions are defined in the following
10 // header files: atlbase.h
11 #ifndef _CCOMBSTR_H_
12 #define _CCOMBSTR_H_
13 
14 #include "PDCHeader/PDCwtypes.h"
15 #include "PDCHeader/PDCwchar.h"
16 #include "PDCHeader/PDCmemory.h"
17 #include "PDCHeader/PDCmalloc.h"
18 #include "PDCHeader/PDCwctype.h"
19 
20 #include "Msi_objbase.h"
21 #include "Msi_bstr.h"
22 #include "Msi_objidl.h"
23 #include "Msi_winuser.h"
24 
26 // define in ATLCONV.h
28 
29 // TODO: remove this symbol from source code
30 #define USES_CONVERSION
31 
33 // CComBSTR
34 class CComBSTR
35 {
36 public:
39  {
40  m_str = NULL;
41  }
42  /*explicit*/ CComBSTR(int nSize)
43  {
44  m_str = ::SysAllocStringLen(NULL, nSize);
45  }
46  /*explicit*/ CComBSTR(int nSize, LPCOLESTR sz)
47  {
48  m_str = ::SysAllocStringLen(sz, nSize);
49  }
50  /*explicit*/ CComBSTR(LPCOLESTR pSrc)
51  {
52  m_str = ::SysAllocString(pSrc);
53  }
54  /*explicit*/ CComBSTR(const CComBSTR& src)
55  {
56  m_str = src.Copy();
57  }
58  /*explicit*/ CComBSTR(REFGUID src)
59  {
60  LPOLESTR szGuid;
61  StringFromCLSID(src, &szGuid);
62  m_str = ::SysAllocString(szGuid);
63  CoTaskMemFree(szGuid);
64 
65  }
67  {
68  if (m_str != src.m_str)
69  {
70  if (m_str)
71  ::SysFreeString(m_str);
72  m_str = src.Copy();
73  }
74  return *this;
75  }
76 
78  {
79  ::SysFreeString(m_str);
80  m_str = ::SysAllocString(pSrc);
81  return *this;
82  }
83 
85  {
86  ::SysFreeString(m_str);
87  }
88  unsigned int Length() const
89  {
90  return (m_str == NULL)? 0 : SysStringLen(m_str);
91  }
92  operator BSTR() const
93  {
94  return m_str;
95  }
97  {
98  return &m_str;
99  }
100  BSTR Copy() const
101  {
102  return ::SysAllocStringLen(m_str, ::SysStringLen(m_str));
103  }
105  {
106  ATLASSERT(pbstr != NULL);
107  if (pbstr == NULL)
108  return E_POINTER;
109  *pbstr = ::SysAllocStringLen(m_str, ::SysStringLen(m_str));
110  if (*pbstr == NULL)
111  return E_OUTOFMEMORY;
112  return S_OK;
113  }
114  void Attach(BSTR src)
115  {
116  ATLASSERT(m_str == NULL);
117  m_str = src;
118  }
120  {
121  BSTR s = m_str;
122  m_str = NULL;
123  return s;
124  }
125  void Empty()
126  {
127  ::SysFreeString(m_str);
128  m_str = NULL;
129  }
130  bool operator!() const
131  {
132  return (m_str == NULL);
133  }
134  HRESULT Append(const CComBSTR& bstrSrc)
135  {
136  return Append(bstrSrc.m_str, SysStringLen(bstrSrc.m_str));
137  }
139  {
140 #ifdef NO_WCHAR_SUPPORT
141  return Append(lpsz, ::strlen(lpsz));
142 #else
143  return Append(lpsz, ::wcslen(lpsz));
144 #endif
145  }
146 
147  // a BSTR is just a LPCOLESTR so we need a special version to signify
148  // that we are appending a BSTR
150  {
151  return Append(p, SysStringLen(p));
152  }
153  HRESULT Append(LPCOLESTR lpsz, size_t nLen)
154  {
155  int n1 = Length();
156  BSTR b;
157  b = ::SysAllocStringLen(NULL, (UINT)(n1+nLen));
158  if (b == NULL)
159  return E_OUTOFMEMORY;
160  memmove(b, m_str, n1*sizeof(OLECHAR));
161  memmove(b+n1, lpsz, nLen*sizeof(OLECHAR));
162  b[n1+nLen] = 0;
163  SysFreeString(m_str);
164  m_str = b;
165  return S_OK;
166  }
168  {
169  if (m_str != NULL)
170  {
171 #ifdef NO_WCHAR_SUPPORT
172  LPTSTR psz = CharLowerA(m_str);
173 #else
174  LPTSTR psz = CharLowerW(m_str);
175 #endif
176  if (psz == NULL)
177  return E_OUTOFMEMORY;
178  BSTR b = ::SysAllocString(psz);
179  if (psz == NULL)
180  return E_OUTOFMEMORY;
181  SysFreeString(m_str);
182  m_str = b;
183  }
184  return S_OK;
185  }
187  {
188  if (m_str != NULL)
189  {
190 #ifdef NO_WCHAR_SUPPORT
191  LPTSTR psz = CharLowerA(m_str);
192 #else
193  LPTSTR psz = CharLowerW(m_str);
194 #endif
195  if (psz == NULL)
196  return E_OUTOFMEMORY;
197  BSTR b = ::SysAllocString(psz);
198  if (psz == NULL)
199  return E_OUTOFMEMORY;
200  SysFreeString(m_str);
201  m_str = b;
202  }
203  return S_OK;
204  }
205 
206  CComBSTR& operator+=(const CComBSTR& bstrSrc)
207  {
208  AppendBSTR(bstrSrc.m_str);
209  return *this;
210  }
211  bool operator<(BSTR bstrSrc) const
212  {
213  if (bstrSrc == NULL && m_str == NULL)
214  return false;
215  if (bstrSrc != NULL && m_str != NULL)
216 #ifdef NO_WCHAR_SUPPORT
217  return strcmp(m_str, bstrSrc) < 0;
218 #else
219  return wcscmp(m_str, bstrSrc) < 0;
220 #endif
221  return m_str == NULL;
222  }
223  bool operator==(BSTR bstrSrc) const
224  {
225  if (bstrSrc == NULL && m_str == NULL)
226  return true;
227  if (bstrSrc != NULL && m_str != NULL)
228 #ifdef NO_WCHAR_SUPPORT
229  return strcmp(m_str, bstrSrc) == 0;
230 #else
231  return wcscmp(m_str, bstrSrc) == 0;
232 #endif
233  return false;
234  }
235  bool operator!=(BSTR bstrSrc) const
236  {
237  return !(*this == bstrSrc);
238  }
239 
241  {
242  ATLASSERT(pStream != NULL);
243  ULONG cb;
244  ULONG cbStrLen = m_str ? SysStringByteLen(m_str)+sizeof(OLECHAR) : 0;
245  HRESULT hr = pStream->Write((void*) &cbStrLen, sizeof(cbStrLen), &cb);
246  if (FAILED(hr))
247  return hr;
248  return cbStrLen ? pStream->Write((void*) m_str, cbStrLen, &cb) : S_OK;
249  }
251  {
252  ATLASSERT(pStream != NULL);
253  ATLASSERT(m_str == NULL); // should be empty
254  ULONG cbStrLen = 0;
255  HRESULT hr = pStream->Read((void*) &cbStrLen, sizeof(cbStrLen), NULL);
256  if ((hr == S_OK) && (cbStrLen != 0))
257  {
258  //subtract size for terminating NULL which we wrote out
259  //since SysAllocStringByteLen overallocates for the NULL
260  //msun, 4-23-2015, temporarily avoid the warnning message by explicitly converting ULONG to UINT. Need to refine in the future.
261  m_str = SysAllocStringByteLen(NULL, (UINT)cbStrLen-sizeof(OLECHAR));
262  if (m_str == NULL)
263  hr = E_OUTOFMEMORY;
264  else
265  hr = pStream->Read((void*) m_str, cbStrLen, NULL);
266  }
267  if (hr == S_FALSE)
268  hr = E_FAIL;
269  return hr;
270  }
271 };
272 
273 #endif /* _CCOMBSTR_H_ */
274 
275 
276 
CComBSTR(REFGUID src)
Definition: Msi_ccombstr.h:58
virtual HRESULT STDMETHODCALLTYPE Write(const void __RPC_FAR *pv, ULONG cb, ULONG __RPC_FAR *pcbWritten)=0
OLECHAR __RPC_FAR * BSTR
Definition: PDCwtypes.h:401
#define REFGUID
Definition: PDCwtypes.h:318
LPOLESTR lpsz
Definition: Msi_objbase.h:85
BSTR Detach()
Definition: Msi_ccombstr.h:119
void Empty()
Definition: Msi_ccombstr.h:125
#define E_POINTER
Definition: PDCwinerror.h:141
#define s(x, c)
Definition: aesopt.h:408
Definition: Msi_ccombstr.h:34
HRESULT Append(LPCOLESTR lpsz)
Definition: Msi_ccombstr.h:138
CComBSTR()
Definition: Msi_ccombstr.h:38
HRESULT CopyTo(BSTR *pbstr)
Definition: Msi_ccombstr.h:104
void Attach(BSTR src)
Definition: Msi_ccombstr.h:114
BSTR m_str
Definition: Msi_ccombstr.h:37
HRESULT AppendBSTR(BSTR p)
Definition: Msi_ccombstr.h:149
long HRESULT
Definition: FragmentedString.h:20
HRESULT ToLower()
Definition: Msi_ccombstr.h:167
#define ATLASSERT(expr)
Definition: Msi_atldef.h:18
CComBSTR & operator+=(const CComBSTR &bstrSrc)
Definition: Msi_ccombstr.h:206
#define S_FALSE
Definition: PDCwinerror.h:32
HRESULT Append(const CComBSTR &bstrSrc)
Definition: Msi_ccombstr.h:134
unsigned int UINT
Definition: PDCwtypes.h:110
#define S_OK
Definition: PDCwinerror.h:31
Definition: Msi_objidl.h:80
CComBSTR(int nSize, LPCOLESTR sz)
Definition: Msi_ccombstr.h:46
#define FAILED(Status)
Definition: PDCwinerror.h:47
BSTR Copy() const
Definition: Msi_ccombstr.h:100
WINOLEAPI DLL_MSICOM_EXIM StringFromCLSID(REFCLSID rclsid, LPOLESTR FAR *lplpsz)
WCHAR OLECHAR
Definition: PDCwtypes.h:390
CComBSTR & operator=(LPCOLESTR pSrc)
Definition: Msi_ccombstr.h:77
BSTR * operator &()
Definition: Msi_ccombstr.h:96
bool operator!() const
Definition: Msi_ccombstr.h:130
HRESULT ReadFromStream(IStream *pStream)
Definition: Msi_ccombstr.h:250
~CComBSTR()
Definition: Msi_ccombstr.h:84
HRESULT WriteToStream(IStream *pStream)
Definition: Msi_ccombstr.h:240
#define E_FAIL
Definition: PDCwinerror.h:168
LPWSTR DLL_MSICOM_EXIM WINAPI CharLowerW(LPWSTR lpsz)
CComBSTR(LPCOLESTR pSrc)
Definition: Msi_ccombstr.h:50
HRESULT Append(LPCOLESTR lpsz, size_t nLen)
Definition: Msi_ccombstr.h:153
#define E_OUTOFMEMORY
Definition: PDCwinerror.h:114
ULONG cb
Definition: Msi_objbase.h:25
HRESULT ToUpper()
Definition: Msi_ccombstr.h:186
TCHAR __RPC_FAR * LPTSTR
Definition: PDCwtypes.h:371
bool operator!=(BSTR bstrSrc) const
Definition: Msi_ccombstr.h:235
bool operator==(BSTR bstrSrc) const
Definition: Msi_ccombstr.h:223
bool operator<(BSTR bstrSrc) const
Definition: Msi_ccombstr.h:211
virtual HRESULT STDMETHODCALLTYPE Read(void __RPC_FAR *pv, ULONG cb, ULONG __RPC_FAR *pcbRead)=0
CComBSTR & operator=(const CComBSTR &src)
Definition: Msi_ccombstr.h:66
OLECHAR __RPC_FAR * LPOLESTR
Definition: PDCwtypes.h:392
unsigned int Length() const
Definition: Msi_ccombstr.h:88
CComBSTR(const CComBSTR &src)
Definition: Msi_ccombstr.h:54
CComBSTR(int nSize)
Definition: Msi_ccombstr.h:42
const OLECHAR __RPC_FAR * LPCOLESTR
Definition: PDCwtypes.h:394
DWORD ULONG
Definition: PDCwtypes.h:127
#define NULL
Definition: Null.h:10