scoped_process_information.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_WIN_SCOPED_PROCESS_INFORMATION_H_
  5. #define BASE_WIN_SCOPED_PROCESS_INFORMATION_H_
  6. #include <windows.h>
  7. #include "base/base_export.h"
  8. #include "base/win/scoped_handle.h"
  9. namespace base {
  10. namespace win {
  11. // Manages the closing of process and thread handles from PROCESS_INFORMATION
  12. // structures. Allows clients to take ownership of either handle independently.
  13. class BASE_EXPORT ScopedProcessInformation {
  14. public:
  15. ScopedProcessInformation();
  16. explicit ScopedProcessInformation(const PROCESS_INFORMATION& process_info);
  17. ScopedProcessInformation(const ScopedProcessInformation&) = delete;
  18. ScopedProcessInformation& operator=(const ScopedProcessInformation&) = delete;
  19. ~ScopedProcessInformation();
  20. // Returns true iff this instance is holding a thread and/or process handle.
  21. bool IsValid() const;
  22. // Closes the held thread and process handles, if any.
  23. void Close();
  24. // Populates this instance with the provided |process_info|.
  25. void Set(const PROCESS_INFORMATION& process_info);
  26. // Populates this instance with duplicate handles and the thread/process IDs
  27. // from |other|. Returns false in case of failure, in which case this instance
  28. // will be completely unpopulated.
  29. bool DuplicateFrom(const ScopedProcessInformation& other);
  30. // Transfers ownership of the held PROCESS_INFORMATION, if any, away from this
  31. // instance.
  32. PROCESS_INFORMATION Take();
  33. // Transfers ownership of the held process handle, if any, away from this
  34. // instance. Note that the related process_id will also be cleared.
  35. HANDLE TakeProcessHandle();
  36. // Transfers ownership of the held thread handle, if any, away from this
  37. // instance. Note that the related thread_id will also be cleared.
  38. HANDLE TakeThreadHandle();
  39. // Returns the held process handle, if any, while retaining ownership.
  40. HANDLE process_handle() const { return process_handle_.get(); }
  41. // Returns the held thread handle, if any, while retaining ownership.
  42. HANDLE thread_handle() const { return thread_handle_.get(); }
  43. // Returns the held process id, if any.
  44. DWORD process_id() const { return process_id_; }
  45. // Returns the held thread id, if any.
  46. DWORD thread_id() const { return thread_id_; }
  47. private:
  48. ScopedHandle process_handle_;
  49. ScopedHandle thread_handle_;
  50. DWORD process_id_ = 0;
  51. DWORD thread_id_ = 0;
  52. };
  53. } // namespace win
  54. } // namespace base
  55. #endif // BASE_WIN_SCOPED_PROCESS_INFORMATION_H_