-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwbase.hpp
More file actions
204 lines (169 loc) · 4.69 KB
/
Copy pathwbase.hpp
File metadata and controls
204 lines (169 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#if !defined(_INC_WBASE_HPP_) && defined(__cplusplus)
#define _INC_WBASE_HPP_
/**
Base class for all classes that embeds handles to different system objects,
such as kernel objects, user objects, SCM objects, registry keys etc, for
short, any object that can me duplicated using DuplicateHandle().
This class has no public constructors. That is because there cannot be any
public destructor since the objects may contain handle to any object and
it is not possible to know how a handle should be closed if all we know is
that it is a handle to something.
*/
class WObjectHandle
{
protected:
/// The handle to the embedded kernel object.
HANDLE hObject;
explicit WObjectHandle(HANDLE handle = NULL)
: hObject(handle)
{
}
public:
/// Stops keeping track of handle without closing it.
HANDLE Abandon()
{
HANDLE h = hObject;
hObject = INVALID_HANDLE_VALUE;
return h;
}
/// Returns the handle to the embedded kernel object.
HANDLE Handle() const
{
return hObject;
}
/// Returns a new handle duplicated from the embedded.
HANDLE NewHandle(BOOL bInheritHandle = FALSE,
DWORD dwOptions = DUPLICATE_SAME_ACCESS,
DWORD dwDesiredAccess = 0)
const
{
if (!IsValidHandle(hObject))
return INVALID_HANDLE_VALUE;
HANDLE hNew;
if (!DuplicateHandle(GetCurrentProcess(), hObject, GetCurrentProcess(),
&hNew, dwDesiredAccess, bInheritHandle, dwOptions))
return INVALID_HANDLE_VALUE;
return hNew;
}
bool ReCreate(BOOL bInheritHandle = FALSE,
DWORD dwOptions = DUPLICATE_SAME_ACCESS,
DWORD dwDesiredAccess = 0)
{
if (!IsValidHandle(hObject))
return false;
if (!DuplicateHandle(GetCurrentProcess(), hObject, GetCurrentProcess(),
&hObject, dwDesiredAccess | DUPLICATE_CLOSE_SOURCE,
bInheritHandle, dwOptions))
{
hObject = INVALID_HANDLE_VALUE;
return false;
}
return true;
}
/// Returns true if embedded handle is invalid.
bool operator!() const
{
return !IsValidHandle();
}
/// Returns true if embedded handle is valid.
operator bool() const
{
return IsValidHandle();
}
/// Returns true if this object's handle is valid.
bool IsValidHandle() const
{
return IsValidHandle(hObject);
}
/// Returns true if given handle is valid.
static bool IsValidHandle(HANDLE handle)
{
return (handle != INVALID_HANDLE_VALUE) && (handle != NULL);
}
};
template<typename T> class WUsing
{
public:
T Object;
void (*Destructor)(T object);
WUsing(T object, void (*destructor)(T)) :
Object(object), Destructor(destructor)
{}
~WUsing()
{
if (Object != NULL && Destructor != NULL)
{
Destructor(Object);
}
Object = NULL;
}
};
/**
Base class for all kernel object wrapping classes.
Uses CloseHandle() on the embedded handle when object is destroyed.
*/
class WKernelObject : public WObjectHandle
{
public:
/// Init with existing handle.
explicit WKernelObject(HANDLE handle = INVALID_HANDLE_VALUE)
: WObjectHandle(handle)
{
}
/// Init with handle from other object.
explicit WKernelObject(const WKernelObject &obj)
: WObjectHandle(obj.Handle())
{
}
/// Closes existing object and init with handle from other object.
WKernelObject& operator=(const WKernelObject &obj)
{
SetHandle(obj.Handle());
return *this;
}
/// Closes existing object and init with supplied handle.
WKernelObject& operator=(HANDLE handle)
{
SetHandle(handle);
return *this;
}
/// Closes existing object and init with supplied handle.
void SetHandle(HANDLE handle)
{
Close();
hObject = handle;
}
/// Closes the handle.
~WKernelObject()
{
Close();
}
bool Duplicate(HANDLE hExisting,
BOOL bInheritHandle = FALSE,
DWORD dwOptions = DUPLICATE_SAME_ACCESS,
DWORD dwDesiredAccess = 0)
{
Close();
if (!DuplicateHandle(GetCurrentProcess(), hExisting, GetCurrentProcess(),
&hObject, dwDesiredAccess, bInheritHandle, dwOptions))
{
hObject = INVALID_HANDLE_VALUE;
return false;
}
return true;
}
/// Closes the embedded handle if it is valid.
bool Close()
{
if (!IsValidHandle(hObject))
return false;
if (CloseHandle(hObject))
{
hObject = INVALID_HANDLE_VALUE;
return true;
}
else
return false;
}
};
#endif // _INC_WBASE_HPP_