Index: chrome_elf/nt_registry/nt_registry.h |
diff --git a/chrome_elf/nt_registry/nt_registry.h b/chrome_elf/nt_registry/nt_registry.h |
index 5115410bcd24359e7186b2d67f3635c663ff372f..2f0852d9b701df1e863dca1e224c016f4fa94d16 100644 |
--- a/chrome_elf/nt_registry/nt_registry.h |
+++ b/chrome_elf/nt_registry/nt_registry.h |
@@ -45,6 +45,31 @@ enum WOW64_OVERRIDE { |
WOW6464 = KEY_WOW64_64KEY |
}; |
+// ScopedHANDLE wraps a generic Windows HANDLE (with an invalid value of |
+// nullptr), allowing it to be closed automatically (via NtClose) when the |
+// object goes out of scope. Use is_valid() function to check validity of |
+// internal HANDLE. This class may not be used with HANDLEs that have an invalid |
+// value of INVALID_HANDLE_VALUE (e.g. CreateFile()). |
+class ScopedHANDLE { |
+ public: |
+ ScopedHANDLE(); |
+ explicit ScopedHANDLE(HANDLE handle); |
+ ScopedHANDLE(ScopedHANDLE&& rvalue) : handle_(rvalue.release()) {} |
+ ScopedHANDLE(const ScopedHANDLE&) = delete; |
+ void operator=(const ScopedHANDLE&) = delete; |
+ ~ScopedHANDLE(); |
+ |
+ HANDLE get() { return handle_; } |
+ bool is_valid() const; |
+ |
+ private: |
+ // Transfers ownership. If the returned is non-null, it needs to be closed by |
+ // the caller. |
+ HANDLE release(); |
+ |
+ HANDLE handle_; |
+}; |
+ |
//------------------------------------------------------------------------------ |
// Create, open, delete, close functions |
//------------------------------------------------------------------------------ |
@@ -52,27 +77,23 @@ enum WOW64_OVERRIDE { |
// Create and/or open a registry key. |
// - This function will recursively create multiple sub-keys if required for |
// |key_path|. |
-// - If the key doesn't need to be left open, pass in nullptr for |out_handle|. |
// - This function will happily succeed if the key already exists. |
// - Optional |out_handle|. If nullptr, function will close handle when done. |
-// Otherwise, will hold the open handle to the deepest subkey. |
-// - Caller must call CloseRegKey on returned handle (on success). |
-bool CreateRegKey(ROOT_KEY root, |
- const wchar_t* key_path, |
- ACCESS_MASK access, |
- HANDLE* out_handle OPTIONAL); |
+// - On success, the returned object will hold the open handle to the deepest |
+// subkey. |
+ScopedHANDLE CreateRegKey(ROOT_KEY root, |
+ const wchar_t* key_path, |
+ ACCESS_MASK access); |
// Open existing registry key. |
-// - Caller must call CloseRegKey on returned handle (on success). |
-// - Optional error code can be returned on failure for extra detail. |
-bool OpenRegKey(ROOT_KEY root, |
- const wchar_t* key_path, |
- ACCESS_MASK access, |
- HANDLE* out_handle, |
- NTSTATUS* error_code OPTIONAL); |
+// - Optional error code can be returned on failure for extra detail. This will |
+// be set if the returned ScopedHANDLE.is_valid() == false. |
+ScopedHANDLE OpenRegKey(ROOT_KEY root, |
+ const wchar_t* key_path, |
+ ACCESS_MASK access, |
+ NTSTATUS* error_code OPTIONAL); |
// Delete a registry key. |
-// - Caller must still call CloseRegKey after the delete. |
bool DeleteRegKey(HANDLE key); |
// Delete a registry key. |
@@ -82,9 +103,6 @@ bool DeleteRegKey(ROOT_KEY root, |
WOW64_OVERRIDE wow64_override, |
const wchar_t* key_path); |
-// Close a registry key handle that was opened with CreateRegKey or OpenRegKey. |
-void CloseRegKey(HANDLE key); |
- |
//------------------------------------------------------------------------------ |
// Getter functions |
//------------------------------------------------------------------------------ |
@@ -102,7 +120,6 @@ bool QueryRegKeyValue(HANDLE key, |
// Query DWORD value. |
// - WRAPPER: Function works with DWORD data type. |
// - Key handle should have been opened with CreateRegKey or OpenRegKey. |
-// - Handle will be left open. Caller must still call CloseRegKey when done. |
bool QueryRegValueDWORD(HANDLE key, |
const wchar_t* value_name, |
DWORD* out_dword); |
@@ -120,7 +137,6 @@ bool QueryRegValueDWORD(ROOT_KEY root, |
// Query SZ (string) value. |
// - WRAPPER: Function works with SZ data type. |
// - Key handle should have been opened with CreateRegKey or OpenRegKey. |
-// - Handle will be left open. Caller must still call CloseRegKey when done. |
bool QueryRegValueSZ(HANDLE key, |
const wchar_t* value_name, |
std::wstring* out_sz); |
@@ -138,7 +154,6 @@ bool QueryRegValueSZ(ROOT_KEY root, |
// Query MULTI_SZ (multiple strings) value. |
// - WRAPPER: Function works with MULTI_SZ data type. |
// - Key handle should have been opened with CreateRegKey or OpenRegKey. |
-// - Handle will be left open. Caller must still call CloseRegKey when done. |
bool QueryRegValueMULTISZ(HANDLE key, |
const wchar_t* value_name, |
std::vector<std::wstring>* out_multi_sz); |
@@ -169,7 +184,6 @@ bool SetRegKeyValue(HANDLE key, |
// Set DWORD value. |
// - WRAPPER: Function works with DWORD data type. |
// - Key handle should have been opened with CreateRegKey or OpenRegKey. |
-// - Handle will be left open. Caller must still call CloseRegKey when done. |
bool SetRegValueDWORD(HANDLE key, const wchar_t* value_name, DWORD value); |
// Set DWORD value. |
@@ -185,7 +199,6 @@ bool SetRegValueDWORD(ROOT_KEY root, |
// Set SZ (string) value. |
// - WRAPPER: Function works with SZ data type. |
// - Key handle should have been opened with CreateRegKey or OpenRegKey. |
-// - Handle will be left open. Caller must still call CloseRegKey when done. |
bool SetRegValueSZ(HANDLE key, |
const wchar_t* value_name, |
const std::wstring& value); |
@@ -203,7 +216,6 @@ bool SetRegValueSZ(ROOT_KEY root, |
// Set MULTI_SZ (multiple strings) value. |
// - WRAPPER: Function works with MULTI_SZ data type. |
// - Key handle should have been opened with CreateRegKey or OpenRegKey. |
-// - Handle will be left open. Caller must still call CloseRegKey when done. |
bool SetRegValueMULTISZ(HANDLE key, |
const wchar_t* value_name, |
const std::vector<std::wstring>& values); |