-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtools.cpp
More file actions
210 lines (186 loc) · 5.03 KB
/
tools.cpp
File metadata and controls
210 lines (186 loc) · 5.03 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// ---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
// #define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <winsock2.h>
// #include <ws2ipdef.h>
// #include <ws2tcpip.h>
#include <iphlpapi.h>
#include <sstream>
#include <iostream>
#include <iomanip>
#include "tools.h"
// ---------------------------------------------------------------------------
String tl_GetModuleName()
{
wchar_t buf[1024];
GetModuleFileName(NULL, buf, 1024);
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();
}
// ---------------------------------------------------------------------------
void t_RunProcess(String runcmd)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
if (CreateProcess(NULL, runcmd.w_str(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
{
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
else
{
String msg;
msg.printf(L"CreateProcess failed. Error: %lx\n%s", GetLastError(),
runcmd.w_str());
throw Exception(msg);
}
}
// ---------------------------------------------------------------------------
String t_GetGateways()
{
String rv;
DWORD dwRetVal = 0;
/* Variables used to print error messages */
LPVOID lpMsgBuf;
/* Variables used to store information returned by
GetIpAddrTable */
PIP_ADAPTER_INFO pAdapterInfo;
PIP_ADAPTER_INFO pAdapter = NULL;
DWORD dwBufLen = sizeof(IP_ADAPTER_INFO);
pAdapterInfo = (IP_ADAPTER_INFO*) malloc(sizeof(IP_ADAPTER_INFO));
if (pAdapterInfo == NULL)
{
throw new Exception(L"Error allocating memory needed to call GetAdaptersinfo");
}
if (GetAdaptersInfo(pAdapterInfo, &dwBufLen) == ERROR_BUFFER_OVERFLOW)
{
free(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO*) malloc(dwBufLen);
if (pAdapterInfo == NULL)
{
throw new Exception
(L"Error allocating memory needed to call GetAdaptersinfo");
}
}
if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &dwBufLen)) == NO_ERROR)
{
pAdapter = pAdapterInfo;
while (pAdapter)
{
// print_gateway(pAdapter);
String str;
String ip(pAdapter->GatewayList.IpAddress.String);
if (ip == L"0.0.0.0")
{
ip = L"(undefined)";
}
str.printf(L"%s\r\n%s\r\n", String(pAdapter->Description).w_str(),
ip.w_str());
rv += str;
pAdapter = pAdapter->Next;
}
}
else
{
String str;
str.printf(L"GetAdaptersInfo failed : %d", dwRetVal);
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwRetVal, MAKELANGID(LANG_NEUTRAL,
SUBLANG_DEFAULT), // Default language
(LPTSTR) & lpMsgBuf, 0, NULL))
{
String str1;
str1.printf(L"\nError: %s", lpMsgBuf);
str += str1;
}
LocalFree(lpMsgBuf);
free(pAdapterInfo);
throw new Exception(str);
}
if (pAdapterInfo)
free(pAdapterInfo);
return rv;
}
// ---------------------------------------------------------------------------
int t_Max(int n1, int n2)
{
if (n1 > n2)
return n1;
else
return n2;
}
// ---------------------------------------------------------------------------
void t_SetScrollWidth(TListBox *lb)
{
int MaxWidth = -1;
lb->Canvas->Font = lb->Font;
for (int i = 0; i < lb->Items->Count; i++)
{
MaxWidth = t_Max(MaxWidth, lb->Canvas->TextWidth(lb->Items->Strings[i]));
}
// consider non-client area
if (MaxWidth != -1)
lb->ScrollWidth = MaxWidth + lb->Width - lb->ClientWidth;
}
// ---------------------------------------------------------------------------