From 9459d1c0ce29fb3d6313feede8593a735b97774b Mon Sep 17 00:00:00 2001 From: Jacek Pazera Date: Sun, 19 Sep 2021 19:42:46 +0200 Subject: [PATCH] [+] Win.Registry: CreateKey, TryCreateKey --- Base/JPL.Win.Registry.pas | 42 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/Base/JPL.Win.Registry.pas b/Base/JPL.Win.Registry.pas index 1a68cab..00d6d16 100644 --- a/Base/JPL.Win.Registry.pas +++ b/Base/JPL.Win.Registry.pas @@ -8,6 +8,7 @@ interface +{$IFDEF MSWINDOWS} uses Windows, ShellAPI, Messages, SysUtils, StrUtils, Classes, Registry, Types, JPL.Console, JPL.Strings, JPL.TStr, JPL.Conversion; @@ -49,6 +50,8 @@ TRegistryHelper = class helper for TRegistry end; TReg = record + class function CreateKey(const RootKey: HKEY; const Key: string): Boolean; static; + class function TryCreateKey(const RootKey: HKEY; const Key: string): Boolean; static; class function ExpandRootKey(const Key: string): string; static; class function IsPredefinedKey(const Key: HKEY): Boolean; static; class function GetRootKeyFromStr(const Key: string): HKEY; static; @@ -64,19 +67,50 @@ TReg = record - +{$ENDIF} // MSWINDOWS implementation - +{$IFDEF MSWINDOWS} {$Region ' TReg '} +class function TReg.CreateKey(const RootKey: HKEY; const Key: string): Boolean; +var + Reg: TRegistry; +begin + Reg := TRegistry.Create(KEY_ALL_ACCESS); + try + Reg.RootKey := RootKey; + Reg.CreateKey(Key); + Result := Reg.KeyExists(Key); + finally + Reg.Free; + end; +end; + +class function TReg.TryCreateKey(const RootKey: HKEY; const Key: string): Boolean; +var + Reg: TRegistry; +begin + Reg := TRegistry.Create(KEY_ALL_ACCESS); + try + Reg.RootKey := RootKey; + try + Reg.CreateKey(Key); + except + end; + Result := Reg.KeyExists(Key); + finally + Reg.Free; + end; +end; + class function TReg.ExpandRootKey(const Key: string): string; var s: string; @@ -266,7 +300,7 @@ class function TReg.RegJump(const Key: string; const ShowCmd: DWORD = SW_SHOWNOR lpFile := PChar('regedit.exe'); nShow := ShowCmd; end; - ShellExecuteEx(@sei); + ShellExecuteExW(@sei); WaitForInputIdle(sei.hProcess, 2000); hMainWindow := FindWindow(MAIN_WINDOW_CLASS, nil); end; @@ -427,5 +461,7 @@ procedure TRegPath.Clear; end; +{$ENDIF} // MSWINDOWS + end.