-
Notifications
You must be signed in to change notification settings - Fork 1
/
UnitFuncs.pas
306 lines (282 loc) · 8.6 KB
/
UnitFuncs.pas
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
unit UnitFuncs;
interface
uses
{$IFDEF MACOS}
MacApi.CoreFoundation, MacApi.Foundation,
{$ENDIF}
{$IFDEF IOS}
iOSapi.UIKit,
{$ENDIF}
{$IFDEF ANDROID}
Androidapi.JNI.JavaTypes, Androidapi.Helpers, Androidapi.JNI.Os,
{$ENDIF}
{$IFDEF MSWINDOWS}
Windows, PJSysInfo,
IpHlpApi, IpTypes,
{$ENDIF}
System.SysUtils,
System.Classes,
IdIcmpClient;
type
TDeviceInfoRec = record
OSNameStr: String;
OSVersionStr: String;
DeviceTypeStr: String;
ComputerNameStr:String;
BoardNameStr:String; //Motherboard name (TODO)
end;
//single NIC info(TODO)
TNICInfoRec = record
MacAddr: array[0..5] of Byte;
IPAddress:string;
end;
//List of NIC(TODO)
TNICList = record
NICList: array of TNICInfoRec;
end;
{$IFDEF MACOS}
function NSUserName: Pointer; cdecl;
external '/System/Library/Frameworks/Foundation.framework/Foundation' name
'_NSUserName';
{$ENDIF}
function getIndyReplyMsg(reply: TReplyStatusTypes): string;
function GetComputerName: string;
function GetUserName: String;
procedure Split(Delimiter: Char; Str: string; ListOfStrings: TStrings);
{$IFDEF ANDROID}
function GetCodename(VerString: string): string;
{$ENDIF}
function GetDeviceInfo(): TDeviceInfoRec;
implementation
procedure Split(Delimiter: Char; Str: string; ListOfStrings: TStrings);
begin
ListOfStrings.Clear;
ListOfStrings.Delimiter := Delimiter;
ListOfStrings.DelimitedText := Str;
end;
function getIndyReplyMsg(reply: TReplyStatusTypes): string;
var
ErrorText: string;
begin
case reply of
rsEcho:
ErrorText := 'An Echo was received.';
rsError:
ErrorText := 'An error has occurred.';
rsTimeOut:
ErrorText := 'Timeout occurred before a response was received.';
rsErrorUnreachable:
ErrorText := 'The address for the ICMP message is not available.';
rsErrorTTLExceeded:
ErrorText := 'Time-To-Live exceeded for an ICMP response.';
rsErrorPacketTooBig:
ErrorText := 'Packet Too Big';
rsErrorParameter:
ErrorText := 'Error Parameter';
rsErrorDatagramConversion:
ErrorText := 'Error Datagram Conversion';
rsErrorSecurityFailure:
ErrorText := 'Error Security Failure';
rsSourceQuench:
ErrorText := 'Source Quench';
rsRedirect:
ErrorText := 'Redirect';
rsTimeStamp:
ErrorText := 'TimeStamp';
rsInfoRequest:
ErrorText := 'InfoRequest';
rsAddressMaskRequest:
ErrorText := 'AddressMaskRequest';
rsTraceRoute:
ErrorText := 'TraceRoute';
rsMobileHostReg:
ErrorText := 'MobileHostReg';
rsMobileHostRedir:
ErrorText := 'MobileHostRedir';
rsIPv6WhereAreYou:
ErrorText := 'IPv6 Where Are You';
rsIPv6IAmHere:
ErrorText := 'IPv6 I Am Here';
rsSKIP:
ErrorText := 'Skip';
else
ErrorText := 'Unknown Error';
end;
result := ErrorText;
end;
function GetComputerName: string;
{$IFDEF MSWINDOWS}
var
buffer: array [0 .. MAX_COMPUTERNAME_LENGTH + 1] of Char;
Size: Cardinal;
{$ENDIF}
begin
{$IFDEF MACOS}
// TODO: MAC OS/Android
result:='TODO: MAC OS/Android';
{$ENDIF}
{$IFDEF MSWINDOWS}
Size := MAX_COMPUTERNAME_LENGTH + 1;
Windows.GetComputerName(@buffer, Size);
result := StrPas(buffer);
{$ENDIF}
end;
function GetUserName: String;
{$IFDEF MSWINDOWS}
var
nSize: DWord;
{$ENDIF}
begin
{$IFDEF MACOS}
result := TNSString.Wrap(NSUserName).UTF8String;
{$ENDIF}
{$IFDEF MSWINDOWS}
nSize := 1024;
SetLength(result, nSize);
if Windows.GetUserName(PChar(result), nSize) then
begin
SetLength(result, nSize - 1)
end
else
begin
RaiseLastOSError;
end
{$ENDIF}
end;
function GetDeviceInfo(): TDeviceInfoRec;
var
{$IFDEF IOS}
Device: UIDevice;
OSNameStr, OSVersionStr, DeviceTypeStr: string;
{$ENDIF}
DeviceInfoRec: TDeviceInfoRec;
begin
{$IFDEF IOS}
Device := TUIDevice.Wrap(TUIDevice.OCClass.currentDevice);
DeviceInfoRec.OSNameStr := Device.systemName.UTF8String;
DeviceInfoRec.OSVersionStr := Device.systemVersion.UTF8String;
DeviceInfoRec.DeviceTypeStr := Device.model.UTF8String;
DeviceInfoRec.ComputerNameStr := GetComputerName;
DeviceInfoRec.BoardNameStr := 'TODO(IOS)';
{$ENDIF}
{$IFDEF ANDROID}
DeviceInfoRec.OSNameStr:= GetCodename(JStringToString(TJBuild_VERSION.JavaClass.RELEASE));
DeviceInfoRec.OSVersionStr:= JStringToString(TJBuild_VERSION.JavaClass.RELEASE);
DeviceInfoRec.DeviceTypeStr:= JStringToString(TJBuild.JavaClass.model);
DeviceInfoRec.ComputerNameStr := GetComputerName;
DeviceInfoRec.BoardNameStr := 'TODO(ANDROID)';
{$ENDIF}
{$IFDEF MSWINDOWS}
DeviceInfoRec.OSNameStr := TPJOSInfo.ProductName;
DeviceInfoRec.OSVersionStr:= intTostr(TPJOSInfo.MajorVersion)+ '.'
+ intTostr(TPJOSInfo.MinorVersion)+ '.'
+ intTostr(TPJOSInfo.BuildNumber)+ '.';
DeviceInfoRec.DeviceTypeStr:=intTostr(Win32ProductType);
DeviceInfoRec.ComputerNameStr := TPJComputerInfo.ComputerName;
DeviceInfoRec.BoardNameStr := TPJComputerInfo.SystemProductName;
{$ENDIF}
result:= DeviceInfoRec;
end;
{$IFDEF ANDROID}
function GetCodename(VerString: string): string;
begin
if VerString = '1.0' then
result := 'BASE'
else if VerString = '1.1' then
result := 'BASE_1_1'
else if VerString = '1.5' then
result := 'CUPCAKE'
else if VerString = '1.6' then
result := 'DONUT'
else if VerString = '2.0' then
result := 'ECLAIR'
else if VerString = '2.0.1' then
result := 'ECLAIR_0_1'
else if VerString = '2.1' then
result := 'ECLAIR_MR1'
else if VerString = '2.2' then
result := 'FROYO'
else if VerString = '2.3' then
result := 'GINGERBREAD'
else if VerString = '2.3.3' then
result := 'GINGERBREAD_MR1'
else if VerString = '3.0' then
result := 'HONEYCOMB'
else if VerString = '3.1' then
result := 'HONEYCOMB_MR1'
else if VerString = '3.2' then
result := 'HONEYCOMB_MR2'
else if VerString = '4.0' then
result := 'ICE_CREAM_SANDWICH'
else if VerString = '4.0.3' then
result := 'ICE_CREAM_SANDWICH_MR1'
else if VerString = '4.1' then
result := 'JELLY_BEAN'
else if VerString = '4.2' then
result := 'JELLY_BEAN_MR1'
else if VerString = '4.3' then
result := 'JELLY_BEAN_MR2'
else if Pos('4.4', VerString) = 1 then
result := 'KITKAT'
else
result := 'UNKNOWN';
end;
{$ENDIF}
{$IFDEF MSWINDOWS}
procedure RetrieveLocalAdapterInformation(strings: TStrings);
var
pAdapterInfo, pTempAdapterInfo: PIP_ADAPTER_INFO;
AdapterInfo: IP_ADAPTER_INFO;
BufLen: DWORD;
Status: DWORD;
strMAC: String;
i: Integer;
begin
strings.Clear;
BufLen:= sizeof(AdapterInfo);
pAdapterInfo:= @AdapterInfo;
Status:= GetAdaptersInfo(nil, BufLen);
pAdapterInfo:= AllocMem(BufLen);
try
Status:= GetAdaptersInfo(pAdapterInfo, BufLen);
if (Status <> ERROR_SUCCESS) then
begin
case Status of
ERROR_NOT_SUPPORTED:
strings.Add('GetAdaptersInfo is not supported by the operating ' +
'system running on the local computer.');
ERROR_NO_DATA:
strings.Add('No network adapter on the local computer.');
else
strings.Add('GetAdaptersInfo failed with error #' + IntToStr(Status));
end;
Dispose(pAdapterInfo);
Exit;
end;
while (pAdapterInfo <> nil) do
begin
strings.Add('Description: ' + pAdapterInfo^.Description);
strings.Add('Name: ' + pAdapterInfo^.AdapterName);
strMAC := '';
for I := 0 to pAdapterInfo^.AddressLength - 1 do
strMAC := strMAC + '-' + IntToHex(pAdapterInfo^.Address[I], 2);
Delete(strMAC, 1, 1);
strings.Add('MAC address: ' + strMAC);
strings.Add('IP address: ' + pAdapterInfo^.IpAddressList.IpAddress.S);
strings.Add('IP subnet mask: ' + pAdapterInfo^.IpAddressList.IpMask.S);
strings.Add('Gateway: ' + pAdapterInfo^.GatewayList.IpAddress.S);
strings.Add('DHCP enabled: ' + IntTOStr(pAdapterInfo^.DhcpEnabled));
strings.Add('DHCP: ' + pAdapterInfo^.DhcpServer.IpAddress.S);
strings.Add('Have WINS: ' + BoolToStr(pAdapterInfo^.HaveWins,True));
strings.Add('Primary WINS: ' + pAdapterInfo^.PrimaryWinsServer.IpAddress.S);
strings.Add('Secondary WINS: ' + pAdapterInfo^.SecondaryWinsServer.IpAddress.S);
pTempAdapterInfo := pAdapterInfo;
pAdapterInfo:= pAdapterInfo^.Next;
if assigned(pAdapterInfo) then Dispose(pTempAdapterInfo);
end;
finally
Dispose(pAdapterInfo);
end;
end;
{$ENDIF}
end.