-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKernel32_Ext.cpp
More file actions
153 lines (146 loc) · 4.06 KB
/
Copy pathKernel32_Ext.cpp
File metadata and controls
153 lines (146 loc) · 4.06 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
// Copyright © 2006-2009, 2026 Ivyware Pty Ltd, Khrustal & Mann
// MELBOURNE, VICTORIA, AUSTRALIA, 3000
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// Implementations for Microsoft Kernel32 C++ extensions
//
#include "stdafx.h"
#include "Kernel32_Ext.h"
///////////////////////////////////////////////////////////////////////
// Safe CRITICAL_SECTION container
// NOTES: Allocated CRITICAL_SECTION is managed within the life cycle
// of this object.
P2PsafeCS::P2PsafeCS ( )
{
}
P2PsafeCS::P2PsafeCS ( CRITICAL_SECTION& rhs )
{
EnterCriticalSection ( &rhs );
m_pCSection = &rhs;
}
P2PsafeCS::~P2PsafeCS ( )
{
if ( m_pCSection )
LeaveCriticalSection ( m_pCSection );
}
P2PsafeCS&
P2PsafeCS::operator = ( CRITICAL_SECTION& rhs )
{
EnterCriticalSection ( &rhs );
if ( m_pCSection )
LeaveCriticalSection ( m_pCSection );
m_pCSection = &rhs;
return *this;
}
///////////////////////////////////////////////////////////////////////
// Safe HANDLE implementation
// NOTES: Assigned HANDLE is managed within the life cycle of this
// object.
//
P2PsafeHANDLE::P2PsafeHANDLE ( ) noexcept
{
m_oHandle = INVALID_HANDLE_VALUE;
}
P2PsafeHANDLE::P2PsafeHANDLE ( HANDLE oHandle ) noexcept
{
m_oHandle = oHandle;
}
P2PsafeHANDLE::~P2PsafeHANDLE ( )
{
if ( m_oHandle != INVALID_HANDLE_VALUE )
CloseHandle ( m_oHandle );
}
P2PsafeHANDLE&
P2PsafeHANDLE::operator = ( HANDLE oHandle ) noexcept
{
if ( m_oHandle != oHandle &&
m_oHandle != INVALID_HANDLE_VALUE )
CloseHandle ( m_oHandle );
m_oHandle = oHandle;
return *this;
}
P2PsafeHANDLE::operator HANDLE ( ) noexcept
{
return m_oHandle;
}
HANDLE
P2PsafeHANDLE::Dereference() noexcept
{
HANDLE oHandle = m_oHandle;
m_oHandle = INVALID_HANDLE_VALUE;
return oHandle;
}
///////////////////////////////////////////////////////////////////////
// Safe HGLOBAL implementation
// NOTES: Assigned HGLOBAL is managed within the life cycle of this
// object.
//
P2PsafeHGLOBAL::P2PsafeHGLOBAL ( )
{
m_oHGLOBAL = nullptr;
}
P2PsafeHGLOBAL::P2PsafeHGLOBAL ( HGLOBAL oHGLOBAL )
{
m_oHGLOBAL = oHGLOBAL;
if ( m_oHGLOBAL )
GlobalLock(m_oHGLOBAL);
}
P2PsafeHGLOBAL::~P2PsafeHGLOBAL ( )
{
if ( m_oHGLOBAL != nullptr )
GlobalUnlock ( m_oHGLOBAL );
}
P2PsafeHGLOBAL&
P2PsafeHGLOBAL::operator = ( HGLOBAL oHGLOBAL )
{
if ( oHGLOBAL == m_oHGLOBAL )
return *this;
if ( m_oHGLOBAL != oHGLOBAL &&
m_oHGLOBAL != nullptr )
GlobalUnlock ( m_oHGLOBAL );
m_oHGLOBAL = oHGLOBAL;
if ( m_oHGLOBAL != nullptr )
GlobalLock ( m_oHGLOBAL );
return *this;
}
P2PsafeHGLOBAL::operator HGLOBAL ( )
{
return m_oHGLOBAL;
}
HGLOBAL
P2PsafeHGLOBAL::Dereference() noexcept
{
HGLOBAL oHGLOBAL = m_oHGLOBAL;
m_oHGLOBAL = nullptr;
return oHGLOBAL;
}
//
// Stack based safe DWORD_PTR global
// NOTES: Sets global value and manages restoration of existing value
// on the stack
// : Sometime global values are set to manage behaviour which particular
// sequences are in operation and then restored immediately after.
// This stack based object can be used to make this exception safe.
// : Intended to be transitory operation.
P2PsafeGlobal::P2PsafeGlobal ( DWORD_PTR *pdwGlobal, DWORD_PTR dwGlobalSet )
{
m_pdwGlobal = pdwGlobal;
m_dwGlobalRestore = *pdwGlobal;
*m_pdwGlobal = dwGlobalSet;
}
P2PsafeGlobal::~P2PsafeGlobal ( )
{
if ( m_pdwGlobal )
*m_pdwGlobal = m_dwGlobalRestore;
}