-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbgtrace.cpp
112 lines (81 loc) · 1.93 KB
/
dbgtrace.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
#include "stdafx.h"
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include <iostream>
#include <fstream>
#ifdef _DEBUG
void INIT_CRT_DEBUG_REPORT()
{
/*
int DbgFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
// DbgFlag |= _CRTDBG_LEAK_CHECK_DF;
// DbgFlag |= _CRTDBG_ALLOC_MEM_DF | _CRTDBG_DELAY_FREE_MEM_DF;
DbgFlag |= _CRTDBG_DELAY_FREE_MEM_DF;
// DbgFlag &= ~_CRTDBG_CHECK_ALWAYS_DF;
_CrtSetDbgFlag(DbgFlag);
*/
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG);
}
bool IsValidAddress(const void *p, UINT nBytes, BOOL bReadWrite)
{
return _CrtIsValidPointer(p, nBytes, bReadWrite) == TRUE;
}
bool IsValidString(LPCTSTR psz, int nLength)
{
if(psz == NULL) return false;
return (::IsBadStringPtr(psz, nLength) == 0);
}
#ifdef _UNICODE
void _TRACE(LPCWSTR szFormat, ...)
{
WCHAR szBuffer[1024];
va_list ap;
va_start(ap, szFormat);
StringCbVPrintfW(szBuffer, sizeof(szBuffer), szFormat, ap);
va_end(ap);
OutputDebugStringW(szBuffer);
}
void _TRACE(LPCSTR pszFormat, ...)
{
CHAR szBuffer[1024];
va_list ap;
va_start(ap, pszFormat);
StringCbVPrintfA(szBuffer, sizeof(szBuffer), pszFormat, ap);
va_end(ap);
OutputDebugStringA(szBuffer);
}
#else
void _TRACE(LPCTSTR szFormat, ...)
{
TCHAR szBuffer[1024];
va_list ap;
va_start(ap, szFormat);
_vsntprintf(szBuffer, sizeof(szBuffer), szFormat, ap);
va_end(ap);
OutputDebugString(szBuffer);
}
#endif // _UNICODE
TCHAR* GetLastErrorMsg()
{
static TCHAR buff[256];
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
buff, sizeof(buff), NULL);
return buff;
}
#endif
void OutputDebugPrint(LPCWSTR szFormat, ...)
{
WCHAR szBuffer[1024];
va_list ap;
va_start(ap, szFormat);
StringCbVPrintfW(szBuffer, sizeof(szBuffer), szFormat, ap);
va_end(ap);
OutputDebugStringW(szBuffer);
}