mirror of
https://github.com/FEX-Emu/FEX.git
synced 2026-10-09 00:00:17 +02:00
66 lines
1.4 KiB
C++
66 lines
1.4 KiB
C++
// SPDX-License-Identifier: MIT
|
|
#pragma once
|
|
|
|
#include <winternl.h>
|
|
|
|
namespace FEX::Windows {
|
|
class ScopedHandle final {
|
|
public:
|
|
ScopedHandle() = default;
|
|
|
|
explicit ScopedHandle(HANDLE Handle)
|
|
: Handle(Handle) {}
|
|
|
|
// Move-only type
|
|
ScopedHandle(const ScopedHandle&) = delete;
|
|
ScopedHandle& operator=(ScopedHandle&) = delete;
|
|
ScopedHandle(ScopedHandle&& rhs)
|
|
: Handle(rhs.Handle) {
|
|
rhs.Handle = INVALID_HANDLE_VALUE;
|
|
}
|
|
|
|
~ScopedHandle() {
|
|
reset(INVALID_HANDLE_VALUE);
|
|
}
|
|
|
|
void reset(HANDLE NewHandle = INVALID_HANDLE_VALUE) {
|
|
if (Handle != INVALID_HANDLE_VALUE) {
|
|
NtClose(Handle);
|
|
}
|
|
Handle = NewHandle;
|
|
}
|
|
|
|
const HANDLE& operator*() const {
|
|
return Handle;
|
|
}
|
|
|
|
HANDLE& operator*() {
|
|
return Handle;
|
|
}
|
|
|
|
operator bool() const {
|
|
return Handle != INVALID_HANDLE_VALUE;
|
|
}
|
|
|
|
private:
|
|
HANDLE Handle {INVALID_HANDLE_VALUE};
|
|
};
|
|
|
|
|
|
inline bool ValidateHandleAccess(HANDLE Handle, ACCESS_MASK Access) {
|
|
OBJECT_BASIC_INFORMATION Info;
|
|
|
|
if (NtQueryObject(Handle, ObjectBasicInformation, &Info, sizeof(Info), nullptr)) {
|
|
return false;
|
|
}
|
|
|
|
return (Info.GrantedAccess & Access) == Access;
|
|
}
|
|
|
|
inline ScopedHandle DupHandle(HANDLE Handle, ACCESS_MASK Access) {
|
|
HANDLE Duplicated = INVALID_HANDLE_VALUE;
|
|
NtDuplicateObject(NtCurrentProcess(), Handle, NtCurrentProcess(), &Duplicated, Access, 0, 0);
|
|
return ScopedHandle {Duplicated};
|
|
}
|
|
} // namespace FEX::Windows
|