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.

164 lines
4.4 KiB

  1. //***************************************************************************
  2. //
  3. // Copyright (c) 1997-1999 Microsoft Corporation
  4. //
  5. // SmartPtr.h
  6. //
  7. // Purpose: Declare smartpointer typedefs
  8. //
  9. //***************************************************************************
  10. #pragma once
  11. #include <io.h>
  12. #include <comdef.h>
  13. _COM_SMARTPTR_TYPEDEF(CInstance, __uuidof(CInstance));
  14. _COM_SMARTPTR_TYPEDEF(IWbemClassObject, __uuidof(IWbemClassObject));
  15. _COM_SMARTPTR_TYPEDEF(IWbemQualifierSet, __uuidof(IWbemQualifierSet));
  16. _COM_SMARTPTR_TYPEDEF(IWbemObjectAccess, __uuidof(IWbemObjectAccess));
  17. _COM_SMARTPTR_TYPEDEF(IWbemServices, __uuidof(IWbemServices));
  18. _COM_SMARTPTR_TYPEDEF(IWbemContext, __uuidof(IWbemContext));
  19. _COM_SMARTPTR_TYPEDEF(IWbemCallResult, __uuidof(IWbemCallResult));
  20. _COM_SMARTPTR_TYPEDEF(IWbemObjectSink, __uuidof(IWbemObjectSink));
  21. _COM_SMARTPTR_TYPEDEF(IEnumWbemClassObject, __uuidof(IEnumWbemClassObject));
  22. _COM_SMARTPTR_TYPEDEF(IWbemLocator, __uuidof(IWbemLocator));
  23. _COM_SMARTPTR_TYPEDEF(IUnsecuredApartment, __uuidof(IUnsecuredApartment));
  24. _COM_SMARTPTR_TYPEDEF(IWbemStatusCodeText, __uuidof(IWbemStatusCodeText));
  25. _COM_SMARTPTR_TYPEDEF(IWbemRefresher, __uuidof(IWbemRefresher));
  26. _COM_SMARTPTR_TYPEDEF(IWbemHiPerfEnum, __uuidof(IWbemHiPerfEnum));
  27. _COM_SMARTPTR_TYPEDEF(IWbemConfigureRefresher, __uuidof(IWbemConfigureRefresher));
  28. _COM_SMARTPTR_TYPEDEF(IMofCompiler, __uuidof(IMofCompiler));
  29. _COM_SMARTPTR_TYPEDEF(ExternalMethodContext, __uuidof(ExternalMethodContext));
  30. _COM_SMARTPTR_TYPEDEF(InternalMethodContext, __uuidof(InternalMethodContext));
  31. _COM_SMARTPTR_TYPEDEF(InternalMethodContextAsynch, __uuidof(InternalMethodContextAsynch));
  32. class SmartCloseHandle
  33. {
  34. private:
  35. HANDLE m_h;
  36. public:
  37. SmartCloseHandle():m_h(INVALID_HANDLE_VALUE){}
  38. SmartCloseHandle(HANDLE h):m_h(h){}
  39. ~SmartCloseHandle(){if (m_h!=INVALID_HANDLE_VALUE) CloseHandle(m_h);}
  40. HANDLE operator =(HANDLE h) {if (m_h!=INVALID_HANDLE_VALUE) CloseHandle(m_h); m_h=h; return h;}
  41. operator HANDLE() const {return m_h;}
  42. HANDLE* operator &() {if (m_h!=INVALID_HANDLE_VALUE) CloseHandle(m_h); m_h = INVALID_HANDLE_VALUE; return &m_h;}
  43. };
  44. class Smart_findclose
  45. {
  46. private:
  47. long m_h;
  48. public:
  49. Smart_findclose():m_h(0){}
  50. Smart_findclose(long h):m_h(h){}
  51. ~Smart_findclose(){if (m_h!=0) _findclose(m_h);}
  52. long operator =(long h) {if (m_h) _findclose(m_h); m_h=h; return h;}
  53. operator long() const {return m_h;}
  54. long* operator &() {if (m_h) _findclose(m_h); m_h = 0; return &m_h;}
  55. };
  56. class SmartFindClose
  57. {
  58. private:
  59. HANDLE m_h;
  60. public:
  61. SmartFindClose():m_h(INVALID_HANDLE_VALUE){}
  62. SmartFindClose(HANDLE h):m_h(h){}
  63. ~SmartFindClose(){if (m_h!=INVALID_HANDLE_VALUE) FindClose(m_h);}
  64. HANDLE operator =(HANDLE h) {if (m_h!=INVALID_HANDLE_VALUE) FindClose(m_h); m_h=h; return h;}
  65. operator HANDLE() const {return m_h;}
  66. HANDLE* operator &() {if (m_h!=INVALID_HANDLE_VALUE) FindClose(m_h); m_h = INVALID_HANDLE_VALUE; return &m_h;}
  67. };
  68. class SmartCloseServiceHandle
  69. {
  70. private:
  71. SC_HANDLE m_h;
  72. public:
  73. SmartCloseServiceHandle():m_h(NULL){}
  74. SmartCloseServiceHandle(SC_HANDLE h):m_h(h){}
  75. ~SmartCloseServiceHandle(){if (m_h!=NULL) CloseServiceHandle(m_h);}
  76. SC_HANDLE operator =(SC_HANDLE h) {if (m_h!=NULL) CloseServiceHandle(m_h); m_h=h; return h;}
  77. operator SC_HANDLE() const {return m_h;}
  78. SC_HANDLE* operator &() {if (m_h!=NULL) CloseServiceHandle(m_h); m_h = NULL; return &m_h;}
  79. };
  80. class CSmartCreatedDC
  81. {
  82. public:
  83. CSmartCreatedDC(HDC hdc) { m_hdc = hdc;}
  84. operator HDC() const {return m_hdc;}
  85. ~CSmartCreatedDC()
  86. {
  87. if (m_hdc)
  88. DeleteDC(m_hdc);
  89. }
  90. protected:
  91. HDC m_hdc;
  92. };
  93. class CSmartBuffer
  94. {
  95. private:
  96. LPBYTE m_pBuffer;
  97. public:
  98. CSmartBuffer() : m_pBuffer(NULL) {}
  99. CSmartBuffer(LPBYTE pBuffer) : m_pBuffer(pBuffer) {}
  100. CSmartBuffer(DWORD dwSize)
  101. {
  102. m_pBuffer = new BYTE[dwSize];
  103. if (m_pBuffer == NULL)
  104. {
  105. throw CHeap_Exception ( CHeap_Exception :: E_ALLOCATION_ERROR ) ;
  106. }
  107. }
  108. ~CSmartBuffer()
  109. {
  110. Free();
  111. }
  112. LPBYTE operator =(LPBYTE pBuffer)
  113. {
  114. Free();
  115. m_pBuffer = pBuffer;
  116. return m_pBuffer;
  117. }
  118. operator LPBYTE() const { return m_pBuffer; }
  119. LPBYTE* operator &()
  120. {
  121. Free();
  122. m_pBuffer = NULL;
  123. return &m_pBuffer;
  124. }
  125. protected:
  126. void Free()
  127. {
  128. if (m_pBuffer != NULL)
  129. {
  130. delete [] m_pBuffer;
  131. }
  132. }
  133. };