forked from daynix/UsbDk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exception.cpp
176 lines (142 loc) · 4.77 KB
/
Exception.cpp
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
/**********************************************************************
* Copyright (c) 2013-2014 Red Hat, Inc.
*
* Developed by Daynix Computing LTD.
*
* Authors:
* Dmitry Fleytman <[email protected]>
* Pavel Gurvich <[email protected]>
*
* 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.
*
**********************************************************************/
#include "stdafx.h"
#include "Exception.h"
UsbDkException::UsbDkException()
{
}
UsbDkException::UsbDkException(LPCTSTR lpzMessage)
{
if (lpzMessage)
{
SetMessage(tstring(lpzMessage));
}
}
UsbDkException::UsbDkException(const tstring &Message)
{
SetMessage(Message);
}
UsbDkException::UsbDkException(const UsbDkException &Other)
{
SetMessage(Other.m_Message);
}
UsbDkException::~UsbDkException(void)
{
}
const char *UsbDkException::what() const
{
return m_MBCSMessage.c_str();
}
LPCTSTR UsbDkException::twhat() const
{
return m_Message.c_str();
}
void UsbDkException::SetMessage(const tstring &Message)
{
m_Message = Message;
m_MBCSMessage = tstring2string(m_Message);
}
UsbDkNumErrorException::UsbDkNumErrorException(LPCTSTR lpzDescription, DWORD dwErrorCode)
: UsbDkException(lpzDescription), m_dwErrorCode(dwErrorCode)
{
}
UsbDkNumErrorException::UsbDkNumErrorException(const tstring &Description, DWORD dwErrorCode)
: UsbDkException(Description), m_dwErrorCode(dwErrorCode)
{
}
UsbDkNumErrorException::UsbDkNumErrorException(const UsbDkNumErrorException& Other)
: UsbDkException(Other)
{
m_dwErrorCode = Other.m_dwErrorCode;
}
UsbDkCRTErrorException::UsbDkCRTErrorException(int nErrorCode)
: UsbDkNumErrorException(GetErrorString(nErrorCode) + TEXT("\n"), (DWORD)nErrorCode)
{
}
UsbDkCRTErrorException::UsbDkCRTErrorException(LPCTSTR lpzDescription, int nErrorCode)
: UsbDkNumErrorException(tstring(lpzDescription) + TEXT(" (") + GetErrorString((DWORD)nErrorCode) + TEXT(")") + TEXT("\n"), (DWORD)nErrorCode)
{
}
UsbDkCRTErrorException::UsbDkCRTErrorException(const tstring &Description, int nErrorCode)
: UsbDkNumErrorException(Description + GetErrorString((DWORD)nErrorCode) + TEXT("\n"), (DWORD)nErrorCode)
{
}
UsbDkCRTErrorException::UsbDkCRTErrorException(const UsbDkCRTErrorException &Other)
: UsbDkNumErrorException(Other)
{
}
tstring UsbDkCRTErrorException::GetErrorString(DWORD dwErrorCode)
{
#ifdef WIN32
TCHAR tcaBuff[256];
_tcserror_s(tcaBuff, TBUF_SIZEOF(tcaBuff), (int)dwErrorCode);
return tcaBuff;
#else
return string2tstring(strerror((int)dwErrorCode));
#endif
}
#ifdef WIN32
UsbDkW32ErrorException::UsbDkW32ErrorException(DWORD dwErrorCode)
: UsbDkNumErrorException(GetErrorString(m_dwErrorCode) + TEXT("\n"), dwErrorCode)
{
}
UsbDkW32ErrorException::UsbDkW32ErrorException(LPCTSTR lpzDescription, DWORD dwErrorCode)
: UsbDkNumErrorException(tstring(lpzDescription) + TEXT(" (") + GetErrorString(dwErrorCode) + TEXT(")") + TEXT("\n"), dwErrorCode)
{
}
UsbDkW32ErrorException::UsbDkW32ErrorException(const tstring &Description, DWORD dwErrorCode)
: UsbDkNumErrorException(Description + TEXT(" (") + GetErrorString(dwErrorCode) + TEXT(")") + TEXT("\n"), dwErrorCode)
{
}
UsbDkW32ErrorException::UsbDkW32ErrorException(const UsbDkW32ErrorException &Other)
: UsbDkNumErrorException(Other)
{
}
tstring UsbDkW32ErrorException::GetErrorString(DWORD dwErrorCode)
{
LPVOID lpMsgBuf;
DWORD msgLen = ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS|
FORMAT_MESSAGE_MAX_WIDTH_MASK,
nullptr,
dwErrorCode,
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
(LPTSTR) &lpMsgBuf,
0,
nullptr);
if(msgLen == 0)
{
tstringstream strm;
strm << TEXT("Failed to get error description for error code: 0x") << hex << dwErrorCode;
return strm.str();
}
else
{
tstring strResult((LPCTSTR)lpMsgBuf, msgLen);
::LocalFree( lpMsgBuf );
strResult += tstring(TEXT(" Error code = ")) + to_tstring(dwErrorCode);
return strResult;
}
}
#endif // WIN32