-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
97 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,3 +50,4 @@ | |
#include "Devcon.h" | ||
#include "NefConSetup.h" | ||
#include "UniUtil.h" | ||
#include "ScopeGuards.hpp" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#pragma once | ||
|
||
#include <Windows.h> | ||
#include <SetupAPI.h> | ||
|
||
namespace nefarius::util | ||
{ | ||
class INFHandleGuard | ||
{ | ||
public: | ||
// Constructor takes the HINF handle to manage | ||
explicit INFHandleGuard(HINF handle) : handle_(handle) | ||
{ | ||
} | ||
|
||
// Destructor releases the HINF resource | ||
~INFHandleGuard() | ||
{ | ||
if (handle_ != INVALID_HANDLE_VALUE) | ||
{ | ||
SetupCloseInfFile(handle_); | ||
} | ||
} | ||
|
||
// Disable copy constructor and copy assignment operator | ||
INFHandleGuard(const INFHandleGuard&) = delete; | ||
INFHandleGuard& operator=(const INFHandleGuard&) = delete; | ||
|
||
// Move constructor | ||
INFHandleGuard(INFHandleGuard&& other) noexcept : handle_(other.handle_) | ||
{ | ||
other.handle_ = INVALID_HANDLE_VALUE; | ||
} | ||
|
||
// Move assignment operator | ||
INFHandleGuard& operator=(INFHandleGuard&& other) noexcept | ||
{ | ||
if (this != &other) | ||
{ | ||
// Release current resource | ||
if (handle_ != INVALID_HANDLE_VALUE) | ||
{ | ||
SetupCloseInfFile(handle_); | ||
} | ||
// Transfer ownership | ||
handle_ = other.handle_; | ||
other.handle_ = INVALID_HANDLE_VALUE; | ||
} | ||
return *this; | ||
} | ||
|
||
// Function to manually release the handle, if needed | ||
void release() | ||
{ | ||
if (handle_ != INVALID_HANDLE_VALUE) | ||
{ | ||
SetupCloseInfFile(handle_); | ||
handle_ = INVALID_HANDLE_VALUE; | ||
} | ||
} | ||
|
||
// Accessor for the handle | ||
HINF get() const | ||
{ | ||
return handle_; | ||
} | ||
|
||
bool is_invalid() const | ||
{ | ||
return handle_ == INVALID_HANDLE_VALUE; | ||
} | ||
|
||
private: | ||
HINF handle_; | ||
}; | ||
} |