8static std::function<void(std::string_view)> OutputDebugStringCallback;
9LONG NTAPI VexHandler(PEXCEPTION_POINTERS ExceptionInfo)
11 PEXCEPTION_RECORD ExceptionRecord = ExceptionInfo->ExceptionRecord;
13 switch (ExceptionRecord->ExceptionCode)
15 case DBG_PRINTEXCEPTION_WIDE_C:
16 case DBG_PRINTEXCEPTION_C:
18 if (ExceptionRecord->NumberParameters >= 2)
20 bool call_free =
false;
21 auto len = (ULONG)ExceptionRecord->ExceptionInformation[0];
30 up = ExceptionRecord->ExceptionInformation[1];
32 if (ExceptionRecord->ExceptionCode == DBG_PRINTEXCEPTION_WIDE_C)
34 if (ULONG n = WideCharToMultiByte(CP_UTF8, 0, pwz, len,
nullptr, 0,
nullptr,
nullptr))
36 auto sz = (PSTR)_malloca(n *
sizeof(CHAR));
38 if (len = WideCharToMultiByte(CP_UTF8, 0, pwz, len, sz, n,
nullptr,
nullptr); len)
45 _freea((
void *)(psz));
51 OutputDebugStringCallback(psz);
55 _freea((
void *)(psz));
58 return EXCEPTION_CONTINUE_EXECUTION;
61 return EXCEPTION_CONTINUE_SEARCH;
64namespace mal_toolkit::debug
66 void RedirectOutputDebugString(std::function<
void(std::string_view)>
const &callback)
68 OutputDebugStringCallback = callback;
69 AddVectoredExceptionHandler(TRUE, VexHandler);