ErrLib
ErrLib_CPP.h
Go to the documentation of this file.
1//Project: ErrLib
2//Author: MSDN.WhiteKnight (https://github.com/MSDN-WhiteKnight)
3
8#ifndef ERRLIB_CPP_H_INCLUDED
9#define ERRLIB_CPP_H_INCLUDED
10#include "ErrLib.h"
11#include <string>
12
13const DWORD ERRLIB_CPP_EXCEPTION = 0xC0400002;
14
15#ifdef __cplusplus
19namespace ErrLib{
20
25class Exception : public std::exception{
26private:
27 CONTEXT _context;
28 std::wstring _msg;
29 std::wstring _stack;
30 DWORD _code;
31 void* _data;
32
33 std::wstring PrintStackTraceImpl(){
34 WCHAR buf[ErrLib_StackLen]=L"";
35 ErrLib_PrintStack(&_context, buf, ErrLib_StackLen);
36 return std::wstring(buf);
37 }
38
39protected:
40
41 void SetMsg(const std::wstring& msg){_msg=msg;}
42
43 void SetCode(DWORD code){_code=code;}
44
45 void SetData(void* data){_data=data;}
46
47public:
48
52 Exception():_msg(L""),_code(ERRLIB_CPP_EXCEPTION),_data(nullptr){
53 RtlCaptureContext(&_context);
54 _stack = this->PrintStackTraceImpl();
55 }
56
60 Exception(const std::wstring& message):_msg(message),_code(ERRLIB_CPP_EXCEPTION),_data(nullptr){
61 RtlCaptureContext(&_context);
62 _stack = this->PrintStackTraceImpl();
63 }
64
68 Exception(const std::wstring& message, DWORD code, void* data):_msg(message),_code(code),_data(data){
69 RtlCaptureContext(&_context);
70 _stack = this->PrintStackTraceImpl();
71 }
72
76 std::wstring GetMsg(){return _msg;}
77
82 DWORD GetCode(){return _code;}
83
87 void* GetData(){return _data;}
88
94 void GetMessageText(WCHAR* pOutput, int cch){
95 const WCHAR* pChars = _msg.c_str();
96 wcscpy_s(pOutput, cch, pChars);
97 }
98
103 void GetContext(CONTEXT* pOutput){
104 memcpy(pOutput, &_context, sizeof(CONTEXT));
105 }
106
112 void PrintStackTrace(WCHAR* pOutput, int cch){
113 const WCHAR* p = this->_stack.c_str();
114 StringCchCopy(pOutput, cch, p);
115 }
116
120 std::wstring PrintStackTrace(){
121 return _stack;
122 }
123
132 void Log(bool visible){
133 BOOL bVisible;
134
135 if (visible) bVisible = TRUE;
136 else bVisible = FALSE;
137
138 ErrLib_LogExceptionInfo(_code, _msg.c_str(), this->PrintStackTrace().c_str(), bVisible);
139 }
140};
141
146public:
147 WinapiException(DWORD code,const std::wstring& msg){
148 this->SetCode(code);
149 this->SetMsg(msg);
150 }
151
157 static WinapiException FromLastError(bool localized){
158 DWORD code = GetLastError();
159 BOOL fLocalized;
160 WCHAR buf[ErrLib_MessageLen]=L"";
161
162 if(localized) fLocalized = TRUE;
163 else fLocalized = FALSE;
164
165 ErrLib_GetWinapiErrorMessage(code, fLocalized, buf, ErrLib_MessageLen);
166 return WinapiException(code, buf);
167 }
168};
169
173class ComException : public Exception{
174public:
175 ComException(HRESULT hr,const std::wstring& msg){
176 this->SetCode((DWORD)hr);
177 this->SetMsg(msg);
178 }
179
184 static ComException FromHResult(HRESULT hr){
185 WCHAR buf[ErrLib_MessageLen]=L"";
186
187 ErrLib_GetHResultMessage(hr, buf, ErrLib_MessageLen);
188 return ComException(hr, buf);
189 }
190};
191
192}
193#endif //__cplusplus
194#endif
#define ErrLib_StackLen
Definition: ErrLib.h:43
ERRLIB_API void __stdcall ErrLib_PrintStack(CONTEXT *ctx, WCHAR *dest, size_t cch)
ERRLIB_API void __stdcall ErrLib_LogExceptionInfo(DWORD dwExcCode, LPCWSTR lpwsMessage, LPCWSTR lpwsStackTrace, BOOL visible)
#define ErrLib_MessageLen
Definition: ErrLib.h:38
Definition: ErrLib_CPP.h:173
static ComException FromHResult(HRESULT hr)
Definition: ErrLib_CPP.h:184
Definition: ErrLib_CPP.h:25
Exception(const std::wstring &message, DWORD code, void *data)
Definition: ErrLib_CPP.h:68
std::wstring PrintStackTrace()
Definition: ErrLib_CPP.h:120
void GetMessageText(WCHAR *pOutput, int cch)
Definition: ErrLib_CPP.h:94
Exception(const std::wstring &message)
Definition: ErrLib_CPP.h:60
void * GetData()
Definition: ErrLib_CPP.h:87
void PrintStackTrace(WCHAR *pOutput, int cch)
Definition: ErrLib_CPP.h:112
DWORD GetCode()
Definition: ErrLib_CPP.h:82
void GetContext(CONTEXT *pOutput)
Definition: ErrLib_CPP.h:103
std::wstring GetMsg()
Definition: ErrLib_CPP.h:76
void Log(bool visible)
Definition: ErrLib_CPP.h:132
Exception()
Definition: ErrLib_CPP.h:52
Definition: ErrLib_CPP.h:145
static WinapiException FromLastError(bool localized)
Definition: ErrLib_CPP.h:157
Definition: ErrLib_CPP.h:19