Login | ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/userdetect2/trunk/testuser/Functions.pas
Revision: 72
Committed: Sat Oct 3 16:28:02 2015 UTC (7 years, 1 month ago) by daniel-marschall
Content type: text/x-pascal
File size: 2432 byte(s)

File Contents

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