Subversion Repositories userdetect2

Rev

Rev 81 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
81 daniel-mar 1
unit MiscUtils;
2
 
3
interface
4
 
5
uses
6
  SysUtils,
7
  Registry,
8
  Windows,
9
  Classes;
10
 
11
function GetUserName: string;
12
function GetComputerName: string;
13
function ExpandEnvironmentStrings(ATemplate: string): string;
14
function GetHomeDir: string;
15
procedure EnvironmentStringsToStrings(outSL: TStrings);
82 daniel-mar 16
function GetPlatformID: integer;
81 daniel-mar 17
 
18
implementation
19
 
20
function GetHomeDir: string;
21
var
22
  reg: TRegistry;
23
begin
24
  result := ExpandEnvironmentStrings('%HOMEDRIVE%%HOMEPATH%');
25
  if result = '%HOMEDRIVE%%HOMEPATH%' then
26
  begin
27
    result := '';
28
 
29
    // Windows 95
30
    reg := TRegistry.Create;
31
    try
32
      reg.RootKey := HKEY_CURRENT_USER;
33
      if reg.OpenKeyReadOnly('Software\Microsoft\Windows\CurrentVersion\ProfileReconciliation') then
34
      begin
35
        result := reg.ReadString('ProfileDirectory');
36
        reg.CloseKey;
37
      end;
38
    finally;
39
      reg.Free;
40
    end;
41
  end;
42
end;
43
 
44
function GetComputerName: string;
82 daniel-mar 45
// Source: http://www.delphi-treff.de/tipps-tricks/netzwerkinternet/netzwerkeigenschaften/computernamen-des-eigenen-rechners-ermitteln/
81 daniel-mar 46
var
47
  Len: DWORD;
48
begin
49
  Len := MAX_COMPUTERNAME_LENGTH+1;
50
  SetLength(Result,Len);
51
  if Windows.GetComputerName(PChar(Result), Len) then
52
    SetLength(Result,Len)
53
  else
54
    RaiseLastOSError;
55
end;
56
 
57
function ExpandEnvironmentStrings(ATemplate: string): string;
58
var
59
  buffer: array[0..MAX_PATH] of Char; // MAX_PATH ?
60
  size: DWORD;
61
begin
62
  size := SizeOf(buffer);
63
  ZeroMemory(@buffer, size);
64
  Windows.ExpandEnvironmentStrings(PChar(ATemplate), buffer, size);
65
  SetString(result, buffer, lstrlen(buffer));
66
end;
67
 
68
procedure EnvironmentStringsToStrings(outSL: TStrings);
69
var
70
  DosEnv: PChar;
71
begin
72
  DosEnv := GetEnvironmentStrings;
73
  try
74
    while DosEnv^ <> #0 do
75
    begin
76
      outSL.Add(StrPas(DosEnv));
77
      Inc(DosEnv, lStrLen(DosEnv) + 1);
78
    end;
79
  finally
80
    FreeEnvironmentStrings(DosEnv);
81
  end;
82
end;
83
 
84
function GetUserName: string; // Source: Luckie@DP
85
var
86
  buffer: array[0..MAX_PATH] of Char; // MAX_PATH ?
87
  size: DWORD;
88
begin
89
  size := SizeOf(buffer);
90
  ZeroMemory(@buffer, size);
91
  Windows.GetUserName(buffer, size);
92
  SetString(result, buffer, lstrlen(buffer));
93
end;
94
 
82 daniel-mar 95
function GetPlatformID: integer;
96
var
97
  OSVersionInfo: TOSVersionInfo;
98
begin
99
  OSVersionInfo.dwOSVersionInfoSize := SizeOf(OSVersionInfo);
100
  GetVersionEx(OSVersionInfo);
101
  result := OSVersionInfo.dwPlatformID;
102
end;
103
 
81 daniel-mar 104
end.