4 * Copyright 2009 Andrew Hill <ash77@reactos.org>
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 Street, Fifth Floor, Boston, MA 02110-1301 USA
27 // It is common to use this in ATL constructors. They only store this for later use, so the usage is safe.
28 #pragma warning(disable:4355)
32 #define _ATL_PACKING 8
35 #ifndef _ATL_FREE_THREADED
36 #ifndef _ATL_APARTMENT_THREADED
37 #ifndef _ATL_SINGLE_THREADED
38 #define _ATL_FREE_THREADED
47 #ifdef _ATL_DISABLE_NO_VTABLE
50 #define ATL_NO_VTABLE __declspec(novtable)
56 inline HRESULT
AtlHresultFromLastError() throw()
58 DWORD dwError
= ::GetLastError();
59 return HRESULT_FROM_WIN32(dwError
);
81 CComPtr(const CComPtr
<T
> &lp
)
108 T
*operator = (const CComPtr
<T
> &lp
)
122 // We cannot enable this until gcc starts supporting __uuidof
125 template <typename Q
>
126 T
* operator=(const CComPtr
<Q
>& lp
)
130 if (!lp
.p
|| FAILED(lp
.p
->QueryInterface(__uuidof(T
), (void**)(IUnknown
**)&p
)))
167 ATLASSERT(p
== NULL
);
178 ATLASSERT(p
!= NULL
);
184 //CComQIIDPtr<I_ID(Itype)> is the gcc compatible version of CComQIPtr<Itype>
185 #define I_ID(Itype) Itype,&IID_##Itype
187 template <class T
, const IID
* piid
>
192 // Let's tell GCC how to find a symbol.
198 CComQIIDPtr(_Inout_opt_ T
* lp
) :
202 CComQIIDPtr(_Inout_
const CComQIIDPtr
<T
,piid
>& lp
):
206 CComQIIDPtr(_Inout_opt_ IUnknown
* lp
)
210 if (FAILED(lp
->QueryInterface(*piid
, (void**)(IUnknown
**)&p
)))
214 T
*operator = (T
*lp
)
228 T
*operator = (const CComQIIDPtr
<T
,piid
> &lp
)
242 T
* operator=(IUnknown
* lp
)
246 if (!lp
|| FAILED(lp
->QueryInterface(*piid
, (void**)(IUnknown
**)&p
)))
267 CComBSTR(LPCOLESTR pSrc
)
272 m_str
= ::SysAllocString(pSrc
);
280 m_str
= ::SysAllocStringLen(NULL
, length
);
283 CComBSTR(int length
, LPCOLESTR pSrc
)
288 m_str
= ::SysAllocStringLen(pSrc
, length
);
295 int len
= MultiByteToWideChar(CP_THREAD_ACP
, 0, pSrc
, -1, NULL
, 0);
296 m_str
= ::SysAllocStringLen(NULL
, len
- 1);
299 int res
= MultiByteToWideChar(CP_THREAD_ACP
, 0, pSrc
, -1, m_str
, len
);
300 ATLASSERT(res
== len
);
303 ::SysFreeString(m_str
);
314 CComBSTR(const CComBSTR
&other
)
316 m_str
= other
.Copy();
319 CComBSTR(REFGUID guid
)
322 ::StringFromGUID2(guid
, szGuid
, 40);
323 m_str
= ::SysAllocString(szGuid
);
328 ::SysFreeString(m_str
);
332 operator BSTR () const
342 CComBSTR
&operator = (const CComBSTR
&other
)
344 ::SysFreeString(m_str
);
345 m_str
= other
.Copy();
349 void Attach(BSTR bstr
)
351 ::SysFreeString(m_str
);
366 return ::SysAllocStringLen(m_str
, ::SysStringLen(m_str
));
369 HRESULT
CopyTo(BSTR
*other
) const
377 bool LoadString(HMODULE module
, DWORD uID
)
379 ::SysFreeString(m_str
);
381 const wchar_t *ptr
= NULL
;
382 int len
= ::LoadStringW(module
, uID
, (PWSTR
)&ptr
, 0);
384 m_str
= ::SysAllocStringLen(ptr
, len
);
385 return m_str
!= NULL
;
388 unsigned int Length() const
390 return ::SysStringLen(m_str
);
393 unsigned int ByteLength() const
395 return ::SysStringByteLen(m_str
);
400 class CComVariant
: public tagVARIANT
408 CComVariant(const CComVariant
& other
)
410 V_VT(this) = VT_EMPTY
;
419 CComVariant(LPCOLESTR lpStr
)
421 V_VT(this) = VT_BSTR
;
422 V_BSTR(this) = ::SysAllocString(lpStr
);
425 CComVariant(LPCSTR lpStr
)
427 V_VT(this) = VT_BSTR
;
429 V_BSTR(this) = str
.Detach();
432 CComVariant(bool value
)
434 V_VT(this) = VT_BOOL
;
435 V_BOOL(this) = value
? VARIANT_TRUE
: VARIANT_FALSE
;
438 CComVariant(char value
)
444 CComVariant(BYTE value
)
450 CComVariant(short value
)
456 CComVariant(unsigned short value
)
462 CComVariant(int value
, VARENUM type
= VT_I4
)
464 if (type
== VT_I4
|| type
== VT_INT
)
471 V_VT(this) = VT_ERROR
;
472 V_ERROR(this) = E_INVALIDARG
;
476 CComVariant(unsigned int value
, VARENUM type
= VT_UI4
)
478 if (type
== VT_UI4
|| type
== VT_UINT
)
485 V_VT(this) = VT_ERROR
;
486 V_ERROR(this) = E_INVALIDARG
;
490 CComVariant(long value
, VARENUM type
= VT_I4
)
492 if (type
== VT_I4
|| type
== VT_ERROR
)
499 V_VT(this) = VT_ERROR
;
500 V_ERROR(this) = E_INVALIDARG
;
504 CComVariant(unsigned long value
)
510 CComVariant(float value
)
516 CComVariant(double value
, VARENUM type
= VT_R8
)
518 if (type
== VT_R8
|| type
== VT_DATE
)
525 V_VT(this) = VT_ERROR
;
526 V_ERROR(this) = E_INVALIDARG
;
530 CComVariant(const LONGLONG
& value
)
536 CComVariant(const ULONGLONG
& value
)
542 CComVariant(const CY
& value
)
545 V_I8(this) = value
.int64
;
551 return ::VariantClear(this);
554 HRESULT
Copy(_In_
const VARIANT
* src
)
556 return ::VariantCopy(this, const_cast<VARIANT
*>(src
));
559 HRESULT
ChangeType(_In_ VARTYPE newType
, _In_opt_
const LPVARIANT src
= NULL
)
561 const LPVARIANT lpSrc
= src
? src
: this;
562 return ::VariantChangeType(this, lpSrc
, 0, newType
);
570 #ifndef _ATL_NO_AUTOMATIC_NAMESPACE
572 #endif //!_ATL_NO_AUTOMATIC_NAMESPACE