-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.cpp
187 lines (165 loc) · 4.58 KB
/
tools.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
184
185
186
187
// ---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <sstream>
#include <iostream>
#include <iomanip>
#include <psapi.h>
#include <System.DateUtils.hpp>
#include "tools.h"
// ---------------------------------------------------------------------------
String tl_GetModuleName()
{
wchar_t buf[MAX_PATH];
GetModuleFileName(NULL, buf, MAX_PATH);
return (String)buf;
}
// ---------------------------------------------------------------------------
String tl_GetProgramPath() // path with trailing '\'
{
static String Path = tl_GetModuleName();
return Path.SubString(1, Path.LastDelimiter(L"\\"));
}
// ---------------------------------------------------------------------------
void tl_RunInMainThread(FTCdef FuncToCall, const String fP1)
{
struct Args
{
String P1;
FTCdef FTC;
void __fastcall ExecFuncQueue()
{
try
{
FTC(P1);
}
__finally
{
delete this;
}
}
};
Args *args = new Args;
args->P1 = fP1;
args->FTC = FuncToCall;
TThread::Queue(NULL, &args->ExecFuncQueue);
}
// ---------------------------------------------------------------------------
String BytesToHexStr(unsigned char *bytes, int count)
{
if (count <= 0)
return L"";
std::wstringstream ss;
ss << std::hex << std::uppercase;
for (int i = 0; i < count; i++)
{
ss << std::setw(2) << std::setfill(L'0') << (int)bytes[i] << L" ";
}
String hexStr(ss.str().c_str());
return hexStr.TrimRight();
}
// ---------------------------------------------------------------------------
String BytesToHexStr(const TIdBytes &bytes)
{
if (bytes.Length <= 0)
return L"";
std::wstringstream ss;
ss << std::hex << std::uppercase;
for (int i = 0; i < bytes.Length; i++)
{
ss << std::setw(2) << std::setfill(L'0') << (int)bytes[i] << L" ";
}
String hexStr(ss.str().c_str());
return hexStr.TrimRight();
}
// ---------------------------------------------------------------------------
String GetWindowClassPlus(HWND hwnd)
{
wchar_t *tbuf = new wchar_t[2048];
tbuf[0] = L'\0';
GetClassName(hwnd, tbuf, 2047);
String ret(tbuf);
delete[]tbuf;
return ret;
}
// ---------------------------------------------------------------------------
String GetWindowTitlePlus(HWND hwnd)
{
wchar_t *tbuf = new wchar_t[2048];
tbuf[0] = L'\0';
GetWindowText(hwnd, tbuf, 2047);
String ret(tbuf);
delete[]tbuf;
return ret;
}
// ---------------------------------------------------------------------------
DWORD PSAPI_EnumProcesses(std::list<DWORD>& listProcessIDs, DWORD dwMaxProcessCount)
{
DWORD dwRet = NO_ERROR;
listProcessIDs.clear();
DWORD *pProcessIds = new DWORD[dwMaxProcessCount];
DWORD cb = dwMaxProcessCount*sizeof(DWORD);
DWORD dwBytesReturned = 0;
// call PSAPI EnumProcesses
if (::EnumProcesses(pProcessIds, cb, &dwBytesReturned))
{
// push returned process IDs into the output list
const int nSize = dwBytesReturned / sizeof(DWORD);
for (int nIndex = 0; nIndex < nSize; nIndex++)
{
listProcessIDs.push_back(pProcessIds[nIndex]);
}
}
else
{
dwRet = ::GetLastError();
}
delete[]pProcessIds;
return dwRet;
}
// ---------------------------------------------------------------------------
int t_DateTimeMs(const TDateTime &dtm)
{
Word dummy, ms;
DecodeDateTime(dtm, dummy, dummy, dummy, dummy, dummy, dummy, ms);
return (int)ms;
}
// ---------------------------------------------------------------------------
String t_DoubleToStr(double dbl)
{
String str;
str.printf(L"%f", dbl);
return str;
}
// ---------------------------------------------------------------------------
String t_MsDigit(const TDateTime &dt)
{
String ms;
ms.printf(L"%03d", t_DateTimeMs(dt));
// base index 1
return ms[1];
}
// ---------------------------------------------------------------------------
String t_MsFullDigits(const TDateTime &dt)
{
String ms;
ms.printf(L"%03d", t_DateTimeMs(dt));
return ms;
}
// ---------------------------------------------------------------------------
String t_FormatDateTimeMs(const TDateTime &dt)
{
String str;
str.printf(L"%s.%s", FormatDateTime(L"dd.mm.yyyy hh:nn:ss", dt).w_str(),
t_MsDigit(dt).w_str());
return str;
}
// ---------------------------------------------------------------------------
String t_FormatTimeMs(const TDateTime &dt)
{
String str;
str.printf(L"%s.%s", FormatDateTime(L"hh:nn:ss", dt).w_str(),
t_MsFullDigits(dt).w_str());
return str;
}
// ---------------------------------------------------------------------------