Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
1.3 KiB

  1. // Copyright (c) 1997-1999 Microsoft Corporation
  2. //
  3. // COM utility code
  4. //
  5. // 2-3-99 sburns
  6. #ifndef COMSTUFF_HPP_INCLUDED
  7. #define COMSTUFF_HPP_INCLUDED
  8. // Ensures that the type of the pointer to receive the interface pointer
  9. // matches the IID of the interface. If it doesn't, the static_cast will
  10. // cause a compiler error.
  11. //
  12. // Example:
  13. // IFoo* fooptr = 0;
  14. // HRESULT hr = punk->QueryInterface(QI_PARAMS(IFoo, &fooptr));
  15. //
  16. // From Box, D. Essential COM. pp 60-61. Addison-Wesley. ISBN 0-201-63446-5
  17. #define QI_PARAMS(Interface, ppvExpression) \
  18. IID_##Interface, reinterpret_cast<void**>(static_cast<Interface**>(ppvExpression))
  19. namespace Burnslib
  20. {
  21. // A BSTR wrapper that frees itself upon destruction.
  22. //
  23. // From Box, D. Essential COM. pp 80-81. Addison-Wesley. ISBN 0-201-63446-5
  24. class AutoBstr
  25. {
  26. public:
  27. explicit
  28. AutoBstr(const String& s)
  29. :
  30. bstr(::SysAllocString(const_cast<wchar_t*>(s.c_str())))
  31. {
  32. }
  33. explicit
  34. AutoBstr(const wchar_t* s)
  35. :
  36. bstr(::SysAllocString(s))
  37. {
  38. ASSERT(s);
  39. }
  40. ~AutoBstr()
  41. {
  42. ::SysFreeString(bstr);
  43. bstr = 0;
  44. }
  45. operator BSTR () const
  46. {
  47. return bstr;
  48. }
  49. private:
  50. BSTR bstr;
  51. };
  52. } // namespace Burnslib
  53. #endif // COMSTUFF_HPP_INCLUDED