-
Notifications
You must be signed in to change notification settings - Fork 14
/
Helpers.Reg.pas
95 lines (79 loc) · 2.59 KB
/
Helpers.Reg.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
unit Helpers.Reg;
interface
uses
Winapi.Windows,
System.SysUtils,
Versions.Helpers;
type
TReg = class
public type
TRegBits = (rbDef, rb32, rb64);
private const
KeyShortNames: array[HKEY_CLASSES_ROOT..HKEY_DYN_DATA] of string = (
'HKCR', 'HKCU', 'HKLM', 'HKU', 'HKPD', 'HKCC', 'HKDD');
RegBits: array[rbDef..rb64] of string = ('', ' /reg:32', ' /reg:64');
REG_SUCCESS = 0;
REG_ERROR = 1;
REG_COMP_DIFF = 0;
REG_COMP_ERROR = 1;
REG_COMP_EQUAL = 2;
private
class function Reg(Params: string): DWORD;
class function IsRelative(const Value: string): Boolean;
public
class function Export(RootKey: HKEY; const Key, FileName: string; Bits: TRegBits = rbDef): Boolean;
class function Import(const FileName: string; Bits: TRegBits = rbDef): Boolean;
end;
implementation
{ TReg }
class function TReg.Reg(Params: string): DWORD;
var
StartUpInfo : TStartUpInfo;
ProcessInfo : TProcessInformation;
CmdString: string;
bRet: BOOL;
begin
ZeroMemory(@StartUpInfo, SizeOf(StartUpInfo));
StartUpInfo.cb := SizeOf(StartUpInfo);
StartUpInfo.dwFlags := STARTF_USESHOWWINDOW;
StartUpInfo.wShowWindow := SW_HIDE;
CmdString := Format('REG %0:s', [Params]);
bRet := CreateProcess(nil, LPTSTR(CmdString), nil, nil, True,
GetPriorityClass(GetCurrentProcess), nil, nil, StartUpInfo, ProcessInfo);
if not bRet then Exit(REG_ERROR);
try
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
bRet := GetExitCodeProcess(ProcessInfo.hProcess, Result);
if not bRet then Exit(REG_ERROR);
finally
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
end;
end;
class function TReg.IsRelative(const Value: string): Boolean;
begin
Result := not ((Value <> '') and (Value[1] = '\'));
end;
class function TReg.Export(RootKey: HKEY; const Key, FileName: string;
Bits: TRegBits = rbDef): Boolean;
const
ExportFmt = 'EXPORT "%0:s%1:s%2:s" "%3:s" /y%4:s';
ExportFmtLegacy = 'EXPORT "%0:s%1:s%2:s" "%3:s"';
var
S, ExportFormat: string;
begin
S := Key;
if not IsRelative(S) then Delete(S, 1, 1);
if IsWindowsVistaOrGreater then
ExportFormat := ExportFmt
else
ExportFormat := ExportFmtLegacy;
Result := Reg(Format(ExportFormat,
[KeyShortNames[RootKey], PathDelim, S, FileName, RegBits[Bits]])) = REG_SUCCESS;
end;
class function TReg.Import(const FileName: string; Bits: TRegBits): Boolean;
begin
Result := Reg(Format('IMPORT "%0:s"%1:s',
[FileName, RegBits[Bits]])) = REG_SUCCESS;
end;
end.