-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDXTrace.cpp
65 lines (54 loc) · 1.94 KB
/
DXTrace.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
#include "DXTrace.h"
#include <cstdio>
HRESULT WINAPI DXTraceW(_In_z_ const WCHAR* strFile, _In_ DWORD dwLine, _In_ HRESULT hr,
_In_opt_ const WCHAR* strMsg, _In_ bool bPopMsgBox)
{
WCHAR strBufferFile[MAX_PATH];
WCHAR strBufferLine[128];
WCHAR strBufferError[300];
WCHAR strBufferMsg[1024];
WCHAR strBufferHR[40];
WCHAR strBuffer[3000];
swprintf_s(strBufferLine, 128, L"%lu", dwLine);
if (strFile)
{
swprintf_s(strBuffer, 3000, L"%ls(%ls): ", strFile, strBufferLine);
OutputDebugStringW(strBuffer);
}
size_t nMsgLen = (strMsg) ? wcsnlen_s(strMsg, 1024) : 0;
if (nMsgLen > 0)
{
OutputDebugStringW(strMsg);
OutputDebugStringW(L" ");
}
// Windows SDK 8.0起DirectX的错误信息已经集成进错误码中,可以通过FormatMessageW获取错误信息字符串
// 不需要分配字符串内存
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, hr, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
strBufferError, 256, nullptr);
WCHAR* errorStr = wcsrchr(strBufferError, L'\r');
if (errorStr)
{
errorStr[0] = L'\0'; // 擦除FormatMessageW带来的换行符(把\r\n的\r置换为\0即可)
}
swprintf_s(strBufferHR, 40, L" (0x%0.8x)", hr);
wcscat_s(strBufferError, strBufferHR);
swprintf_s(strBuffer, 3000, L"错误码含义:%ls", strBufferError);
OutputDebugStringW(strBuffer);
OutputDebugStringW(L"\n");
if (bPopMsgBox)
{
wcscpy_s(strBufferFile, MAX_PATH, L"");
if (strFile)
wcscpy_s(strBufferFile, MAX_PATH, strFile);
wcscpy_s(strBufferMsg, 1024, L"");
if (nMsgLen > 0)
swprintf_s(strBufferMsg, 1024, L"当前调用:%ls\n", strMsg);
swprintf_s(strBuffer, 3000, L"文件名:%ls\n行号:%ls\n错误码含义:%ls\n%ls您需要调试当前应用程序吗?",
strBufferFile, strBufferLine, strBufferError, strBufferMsg);
int nResult = MessageBoxW(GetForegroundWindow(), strBuffer, L"错误", MB_YESNO | MB_ICONERROR);
if (nResult == IDYES)
DebugBreak();
}
return hr;
}