Leaked source code of windows server 2003
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.

124 lines
1.8 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. }
  39. ~AutoBstr()
  40. {
  41. ::SysFreeString(bstr);
  42. bstr = 0;
  43. }
  44. operator BSTR () const
  45. {
  46. return bstr;
  47. }
  48. private:
  49. BSTR bstr;
  50. };
  51. class AutoCoInitialize
  52. {
  53. public:
  54. AutoCoInitialize()
  55. {
  56. hr = ::CoInitialize(0);
  57. }
  58. // uncomment if this ever becomes necessary
  59. // AutoCoInitialize(DWORD dwCoInit)
  60. // {
  61. // hr = ::CoInitializeEx(0, dwCoInit);
  62. // }
  63. ~AutoCoInitialize()
  64. {
  65. if (SUCCEEDED(hr))
  66. {
  67. ::CoUninitialize();
  68. }
  69. }
  70. HRESULT
  71. Result() const
  72. {
  73. return hr;
  74. }
  75. private:
  76. HRESULT hr;
  77. };
  78. } // namespace Burnslib
  79. #endif // COMSTUFF_HPP_INCLUDED