-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtilityFx.cpp
183 lines (155 loc) · 3.67 KB
/
UtilityFx.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*---------------------------------------------------------------------------*/
// Author : hiyohiyo
// Mail : [email protected]
// Web : https://crystalmark.info/
// License : The MIT License
/*---------------------------------------------------------------------------*/
#include "pch.h"
#include "UtilityFx.h"
#include <io.h>
#pragma comment(lib,"version.lib")
////------------------------------------------------
// Debug
////------------------------------------------------
static const DWORD DEBUG_MODE_NONE = 0;
static const DWORD DEBUG_MODE_LOG = 1;
static const DWORD DEBUG_MODE_MESSAGE = 2;
static DWORD debugMode = DEBUG_MODE_NONE;
void SetDebugMode(DWORD mode)
{
if (mode <= DEBUG_MODE_MESSAGE)
{
debugMode = mode;
}
else
{
debugMode = DEBUG_MODE_NONE;
}
}
void DebugPrint(CString cstr)
{
static BOOL flag = TRUE;
static TCHAR file[MAX_PATH] = L"";
static DWORD first = (DWORD)GetTickCountFx();
CString output;
output.Format(L"%08d ", (DWORD)GetTickCountFx() - first);
output += cstr;
output.Append(L"\n");
output.Replace(L"\r", L"");
if (flag)
{
TCHAR* ptrEnd;
::GetModuleFileName(NULL, file, MAX_PATH);
if ((ptrEnd = _tcsrchr(file, '.')) != NULL)
{
*ptrEnd = '\0';
_tcscat_s(file, MAX_PATH, L".log");
}
DeleteFile(file);
flag = FALSE;
}
if (debugMode == DEBUG_MODE_NONE)
{
return;
}
FILE* fp;
_tfopen_s(&fp, file, L"ac");
if (fp != NULL)
{
_ftprintf(fp, L"%s", (LPCTSTR)output);
fflush(fp);
fclose(fp);
}
if (debugMode == DEBUG_MODE_MESSAGE)
{
AfxMessageBox(output);
}
}
////------------------------------------------------
// File Information
////------------------------------------------------
int GetFileVersion(const TCHAR* file, TCHAR* version)
{
ULONG reserved = 0;
VS_FIXEDFILEINFO vffi;
TCHAR* buf = NULL;
int Locale = 0;
TCHAR str[256];
str[0] = '\0';
UINT size = GetFileVersionInfoSize((TCHAR*)file, &reserved);
TCHAR* vbuf = new TCHAR[size];
if (GetFileVersionInfo((TCHAR*)file, 0, size, vbuf))
{
VerQueryValue(vbuf, L"\\", (void**)&buf, &size);
CopyMemory(&vffi, buf, sizeof(VS_FIXEDFILEINFO));
VerQueryValue(vbuf, L"\\VarFileInfo\\Translation", (void**)&buf, &size);
CopyMemory(&Locale, buf, sizeof(int));
wsprintf(str, L"\\StringFileInfo\\%04X%04X\\%s",
LOWORD(Locale), HIWORD(Locale), L"FileVersion");
VerQueryValue(vbuf, str, (void**)&buf, &size);
_tcscpy_s(str, 256, buf);
if (version != NULL)
{
_tcscpy_s(version, 256, buf);
}
}
delete[] vbuf;
if (_tcscmp(str, L"") != 0)
{
return int(_tstof(str) * 100);
}
else
{
return 0;
}
}
BOOL IsFileExist(const TCHAR* path)
{
FILE* fp;
errno_t err;
err = _tfopen_s(&fp, path, L"rb");
if (err != 0 || fp == NULL)
{
return FALSE;
}
fclose(fp);
return TRUE;
}
////------------------------------------------------
// Utility
////------------------------------------------------
typedef ULONGLONG(WINAPI* FuncGetTickCount64)();
ULONGLONG GetTickCountFx()
{
static FuncGetTickCount64 pGetTickCount64 = (FuncGetTickCount64)GetProcAddress(GetModuleHandle(L"kernel32"), "GetTickCount64");
if (pGetTickCount64 != NULL)
{
return (ULONGLONG)pGetTickCount64();
}
else
{
return (ULONGLONG)GetTickCount();
}
}
ULONG64 B8toB64(BYTE b0, BYTE b1, BYTE b2, BYTE b3, BYTE b4, BYTE b5, BYTE b6, BYTE b7)
{
ULONG64 data =
((ULONG64)b7 << 56)
+ ((ULONG64)b6 << 48)
+ ((ULONG64)b5 << 40)
+ ((ULONG64)b4 << 32)
+ ((ULONG64)b3 << 24)
+ ((ULONG64)b2 << 16)
+ ((ULONG64)b1 << 8)
+ ((ULONG64)b0 << 0);
return data;
}
DWORD B8toB32(BYTE b0, BYTE b1, BYTE b2, BYTE b3)
{
DWORD data =
((DWORD)b3 << 24)
+ ((DWORD)b2 << 16)
+ ((DWORD)b1 << 8)
+ ((DWORD)b0 << 0);
return data;
}