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..b1de0463745d955d4943f266d8caf76e79e01391 100644 |
--- a/chrome_elf/nt_registry/nt_registry.h |
+++ b/chrome_elf/nt_registry/nt_registry.h |
@@ -45,6 +45,23 @@ enum WOW64_OVERRIDE { |
WOW6464 = KEY_WOW64_64KEY |
}; |
penny
2016/11/21 21:35:48
Please add a small class comment.
"
// ScopedHAND
scottmg
2016/11/22 22:47:36
Done.
|
+class ScopedRegKeyHANDLE { |
penny
2016/11/21 21:35:47
Let's make this class generic ScopedHANDLE. Nothi
scottmg
2016/11/22 22:47:36
The INVALID_HANDLE_VALUE vs nullptr was why I didn
|
+ public: |
+ ScopedRegKeyHANDLE(); |
+ explicit ScopedRegKeyHANDLE(HANDLE handle); |
penny
2016/11/21 21:35:47
Just for my own c++ learnin': why did you think t
scottmg
2016/11/22 22:47:37
Yeah, it's for this. Otherwise, any old HANDLE cou
|
+ ScopedRegKeyHANDLE(ScopedRegKeyHANDLE&& rvalue) : handle_(rvalue.release()) {} |
penny
2016/11/21 21:35:47
Why did you have to release and then set in this c
scottmg
2016/11/22 22:47:36
The ones below are =delete, so they're not impleme
|
+ ScopedRegKeyHANDLE(const ScopedRegKeyHANDLE&) = delete; |
penny
2016/11/21 21:35:47
Did you need to add one with "const"? Is that jus
scottmg
2016/11/22 22:47:36
You kind of lost me by this point. But the operato
|
+ void operator=(ScopedRegKeyHANDLE&) = delete; |
+ ~ScopedRegKeyHANDLE(); |
+ |
+ HANDLE get() { return handle_; } |
+ bool is_valid() const { return handle_ != INVALID_HANDLE_VALUE; } |
penny
2016/11/21 21:35:47
I think I'd like is_valid to *also* check for NULL
scottmg
2016/11/22 22:47:36
How about I assert for non-null? I'd prefer to kee
|
+ HANDLE release(); |
penny
2016/11/21 21:35:47
Please add tiny comment like:
"
// Caller is resp
scottmg
2016/11/22 22:47:36
Done. Also made it private for now, since we don't
|
+ |
+ private: |
+ HANDLE handle_; |
+}; |
+ |
//------------------------------------------------------------------------------ |
// Create, open, delete, close functions |
//------------------------------------------------------------------------------ |
@@ -52,28 +69,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. |
penny
2016/11/21 21:35:47
Change this comment to be:
// - On success, the r
scottmg
2016/11/22 22:47:36
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); |
+ScopedRegKeyHANDLE 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 ScopedRegKeyHANDLE.is_valid() == false. |
+ScopedRegKeyHANDLE 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); |
+bool DeleteRegKey(HANDLE handle); |
penny
2016/11/21 21:35:47
I was fine with the name change, but then I realiz
scottmg
2016/11/22 22:47:36
Oops, sorry, unintentional. I was fiddling a bit m
|
// Delete a registry key. |
// - WRAPPER: Function opens and closes the target key for caller. |
@@ -82,9 +94,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 +111,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 +128,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 +145,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 +175,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 +190,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 +207,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); |