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.

73 lines
1.4 KiB

  1. //
  2. // auto_h.h
  3. //
  4. #pragma once
  5. class auto_reg
  6. {
  7. public:
  8. auto_reg(HKEY p = 0)
  9. : h(p) {};
  10. auto_reg(auto_reg& rhs)
  11. : h(rhs.release()) {};
  12. ~auto_reg()
  13. { if (h) RegCloseKey(h); };
  14. auto_reg& operator= (auto_reg& rhs)
  15. { if (this != rhs.getThis())
  16. reset (rhs.release() );
  17. return *this;
  18. };
  19. auto_reg& operator= (HKEY rhs)
  20. { if ((NULL == rhs) || (INVALID_HANDLE_VALUE == rhs))
  21. { // be sure and go through auto_os for dbg.lib
  22. auto_os os;
  23. os = (BOOL)FALSE;
  24. }
  25. reset (rhs);
  26. return *this;
  27. };
  28. HKEY* operator& ()
  29. { reset(); return &h; };
  30. operator HKEY ()
  31. { return h; };
  32. // Checks for NULL
  33. bool operator== (LPVOID lpv)
  34. { return h == lpv; };
  35. bool operator!= (LPVOID lpv)
  36. { return h != lpv; };
  37. // return value of current dumb pointer
  38. HKEY get() const
  39. { return h; };
  40. // relinquish ownership
  41. HKEY release()
  42. { HKEY oldh = h;
  43. h = 0;
  44. return oldh;
  45. };
  46. // delete owned pointer; assume ownership of p
  47. BOOL reset (HKEY p = 0)
  48. {
  49. BOOL rt = TRUE;
  50. if (h)
  51. rt = RegCloseKey(h);
  52. h = p;
  53. return rt;
  54. };
  55. private:
  56. // operator& throws off operator=
  57. const auto_reg* getThis() const
  58. { return this; };
  59. HKEY h;
  60. };