Subversion Repositories userdetect2

Rev

Rev 72 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 72 Rev 81
1
unit Functions;
1
unit Functions;
2
 
2
 
3
interface
3
interface
4
 
4
 
5
function GetComputerName: string;
5
function GetComputerName: string;
6
function GetUserName: string;
6
function GetUserName: string;
7
function GetCurrentUserSid: string;
7
function GetCurrentUserSid: string;
8
function ExpandEnvironmentStrings(ATemplate: string): string;
8
function ExpandEnvironmentStrings(ATemplate: string): string;
9
function StrICmp(a, b: string): boolean;
9
function StrICmp(a, b: string): boolean;
10
function EnforceLength(s: string; len: integer; filler: char;
10
function EnforceLength(s: string; len: integer; filler: char;
11
  appendRight: boolean): string;
11
  appendRight: boolean): string;
12
function GetHomeDir: string;
12
function GetHomeDir: string;
13
 
13
 
14
implementation
14
implementation
15
 
15
 
16
uses
16
uses
17
  Windows, SysUtils, Registry, SPGetSid;
17
  Windows, SysUtils, Registry, SPGetSid;
18
 
18
 
19
function GetComputerName: string; // Source: Luckie@DP
19
function GetComputerName: string; // Source: Luckie@DP
20
var
20
var
21
  buffer: array[0..MAX_PATH] of Char; // MAX_PATH ?
21
  buffer: array[0..MAX_PATH] of Char; // MAX_PATH ?
22
  size: DWORD;
22
  size: DWORD;
23
begin
23
begin
24
  size := SizeOf(buffer);
24
  size := SizeOf(buffer);
25
  ZeroMemory(@buffer, size);
25
  ZeroMemory(@buffer, size);
26
  Windows.GetComputerName(buffer, size);
26
  Windows.GetComputerName(buffer, size);
27
  SetString(result, buffer, lstrlen(buffer));
27
  SetString(result, buffer, lstrlen(buffer));
28
end;
28
end;
29
 
29
 
30
function GetUserName: string; // Source: Luckie@DP
30
function GetUserName: string; // Source: Luckie@DP
31
var
31
var
32
  buffer: array[0..MAX_PATH] of Char; // MAX_PATH ?
32
  buffer: array[0..MAX_PATH] of Char; // MAX_PATH ?
33
  size: DWORD;
33
  size: DWORD;
34
begin
34
begin
35
  size := SizeOf(buffer);
35
  size := SizeOf(buffer);
36
  ZeroMemory(@buffer, size);
36
  ZeroMemory(@buffer, size);
37
  Windows.GetUserName(buffer, size);
37
  Windows.GetUserName(buffer, size);
38
  SetString(result, buffer, lstrlen(buffer));
38
  SetString(result, buffer, lstrlen(buffer));
39
end;
39
end;
40
 
40
 
41
function GetCurrentUserSid: string;
41
function GetCurrentUserSid: string;
42
begin
42
begin
43
  result := SPGetSid.GetCurrentUserSid;
43
  result := SPGetSid.GetCurrentUserSid;
44
end;
44
end;
45
 
45
 
46
function ExpandEnvironmentStrings(ATemplate: string): string;
46
function ExpandEnvironmentStrings(ATemplate: string): string;
47
var
47
var
48
  buffer: array[0..MAX_PATH] of Char; // MAX_PATH ?
48
  buffer: array[0..MAX_PATH] of Char; // MAX_PATH ?
49
  size: DWORD;
49
  size: DWORD;
50
begin
50
begin
51
  size := SizeOf(buffer);
51
  size := SizeOf(buffer);
52
  ZeroMemory(@buffer, size);
52
  ZeroMemory(@buffer, size);
53
  Windows.ExpandEnvironmentStrings(PChar(ATemplate), buffer, size);
53
  Windows.ExpandEnvironmentStrings(PChar(ATemplate), buffer, size);
54
  SetString(result, buffer, lstrlen(buffer));
54
  SetString(result, buffer, lstrlen(buffer));
55
end;
55
end;
56
 
56
 
57
function StrICmp(a, b: string): boolean;
57
function StrICmp(a, b: string): boolean;
58
begin
58
begin
59
  result := UpperCase(a) = UpperCase(b);
59
  result := LowerCase(a) = LowerCase(b);
60
end;
60
end;
61
 
61
 
62
function EnforceLength(s: string; len: integer; filler: char;
62
function EnforceLength(s: string; len: integer; filler: char;
63
  appendRight: boolean): string;
63
  appendRight: boolean): string;
64
begin
64
begin
65
  result := s;
65
  result := s;
66
  while (Length(result) < len) do
66
  while (Length(result) < len) do
67
  begin
67
  begin
68
    if appendRight then
68
    if appendRight then
69
    begin
69
    begin
70
      result := result + filler;
70
      result := result + filler;
71
    end
71
    end
72
    else
72
    else
73
    begin
73
    begin
74
      result := filler + result;
74
      result := filler + result;
75
    end;
75
    end;
76
  end;
76
  end;
77
end;
77
end;
78
 
78
 
79
function GetHomeDir: string;
79
function GetHomeDir: string;
80
var
80
var
81
  reg: TRegistry;
81
  reg: TRegistry;
82
begin
82
begin
83
  result := Functions.ExpandEnvironmentStrings('%HOMEDRIVE%%HOMEPATH%');
83
  result := Functions.ExpandEnvironmentStrings('%HOMEDRIVE%%HOMEPATH%');
84
  if result = '%HOMEDRIVE%%HOMEPATH%' then
84
  if result = '%HOMEDRIVE%%HOMEPATH%' then
85
  begin
85
  begin
86
    result := '';
86
    result := '';
87
   
87
   
88
    // Windows 95
88
    // Windows 95
89
    reg := TRegistry.Create;
89
    reg := TRegistry.Create;
90
    try
90
    try
91
      reg.RootKey := HKEY_CURRENT_USER;
91
      reg.RootKey := HKEY_CURRENT_USER;
92
      if reg.OpenKeyReadOnly('Software\Microsoft\Windows\CurrentVersion\ProfileReconciliation') then
92
      if reg.OpenKeyReadOnly('Software\Microsoft\Windows\CurrentVersion\ProfileReconciliation') then
93
      begin
93
      begin
94
        result := reg.ReadString('ProfileDirectory');
94
        result := reg.ReadString('ProfileDirectory');
95
        reg.CloseKey;
95
        reg.CloseKey;
96
      end;
96
      end;
97
    finally;
97
    finally;
98
      reg.Free;
98
      reg.Free;
99
    end;
99
    end;
100
  end;
100
  end;
101
end;
101
end;
102
 
102
 
103
end.
103
end.
104
 
104