-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuptime.cpp
More file actions
142 lines (128 loc) · 2.99 KB
/
Copy pathuptime.cpp
File metadata and controls
142 lines (128 loc) · 2.99 KB
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
#include "common.h"
#include "osver.h"
#include "Support\\LWAnsiString\LWAnsiString.h"
typedef ULONGLONG(WINAPI* GetTickCount64_PTR)();
typedef DWORD(WINAPI* GetTickCount_PTR)();
#define WINDOWS_VISTA_MAJOR 6
#define WINDOWS_VISTA_MINOR 0
/// <summary>
/// GetTickCount or GetTickCount64() and return it in out.
/// </summary>
/// <param name="out">result of the call. IF GetTickCount, upper ULONG is 0</param>
/// <param name="message_result">message to display if needed</param>
/// <param name="result"></param>
/// <returns></returns>
int GetUptime(ULONGLONG* out, const char** message_result, int* result)
{
bool Probe64 = false;
ULONGLONG uptime = 0;
// arg validate
if (result == nullptr)
{
return -3;
}
if (out == nullptr)
{
return -1;
}
if (message_result == nullptr)
{
return -2;
}
*message_result = nullptr;
*result = 0;
// check if we have the version info. If not, get it.
if (!VERSION_INFO_WAS_GOTTON)
FetchVersionInfo(&GlobalVersionInfo, &VERISON_INFO_IS_UNICODE); // note this is the same routien osver tool uses.
if (!VERSION_INFO_WAS_GOTTON)
{
return -4;
}
if ((GlobalVersionInfo.A.dwMajorVersion >= WINDOWS_VISTA_MAJOR) && (GlobalVersionInfo.A.dwMinorVersion >= WINDOWS_VISTA_MINOR))
{
// Vista+. Dynamticly Preference is GetTickCount64.
Probe64 = true;
}
HMODULE kernel32 = LoadLibraryA("kernel32.dll");
// check kernel failure.
if (!kernel32)
{
if (message_result != nullptr)
{
*message_result = Message_CantLoadKernel32;
}
if (result != nullptr)
{
*result = -5;
}
return -6;
}
else
{
GetTickCount64_PTR Tick64 = nullptr;
if (Probe64)
{
Tick64 = (GetTickCount64_PTR)GetProcAddress(kernel32, "GetTickCount64");
}
if (Tick64 != nullptr)
{
*out = Tick64();
}
else
{
*out = GetTickCount();
}
FreeLibrary(kernel32);
}
return -99;
}
bool ReportUpTimeAsExitCode(int* result, const char** message_result, const char* argv[], int argc)
{
ULONGLONG update;
GetUptime(&update, nullptr, nullptr);
*result = (int)update;
return true;
}
bool ReportUpTimeToStdout(int* result, const char** message_result, const char* argv[], int argc)
{
ULONGLONG update=0;
GetUptime(&update, nullptr, nullptr);
if (update == 0)
{
if (message_result != nullptr)
{
*message_result = "Failed to get uptime";
}
if (result != nullptr)
{
*result = -1;
}
return false;
}
else
{
LWAnsiString* output = LWAnsiString_CreateFromString("Uptime (millseconds): "); // create a new string to hold the output
char* local = nullptr;
int size = 0;
if (output == nullptr)
{
if (message_result != nullptr)
{
*message_result = "Failed to create output string";
}
if (result != nullptr)
{
*result = -2;
}
return false;
}
else
{
LWAnsiString_AppendNumberA(update, output, &size); // append the number to the output string
WriteStdout(LWAnsiString_ToCStr(output)); // write the output to stdout
LWAnsiString_FreeString(output); // free the output string
return true;
}
return false;
}
}