scoped_process_information.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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/macros.h"
  9. #include "base/win/scoped_handle.h"
  10. namespace base {
  11. namespace win {
  12. // Manages the closing of process and thread handles from PROCESS_INFORMATION
  13. // structures. Allows clients to take ownership of either handle independently.
  14. class BASE_EXPORT ScopedProcessInformation {
  15. public:
  16. ScopedProcessInformation();
  17. explicit ScopedProcessInformation(const PROCESS_INFORMATION& process_info);
  18. ~ScopedProcessInformation();
  19. // Returns true iff this instance is holding a thread and/or process handle.
  20. bool IsValid() const;
  21. // Closes the held thread and process handles, if any.
  22. void Close();
  23. // Populates this instance with the provided |process_info|.
  24. void Set(const PROCESS_INFORMATION& process_info);
  25. // Populates this instance with duplicate handles and the thread/process IDs
  26. // from |other|. Returns false in case of failure, in which case this instance
  27. // will be completely unpopulated.
  28. bool DuplicateFrom(const ScopedProcessInformation& other);
  29. // Transfers ownership of the held PROCESS_INFORMATION, if any, away from this
  30. // instance.
  31. PROCESS_INFORMATION Take();
  32. // Transfers ownership of the held process handle, if any, away from this
  33. // instance. Note that the related process_id will also be cleared.
  34. HANDLE TakeProcessHandle();
  35. // Transfers ownership of the held thread handle, if any, away from this
  36. // instance. Note that the related thread_id will also be cleared.
  37. HANDLE TakeThreadHandle();
  38. // Returns the held process handle, if any, while retaining ownership.
  39. HANDLE process_handle() const { return process_handle_.Get(); }
  40. // Returns the held thread handle, if any, while retaining ownership.
  41. HANDLE thread_handle() const { return thread_handle_.Get(); }
  42. // Returns the held process id, if any.
  43. DWORD process_id() const { return process_id_; }
  44. // Returns the held thread id, if any.
  45. DWORD thread_id() const { return thread_id_; }
  46. private:
  47. ScopedHandle process_handle_;
  48. ScopedHandle thread_handle_;
  49. DWORD process_id_ = 0;
  50. DWORD thread_id_ = 0;
  51. DISALLOW_COPY_AND_ASSIGN(ScopedProcessInformation);
  52. };
  53. } // namespace win
  54. } // namespace base
  55. #endif // BASE_WIN_SCOPED_PROCESS_INFORMATION_H_