Menu

[r3207]: / trunk / Src / USystemInfo.pas  Maximize  Restore  History

Download this file

443 lines (395 with data), 14.0 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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2006-2013, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Static classes that provide information about the host system.
*
* Requires DelphiDabbler System information unit v3 or later.
}
unit USystemInfo;
interface
uses
// Delphi
ShlObj,
// DelphiDabbler library
PJSysInfo,
// Project
UBaseObjects;
type
{
TOSVer:
Record that records operation system major and minor version numbers.
}
TOSVer = packed record
VerHi: Word; // Major OS version number
VerLo: Word; // Minor OS version number
constructor Create(const AVerHi, AVerLo: Word);
{Record constructor. Sets initial field values.
@param AVerHi [in] Major version number.
@param AVerLo [in] Minor version number.
}
class operator GreaterThanOrEqual(const R1, R2: TOSVer): Boolean;
{Overload for >= operator on TOSVer.
@param R1 [in] First record to be checked.
@param R2 [in] Second record to be checked.
@return True in R1 >= R2, False otherwise.
}
end;
{
TOSInfo:
Exposes TPJOSInfo class to rest of program under a new name and extends to
add new methods. Adds a constructor that enforces nature as static class by
causing and assertion failure when called.
}
TOSInfo = class(TPJOSInfo)
strict private
class function CheckForKernelFn(const FnName: string): Boolean;
{Checks if a specified function exists in OSs kernel.
@param FnName [in] Name of required function.
@return True if function is present in kernel, false if not.
}
public
const Win2K: TOSVer = (VerHi: 5; VerLo: 0);
{Identifies Windows 2K}
const WinXP: TOSVer = (VerHi: 5; VerLo: 1);
{Identifies Windows XP}
const WinVista: TOSVer = (VerHi: 6; VerLo: 0);
{Identifies Windows Vista}
constructor Create;
{Class constructor. Causes an assertion failure if called. The object must
not be, and is never, constructed.
}
class function IsVistaOrLater: Boolean;
{Checks if the underlying operating system is Windows Vista or later.
Ignores any OS emulation.
@return True if OS is Vista or later, False if not.
}
class function IsXPOrLater: Boolean;
{Checks if the underlying operating system is Windows XP or later. Ignores
any OS emulation.
@return True if OS is XP or later, False if not.
}
class function CheckReportedOS(const MinVer: TOSVer): Boolean;
{Checks if the OS reported by Windows is the same as or later than a
specified version number.
@param MinVer [in] Minimum OS version required.
@return True if OS reported by Windows is same as or later than MinVer.
}
end;
/// <summary>Static class that provides information about the installed
/// version of Internet Explorer.</summary>
TIEInfo = class(TNoConstructObject)
strict private
const
/// <summary>Majic value indicating that browser version number has not
/// yet been determined.</summary>
UnsetMajorVersion = $FFFF;
class var
/// <summary>Records browser's major version number.</summary>
/// <remarks>Is set to UnsetMajorVersion until calculated.</remarks>
fMajorVersion: Word;
strict private
/// <summary>Returns version number string of current Internet Explorer
/// installation.</summary>
class function GetIEVersionStr: string;
/// <summary>Calculates major version number of Internet Explorer if it has
/// not already been calculated.</summary>
/// <remarks>See http://support.microsoft.com/kb/969393/en-us.</remarks>
class procedure InitMajorVersion;
public
const
/// <summary>Minimum version of Internet Explorer required to run
/// CodeSnip.</summary>
MinSupportedVersion = 6;
public
/// <summary>Static class constructor: initialises class.</summary>
class constructor Create;
/// <summary>Returns the major version number of the installed version of
/// Internet Explorer.</summary>
class function MajorVersion: Word;
/// <summary>Checks if the installed version of Internet Explorer is
//// supported by CodeSnip.</summary>
class function IsSupportedBrowser: Boolean; inline;
/// <summary>Checks if the installed version of Internet Explorer supports
/// the "max-height" CSS property.</summary>
class function SupportsCSSMaxHeight: Boolean; inline;
/// <summary>Checks if the installed version of Internet Explorer supports
/// the "overflow-x" CSS property sufficiently well to be used.</summary>
class function SupportsCSSOverflowX: Boolean; inline;
/// <summary>Checks if the installed version of Internet Explorer has the
/// "auto" overflow bug and needs to have its behaviour fixed when using
/// "auto" with the "overflow-x" property.</summary>
/// <remarks>Always returns False if Internet Explorer does not have
/// sufficient support for the "overflow-x" property.</remarks>
class function RequiresCSSOverflowXFix: Boolean; inline;
end;
{
TComputerInfo:
Exposes TPJComputerInfo class to rest of program under a new name with no
additions or changes other than to enforce nature as static class by adding
a constructor that causes an assertion failure when called.
}
TComputerInfo = class(TPJComputerInfo)
constructor Create;
{Class constructor. Causes an assertion failure if called. The object must
not be, and is never, constructed.
}
end;
{
TSystemFolders:
Exposes TPJSystemFolders class to rest of program under a new name and
extends to add new methods. Adds a constructor that enforces nature as
static class by causing and assertion failure when called.
}
TSystemFolders = class(TPJSystemFolders)
strict protected
class procedure FreePIDL(PIDL: PItemIDList);
{Uses to shell allocator to free the memory used by a PIDL.
@param PIDL [in] PIDL that is to be freed.
}
class function PIDLToFolderPath(PIDL: PItemIDList): string;
{Returns the full path to a file system folder described by a PIDL.
@param PIDL [in] PIDL describing folder.
@return Full path to folder described by PIDL or '' if PIDL refers to
virtual folder.
}
class function SpecialFolderPath(CSIDL: Integer): string;
{Returns the full path to a special file system folder.
@param CSIDL [in] Constant specifying the special folder.
@return Folder path or '' if the special folder is virtual or CSIDL not
supported on the OS.
}
public
constructor Create;
{Class constructor. Causes an assertion failure if called. The object must
not be, and is never, constructed.
}
class function CommonAppData: string;
{Gets common application data directory.
@return Required directory.
}
class function PerUserAppData: string;
{Gets the current user's application data directory.
@return Required directory.
}
end;
implementation
uses
// Delphi
SysUtils, Registry, Windows, ActiveX, ShFolder,
// Project
UIStringList;
{ TOSVer }
constructor TOSVer.Create(const AVerHi, AVerLo: Word);
{Record constructor. Sets initial field values.
@param AVerHi [in] Major version number.
@param AVerLo [in] Minor version number.
}
begin
VerHi := AVerHi;
VerLo := AVerLo;
end;
class operator TOSVer.GreaterThanOrEqual(const R1, R2: TOSVer): Boolean;
{Overload for >= operator on TOSVer.
@param R1 [in] First record to be checked.
@param R2 [in] Second record to be checked.
@return True in R1 >= R2, False otherwise.
}
begin
Result := (R1.VerHi > R2.VerHi)
or ((R1.VerHi = R2.VerHi) and (R1.VerLo >= R2.VerLo));
end;
{ TOSInfo }
class function TOSInfo.CheckForKernelFn(const FnName: string): Boolean;
{Checks if a specified function exists in OSs kernel.
@param FnName [in] Name of required function.
@return True if function is present in kernel, false if not.
}
const
cKernelDLL = 'kernel32.dll'; // name of kernel DLL
var
PFunction: Pointer; // pointer to required function if exists
begin
// Try to load GetProductInfo func from Kernel32: present if Vista
PFunction := GetProcAddress(GetModuleHandle(cKernelDLL), PChar(FnName));
Result := Assigned(PFunction);
end;
class function TOSInfo.CheckReportedOS(const MinVer: TOSVer): Boolean;
{Checks if the OS reported by Windows is the same as or later than a specified
version number.
@param MinVer [in] Minimum OS version required.
@return True if OS reported by Windows is same as or later than MinVer.
}
begin
Result := TOSVer.Create(TOSInfo.MajorVersion, TOSInfo.MinorVersion)
>= MinVer;
end;
constructor TOSInfo.Create;
{Class constructor. Causes an assertion failure if called. The object must not
be, and is never, constructed.
}
begin
Assert(False, ClassName + '.Create: Constructor can''t be called');
end;
class function TOSInfo.IsVistaOrLater: Boolean;
{Checks if the underlying operating system is Windows Vista or later. Ignores
any OS emulation.
@return True if OS is Vista or later, False if not.
}
begin
// The "GetProductInfo" API function only exists in the kernel of Vista and
// Win 2008 server and later
Result := CheckForKernelFn('GetProductInfo');
end;
class function TOSInfo.IsXPOrLater: Boolean;
{Checks if the underlying operating system is Windows XP or later. Ignores
any OS emulation.
@return True if OS is XP or later, False if not.
}
begin
// The "ActivateActCtx" API function only exists in the kernel of XP and Win
// 2003 server and later
Result := CheckForKernelFn('ActivateActCtx');
end;
{ TComputerInfo }
constructor TComputerInfo.Create;
{Class constructor. Causes an assertion failure if called. The object must not
be, and is never, constructed.
}
begin
Assert(False, ClassName + '.Create: Constructor can''t be called');
end;
{ TSystemFolders }
class function TSystemFolders.CommonAppData: string;
{Gets common application data directory.
@return Required directory.
}
begin
Result := SpecialFolderPath(CSIDL_COMMON_APPDATA);
end;
constructor TSystemFolders.Create;
{Class constructor. Causes an assertion failure if called. The object must not
be, and is never, constructed.
}
begin
Assert(False, ClassName + '.Create: Constructor can''t be called');
end;
class procedure TSystemFolders.FreePIDL(PIDL: PItemIDList);
{Uses to shell allocator to free the memory used by a PIDL.
@param PIDL [in] PIDL that is to be freed.
}
var
Malloc: IMalloc; // shell's allocator
begin
if Succeeded(SHGetMalloc(Malloc)) then
Malloc.Free(PIDL);
end;
class function TSystemFolders.PerUserAppData: string;
{Gets the current user's application data directory.
@return Required directory.
}
begin
Result := SpecialFolderPath(CSIDL_APPDATA);
end;
class function TSystemFolders.PIDLToFolderPath(PIDL: PItemIDList): string;
{Returns the full path to a file system folder described by a PIDL.
@param PIDL [in] PIDL describing folder.
@return Full path to folder described by PIDL or '' if PIDL refers to
virtual folder.
}
begin
SetLength(Result, MAX_PATH);
if SHGetPathFromIDList(PIDL, PChar(Result)) then
Result := PChar(Result)
else
Result := '';
end;
class function TSystemFolders.SpecialFolderPath(CSIDL: Integer): string;
{Returns the full path to a special file system folder.
@param CSIDL [in] Constant specifying the special folder.
@return Folder path or '' if the special folder is virtual or CSIDL not
supported on the OS.
}
var
PIDL: PItemIDList; // PIDL of the special folder
begin
Result := '';
if Succeeded(SHGetSpecialFolderLocation(0, CSIDL, PIDL)) then
begin
try
Result := ExcludeTrailingPathDelimiter(PIDLToFolderPath(PIDL));
finally
FreePIDL(PIDL);
end;
end
end;
{ TIEInfo }
class constructor TIEInfo.Create;
begin
fMajorVersion := UnsetMajorVersion;
end;
class function TIEInfo.GetIEVersionStr: string;
var
Reg: TRegistry; // registry access object
const
IERegKey = 'Software\Microsoft\Internet Explorer';
RegValName = 'Version'; // name of registry value for IE up to 9
RegValNameIE10 = 'svcVersion'; // name of registry value for IE 10
begin
Result := '';
Reg := TRegistry.Create;
try
Reg.RootKey := HKEY_LOCAL_MACHINE;
if Reg.OpenKeyReadOnly(IERegKey) then
begin
if Reg.ValueExists(RegValNameIE10) then
Result := Reg.ReadString(RegValNameIE10)
else if Reg.ValueExists(RegValName) then
Result := Reg.ReadString(RegValName);
end;
finally
Reg.Free;
end;
end;
class procedure TIEInfo.InitMajorVersion;
var
Vers: IStringList; // receives parts of version number string
begin
if fMajorVersion <> UnsetMajorVersion then
Exit;
Vers := TIStringList.Create(GetIEVersionStr, '.', False, True);
if Vers.Count > 0 then
fMajorVersion := StrToIntDef(Vers[0], 0)
else
fMajorVersion := 0;
end;
class function TIEInfo.IsSupportedBrowser: Boolean;
begin
Result := MajorVersion >= MinSupportedVersion;
end;
class function TIEInfo.MajorVersion: Word;
begin
if fMajorVersion = UnsetMajorVersion then
InitMajorVersion;
Result := fMajorVersion;
end;
class function TIEInfo.RequiresCSSOverflowXFix: Boolean;
begin
// Can't require "overflow-x" fix if "overflow-x" is not itself supported
Result := SupportsCSSOverflowX;
end;
class function TIEInfo.SupportsCSSMaxHeight: Boolean;
begin
Result := MajorVersion >= 7;
end;
class function TIEInfo.SupportsCSSOverflowX: Boolean;
begin
Result := MajorVersion >= 9;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.