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