mal-packet-weaver
C++20 packet serialization/deserialization library.
Loading...
Searching...
No Matches
win-debug.cpp
Go to the documentation of this file.
1#if defined(_WIN32)
7#include "win-debug.hpp"
8static std::function<void(std::string_view)> OutputDebugStringCallback;
9LONG NTAPI VexHandler(PEXCEPTION_POINTERS ExceptionInfo)
10{
11 PEXCEPTION_RECORD ExceptionRecord = ExceptionInfo->ExceptionRecord;
12
13 switch (ExceptionRecord->ExceptionCode)
14 {
15 case DBG_PRINTEXCEPTION_WIDE_C:
16 case DBG_PRINTEXCEPTION_C:
17
18 if (ExceptionRecord->NumberParameters >= 2)
19 {
20 bool call_free = false;
21 auto len = (ULONG)ExceptionRecord->ExceptionInformation[0];
22
23 union
24 {
25 ULONG_PTR up;
26 PCWSTR pwz;
27 PCSTR psz;
28 };
29
30 up = ExceptionRecord->ExceptionInformation[1];
31
32 if (ExceptionRecord->ExceptionCode == DBG_PRINTEXCEPTION_WIDE_C)
33 {
34 if (ULONG n = WideCharToMultiByte(CP_UTF8, 0, pwz, len, nullptr, 0, nullptr, nullptr))
35 {
36 auto sz = (PSTR)_malloca(n * sizeof(CHAR));
37
38 if (len = WideCharToMultiByte(CP_UTF8, 0, pwz, len, sz, n, nullptr, nullptr); len)
39 {
40 psz = sz;
41 call_free = true;
42 }
43 else
44 {
45 _freea((void *)(psz));
46 }
47 }
48 }
49 if (psz)
50 {
51 OutputDebugStringCallback(psz);
52 }
53 if (call_free)
54 {
55 _freea((void *)(psz));
56 }
57 }
58 return EXCEPTION_CONTINUE_EXECUTION;
59 }
60
61 return EXCEPTION_CONTINUE_SEARCH;
62}
63
64namespace mal_toolkit::debug
65{
66 void RedirectOutputDebugString(std::function<void(std::string_view)> const &callback)
67 {
68 OutputDebugStringCallback = callback;
69 AddVectoredExceptionHandler(TRUE, VexHandler);
70 }
71} // namespace mal_toolkit::debug
72#endif