-
Notifications
You must be signed in to change notification settings - Fork 1
/
RegFilesCollection.pas
173 lines (147 loc) · 4.48 KB
/
RegFilesCollection.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
unit RegFilesCollection;
interface
uses Windows, SysUtils, Classes, Registry, Dialogs, Psapi, tlhelp32, IniFiles,
Settings;
const
SysHiveList = 'System\CurrentControlSet\Control\hivelist';
function GetFileSize(FileName: string): int64; //64 bit files
function GetHiveList: boolean;
function GetKeyName(KeyHandler: HKEY): string;
procedure CreateWinNTProcessList(List: TstringList);
var
RegKeysNames: TStringList;
RegKeysList: TStringList;
HivePath: TStringList;
AllSize: int64;
NewAllFilesSize: int64;
implementation
var
DiskList: TStringList;
BeehiveCount: integer;
errorBuf: array[0..80] of WideChar;
lastError: DWORD;
function GetFileSize(FileName: string): int64; //64 bit files
var
SearchRec: TSearchRec;
begin
Result := 0;
if FindFirst(ExpandFileName(FileName), faAnyFile, SearchRec) = 0 then
Result := (SearchRec.FindData.nFileSizeHigh shl 32) + SearchRec.FindData.nFileSizeLow;
FindClose(SearchRec);
end;
//http://expert.delphi.int.ru/question/1713/
procedure GetDosDevices;
var
Device: char;
Volume: string;
lpQuery: array[0..MAXCHAR - 1] of char;
FInfo: string;
begin
for Device := 'A' to 'Z' do
if GetDriveType(PAnsiChar(string(Device + ':\'))) = DRIVE_FIXED then
begin
Volume := Device + ':' + #0;
QueryDosDevice(PChar(Volume), @lpQuery[0], MAXCHAR);
Volume[3] := '\';
FInfo := string(lpQuery);
DiskList.Values[FInfo] := Volume;
end;
end;
function GetHiveList: boolean;
var
Registry: TRegistry;
KeyNum: integer;
KeyNameSub, KeyName, KeyValue: string;
FilePath: string;
FileSize: longint;
SaveKey: HKEY;
begin
Result := false;
Registry := TRegistry.Create;
try
GetDosDevices;
Registry.RootKey := HKEY_LOCAL_MACHINE;
Registry.Access := KEY_WOW64_64KEY or KEY_ALL_ACCESS;
if not Registry.OpenKeyReadOnly(SysHiveList) then AddToLog('Can''t access to system registy files list.')
else
begin
Registry.GetValueNames(RegKeysNames);
AllSize := 0;
BeehiveCount := 0;
for KeyNum := 0 to RegKeysNames.Count - 1 do
begin
KeyName := GetWordString(2, RegKeysNames.Strings[KeyNum], ['\']);
KeyNameSub := GetWordString(3, RegKeysNames.Strings[KeyNum], ['\']);
KeyValue := Registry.ReadString(RegKeysNames.Strings[KeyNum]);
if KeyValue <> '' then
begin
FilePath := TrimRight(DiskList.Values[Copy(KeyValue, 0, GetWordStringPos(3, KeyValue, ['\']) - 2)])
+ Copy(KeyValue, GetWordStringPos(3, KeyValue, ['\']), Length(KeyValue));
FileSize := GetFileSize(FilePath);
AllSize := AllSize + FileSize;
Inc(BeehiveCount);
HivePath.AddObject(FilePath, TObject(FileSize));
SaveKey := 0;
if AnsiUpperCase(KeyName) = 'USER' then SaveKey := HKEY_USERS;
if AnsiUpperCase(KeyName) = 'MACHINE' then SaveKey := HKEY_LOCAL_MACHINE;
if SaveKey <> 0 then RegKeysList.AddObject(KeyNameSub, TObject(SaveKey));
end;
end;
result := true;
end;
finally
Registry.Destroy;
end;
end;
function GetKeyName(KeyHandler: HKEY): string;
begin
Result := '';
if KeyHandler = HKEY_CLASSES_ROOT then Result := 'HKEY_CLASSES_ROOT'
else
if KeyHandler = HKEY_CURRENT_USER then Result := 'HKEY_CURRENT_USER'
else
if KeyHandler = HKEY_LOCAL_MACHINE then Result := 'HKEY_LOCAL_MACHINE'
else
if KeyHandler = HKEY_LOCAL_MACHINE then Result := 'HKEY_LOCAL_MACHINE'
else
if KeyHandler = HKEY_USERS then Result := 'HKEY_USERS';
end;
procedure CreateWinNTProcessList(List: TstringList);
var
PIDArray: array[0..1023] of DWORD;
cb: DWORD;
I: Integer;
ProcCount: Integer;
hMod: HMODULE;
hProcess: THandle;
ModuleName: array[0..300] of Char;
begin
if List = nil then Exit;
EnumProcesses(@PIDArray, SizeOf(PIDArray), cb);
ProcCount := cb div SizeOf(DWORD);
for I := 0 to ProcCount - 1 do
begin
hProcess := OpenProcess(PROCESS_QUERY_INFORMATION or
PROCESS_VM_READ,
False,
PIDArray[I]);
if (hProcess <> 0) then
begin
EnumProcessModules(hProcess, @hMod, SizeOf(hMod), cb);
GetModuleFilenameEx(hProcess, hMod, ModuleName, SizeOf(ModuleName));
List.Add(ModuleName);
CloseHandle(hProcess);
end;
end;
end;
initialization
HivePath := TStringList.Create;
DiskList := TStringList.Create;
RegKeysNames := TStringList.Create;
RegKeysList := TStringList.Create;
finalization
HivePath.Destroy;
DiskList.Destroy;
RegKeysNames.Destroy;
RegKeysList.Destroy;
end.