This repository has been archived by the owner on Mar 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeaddin.cpp
166 lines (140 loc) · 4.05 KB
/
timeaddin.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <tchar.h>
#include "custview.h"
#include "TimeAddIn.h"
#include <string>
using namespace std;
static string FormatSystemTime( const SYSTEMTIME & st )
{
char szDate[50];
size_t NumChars = GetDateFormatA( GetThreadLocale(), DATE_SHORTDATE, &st, NULL, szDate, _countof( szDate ) );
if ( NumChars == 0 )
{
// Failed
szDate[0] = '\0';
}
char szTime[50];
NumChars = GetTimeFormatA( GetThreadLocale(), TIME_FORCE24HOURFORMAT, &st, NULL, szTime, _countof( szTime ) );
if ( NumChars == 0 )
{
// Failed
szTime[0] = '\0';
}
string str;
str = szDate;
str += ' ';
str += szTime;
// If the string isn't more than the separator something's wrong, return an empty string
if ( str.length() <= 1 )
{
str.clear();
}
return str;
}
ADDIN_API HRESULT WINAPI AddIn_SystemTime( DWORD /*dwAddress*/, DEBUGHELPER *pHelper, int /*nBase*/, BOOL /*bUniStrings*/,
char *pResult, size_t max, DWORD /*reserved*/ )
{
SYSTEMTIME SysTime;
DWORD nGot;
// read system time from debuggee memory space
HRESULT hr;
hr = pHelper->ReadDebuggeeMemoryEx( pHelper, pHelper->GetRealAddress( pHelper ), sizeof( SysTime ), &SysTime, &nGot );
if ( hr == S_OK )
{
if ( nGot == sizeof( SysTime ) )
{
string str = FormatSystemTime( SysTime );
lstrcpynA( pResult,
!str.empty() ?
str.c_str() :
"Invalid",
max );
}
else
{
hr = E_FAIL;
}
}
return hr;
}
ADDIN_API HRESULT WINAPI AddIn_FileTime( DWORD /*dwAddress*/, DEBUGHELPER *pHelper, int nBase, BOOL /*bUniStrings*/,
char *pResult, size_t BufferMax, DWORD /*reserved*/ )
{
FILETIME ftUtc;
DWORD nGot;
HRESULT hr;
// read file time from debuggee memory space
hr = pHelper->ReadDebuggeeMemoryEx( pHelper, pHelper->GetRealAddress( pHelper ), sizeof( ftUtc ), &ftUtc, &nGot );
if ( hr == S_OK )
{
if ( nGot == sizeof( ftUtc ) )
{
/* Note: VS2008 (for me) passes a buffer of size 398 */
// convert to SystemTime
SYSTEMTIME SysTime;
if ( FileTimeToSystemTime( &ftUtc, &SysTime ) )
{
string strUtc = FormatSystemTime( SysTime );
SYSTEMTIME lst;
if ( SystemTimeToTzSpecificLocalTime( NULL, &SysTime, &lst ) )
{
string strLoc;
strLoc = FormatSystemTime( lst );
// Determine if there's a difference between local & utc so that I can display the most appropriate zone name
LONGLONG diffInMins;
{
FILETIME lft;
SystemTimeToFileTime( &lst, &lft );
LONGLONG diffInTicks = reinterpret_cast<LARGE_INTEGER*>(&lft)->QuadPart - reinterpret_cast<LARGE_INTEGER*>(&ftUtc)->QuadPart;
diffInMins = diffInTicks / (10000000 * 60);
}
TIME_ZONE_INFORMATION tzi;
GetTimeZoneInformation( &tzi );
// Remove the current bias from UTC and if it's now non-zero, its the DST zone
diffInMins += tzi.Bias;
LPCWSTR ZoneName = diffInMins != 0 ? tzi.DaylightName : tzi.StandardName;
sprintf_s( pResult, BufferMax, "[utc] %s [%ls] %s", strUtc.c_str(), ZoneName, strLoc.c_str() );
}
else
{
sprintf_s( pResult, BufferMax, "utc: %s ???", strUtc.c_str() );
}
}
else
{
// Prior to VS2017 RC2, VS only ever passed 10 for nBase, newer versions should support base values of 2, 8, 10, or 16
char szHigh[33]; // 32 chars max for base 2 from a DWORD value
char szLow[33];
_ui64toa_s( ftUtc.dwHighDateTime, szHigh, _countof(szHigh), nBase );
_ui64toa_s( ftUtc.dwLowDateTime, szLow, _countof(szLow), nBase );
char * Prefix;
switch ( nBase )
{
case 2:
Prefix = "0b"; // binary
break;
case 8:
Prefix = "0"; // octal
break;
case 10:
Prefix = ""; // decimal
break;
case 16:
Prefix = "0x"; // hex
break;
default:
Prefix = "?"; // unknown
break;
}
sprintf_s( pResult, BufferMax, "Invalid [%s%s:%s%s]", Prefix, szHigh, Prefix, szLow );
}
}
else
{
hr = E_FAIL;
}
}
return hr;
}